From 27152cb1450f26363e5b6d55f308dfe958728a4d Mon Sep 17 00:00:00 2001 From: Kleissner Date: Thu, 22 Apr 2021 05:45:46 +0200 Subject: [PATCH] Provide old and new peer in ShouldEvict. New comparison function IsNodeCloser. Remove unused code (which was likely buggy anyway). --- dht/DHT Lite.go | 12 ++++++++++-- dht/Hash Table.go | 30 ++---------------------------- dht/Node.go | 6 +----- 3 files changed, 13 insertions(+), 35 deletions(-) diff --git a/dht/DHT Lite.go b/dht/DHT Lite.go index 87cecef..36b2063 100644 --- a/dht/DHT Lite.go +++ b/dht/DHT Lite.go @@ -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. diff --git a/dht/Hash Table.go b/dht/Hash Table.go index 551a3f5..3dc48ca 100644 --- a/dht/Hash Table.go +++ b/dht/Hash Table.go @@ -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() diff --git a/dht/Node.go b/dht/Node.go index ff849c8..22611a4 100644 --- a/dht/Node.go +++ b/dht/Node.go @@ -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 {