Restore cidOrTreeCid

This commit is contained in:
Ben 2024-12-17 12:52:03 +01:00
parent 512a736c79
commit 3e706b3eea
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
4 changed files with 15 additions and 21 deletions

View File

@ -56,19 +56,10 @@ type
discoveryLoopSleep: Duration # Discovery loop sleep discoveryLoopSleep: Duration # Discovery loop sleep
inFlightDiscReqs*: Table[Cid, Future[seq[SignedPeerRecord]]] # Inflight discovery requests inFlightDiscReqs*: Table[Cid, Future[seq[SignedPeerRecord]]] # Inflight discovery requests
proc getCid(address: BlockAddress): Cid =
# We advertise and discover only the CID part of a block address.
# Indices are ignored. This means that multiple blocks of the same tree will
# have a single DHT entry.
if address.leaf:
address.treeCid
else:
address.cid
proc discoveryQueueLoop(b: DiscoveryEngine) {.async: (raises: []).} = proc discoveryQueueLoop(b: DiscoveryEngine) {.async: (raises: []).} =
while b.discEngineRunning: while b.discEngineRunning:
for address in toSeq(b.pendingBlocks.wantList): for address in toSeq(b.pendingBlocks.wantList):
let cid = address.getCid() let cid = address.cidOrTreeCid
try: try:
await b.discoveryQueue.put(cid) await b.discoveryQueue.put(cid)
except CancelledError: except CancelledError:
@ -134,15 +125,17 @@ proc discoveryTaskLoop(b: DiscoveryEngine) {.async: (raises: []).} =
info "Exiting discovery task runner" info "Exiting discovery task runner"
proc queueFindBlocksReq*(b: DiscoveryEngine, addresses: seq[BlockAddress]) {.inline.} = proc queueFindBlocksReq*(b: DiscoveryEngine, cids: seq[Cid]) {.inline.} =
for address in addresses: for cid in cids:
let cid = address.getCid()
if cid notin b.discoveryQueue: if cid notin b.discoveryQueue:
try: try:
b.discoveryQueue.putNoWait(cid) b.discoveryQueue.putNoWait(cid)
except CatchableError as exc: except CatchableError as exc:
warn "Exception queueing discovery request", exc = exc.msg warn "Exception queueing discovery request", exc = exc.msg
proc queueFindBlocksReq*(b: DiscoveryEngine, addresses: seq[BlockAddress]) {.inline.} =
b.queueFindBlocksReq(addresses.mapIt(it.cidOrTreeCid))
proc start*(b: DiscoveryEngine) {.async.} = proc start*(b: DiscoveryEngine) {.async.} =
## Start the discengine task ## Start the discengine task
## ##

View File

@ -65,12 +65,7 @@ func peersHave*(self: PeerCtxStore, address: BlockAddress): seq[BlockExcPeerCtx]
toSeq(self.peers.values).filterIt( it.peerHave.anyIt( it == address ) ) toSeq(self.peers.values).filterIt( it.peerHave.anyIt( it == address ) )
func countPeersWhoHave*(self: PeerCtxStore, cid: Cid): int = func countPeersWhoHave*(self: PeerCtxStore, cid: Cid): int =
proc getCid(address: BlockAddress): Cid = self.peers.values.countIt(it.peerHave.anyIt( it.cidOrTreeCid == cid ) )
if address.leaf:
address.treeCid
else:
address.cid
self.peers.values.countIt(it.peerHave.anyIt( it.getCid() == cid ) )
func peersWant*(self: PeerCtxStore, address: BlockAddress): seq[BlockExcPeerCtx] = func peersWant*(self: PeerCtxStore, address: BlockAddress): seq[BlockExcPeerCtx] =
toSeq(self.peers.values).filterIt( it.peerWants.anyIt( it == address ) ) toSeq(self.peers.values).filterIt( it.peerWants.anyIt( it == address ) )

View File

@ -66,6 +66,12 @@ proc `$`*(a: BlockAddress): string =
else: else:
"cid: " & $a.cid "cid: " & $a.cid
proc cidOrTreeCid*(a: BlockAddress): Cid =
if a.leaf:
a.treeCid
else:
a.cid
proc address*(b: Block): BlockAddress = proc address*(b: Block): BlockAddress =
BlockAddress(leaf: false, cid: b.cid) BlockAddress(leaf: false, cid: b.cid)

View File

@ -63,8 +63,8 @@ checksuite "Pending Blocks":
discard blks.mapIt( pendingBlocks.getWantHandle( it.cid ) ) discard blks.mapIt( pendingBlocks.getWantHandle( it.cid ) )
check: check:
blks.mapIt( $it.cid ).sorted(cmp[string]) == blks.mapIt( $BlockAddress.init(it.cid) ).sorted(cmp[string]) ==
toSeq(pendingBlocks.wantListBlockCids).mapIt( $it ).sorted(cmp[string]) toSeq(pendingBlocks.wantList).mapIt( $it ).sorted(cmp[string])
test "Should get want handles list": test "Should get want handles list":
let let