diff --git a/Kademlia.go b/Kademlia.go index 1fa7e0b..56341e9 100644 --- a/Kademlia.go +++ b/Kademlia.go @@ -18,10 +18,21 @@ var nodesDHT *dht.DHT func initKademlia() { nodesDHT = dht.NewDHT(&dht.Node{ID: nodeID}, 256, 20, 5) - // ShouldEvict determines whether the given node shall be evicted - nodesDHT.ShouldEvict = func(node *dht.Node) bool { - // TODO: logic - return true + // ShouldEvict determines whether node 1 shall be evicted in favor of node 2 + nodesDHT.ShouldEvict = func(node1, node2 *dht.Node) bool { + rttOld := node1.Info.(*PeerInfo).GetRTT() + rttNew := node2.Info.(*PeerInfo).GetRTT() + + // evict the old node if the new one has a faster ping time + if rttOld == 0 { // old one has no recent RTT (happens if all connections are inactive)? + return true + } else if rttNew > 0 { + // If new RTT is smaller, evict old one. + return rttNew < rttOld + } + + // If here, none has a RTT. Keep the closer (by distance) one. + return nodesDHT.IsNodeCloser(node1.ID, node2.ID) } // SendRequestStore sends a store message to the remote node. I.e. asking it to store the given key-value diff --git a/README.md b/README.md index 9214d67..384678f 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,8 @@ Above limits are constants and can be adjusted in the code via `pingTime`, `conn The routing table has a bucket size of 20 and the size of keys 256 bits (blake3 hash). Nodes within buckets are sorted by least recently seen. The number of nodes to contact concurrently in DHT lookups (also known as alpha number) is set to 5. +If a bucket is full when a new peer connects `ShouldEvict` is called. It compares the RTTs (favoring smaller one) and in absence of the RTT time it will favor the node which is closer by XOR distance. + ### Timeouts * The default reply timeout (round-trip time) is 20 seconds set in `ReplyTimeout`. This applies to Response and Pong messages. The RTT timeout implies an average minimum connection speed between peers of about 6.4 KB/s for files of 64 KB size. diff --git a/go.mod b/go.mod index d675808..ecfac6d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/PeernetOfficial/core go 1.16 require ( - github.com/PeernetOfficial/core/dht v0.0.0-20210421010630-e432696ad116 + github.com/PeernetOfficial/core/dht v0.0.0-20210422034546-27152cb1450f github.com/btcsuite/btcd v0.21.0-beta.0.20210401013323-36a96f6a0025 github.com/pkg/errors v0.9.1 golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc diff --git a/go.sum b/go.sum index 6f06fd1..3b00207 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/PeernetOfficial/core/dht v0.0.0-20210421010630-e432696ad116 h1:1dqRhS20S6dRPdLL5dqp4TjTWm3vNvSm/EtRpPfxU6U= -github.com/PeernetOfficial/core/dht v0.0.0-20210421010630-e432696ad116/go.mod h1:sb2H21VIVTohCXVrDFo/Skl2tN8Yzba+MXSS6NgCYXw= +github.com/PeernetOfficial/core/dht v0.0.0-20210422034546-27152cb1450f h1:1Pp9AaYRFwFNd/ftTJ2CfVz0xr0MIoM7EvkFa52HUYM= +github.com/PeernetOfficial/core/dht v0.0.0-20210422034546-27152cb1450f/go.mod h1:sb2H21VIVTohCXVrDFo/Skl2tN8Yzba+MXSS6NgCYXw= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.21.0-beta.0.20210401013323-36a96f6a0025 h1:aoVqvZk4mLyF3WZbqEVPq+vXnwL2wekZg4P4mjYJNLs=