Remove double bookkeeping in peerHave and peerPrices

This commit is contained in:
Mark Spanbroek 2021-05-10 14:08:40 +02:00 committed by markspanbroek
parent e3b68f96d8
commit d4dd15734e
2 changed files with 9 additions and 4 deletions

View File

@ -21,6 +21,9 @@ type
pricing*: ?Pricing # optional bandwidth price for this peer pricing*: ?Pricing # optional bandwidth price for this peer
paymentChannel*: ?ChannelId # payment channel id paymentChannel*: ?ChannelId # payment channel id
proc peerHave*(context: BitswapPeerCtx): seq[Cid] =
toSeq(context.peerPrices.keys)
proc contains*(a: openArray[BitswapPeerCtx], b: PeerID): bool = proc contains*(a: openArray[BitswapPeerCtx], b: PeerID): bool =
## Convenience method to check for peer prepense ## Convenience method to check for peer prepense
## ##
@ -32,10 +35,8 @@ func updatePresence*(context: BitswapPeerCtx, presence: Presence) =
let price = presence.price let price = presence.price
if cid notin context.peerHave and presence.have: if cid notin context.peerHave and presence.have:
context.peerHave.add(cid)
context.peerPrices[cid] = price context.peerPrices[cid] = price
elif cid in context.peerHave and not presence.have: elif cid in context.peerHave and not presence.have:
context.peerHave.keepItIf(it != cid)
context.peerPrices.del(cid) context.peerPrices.del(cid)
func price*(context: BitswapPeerCtx, cids: seq[Cid]): UInt256 = func price*(context: BitswapPeerCtx, cids: seq[Cid]): UInt256 =

View File

@ -201,8 +201,12 @@ suite "Bitswap - multiple nodes":
await connectNodes(switch) await connectNodes(switch)
await sleepAsync(1.seconds) await sleepAsync(1.seconds)
check engine.peers[0].peerHave == blocks[0..3].mapIt( it.cid )
check engine.peers[3].peerHave == blocks[12..15].mapIt( it.cid ) check:
engine.peers[0].peerHave.mapIt($it).sorted(cmp[string]) ==
blocks[0..3].mapIt( it.cid ).mapIt($it).sorted(cmp[string])
engine.peers[3].peerHave.mapIt($it).sorted(cmp[string]) ==
blocks[12..15].mapIt( it.cid ).mapIt($it).sorted(cmp[string])
test "should exchange blocks with multiple nodes": test "should exchange blocks with multiple nodes":
let let