mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-01-14 06:54:39 +00:00
Cleanup cheapestPeer. Fixes test for peers lists
This commit is contained in:
parent
3607b88e9d
commit
7c2bff79c0
@ -82,33 +82,6 @@ proc getPeersForBlock*(self: PeerCtxStore, address: BlockAddress): PeersForBlock
|
|||||||
res.without.add(peer)
|
res.without.add(peer)
|
||||||
res
|
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 =
|
proc new*(T: type PeerCtxStore): PeerCtxStore =
|
||||||
## create new instance of a peer context store
|
## create new instance of a peer context store
|
||||||
PeerCtxStore(peers: initOrderedTable[PeerId, BlockExcPeerCtx]())
|
PeerCtxStore(peers: initOrderedTable[PeerId, BlockExcPeerCtx]())
|
||||||
|
@ -69,27 +69,6 @@ checksuite "Peer Context Store Peer Selection":
|
|||||||
check peerCtxs[0] in peers
|
check peerCtxs[0] in peers
|
||||||
check peerCtxs[5] 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":
|
test "Should select peers that want Cid":
|
||||||
let
|
let
|
||||||
entries = addresses.mapIt(
|
entries = addresses.mapIt(
|
||||||
@ -109,3 +88,19 @@ checksuite "Peer Context Store Peer Selection":
|
|||||||
check peers.len == 2
|
check peers.len == 2
|
||||||
check peerCtxs[0] in peers
|
check peerCtxs[0] in peers
|
||||||
check peerCtxs[5] 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user