From a0209a8cf3549d2ed6908e70f2685aa578902207 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 10 Jun 2019 14:01:39 +1200 Subject: [PATCH 1/3] feat(Ipfs.Engine): added IsStarted --- src/IpfsEngine.cs | 10 ++++++++++ test/IpfsEngineTest.cs | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/IpfsEngine.cs b/src/IpfsEngine.cs index 492e5aef..57883636 100644 --- a/src/IpfsEngine.cs +++ b/src/IpfsEngine.cs @@ -328,6 +328,16 @@ void Init() return Cid.Decode(r.Remove(0, 6)); // strip '/ipfs/'. } + /// + /// Determines if the engine has started. + /// + /// + /// true if the engine has started; otherwise, false. + /// + /// + /// + public bool IsStarted => stopTasks.Count > 0; + /// /// Starts the network services. /// diff --git a/test/IpfsEngineTest.cs b/test/IpfsEngineTest.cs index c1abde05..00e8ec9a 100644 --- a/test/IpfsEngineTest.cs +++ b/test/IpfsEngineTest.cs @@ -95,9 +95,12 @@ public async Task Can_Start_And_Stop() var ipfs = TestFixture.Ipfs; var peer = await ipfs.LocalPeer; + Assert.IsFalse(ipfs.IsStarted); await ipfs.StartAsync(); + Assert.IsTrue(ipfs.IsStarted); Assert.AreNotEqual(0, peer.Addresses.Count()); await ipfs.StopAsync(); + Assert.IsFalse(ipfs.IsStarted); Assert.AreEqual(0, peer.Addresses.Count()); await ipfs.StartAsync(); From c25dfd3ac79a0f5cc5d240eedf77818c39187688 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 11 Jun 2019 12:23:52 +1200 Subject: [PATCH 2/3] feat(BlockApi): tell DHT about new blocks #112 --- src/CoreApi/BlockApi.cs | 4 ++++ test/CoreApi/BlockApiTest.cs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/CoreApi/BlockApi.cs b/src/CoreApi/BlockApi.cs index 44877d5b..b4b159fd 100644 --- a/src/CoreApi/BlockApi.cs +++ b/src/CoreApi/BlockApi.cs @@ -197,6 +197,10 @@ public async Task PutAsync( else { await Store.PutAsync(cid, block, cancel).ConfigureAwait(false); + if (ipfs.IsStarted) + { + await ipfs.Dht.ProvideAsync(cid, advertise: false, cancel: cancel).ConfigureAwait(false); + } log.DebugFormat("Added block '{0}'", cid); } diff --git a/test/CoreApi/BlockApiTest.cs b/test/CoreApi/BlockApiTest.cs index fc014280..edda384c 100644 --- a/test/CoreApi/BlockApiTest.cs +++ b/test/CoreApi/BlockApiTest.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.IO; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -238,5 +239,26 @@ public async Task Put_Informs_Bitswap() Assert.AreEqual(data.Length, wantTask.Result.Size); CollectionAssert.AreEqual(data, wantTask.Result.DataBytes); } + + [TestMethod] + public async Task Put_Informs_Dht() + { + var data = Guid.NewGuid().ToByteArray(); + var ipfs = TestFixture.Ipfs; + await ipfs.StartAsync(); + try + { + var self = await ipfs.LocalPeer; + var cid = await ipfs.Block.PutAsync(data); + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); + var peers = await ipfs.Dht.FindProvidersAsync(cid, limit: 1, cancel: cts.Token); + Assert.AreEqual(self, peers.First()); + } + finally + { + await ipfs.StopAsync(); + } + + } } } From 1dd27939e054c81de0e2157af4c6d07615b36f63 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 11 Jun 2019 14:38:35 +1200 Subject: [PATCH 3/3] feat(FileSystemApi): advertise root node immediately #111 --- src/CoreApi/FileSystemApi.cs | 13 +++++++++++-- src/IpfsEngine.csproj | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/CoreApi/FileSystemApi.cs b/src/CoreApi/FileSystemApi.cs index 17c851df..ada7db8d 100644 --- a/src/CoreApi/FileSystemApi.cs +++ b/src/CoreApi/FileSystemApi.cs @@ -105,11 +105,20 @@ public async Task AddAsync( { var link = node.ToLink(name); var wlinks = new IFileSystemLink[] { link }; - return await CreateDirectoryAsync(wlinks, options, cancel).ConfigureAwait(false); + node = await CreateDirectoryAsync(wlinks, options, cancel).ConfigureAwait(false); + } + else + { + node.Name = name; + } + + // Advertise the root node. + if (options.Pin && ipfs.IsStarted) + { + await ipfs.Dht.ProvideAsync(node.Id, advertise: true, cancel: cancel).ConfigureAwait(false); } // Return the file system node. - node.Name = name; return node; } diff --git a/src/IpfsEngine.csproj b/src/IpfsEngine.csproj index 33fb4c31..151d486a 100644 --- a/src/IpfsEngine.csproj +++ b/src/IpfsEngine.csproj @@ -40,7 +40,7 @@ - +