Skip to content

Commit

Permalink
feat(Dht1): remove peer when swarm indicates
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 10, 2019
1 parent 5b6491d commit 46588bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Routing/Dht1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public Task StartAsync()
ContentRouter = new ContentRouter();
Swarm.AddProtocol(this);
Swarm.PeerDiscovered += Swarm_PeerDiscovered;
Swarm.PeerRemoved += Swarm_PeerRemoved;
foreach (var peer in Swarm.KnownPeers)
{
RoutingTable.Add(peer);
Expand Down Expand Up @@ -142,6 +143,14 @@ void Swarm_PeerDiscovered(object sender, Peer e)
RoutingTable.Add(e);
}

/// <summary>
/// The swarm has removed a peer, update the routing table.
/// </summary>
private void Swarm_PeerRemoved(object sender, Peer e)
{
RoutingTable.Remove(e);
}

/// <inheritdoc />
public async Task<Peer> FindPeerAsync(MultiHash id, CancellationToken cancel = default(CancellationToken))
{
Expand Down
20 changes: 20 additions & 0 deletions test/Routing/Dht1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public async Task AddDiscoveredPeerToRoutingTable()
}
}

[TestMethod]
public async Task RemovesPeerFromRoutingTable()
{
var swarm = new Swarm { LocalPeer = self };
var dht = new Dht1 { Swarm = swarm };
await dht.StartAsync();
try
{
var peer = await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h");
Assert.IsTrue(dht.RoutingTable.Contains(peer));

swarm.DeregisterPeer(peer);
Assert.IsFalse(dht.RoutingTable.Contains(peer));
}
finally
{
await dht.StopAsync();
}
}

[TestMethod]
public async Task ProcessFindNodeMessage_Self()
{
Expand Down

0 comments on commit 46588bc

Please sign in to comment.