Cleanup cheapestPeer. Fixes test for peers lists

This commit is contained in:
Ben 2024-12-04 14:31:35 +01:00
parent 3607b88e9d
commit 7c2bff79c0
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
2 changed files with 16 additions and 48 deletions

View File

@ -82,33 +82,6 @@ proc getPeersForBlock*(self: PeerCtxStore, address: BlockAddress): PeersForBlock
res.without.add(peer)
res
func selectCheapest*(self: PeerCtxStore, address: BlockAddress): seq[BlockExcPeerCtx] =
# assume that the price for all leaves in a tree is the same
let rootAddress = BlockAddress(leaf: false, cid: address.cidOrTreeCid)
var peers = self.peersHave(rootAddress)
func cmp(a, b: BlockExcPeerCtx): int =
var
priceA = 0.u256
priceB = 0.u256
a.blocks.withValue(rootAddress, precense):
priceA = precense[].price
b.blocks.withValue(rootAddress, precense):
priceB = precense[].price
if priceA == priceB:
0
elif priceA > priceB:
1
else:
-1
peers.sort(cmp)
trace "Selected cheapest peers", peers = peers.len
return peers
proc new*(T: type PeerCtxStore): PeerCtxStore =
## create new instance of a peer context store
PeerCtxStore(peers: initOrderedTable[PeerId, BlockExcPeerCtx]())

View File

@ -69,27 +69,6 @@ checksuite "Peer Context Store Peer Selection":
check peerCtxs[0] in peers
check peerCtxs[5] in peers
test "Should select cheapest peers for Cid":
peerCtxs[0].blocks = collect(initTable):
for i, a in addresses:
{ a: Presence(address: a, price: (5 + i).u256) }
peerCtxs[5].blocks = collect(initTable):
for i, a in addresses:
{ a: Presence(address: a, price: (2 + i).u256) }
peerCtxs[9].blocks = collect(initTable):
for i, a in addresses:
{ a: Presence(address: a, price: i.u256) }
let
peers = store.selectCheapest(addresses[0])
check peers.len == 3
check peers[0] == peerCtxs[9]
check peers[1] == peerCtxs[5]
check peers[2] == peerCtxs[0]
test "Should select peers that want Cid":
let
entries = addresses.mapIt(
@ -109,3 +88,19 @@ checksuite "Peer Context Store Peer Selection":
check peers.len == 2
check peerCtxs[0] in peers
check peerCtxs[5] in peers
test "Should return peers with and without block":
let address = addresses[2]
peerCtxs[1].blocks[address] = Presence(address: address, price: 0.u256)
peerCtxs[2].blocks[address] = Presence(address: address, price: 0.u256)
let peers = store.getPeersForBlock(address)
for i, pc in peerCtxs:
if i == 1 or i == 2:
check pc in peers.with
check pc notin peers.without
else:
check pc notin peers.with
check pc in peers.without