Provide old and new peer in ShouldEvict. New comparison function IsNodeCloser.

Remove unused code (which was likely buggy anyway).
This commit is contained in:
Kleissner
2021-04-22 05:45:46 +02:00
parent 4b905f4eab
commit 27152cb145
3 changed files with 13 additions and 35 deletions

View File

@@ -25,8 +25,8 @@ type DHT struct {
// Functions below must be set and provided by the caller.
// ShouldEvict determines whether the given node shall be evicted
ShouldEvict func(node *Node) bool
// ShouldEvict determines whether node 1 shall be evicted in favor of node 2
ShouldEvict func(node1, node2 *Node) bool
// SendRequestStore sends an announcement-store message to the remote node. It informs the remote node that the local one stores the given key-value.
SendRequestStore func(node *Node, key []byte, dataSize uint64)
@@ -88,6 +88,14 @@ func (dht *DHT) MarkNodeAsSeen(ID []byte) {
dht.ht.markNodeAsSeen(dht.ht.getBucketIndexFromDifferingBit(ID), ID)
}
// IsNodeCloser compares 2 nodes to self. If true, the first node is closer (= smaller distance) to self than the second.
func (dht *DHT) IsNodeCloser(node1, node2 []byte) bool {
iDist := getDistance(node1, dht.ht.Self.ID)
jDist := getDistance(node2, dht.ht.Self.ID)
return iDist.Cmp(jDist) == -1
}
// ---- Synchronous network query functions below ----
// Store informs the network about data stored locally.

View File

@@ -9,7 +9,6 @@ package dht
import (
"bytes"
"math"
"math/big"
"math/rand"
"sort"
"sync"
@@ -137,7 +136,7 @@ func (ht *hashTable) getClosestContacts(num int, target []byte, filterFunc NodeF
return sl
}
func (ht *hashTable) insertNode(node *Node, shouldEvict func(*Node) bool) {
func (ht *hashTable) insertNode(node *Node, shouldEvict func(nodeOld *Node, nodeNew *Node) bool) {
index := ht.getBucketIndexFromDifferingBit(node.ID)
// If the node already exist, mark it as seen
@@ -154,7 +153,7 @@ func (ht *hashTable) insertNode(node *Node, shouldEvict func(*Node) bool) {
bucket := ht.RoutingTable[index]
if len(bucket) == ht.bSize {
if shouldEvict(bucket[0]) {
if shouldEvict(bucket[0], node) {
bucket = append(bucket, node)
bucket = bucket[1:]
}
@@ -181,37 +180,12 @@ func (ht *hashTable) removeNode(ID []byte) {
ht.RoutingTable[index] = bucket
}
func (ht *hashTable) getAllNodesInBucketCloserThan(bucket int, id []byte) [][]byte {
b := ht.RoutingTable[bucket]
var nodes [][]byte
for _, v := range b {
d1 := ht.getDistance(id, ht.Self.ID)
d2 := ht.getDistance(id, v.ID)
result := d1.Sub(d1, d2)
if result.Sign() > -1 {
nodes = append(nodes, v.ID)
}
}
return nodes
}
func (ht *hashTable) getTotalNodesInBucket(bucket int) int {
ht.mutex.Lock()
defer ht.mutex.Unlock()
return len(ht.RoutingTable[bucket])
}
func (ht *hashTable) getDistance(id1 []byte, id2 []byte) *big.Int {
dst := make([]byte, ht.bSize)
for i := 0; i < ht.bSize; i++ {
dst[i] = id1[i] ^ id2[i]
}
ret := big.NewInt(0)
return ret.SetBytes(dst[:])
}
func (ht *hashTable) getRandomIDFromBucket(bucket int) []byte {
ht.mutex.Lock()
defer ht.mutex.Unlock()

View File

@@ -90,11 +90,7 @@ func (n *shortList) Less(i, j int) bool {
iDist := getDistance(n.Nodes[i].ID, n.Comparator)
jDist := getDistance(n.Nodes[j].ID, n.Comparator)
if iDist.Cmp(jDist) == -1 {
return true
}
return false
return iDist.Cmp(jDist) == -1
}
func getDistance(id1 []byte, id2 []byte) *big.Int {