mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Implement auto refresh of buckets. Close #11
This commit is contained in:
19
Kademlia.go
19
Kademlia.go
@@ -13,10 +13,13 @@ import (
|
||||
"github.com/PeernetOfficial/core/dht"
|
||||
)
|
||||
|
||||
const alpha = 5 // Count of nodes to be contacted in parallel for finding a key
|
||||
const bucketSize = 20 // Count of nodes per bucket
|
||||
|
||||
var nodesDHT *dht.DHT
|
||||
|
||||
func initKademlia() {
|
||||
nodesDHT = dht.NewDHT(&dht.Node{ID: nodeID}, 256, 20, 5)
|
||||
nodesDHT = dht.NewDHT(&dht.Node{ID: nodeID}, 256, bucketSize, alpha)
|
||||
|
||||
// ShouldEvict determines whether node 1 shall be evicted in favor of node 2
|
||||
nodesDHT.ShouldEvict = func(node1, node2 *dht.Node) bool {
|
||||
@@ -53,6 +56,20 @@ func initKademlia() {
|
||||
node.Info.(*PeerInfo).sendAnnouncementFindValue(request)
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh buckets every 5 minutes to meet the alpha nodes per bucket target. Force full refresh every hour.
|
||||
go func() {
|
||||
for minute := 5; ; minute += 5 {
|
||||
time.Sleep(time.Minute * 5)
|
||||
|
||||
target := alpha
|
||||
if minute%60 == 0 {
|
||||
target = 0
|
||||
}
|
||||
|
||||
nodesDHT.RefreshBuckets(target)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Future sendAnnouncementX: If it detects that announcements are sent out to the same peer within 50ms it should activate a wait-and-group scheme.
|
||||
|
||||
@@ -172,6 +172,8 @@ func networkChangeInterfaceNew(iface net.Interface, addresses []net.Addr) {
|
||||
log.Printf("networkChangeInterfaceNew new interface '%s' (%d IPs)\n", iface.Name, len(addresses))
|
||||
|
||||
networkStart(iface, addresses)
|
||||
|
||||
go nodesDHT.RefreshBuckets(0)
|
||||
}
|
||||
|
||||
// networkChangeInterfaceRemove is called when an existing interface is removed
|
||||
@@ -213,6 +215,8 @@ func networkChangeIPNew(iface net.Interface, address net.Addr) {
|
||||
log.Printf("networkChangeIPNew new interface '%s' IP %s\n", iface.Name, address.String())
|
||||
|
||||
networkStart(iface, []net.Addr{address})
|
||||
|
||||
go nodesDHT.RefreshBuckets(0)
|
||||
}
|
||||
|
||||
// networkChangeIPRemove is called when an existing interface removes an IP
|
||||
|
||||
@@ -79,6 +79,10 @@ The Private Key is required to make any changes to the user's blockchain, includ
|
||||
* Send out immediately when starting.
|
||||
* Phase 1: Resend every 10 seconds until at least 1 peer in the peer list.
|
||||
* Phase 2: Every 10 minutes.
|
||||
* Network adapter changes:
|
||||
* Check every 10 seconds for new/removed network interfaces and for new/removed IPs.
|
||||
* Automatically listen on any newly detected network interfaces and IPs.
|
||||
* Start a full Kademlia bucket refresh in case of a new network interface or IP.
|
||||
|
||||
### Ping
|
||||
|
||||
@@ -99,7 +103,7 @@ 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.
|
||||
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. Refresh of buckets is done every 5 minutes and queries a random ID in that bucket if there are not at least alpha nodes. A full refresh of all buckets is done every hour.
|
||||
|
||||
### Timeouts
|
||||
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module github.com/PeernetOfficial/core
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/PeernetOfficial/core/dht v0.0.0-20210422034546-27152cb1450f
|
||||
github.com/PeernetOfficial/core/dht v0.0.0-20210424204239-6693d442a6e2
|
||||
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
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
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/PeernetOfficial/core/dht v0.0.0-20210424204239-6693d442a6e2 h1:h4EP52078qatwYoW1DztuQhg5Y8i9wwDbkdO92usM+o=
|
||||
github.com/PeernetOfficial/core/dht v0.0.0-20210424204239-6693d442a6e2/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=
|
||||
|
||||
Reference in New Issue
Block a user