diff --git a/src/Routing/Dht1.cs b/src/Routing/Dht1.cs index 5f186ea..3fea7b5 100644 --- a/src/Routing/Dht1.cs +++ b/src/Routing/Dht1.cs @@ -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); @@ -142,6 +143,14 @@ void Swarm_PeerDiscovered(object sender, Peer e) RoutingTable.Add(e); } + /// + /// The swarm has removed a peer, update the routing table. + /// + private void Swarm_PeerRemoved(object sender, Peer e) + { + RoutingTable.Remove(e); + } + /// public async Task FindPeerAsync(MultiHash id, CancellationToken cancel = default(CancellationToken)) { diff --git a/test/Routing/Dht1Test.cs b/test/Routing/Dht1Test.cs index 521fb6a..82dc1fa 100644 --- a/test/Routing/Dht1Test.cs +++ b/test/Routing/Dht1Test.cs @@ -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() {