mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-02-20 13:33:09 +00:00
replace list operations with sets
This commit is contained in:
parent
b6e120394a
commit
a53f8da855
@ -18,6 +18,7 @@ import pkg/libp2p/[cid, switch, multihash, multicodec]
|
||||
import pkg/metrics
|
||||
import pkg/stint
|
||||
import pkg/questionable
|
||||
import pkg/stew/shims/sets
|
||||
|
||||
import ../../rng
|
||||
import ../../stores/blockstore
|
||||
@ -297,47 +298,31 @@ proc blockPresenceHandler*(
|
||||
trace "Received block presence from peer", peer, len = blocks.len
|
||||
let
|
||||
peerCtx = self.peers.get(peer)
|
||||
ourWantList = toSeq(self.pendingBlocks.wantList)
|
||||
ourWantList = toHashSet(self.pendingBlocks.wantList.toSeq)
|
||||
|
||||
if peerCtx.isNil:
|
||||
return
|
||||
|
||||
trace "Build presence list"
|
||||
|
||||
for blk in blocks:
|
||||
if presence =? Presence.init(blk):
|
||||
peerCtx.setPresence(presence)
|
||||
|
||||
trace "Built presence list"
|
||||
|
||||
trace "Remove dont want cids"
|
||||
|
||||
let
|
||||
peerHave = peerCtx.peerHave
|
||||
dontWantCids = peerHave.filterIt(it notin ourWantList)
|
||||
|
||||
trace "Removed dont want cids"
|
||||
peerHave = peerCtx.peerHave.toHashSet
|
||||
dontWantCids = peerHave - ourWantList
|
||||
|
||||
if dontWantCids.len > 0:
|
||||
peerCtx.cleanPresence(dontWantCids)
|
||||
|
||||
trace "Remove want cids"
|
||||
peerCtx.cleanPresence(dontWantCids.toSeq)
|
||||
|
||||
let ourWantCids = ourWantList.filterIt(
|
||||
it in peerHave and not self.pendingBlocks.retriesExhausted(it) and
|
||||
not self.pendingBlocks.isInFlight(it)
|
||||
)
|
||||
|
||||
trace "Removed want cids"
|
||||
|
||||
trace "Update pending blocks"
|
||||
).toSeq
|
||||
|
||||
for address in ourWantCids:
|
||||
self.pendingBlocks.setInFlight(address, true)
|
||||
self.pendingBlocks.decRetries(address)
|
||||
|
||||
trace "Updated pending blocks"
|
||||
|
||||
if ourWantCids.len > 0:
|
||||
trace "Peer has blocks in our wantList", peer, wants = ourWantCids
|
||||
# FIXME: this will result in duplicate requests for blocks
|
||||
|
||||
@ -34,7 +34,7 @@ type BlockExcPeerCtx* = ref object of RootObj
|
||||
lastRefresh*: Moment # last time we refreshed our knowledge of the blocks this peer has
|
||||
account*: ?Account # ethereum account of this peer
|
||||
paymentChannel*: ?ChannelId # payment channel id
|
||||
blocksInFlight*: seq[BlockAddress] # blocks in flight towards peer
|
||||
blocksInFlight*: HashSet[BlockAddress] # blocks in flight towards peer
|
||||
|
||||
proc isKnowledgeStale*(self: BlockExcPeerCtx): bool =
|
||||
self.lastRefresh + 15.seconds < Moment.now()
|
||||
@ -43,13 +43,10 @@ proc isInFlight*(self: BlockExcPeerCtx, address: BlockAddress): bool =
|
||||
address in self.blocksInFlight
|
||||
|
||||
proc addInFlight*(self: BlockExcPeerCtx, address: BlockAddress) =
|
||||
if not self.isInFlight(address):
|
||||
self.blocksInFlight.add(address)
|
||||
self.blocksInFlight.incl(address)
|
||||
|
||||
proc removeInFlight*(self: BlockExcPeerCtx, address: BlockAddress) =
|
||||
let index = self.blocksInFlight.find(address)
|
||||
if index != -1:
|
||||
self.blocksInFlight.delete(index)
|
||||
self.blocksInFlight.excl(address)
|
||||
|
||||
proc refreshed*(self: BlockExcPeerCtx) =
|
||||
self.lastRefresh = Moment.now()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user