Skip to content

Commit

Permalink
Merge pull request #114 from richardschneider/provide-blocks
Browse files Browse the repository at this point in the history
Provide blocks
  • Loading branch information
richardschneider authored Jun 11, 2019
2 parents f5e505c + 1dd2793 commit 479ad4b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/CoreApi/BlockApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ public async Task<Cid> 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);
}

Expand Down
13 changes: 11 additions & 2 deletions src/CoreApi/FileSystemApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,20 @@ public async Task<IFileSystemNode> 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;
}

Expand Down
10 changes: 10 additions & 0 deletions src/IpfsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ void Init()
return Cid.Decode(r.Remove(0, 6)); // strip '/ipfs/'.
}

/// <summary>
/// Determines if the engine has started.
/// </summary>
/// <value>
/// <b>true</b> if the engine has started; otherwise, <b>false</b>.
/// </value>
/// <seealso cref="Start"/>
/// <seealso cref="StartAsync"/>
public bool IsStarted => stopTasks.Count > 0;

/// <summary>
/// Starts the network services.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/IpfsEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="Makaretu.Dns.Unicast" Version="0.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.0.0" />
<PackageReference Include="PeerTalk" Version="0.12.0" />
<PackageReference Include="PeerTalk" Version="0.13.0" />
<PackageReference Include="PeterO.Cbor" Version="3.1.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="protobuf-net" Version="2.4.0" />
Expand Down
22 changes: 22 additions & 0 deletions test/CoreApi/BlockApiTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

}
}
}
3 changes: 3 additions & 0 deletions test/IpfsEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 479ad4b

Please sign in to comment.