@@ -58,11 +58,17 @@ const (
5858 kad2 protocol.ID = "/kad/2.0.0"
5959)
6060
61+ const (
62+ dhtUsefulTag = "dht-useful"
63+ kbucketTag = "kbucket"
64+ )
65+
6166// IpfsDHT is an implementation of Kademlia with S/Kademlia modifications.
6267// It is used to implement the base Routing module.
6368type IpfsDHT struct {
64- host host.Host // the network services we need
65- self peer.ID // Local peer (yourself)
69+ host host.Host // the network services we need
70+ self peer.ID // Local peer (yourself)
71+ selfKey kb.ID
6672 peerstore peerstore.Peerstore // Peer Registry
6773
6874 datastore ds.Datastore // Local data
@@ -250,6 +256,7 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
250256 dht := & IpfsDHT {
251257 datastore : cfg .datastore ,
252258 self : h .ID (),
259+ selfKey : kb .ConvertPeerID (h .ID ()),
253260 peerstore : h .Peerstore (),
254261 host : h ,
255262 strmap : make (map [peer.ID ]* messageSender ),
@@ -336,17 +343,16 @@ func makeRtRefreshManager(dht *IpfsDHT, cfg config, maxLastSuccessfulOutboundThr
336343}
337344
338345func makeRoutingTable (dht * IpfsDHT , cfg config , maxLastSuccessfulOutboundThreshold time.Duration ) (* kb.RoutingTable , error ) {
339- self := kb .ConvertPeerID (dht .host .ID ())
340-
341- rt , err := kb .NewRoutingTable (cfg .bucketSize , self , time .Minute , dht .host .Peerstore (), maxLastSuccessfulOutboundThreshold )
346+ rt , err := kb .NewRoutingTable (cfg .bucketSize , dht .selfKey , time .Minute , dht .host .Peerstore (), maxLastSuccessfulOutboundThreshold )
342347 cmgr := dht .host .ConnManager ()
343348
344349 rt .PeerAdded = func (p peer.ID ) {
345- commonPrefixLen := kb .CommonPrefixLen (self , kb .ConvertPeerID (p ))
346- cmgr .TagPeer (p , "kbucket" , BaseConnMgrScore + commonPrefixLen )
350+ commonPrefixLen := kb .CommonPrefixLen (dht . selfKey , kb .ConvertPeerID (p ))
351+ cmgr .TagPeer (p , kbucketTag , BaseConnMgrScore + commonPrefixLen )
347352 }
348353 rt .PeerRemoved = func (p peer.ID ) {
349- cmgr .UntagPeer (p , "kbucket" )
354+ cmgr .Unprotect (p , dhtUsefulTag )
355+ cmgr .UntagPeer (p , kbucketTag )
350356
351357 // try to fix the RT
352358 dht .fixRTIfNeeded ()
0 commit comments