Skip to content

Commit 572b448

Browse files
committed
fix: fix block exchange test to stricter protocol; minor refactor
1 parent 97fd68e commit 572b448

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

codex/blockexchange/engine/engine.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ proc downloadInternal(
279279
if not self.pendingBlocks.isRequested(address):
280280
let peer = self.selectPeer(peers.with)
281281
self.pendingBlocks.markRequested(address, peer.id)
282-
peer.blockRequested(address)
282+
peer.blockRequestScheduled(address)
283283
trace "Request block from block retry loop"
284284
await self.sendWantBlock(@[address], peer)
285285
peer
@@ -412,7 +412,7 @@ proc blockPresenceHandler*(
412412
for address in ourWantCids:
413413
self.pendingBlocks.decRetries(address)
414414
self.pendingBlocks.markRequested(address, peer)
415-
peerCtx.blockRequested(address)
415+
peerCtx.blockRequestScheduled(address)
416416

417417
if ourWantCids.len > 0:
418418
trace "Peer has blocks in our wantList", peer, wants = ourWantCids
@@ -773,6 +773,8 @@ proc taskHandler*(
773773
wantedBlocks = peerCtx.wantedBlocks.filterIt(not peerCtx.isBlockSent(it))
774774
sent: HashSet[BlockAddress]
775775

776+
trace "Running task for peer", peer = peerCtx.id
777+
776778
for wantedBlock in wantedBlocks:
777779
peerCtx.markBlockAsSent(wantedBlock)
778780

codex/blockexchange/peers/peercontext.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ func price*(self: BlockExcPeerCtx, addresses: seq[BlockAddress]): UInt256 =
8080

8181
price
8282

83-
proc blockRequested*(self: BlockExcPeerCtx, address: BlockAddress) =
84-
# We start counting the timeout from the first block requested.
83+
proc blockRequestScheduled*(self: BlockExcPeerCtx, address: BlockAddress) =
84+
## Adds a block the set of blocks that have been requested to this peer
85+
## (its request schedule).
8586
if self.blocksRequested.len == 0:
8687
self.lastExchange = Moment.now()
8788
self.blocksRequested.incl(address)
8889

8990
proc blockRequestCancelled*(self: BlockExcPeerCtx, address: BlockAddress) =
91+
## Removes a block from the set of blocks that have been requested to this peer
92+
## (its request schedule).
9093
self.blocksRequested.excl(address)
9194

9295
proc blockReceived*(self: BlockExcPeerCtx, address: BlockAddress): bool =

tests/codex/blockexchange/engine/testblockexc.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ asyncchecksuite "NetworkStore engine - 2 nodes":
9696
test "Should send want-have for block":
9797
let blk = bt.Block.new("Block 1".toBytes).tryGet()
9898
let blkFut = nodeCmps1.pendingBlocks.getWantHandle(blk.cid)
99+
peerCtx2.blockRequestScheduled(blk.address)
100+
99101
(await nodeCmps2.localStore.putBlock(blk)).tryGet()
100102

101103
peerCtx1.wantedBlocks.incl(blk.address)

tests/codex/blockexchange/engine/testengine.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ asyncchecksuite "NetworkStore engine handlers":
242242
let pending = blocks.mapIt(engine.pendingBlocks.getWantHandle(it.cid))
243243

244244
for blk in blocks:
245-
peerCtx.blockRequested(blk.address)
245+
peerCtx.blockRequestScheduled(blk.address)
246246

247247
let blocksDelivery = blocks.mapIt(BlockDelivery(blk: it, address: it.address))
248248

@@ -270,7 +270,7 @@ asyncchecksuite "NetworkStore engine handlers":
270270
).toTable
271271

272272
for blk in blocks:
273-
peerContext.blockRequested(blk.address)
273+
peerContext.blockRequestScheduled(blk.address)
274274

275275
engine.network = BlockExcNetwork(
276276
request: BlockExcRequest(
@@ -349,10 +349,10 @@ asyncchecksuite "NetworkStore engine handlers":
349349

350350
engine.peers.add(senderPeerCtx)
351351
for address in reqBlockAddrs:
352-
pendingPeerCtx.blockRequested(address)
352+
pendingPeerCtx.blockRequestScheduled(address)
353353

354354
for address in blocks.mapIt(it.address):
355-
senderPeerCtx.blockRequested(address)
355+
senderPeerCtx.blockRequestScheduled(address)
356356

357357
proc sendWantCancellations(
358358
id: PeerId, addresses: seq[BlockAddress]

tests/codex/blockexchange/testnetwork.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ asyncchecksuite "Network - Handlers":
4040
done = newFuture[void]()
4141
buffer = BufferStream.new()
4242
network = BlockExcNetwork.new(switch = newStandardSwitch(), connProvider = getConn)
43-
network.setupPeer(peerId)
43+
await network.handlePeerJoined(peerId)
4444
networkPeer = network.peers[peerId]
4545
discard await networkPeer.connect()
4646

0 commit comments

Comments
 (0)