From fbe68e11eba2a4af82398044b1a21ee2d119787b Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 2 May 2019 14:14:07 +1200 Subject: [PATCH 1/5] chore: more debug logging --- src/CoreApi/SwarmApi.cs | 6 +++++- src/IpfsEngine.cs | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/CoreApi/SwarmApi.cs b/src/CoreApi/SwarmApi.cs index 1abd90f0..fd45de9a 100644 --- a/src/CoreApi/SwarmApi.cs +++ b/src/CoreApi/SwarmApi.cs @@ -8,11 +8,13 @@ using System.Linq; using System.Collections.Concurrent; using PeerTalk; +using Common.Logging; namespace Ipfs.Engine.CoreApi { class SwarmApi : ISwarmApi { + static ILog log = LogManager.GetLogger(typeof(SwarmApi)); IpfsEngine ipfs; static MultiAddress[] defaultFilters = new MultiAddress[] @@ -48,7 +50,9 @@ public SwarmApi(IpfsEngine ipfs) public async Task ConnectAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) { var swarm = await ipfs.SwarmService; - await swarm.ConnectAsync(address, cancel); + log.Debug($"Connecting to {address}"); + var peer = await swarm.ConnectAsync(address, cancel); + log.Debug($"Connected to {peer.ConnectedAddress}"); } public async Task DisconnectAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) diff --git a/src/IpfsEngine.cs b/src/IpfsEngine.cs index 1f621430..44ba4135 100644 --- a/src/IpfsEngine.cs +++ b/src/IpfsEngine.cs @@ -160,6 +160,8 @@ void Init() ? null : new Psk1Protector { Key = Options.Swarm.PrivateNetworkKey } }; + if (Options.Swarm.PrivateNetworkKey != null) + log.Debug($"Private network {Options.Swarm.PrivateNetworkKey.Fingerprint().ToHexString()}"); log.Debug("Built swarm service"); return swarm; From e27dbeba53a57703ab383647186c8c82ef494bf5 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 2 May 2019 14:15:51 +1200 Subject: [PATCH 2/5] chore: remove some warnings --- test/CoreApi/FileSystemApiTest.cs | 6 ++++-- test/CoreApi/SwarmApiTest.cs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/CoreApi/FileSystemApiTest.cs b/test/CoreApi/FileSystemApiTest.cs index 101feb1d..9f9e04aa 100644 --- a/test/CoreApi/FileSystemApiTest.cs +++ b/test/CoreApi/FileSystemApiTest.cs @@ -72,8 +72,10 @@ public async Task AddEmpty_Check_Object() public async Task AddDuplicateWithPin() { var ipfs = TestFixture.Ipfs; - var options = new AddFileOptions(); - options.Pin = true; + var options = new AddFileOptions + { + Pin = true + }; var node = await ipfs.FileSystem.AddTextAsync("hello world", options); Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", (string)node.Id); var pins = await ipfs.Pin.ListAsync(); diff --git a/test/CoreApi/SwarmApiTest.cs b/test/CoreApi/SwarmApiTest.cs index d1617df0..3018b4d5 100644 --- a/test/CoreApi/SwarmApiTest.cs +++ b/test/CoreApi/SwarmApiTest.cs @@ -14,7 +14,7 @@ namespace Ipfs.Engine public class SwarmApiTest { IpfsEngine ipfs = TestFixture.Ipfs; - MultiAddress somewhere = "/ip4/127.0.0.1"; + readonly MultiAddress somewhere = "/ip4/127.0.0.1"; [TestMethod] public async Task Filter_Add_Remove() From 66f6f8fb005e6a1d5118cc0dbc2c3f2fb359d9ec Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 2 May 2019 15:59:40 +1200 Subject: [PATCH 3/5] feat(AutoDial): make it opt-in --- src/IpfsEngine.csproj | 2 +- src/SwarmOptions.cs | 6 +++--- test/SwarmOptionsTest.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/IpfsEngine.csproj b/src/IpfsEngine.csproj index bb83afcc..77a2958f 100644 --- a/src/IpfsEngine.csproj +++ b/src/IpfsEngine.csproj @@ -43,7 +43,7 @@ - + diff --git a/src/SwarmOptions.cs b/src/SwarmOptions.cs index 02e27e66..4ccf82ba 100644 --- a/src/SwarmOptions.cs +++ b/src/SwarmOptions.cs @@ -36,16 +36,16 @@ public class SwarmOptions /// The low water mark for peer connections. /// /// - /// Defaults to 16. + /// Defaults to 0. /// /// /// The is used to maintain at /// least this number of connections. /// - /// Setting this to zero will disable the auto dial feature. + /// This is an opt-feature. The value must be positive to enable it. /// /// - public int MinConnections { get; set; } = 16; + public int MinConnections { get; set; } = 0; } } diff --git a/test/SwarmOptionsTest.cs b/test/SwarmOptionsTest.cs index 7ae22406..143b19f6 100644 --- a/test/SwarmOptionsTest.cs +++ b/test/SwarmOptionsTest.cs @@ -18,7 +18,7 @@ public void Defaults() { var options = new SwarmOptions(); Assert.IsNull(options.PrivateNetworkKey); - Assert.AreNotEqual(0, options.MinConnections); + Assert.AreEqual(0, options.MinConnections); } } From f75201cb1bdbc180aab874399aba05f0d523282a Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 2 May 2019 17:22:12 +1200 Subject: [PATCH 4/5] feat(IpfsEngine): avoid SynchronizationContext on StartAsync and StopAsync #93 --- src/IpfsEngine.cs | 76 +++++++++++++++++++++---------------------- src/IpfsEngine.csproj | 2 +- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/IpfsEngine.cs b/src/IpfsEngine.cs index 44ba4135..a057e609 100644 --- a/src/IpfsEngine.cs +++ b/src/IpfsEngine.cs @@ -120,13 +120,13 @@ void Init() LocalPeer = new AsyncLazy(async () => { log.Debug("Building local peer"); - var keyChain = await KeyChain(); + var keyChain = await KeyChain().ConfigureAwait(false); log.Debug("Getting key info about self"); - var self = await keyChain.FindKeyByNameAsync("self"); + var self = await keyChain.FindKeyByNameAsync("self").ConfigureAwait(false); var localPeer = new Peer { Id = self.Id, - PublicKey = await keyChain.GetPublicKeyAsync("self"), + PublicKey = await keyChain.GetPublicKeyAsync("self").ConfigureAwait(false), ProtocolVersion = "ipfs/0.1.0" }; var version = typeof(IpfsEngine).GetTypeInfo().Assembly.GetName().Version; @@ -149,9 +149,9 @@ void Init() } } } - var peer = await LocalPeer; - var keyChain = await KeyChain(); - var self = await keyChain.GetPrivateKeyAsync("self"); + var peer = await LocalPeer.ConfigureAwait(false); + var keyChain = await KeyChain().ConfigureAwait(false); + var self = await keyChain.GetPrivateKeyAsync("self").ConfigureAwait(false); var swarm = new Swarm { LocalPeer = peer, @@ -171,7 +171,7 @@ void Init() log.Debug("Building bitswap service"); var bitswap = new BlockExchange.Bitswap { - Swarm = await SwarmService, + Swarm = await SwarmService.ConfigureAwait(false), BlockService = Block }; log.Debug("Built bitswap service"); @@ -182,7 +182,7 @@ void Init() log.Debug("Building DHT service"); var dht = new PeerTalk.Routing.Dht1 { - Swarm = await SwarmService + Swarm = await SwarmService.ConfigureAwait(false) }; dht.Swarm.Router = dht; log.Debug("Built DHT service"); @@ -268,13 +268,13 @@ void Init() } } - await keyChain.SetPassphraseAsync(passphrase, cancel); + await keyChain.SetPassphraseAsync(passphrase, cancel).ConfigureAwait(false); // Maybe create "self" key, this is the local peer's id. - var self = await keyChain.FindKeyByNameAsync("self", cancel); + var self = await keyChain.FindKeyByNameAsync("self", cancel).ConfigureAwait(false); if (self == null) { - self = await keyChain.CreateAsync("self", null, 0, cancel); + self = await keyChain.CreateAsync("self", null, 0, cancel).ConfigureAwait(false); } } return keyChain; @@ -306,7 +306,7 @@ void Init() /// public async Task ResolveIpfsPathToCidAsync (string path, CancellationToken cancel = default(CancellationToken)) { - var r = await Generic.ResolveAsync(path, true, cancel); + var r = await Generic.ResolveAsync(path, true, cancel).ConfigureAwait(false); return Cid.Decode(r.Remove(0, 6)); // strip '/ipfs/'. } @@ -330,45 +330,45 @@ public async Task StartAsync() throw new Exception("IPFS engine is already started."); } - var localPeer = await LocalPeer; + var localPeer = await LocalPeer.ConfigureAwait(false); log.Debug("starting " + localPeer.Id); // Everybody needs the swarm. - var swarm = await SwarmService; + var swarm = await SwarmService.ConfigureAwait(false); stopTasks.Add(async () => { - await swarm.StopAsync(); + await swarm.StopAsync().ConfigureAwait(false); }); - await swarm.StartAsync(); + await swarm.StartAsync().ConfigureAwait(false); // Start the primary services. var tasks = new List> { async () => { - var bitswap = await BitswapService; - stopTasks.Add(async () => await bitswap.StopAsync()); - await bitswap.StartAsync(); + var bitswap = await BitswapService.ConfigureAwait(false); + stopTasks.Add(async () => await bitswap.StopAsync().ConfigureAwait(false)); + await bitswap.StartAsync().ConfigureAwait(false); }, async () => { - var dht = await DhtService; - stopTasks.Add(async () => await dht.StopAsync()); - await dht.StartAsync(); + var dht = await DhtService.ConfigureAwait(false); + stopTasks.Add(async () => await dht.StopAsync().ConfigureAwait(false)); + await dht.StartAsync().ConfigureAwait(false); }, }; log.Debug("waiting for services to start"); - await Task.WhenAll(tasks.Select(t => t())); + await Task.WhenAll(tasks.Select(t => t())).ConfigureAwait(false); // Starting listening to the swarm. - var json = await Config.GetAsync("Addresses.Swarm"); + var json = await Config.GetAsync("Addresses.Swarm").ConfigureAwait(false); var numberListeners = 0; foreach (string a in json) { try { - await swarm.StartListeningAsync(a); + await swarm.StartListeningAsync(a).ConfigureAwait(false); ++numberListeners; } catch (Exception e) @@ -410,8 +410,8 @@ public async Task StartAsync() Addresses = await this.Bootstrap.ListAsync() }; bootstrap.PeerDiscovered += OnPeerDiscovered; - stopTasks.Add(async () => await bootstrap.StopAsync()); - await bootstrap.StartAsync(); + stopTasks.Add(async () => await bootstrap.StopAsync().ConfigureAwait(false)); + await bootstrap.StartAsync().ConfigureAwait(false); }, async () => { @@ -423,8 +423,8 @@ public async Task StartAsync() MulticastService = multicast }; mdns.PeerDiscovered += OnPeerDiscovered; - stopTasks.Add(async () => await mdns.StopAsync()); - await mdns.StartAsync(); + stopTasks.Add(async () => await mdns.StopAsync().ConfigureAwait(false)); + await mdns.StartAsync().ConfigureAwait(false); }, async () => { @@ -436,8 +436,8 @@ public async Task StartAsync() MulticastService = multicast }; mdns.PeerDiscovered += OnPeerDiscovered; - stopTasks.Add(async () => await mdns.StopAsync()); - await mdns.StartAsync(); + stopTasks.Add(async () => await mdns.StopAsync().ConfigureAwait(false)); + await mdns.StartAsync().ConfigureAwait(false); }, async () => { @@ -449,12 +449,12 @@ public async Task StartAsync() MulticastService = multicast }; mdns.PeerDiscovered += OnPeerDiscovered; - stopTasks.Add(async () => await mdns.StopAsync()); - await mdns.StartAsync(); + stopTasks.Add(async () => await mdns.StopAsync().ConfigureAwait(false)); + await mdns.StartAsync().ConfigureAwait(false); }, }; log.Debug("waiting for discovery services to start"); - await Task.WhenAll(tasks.Select(t => t())); + await Task.WhenAll(tasks.Select(t => t())).ConfigureAwait(false); multicast?.Start(); @@ -477,7 +477,7 @@ public async Task StopAsync() { var tasks = stopTasks.ToArray(); stopTasks = new ConcurrentBag>(); - await Task.WhenAll(tasks.Select(t => t())); + await Task.WhenAll(tasks.Select(t => t())).ConfigureAwait(false); } catch (Exception e) { @@ -487,7 +487,7 @@ public async Task StopAsync() // Many services use cancellation to stop. A cancellation may not run // immediately, so we need to give them some. // TODO: Would be nice to make this deterministic. - await Task.Delay(TimeSpan.FromMilliseconds(100)); + await Task.Delay(TimeSpan.FromMilliseconds(100)).ConfigureAwait(false); log.Debug("stopped"); } @@ -519,8 +519,8 @@ async void OnPeerDiscovered(object sender, PeerDiscoveredEventArgs e) { try { - var swarm = await SwarmService; - var peer = await swarm.RegisterPeerAsync(e.Address); + var swarm = await SwarmService.ConfigureAwait(false); + var peer = await swarm.RegisterPeerAsync(e.Address).ConfigureAwait(false); } catch (Exception ex) { diff --git a/src/IpfsEngine.csproj b/src/IpfsEngine.csproj index 77a2958f..0337696c 100644 --- a/src/IpfsEngine.csproj +++ b/src/IpfsEngine.csproj @@ -43,7 +43,7 @@ - + From 4917c0941487fcbea2700b86ec90fbf22b2e6aca Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 2 May 2019 17:40:23 +1200 Subject: [PATCH 5/5] feat(IpfsEngine): a syncrhonous Start and Stop --- src/IpfsEngine.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/IpfsEngine.cs b/src/IpfsEngine.cs index a057e609..1d50eeff 100644 --- a/src/IpfsEngine.cs +++ b/src/IpfsEngine.cs @@ -492,6 +492,28 @@ public async Task StopAsync() log.Debug("stopped"); } + /// + /// A synchronous start. + /// + /// + /// Calls and waits for it to complete. + /// + public void Start() + { + StartAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + } + + /// + /// A synchronous stop. + /// + /// + /// Calls and waits for it to complete. + /// + public void Stop() + { + StopAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + } + /// /// Manages communication with other peers. /// @@ -543,13 +565,13 @@ protected virtual void Dispose(bool disposing) { if (!disposedValue) { + disposedValue = true; + if (disposing) { passphrase?.Dispose(); - StopAsync().Wait(); + Stop(); } - - disposedValue = true; } }