Initial working Kademlia network - peer list exchange is working!

Using the announcement and response messages to exchange peer lists. Fixes in encoding.
Add arbitrary Info structure to DHT Lite/Node.
This commit is contained in:
Kleissner
2021-03-15 22:25:05 +01:00
parent 323f379ee7
commit 3080119fc1
13 changed files with 210 additions and 33 deletions

View File

@@ -83,7 +83,7 @@ func (dht *DHT) RemoveNode(ID []byte) {
}
// GetClosestContacts returns the closes contacts in the hash table
func (dht *DHT) GetClosestContacts(count int, target []byte, ignoredNodes ...Node) []*Node {
func (dht *DHT) GetClosestContacts(count int, target []byte, ignoredNodes ...[]byte) []*Node {
closest := dht.ht.getClosestContacts(count, target, ignoredNodes...)
return closest.Nodes
}

View File

@@ -50,10 +50,9 @@ func newHashTable(self *Node, bits, bucketSize int) *hashTable {
return ht
}
func (ht *hashTable) markNodeAsSeen(ID []byte) {
func (ht *hashTable) markNodeAsSeen(index int, ID []byte) {
ht.mutex.Lock()
defer ht.mutex.Unlock()
index := ht.getBucketIndexFromDifferingBit(ID)
bucket := ht.RoutingTable[index]
nodeIndex := -1
for i, v := range bucket {
@@ -87,7 +86,7 @@ func (ht *hashTable) doesNodeExistInBucket(bucket int, node []byte) bool {
return false
}
func (ht *hashTable) getClosestContacts(num int, target []byte, ignoredNodes ...Node) *shortList {
func (ht *hashTable) getClosestContacts(num int, target []byte, ignoredNodes ...[]byte) *shortList {
ht.mutex.Lock()
defer ht.mutex.Unlock()
// First we need to build the list of adjacent indices to our target in order
@@ -113,7 +112,7 @@ func (ht *hashTable) getClosestContacts(num int, target []byte, ignoredNodes ...
for i := 0; i < bucketContacts; i++ {
ignored := false
for j := 0; j < len(ignoredNodes); j++ {
if bytes.Compare(ht.RoutingTable[index][i].ID, ignoredNodes[j].ID) == 0 {
if bytes.Compare(ht.RoutingTable[index][i].ID, ignoredNodes[j]) == 0 {
ignored = true
}
}
@@ -137,7 +136,7 @@ func (ht *hashTable) insertNode(node *Node, shouldEvict func(*Node) bool) {
// If the node already exist, mark it as seen
if ht.doesNodeExistInBucket(index, node.ID) {
ht.markNodeAsSeen(node.ID)
ht.markNodeAsSeen(index, node.ID)
return
}

View File

@@ -19,6 +19,9 @@ type Node struct {
// LastSeen when was this node last considered seen by the DHT
LastSeen time.Time
// Info is an arbitrary pointer specified by the caller
Info interface{}
}
// shortList is used in order to sort a list of arbitrary nodes against a comparator. These nodes are sorted by xor distance