Skip to content

Commit

Permalink
Merge pull request #25 from cortze/feat/break-lookup-on-first-value
Browse files Browse the repository at this point in the history
Enable end of the lookup if value is found
  • Loading branch information
cortze authored Aug 24, 2023
2 parents 8de0bd5 + d3fb209 commit 66ef42d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions dht/dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def bootstrap(self) -> str:
# Return the summary of the RoutingTable
return self.rt.summary()

def lookup_for_hash(self, key: Hash, trackaccuracy: bool = False):
def lookup_for_hash(self, key: Hash, trackaccuracy: bool = False, finishwithfirstvalue: bool = True):
""" search for the closest peers to any given key, starting the lookup for the closest nodes in
the local routing table, and contacting Alpha nodes in parallel """
lookupsummary = {
Expand Down Expand Up @@ -73,8 +73,10 @@ def has_closer_nodes(prev, new):
stepscnt = 0

while (stepscnt < self.lookupsteptostop) and (len(nodestotry) > 0):
nodes = nodestotry.copy()
if finishwithfirstvalue and lookupvalue != "":
break

nodes = nodestotry.copy()
for node in nodes:
nodestotry.pop(node) # remove item from peers to attempt
if node in triednodes: # make sure we don't contact the same node twice
Expand Down Expand Up @@ -175,7 +177,7 @@ def provide_block_segment(self, segment) -> dict:
'aggrDelay': 0,
}
segH = Hash(segment)
closestnodes, _, lookupsummary, lookupdelay = self.lookup_for_hash(segH)
closestnodes, _, lookupsummary, lookupdelay = self.lookup_for_hash(segH, finishwithfirstvalue=False)
providesummary['aggrDelay'] += lookupdelay
for cn in closestnodes:
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_networks_closest_peers_to_hash(self):
randomid = random.sample(range(1, size), 1)[0]
rnode = network.nodestore.get_node(randomid)

closestnodes, _, _, _ = rnode.lookup_for_hash(segH)
closestnodes, _, _, _ = rnode.lookup_for_hash(segH, finishwithfirstvalue=False)
network_closest = network.get_closest_nodes_to_hash(segH, b)
for nodeid, _ in network_closest:
self.assertEqual(nodeid in closestnodes, True)
Expand Down Expand Up @@ -529,7 +529,7 @@ def test_aggregated_delays_and_alpha(self):
segH = Hash(randomSegment)
interestednodeid = random.sample(range(1, size), 1)[0]
inode = n.nodestore.get_node(interestednodeid)
closestnodes, _, summary, aggrdelay = inode.lookup_for_hash(segH)
closestnodes, _, summary, aggrdelay = inode.lookup_for_hash(segH, finishwithfirstvalue=False)
self.assertEqual(len(closestnodes), k)
rounds = int(summary['connectionFinished'] / alpha)
if (summary['connectionFinished'] % alpha) > 0:
Expand Down

0 comments on commit 66ef42d

Please sign in to comment.