2022-05-19 02:29:15 +00:00
|
|
|
import std/sugar
|
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/unittest2
|
|
|
|
import pkg/libp2p
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/blockexchange/peers
|
|
|
|
import pkg/codex/blockexchange/protobuf/blockexc
|
2022-11-15 15:46:21 +00:00
|
|
|
import pkg/codex/blockexchange/protobuf/presence
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../helpers
|
2022-05-19 02:29:15 +00:00
|
|
|
import ../examples
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
checksuite "Peer Context Store":
|
2022-05-19 02:29:15 +00:00
|
|
|
var
|
|
|
|
store: PeerCtxStore
|
|
|
|
peerCtx: BlockExcPeerCtx
|
|
|
|
|
|
|
|
setup:
|
|
|
|
store = PeerCtxStore.new()
|
|
|
|
peerCtx = BlockExcPeerCtx.example
|
|
|
|
store.add(peerCtx)
|
|
|
|
|
|
|
|
test "Should add peer":
|
|
|
|
check peerCtx.id in store
|
|
|
|
|
|
|
|
test "Should remove peer":
|
|
|
|
store.remove(peerCtx.id)
|
|
|
|
check peerCtx.id notin store
|
|
|
|
|
|
|
|
test "Should get peer":
|
|
|
|
check store.get(peerCtx.id) == peerCtx
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
checksuite "Peer Context Store Peer Selection":
|
2022-05-19 02:29:15 +00:00
|
|
|
var
|
|
|
|
store: PeerCtxStore
|
|
|
|
peerCtxs: seq[BlockExcPeerCtx]
|
2023-10-12 13:56:10 +00:00
|
|
|
addresses: seq[BlockAddress]
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
store = PeerCtxStore.new()
|
2023-10-12 13:56:10 +00:00
|
|
|
addresses = collect(newSeq):
|
|
|
|
for i in 0..<10: BlockAddress(leaf: false, cid: Cid.example)
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
peerCtxs = collect(newSeq):
|
|
|
|
for i in 0..<10: BlockExcPeerCtx.example
|
|
|
|
|
|
|
|
for p in peerCtxs:
|
|
|
|
store.add(p)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
store = nil
|
2023-10-12 13:56:10 +00:00
|
|
|
addresses = @[]
|
2022-05-19 02:29:15 +00:00
|
|
|
peerCtxs = @[]
|
|
|
|
|
|
|
|
test "Should select peers that have Cid":
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtxs[0].blocks = collect(initTable):
|
2023-10-12 13:56:10 +00:00
|
|
|
for i, a in addresses:
|
|
|
|
{ a: Presence(address: a, price: i.u256) }
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtxs[5].blocks = collect(initTable):
|
2023-10-12 13:56:10 +00:00
|
|
|
for i, a in addresses:
|
|
|
|
{ a: Presence(address: a, price: i.u256) }
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
let
|
2023-10-12 13:56:10 +00:00
|
|
|
peers = store.peersHave(addresses[0])
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
check peers.len == 2
|
|
|
|
check peerCtxs[0] in peers
|
|
|
|
check peerCtxs[5] in peers
|
|
|
|
|
|
|
|
test "Should select cheapest peers for Cid":
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtxs[0].blocks = collect(initTable):
|
2023-10-12 13:56:10 +00:00
|
|
|
for i, a in addresses:
|
|
|
|
{ a: Presence(address: a, price: (5 + i).u256) }
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtxs[5].blocks = collect(initTable):
|
2023-10-12 13:56:10 +00:00
|
|
|
for i, a in addresses:
|
|
|
|
{ a: Presence(address: a, price: (2 + i).u256) }
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
peerCtxs[9].blocks = collect(initTable):
|
2023-10-12 13:56:10 +00:00
|
|
|
for i, a in addresses:
|
|
|
|
{ a: Presence(address: a, price: i.u256) }
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
let
|
2023-10-12 13:56:10 +00:00
|
|
|
peers = store.selectCheapest(addresses[0])
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
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
|
2023-10-12 13:56:10 +00:00
|
|
|
entries = addresses.mapIt(
|
|
|
|
WantListEntry(
|
|
|
|
address: it,
|
2022-05-19 02:29:15 +00:00
|
|
|
priority: 1,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantBlock,
|
2022-05-19 02:29:15 +00:00
|
|
|
sendDontHave: false))
|
|
|
|
|
|
|
|
peerCtxs[0].peerWants = entries
|
|
|
|
peerCtxs[5].peerWants = entries
|
|
|
|
|
|
|
|
let
|
2023-10-12 13:56:10 +00:00
|
|
|
peers = store.peersWant(addresses[4])
|
2022-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
check peers.len == 2
|
|
|
|
check peerCtxs[0] in peers
|
|
|
|
check peerCtxs[5] in peers
|