Skip to content

Commit a53f8da

Browse files
committed
replace list operations with sets
1 parent b6e1203 commit a53f8da

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

codex/blockexchange/engine/engine.nim

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import pkg/libp2p/[cid, switch, multihash, multicodec]
1818
import pkg/metrics
1919
import pkg/stint
2020
import pkg/questionable
21+
import pkg/stew/shims/sets
2122

2223
import ../../rng
2324
import ../../stores/blockstore
@@ -297,47 +298,31 @@ proc blockPresenceHandler*(
297298
trace "Received block presence from peer", peer, len = blocks.len
298299
let
299300
peerCtx = self.peers.get(peer)
300-
ourWantList = toSeq(self.pendingBlocks.wantList)
301+
ourWantList = toHashSet(self.pendingBlocks.wantList.toSeq)
301302

302303
if peerCtx.isNil:
303304
return
304305

305-
trace "Build presence list"
306-
307306
for blk in blocks:
308307
if presence =? Presence.init(blk):
309308
peerCtx.setPresence(presence)
310309

311-
trace "Built presence list"
312-
313-
trace "Remove dont want cids"
314-
315310
let
316-
peerHave = peerCtx.peerHave
317-
dontWantCids = peerHave.filterIt(it notin ourWantList)
318-
319-
trace "Removed dont want cids"
311+
peerHave = peerCtx.peerHave.toHashSet
312+
dontWantCids = peerHave - ourWantList
320313

321314
if dontWantCids.len > 0:
322-
peerCtx.cleanPresence(dontWantCids)
323-
324-
trace "Remove want cids"
315+
peerCtx.cleanPresence(dontWantCids.toSeq)
325316

326317
let ourWantCids = ourWantList.filterIt(
327318
it in peerHave and not self.pendingBlocks.retriesExhausted(it) and
328319
not self.pendingBlocks.isInFlight(it)
329-
)
330-
331-
trace "Removed want cids"
332-
333-
trace "Update pending blocks"
320+
).toSeq
334321

335322
for address in ourWantCids:
336323
self.pendingBlocks.setInFlight(address, true)
337324
self.pendingBlocks.decRetries(address)
338325

339-
trace "Updated pending blocks"
340-
341326
if ourWantCids.len > 0:
342327
trace "Peer has blocks in our wantList", peer, wants = ourWantCids
343328
# FIXME: this will result in duplicate requests for blocks

codex/blockexchange/peers/peercontext.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type BlockExcPeerCtx* = ref object of RootObj
3434
lastRefresh*: Moment # last time we refreshed our knowledge of the blocks this peer has
3535
account*: ?Account # ethereum account of this peer
3636
paymentChannel*: ?ChannelId # payment channel id
37-
blocksInFlight*: seq[BlockAddress] # blocks in flight towards peer
37+
blocksInFlight*: HashSet[BlockAddress] # blocks in flight towards peer
3838

3939
proc isKnowledgeStale*(self: BlockExcPeerCtx): bool =
4040
self.lastRefresh + 15.seconds < Moment.now()
@@ -43,13 +43,10 @@ proc isInFlight*(self: BlockExcPeerCtx, address: BlockAddress): bool =
4343
address in self.blocksInFlight
4444

4545
proc addInFlight*(self: BlockExcPeerCtx, address: BlockAddress) =
46-
if not self.isInFlight(address):
47-
self.blocksInFlight.add(address)
46+
self.blocksInFlight.incl(address)
4847

4948
proc removeInFlight*(self: BlockExcPeerCtx, address: BlockAddress) =
50-
let index = self.blocksInFlight.find(address)
51-
if index != -1:
52-
self.blocksInFlight.delete(index)
49+
self.blocksInFlight.excl(address)
5350

5451
proc refreshed*(self: BlockExcPeerCtx) =
5552
self.lastRefresh = Moment.now()

0 commit comments

Comments
 (0)