mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
New DHT function IsNodeContact to check if node is in local routing table
This commit is contained in:
@@ -96,6 +96,11 @@ func (dht *DHT) IsNodeCloser(node1, node2 []byte) bool {
|
||||
return iDist.Cmp(jDist) == -1
|
||||
}
|
||||
|
||||
// IsNodeContact checks if the given node is in the local routing table
|
||||
func (dht *DHT) IsNodeContact(ID []byte) (node *Node) {
|
||||
return dht.ht.doesNodeExist(ID)
|
||||
}
|
||||
|
||||
// ---- Synchronous network query functions below ----
|
||||
|
||||
// Store informs the network about data stored locally.
|
||||
|
||||
@@ -74,15 +74,19 @@ func (ht *hashTable) markNodeAsSeen(index int, ID []byte) {
|
||||
ht.RoutingTable[index] = bucket
|
||||
}
|
||||
|
||||
func (ht *hashTable) doesNodeExistInBucket(bucket int, node []byte) bool {
|
||||
func (ht *hashTable) doesNodeExistInBucket(bucket int, ID []byte) (node *Node) {
|
||||
ht.mutex.RLock()
|
||||
defer ht.mutex.RUnlock()
|
||||
for _, v := range ht.RoutingTable[bucket] {
|
||||
if bytes.Compare(v.ID, node) == 0 {
|
||||
return true
|
||||
for _, node = range ht.RoutingTable[bucket] {
|
||||
if bytes.Compare(node.ID, ID) == 0 {
|
||||
return node
|
||||
}
|
||||
}
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ht *hashTable) doesNodeExist(ID []byte) (node *Node) {
|
||||
return ht.doesNodeExistInBucket(ht.getBucketIndexFromDifferingBit(ID), ID)
|
||||
}
|
||||
|
||||
// getClosestContacts returns the closest nodes to the target. filterFunc is optional and allows the caller to filter the nodes.
|
||||
@@ -140,7 +144,7 @@ func (ht *hashTable) insertNode(node *Node, shouldEvict func(nodeOld *Node, node
|
||||
index := ht.getBucketIndexFromDifferingBit(node.ID)
|
||||
|
||||
// If the node already exist, mark it as seen
|
||||
if ht.doesNodeExistInBucket(index, node.ID) {
|
||||
if ht.doesNodeExistInBucket(index, node.ID) != nil {
|
||||
ht.markNodeAsSeen(index, node.ID)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user