Skip to content

Commit

Permalink
Fluffy: Prioritize nodes that have radius in range of target content …
Browse files Browse the repository at this point in the history
…in lookup (#2841)

* Prioritize nodes that have radius in range of target content in content lookup.
  • Loading branch information
bhartnett authored Nov 7, 2024
1 parent 70a1f76 commit 9dceb58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions fluffy/network/wire/portal_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ declareHistogram portal_lookup_node_requests,
labels = ["protocol_id"],
buckets = requestBuckets
declareHistogram portal_lookup_content_requests,
"Portal wire protocol amount of requests per node lookup",
"Portal wire protocol amount of requests per content lookup",
labels = ["protocol_id"],
buckets = requestBuckets
declareCounter portal_lookup_content_failures,
Expand Down Expand Up @@ -1139,14 +1139,31 @@ proc contentLookup*(
p: PortalProtocol, target: ContentKeyByteList, targetId: UInt256
): Future[Opt[ContentLookupResult]] {.async: (raises: [CancelledError]).} =
## Perform a lookup for the given target, return the closest n nodes to the
## target. Maximum value for n is `BUCKET_SIZE`.
## target.
# `closestNodes` holds the k closest nodes to target found, sorted by distance
# Unvalidated nodes are used for requests as a form of validation.
var closestNodes = p.routingTable.neighbours(targetId, BUCKET_SIZE, seenOnly = false)

# Shuffling the order of the nodes in order to not always hit the same node
# first for the same request.
p.baseProtocol.rng[].shuffle(closestNodes)

# Sort closestNodes so that nodes that are in range of the target content
# are queried first
proc nodesCmp(x, y: Node): int =
let
xRadius = p.radiusCache.get(x.id)
yRadius = p.radiusCache.get(y.id)

if xRadius.isSome() and p.inRange(x.id, xRadius.unsafeGet(), targetId):
-1
elif yRadius.isSome() and p.inRange(y.id, yRadius.unsafeGet(), targetId):
1
else:
0

closestNodes.sort(nodesCmp)

var asked, seen = HashSet[NodeId]()
asked.incl(p.localNode.id) # No need to ask our own node
seen.incl(p.localNode.id) # No need to discover our own node
Expand Down
5 changes: 4 additions & 1 deletion fluffy/tools/portal_bridge/portal_bridge_state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ proc runBackfillGossipBlockOffersLoop(
break
except CatchableError as e:
warn "Failed to find content with key: ",
contentKey = k, error = e.msg, workerId
contentKey = k.to0xHex(), error = e.msg, workerId
retryGossip = true
break

Expand All @@ -349,6 +349,9 @@ proc runBackfillGossipBlockOffersLoop(
if blockOffers.blockNumber mod 1000 == 0:
info "Finished gossiping offers for block number: ",
workerId, blockNumber = blockOffers.blockNumber, offerCount = offersMap.len()
else:
debug "Finished gossiping offers for block number: ",
workerId, blockNumber = blockOffers.blockNumber, offerCount = offersMap.len()

blockOffers = await blockOffersQueue.popFirst()

Expand Down

0 comments on commit 9dceb58

Please sign in to comment.