2021-02-26 00:23:22 +00:00
|
|
|
import std/sequtils
|
2021-05-10 11:47:15 +00:00
|
|
|
import std/random
|
2022-04-13 16:32:35 +00:00
|
|
|
import std/algorithm
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/libp2p/routing_record
|
|
|
|
import pkg/libp2pdht/discv5/protocol as discv5
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/blockexchange
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/discovery
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/utils/asyncheapqueue
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../../helpers
|
|
|
|
import ../../examples
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore engine basic":
|
2022-05-19 02:29:15 +00:00
|
|
|
var
|
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
|
|
|
peerId: PeerID
|
|
|
|
chunker: Chunker
|
|
|
|
wallet: WalletRef
|
|
|
|
blockDiscovery: Discovery
|
|
|
|
peerStore: PeerCtxStore
|
|
|
|
pendingBlocks: PendingBlocksManager
|
|
|
|
blocks: seq[bt.Block]
|
|
|
|
done: Future[void]
|
|
|
|
|
|
|
|
setup:
|
2021-02-26 00:23:22 +00:00
|
|
|
rng = Rng.instance()
|
2022-01-13 00:42:18 +00:00
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
|
|
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet = WalletRef.example
|
2022-05-19 02:29:15 +00:00
|
|
|
blockDiscovery = Discovery.new()
|
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
done = newFuture[void]()
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should send want list to new peers":
|
2021-02-26 00:23:22 +00:00
|
|
|
proc sendWantList(
|
|
|
|
id: PeerID,
|
|
|
|
cids: seq[Cid],
|
|
|
|
priority: int32 = 0,
|
|
|
|
cancel: bool = false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType = WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
full: bool = false,
|
2022-07-29 16:19:34 +00:00
|
|
|
sendDontHave: bool = false) {.gcsafe, async.} =
|
2022-04-13 16:32:35 +00:00
|
|
|
check cids.mapIt($it).sorted == blocks.mapIt( $it.cid ).sorted
|
2021-02-26 00:23:22 +00:00
|
|
|
done.complete()
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
|
|
|
network = BlockExcNetwork(request: BlockExcRequest(
|
|
|
|
sendWantList: sendWantList,
|
|
|
|
))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore = CacheStore.new(blocks.mapIt( it ))
|
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
engine = BlockExcEngine.new(
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore,
|
2022-01-10 15:32:56 +00:00
|
|
|
wallet,
|
2022-04-13 16:32:35 +00:00
|
|
|
network,
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
for b in blocks:
|
2022-05-12 21:52:03 +00:00
|
|
|
discard engine.pendingBlocks.getWantHandle(b.cid)
|
2022-07-29 16:19:34 +00:00
|
|
|
await engine.setupPeer(peerId)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
await done.wait(100.millis)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should send account to new peers":
|
2021-04-08 12:27:49 +00:00
|
|
|
let pricing = Pricing.example
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
proc sendAccount(peer: PeerID, account: Account) {.gcsafe, async.} =
|
2021-05-10 14:21:47 +00:00
|
|
|
check account.address == pricing.address
|
2021-04-08 12:27:49 +00:00
|
|
|
done.complete()
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
2022-07-29 16:19:34 +00:00
|
|
|
network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendAccount: sendAccount
|
2022-01-10 15:32:56 +00:00
|
|
|
))
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore = CacheStore.new()
|
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks)
|
|
|
|
|
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
engine.pricing = pricing.some
|
2022-07-29 16:19:34 +00:00
|
|
|
await engine.setupPeer(peerId)
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2021-04-08 12:27:49 +00:00
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore engine handlers":
|
2021-02-26 00:23:22 +00:00
|
|
|
var
|
2022-05-19 02:29:15 +00:00
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
|
|
|
peerId: PeerID
|
|
|
|
chunker: Chunker
|
|
|
|
wallet: WalletRef
|
|
|
|
blockDiscovery: Discovery
|
|
|
|
peerStore: PeerCtxStore
|
|
|
|
pendingBlocks: PendingBlocksManager
|
|
|
|
network: BlockExcNetwork
|
2021-08-30 19:25:20 +00:00
|
|
|
engine: BlockExcEngine
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery: DiscoveryEngine
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx: BlockExcPeerCtx
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore: BlockStore
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
setup:
|
2022-05-19 02:29:15 +00:00
|
|
|
rng = Rng.instance()
|
|
|
|
chunker = RandomChunker.new(rng, size = 1024, chunkSize = 256)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
|
|
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
|
|
|
wallet = WalletRef.example
|
|
|
|
blockDiscovery = Discovery.new()
|
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
|
|
|
|
localStore = CacheStore.new()
|
|
|
|
network = BlockExcNetwork()
|
|
|
|
|
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks)
|
|
|
|
|
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx = BlockExcPeerCtx(
|
2021-02-26 00:23:22 +00:00
|
|
|
id: peerId
|
|
|
|
)
|
|
|
|
engine.peers.add(peerCtx)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
test "Should schedule block requests":
|
|
|
|
let
|
|
|
|
wantList = makeWantList(
|
|
|
|
blocks.mapIt( it.cid ),
|
|
|
|
wantType = WantType.WantBlock) # only `wantBlock` are stored in `peerWants`
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc handler() {.async.} =
|
|
|
|
let ctx = await engine.taskQueue.pop()
|
2021-02-26 00:23:22 +00:00
|
|
|
check ctx.id == peerId
|
2022-11-15 15:46:21 +00:00
|
|
|
# only `wantBlock` scheduled
|
2021-02-26 00:23:22 +00:00
|
|
|
check ctx.peerWants.mapIt( it.cid ) == blocks.mapIt( it.cid )
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let done = handler()
|
|
|
|
await engine.wantListHandler(peerId, wantList)
|
2021-02-26 00:23:22 +00:00
|
|
|
await done
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
test "Should handle want list":
|
|
|
|
let
|
|
|
|
done = newFuture[void]()
|
|
|
|
wantList = makeWantList(blocks.mapIt( it.cid ))
|
|
|
|
|
|
|
|
proc sendPresence(peerId: PeerID, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
|
|
|
check presence.mapIt( it.cid ) == wantList.entries.mapIt( it.`block` )
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendPresence: sendPresence
|
|
|
|
))
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(blocks.mapIt( localStore.putBlock(it) )))
|
|
|
|
|
|
|
|
await engine.wantListHandler(peerId, wantList)
|
|
|
|
await done
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should handle want list - `dont-have`":
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
done = newFuture[void]()
|
|
|
|
wantList = makeWantList(
|
|
|
|
blocks.mapIt( it.cid ),
|
|
|
|
sendDontHave = true)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
proc sendPresence(peerId: PeerID, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
check presence.mapIt( it.cid ) == wantList.entries.mapIt( it.`block` )
|
|
|
|
for p in presence:
|
|
|
|
check:
|
2022-11-15 15:46:21 +00:00
|
|
|
p.`type` == BlockPresenceType.DontHave
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
engine.network = BlockExcNetwork(request: BlockExcRequest(
|
|
|
|
sendPresence: sendPresence
|
|
|
|
))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
await engine.wantListHandler(peerId, wantList)
|
2021-02-26 00:23:22 +00:00
|
|
|
await done
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should handle want list - `dont-have` some blocks":
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
done = newFuture[void]()
|
|
|
|
wantList = makeWantList(
|
|
|
|
blocks.mapIt( it.cid ),
|
|
|
|
sendDontHave = true)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
proc sendPresence(peerId: PeerID, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
cid1Buf = blocks[0].cid.data.buffer
|
|
|
|
cid2Buf = blocks[1].cid.data.buffer
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
for p in presence:
|
2022-11-15 15:46:21 +00:00
|
|
|
if p.cid != cid1Buf and p.cid != cid2Buf:
|
|
|
|
check p.`type` == BlockPresenceType.DontHave
|
|
|
|
else:
|
|
|
|
check p.`type` == BlockPresenceType.Have
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendPresence: sendPresence
|
2022-01-10 15:32:56 +00:00
|
|
|
))
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
(await engine.localStore.putBlock(blocks[0])).tryGet()
|
|
|
|
(await engine.localStore.putBlock(blocks[1])).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
await engine.wantListHandler(peerId, wantList)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await done
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should store blocks in local store":
|
2021-02-26 00:23:22 +00:00
|
|
|
let pending = blocks.mapIt(
|
2022-05-12 21:52:03 +00:00
|
|
|
engine.pendingBlocks.getWantHandle( it.cid )
|
2021-02-26 00:23:22 +00:00
|
|
|
)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
await engine.blocksHandler(peerId, blocks)
|
2021-02-26 00:23:22 +00:00
|
|
|
let resolved = await allFinished(pending)
|
|
|
|
check resolved.mapIt( it.read ) == blocks
|
|
|
|
for b in blocks:
|
2022-07-28 00:39:17 +00:00
|
|
|
let present = await engine.localStore.hasBlock(b.cid)
|
|
|
|
check present.tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should send payments for received blocks":
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
done = newFuture[void]()
|
|
|
|
account = Account(address: EthAddress.example)
|
|
|
|
peerContext = peerStore.get(peerId)
|
2021-04-14 14:19:45 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
peerContext.account = account.some
|
|
|
|
peerContext.blocks = blocks.mapIt(
|
|
|
|
(it.cid, Presence(cid: it.cid, price: rand(uint16).u256))
|
|
|
|
).toTable
|
|
|
|
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendPayment: proc(receiver: PeerID, payment: SignedState) {.gcsafe, async.} =
|
|
|
|
let
|
|
|
|
amount =
|
|
|
|
blocks.mapIt(
|
|
|
|
peerContext.blocks[it.cid].price
|
|
|
|
).foldl(a + b)
|
|
|
|
|
|
|
|
balances = !payment.state.outcome.balances(Asset)
|
|
|
|
|
|
|
|
check receiver == peerId
|
|
|
|
check balances[account.address.toDestination] == amount
|
|
|
|
done.complete()
|
2022-01-10 15:32:56 +00:00
|
|
|
))
|
2021-04-14 14:19:45 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
await engine.blocksHandler(peerId, blocks)
|
2021-04-14 14:19:45 +00:00
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should handle block presence":
|
2022-11-15 15:46:21 +00:00
|
|
|
var
|
|
|
|
handles: Table[Cid, Future[bt.Block]]
|
|
|
|
|
|
|
|
proc sendWantList(
|
|
|
|
id: PeerID,
|
|
|
|
cids: seq[Cid],
|
|
|
|
priority: int32 = 0,
|
|
|
|
cancel: bool = false,
|
|
|
|
wantType: WantType = WantType.WantHave,
|
|
|
|
full: bool = false,
|
|
|
|
sendDontHave: bool = false) {.gcsafe, async.} =
|
|
|
|
engine.pendingBlocks.resolve(blocks.filterIt( it.cid in cids ))
|
|
|
|
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendWantList: sendWantList
|
|
|
|
))
|
|
|
|
|
|
|
|
# only Cids in peer want lists are requested
|
|
|
|
handles = blocks.mapIt(
|
|
|
|
(it.cid, engine.pendingBlocks.getWantHandle( it.cid ))).toTable
|
|
|
|
|
2021-04-26 15:11:11 +00:00
|
|
|
let price = UInt256.example
|
2022-01-10 15:32:56 +00:00
|
|
|
await engine.blockPresenceHandler(
|
2021-02-26 00:23:22 +00:00
|
|
|
peerId,
|
|
|
|
blocks.mapIt(
|
2021-04-26 15:11:11 +00:00
|
|
|
PresenceMessage.init(
|
|
|
|
Presence(
|
|
|
|
cid: it.cid,
|
|
|
|
have: true,
|
|
|
|
price: price
|
|
|
|
))))
|
|
|
|
|
|
|
|
for cid in blocks.mapIt(it.cid):
|
2022-11-15 15:46:21 +00:00
|
|
|
check cid in peerCtx.peerHave
|
|
|
|
check peerCtx.blocks[cid].price == price
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
suite "Task Handler":
|
|
|
|
var
|
2022-05-19 02:29:15 +00:00
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
|
|
|
peerId: PeerID
|
|
|
|
chunker: Chunker
|
|
|
|
wallet: WalletRef
|
|
|
|
blockDiscovery: Discovery
|
|
|
|
peerStore: PeerCtxStore
|
|
|
|
pendingBlocks: PendingBlocksManager
|
|
|
|
network: BlockExcNetwork
|
2021-08-30 19:25:20 +00:00
|
|
|
engine: BlockExcEngine
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery: DiscoveryEngine
|
|
|
|
localStore: BlockStore
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
peersCtx: seq[BlockExcPeerCtx]
|
2021-02-26 00:23:22 +00:00
|
|
|
peers: seq[PeerID]
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
setup:
|
2022-05-19 02:29:15 +00:00
|
|
|
rng = Rng.instance()
|
|
|
|
chunker = RandomChunker.new(rng, size = 1024, chunkSize = 256)
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
|
|
|
peerId = PeerID.init(seckey.getPublicKey().tryGet()).tryGet()
|
|
|
|
wallet = WalletRef.example
|
|
|
|
blockDiscovery = Discovery.new()
|
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
|
|
|
|
localStore = CacheStore.new()
|
|
|
|
network = BlockExcNetwork()
|
|
|
|
|
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks)
|
|
|
|
|
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
2021-02-26 00:23:22 +00:00
|
|
|
peersCtx = @[]
|
|
|
|
|
|
|
|
for i in 0..3:
|
2022-01-13 00:42:18 +00:00
|
|
|
let seckey = PrivateKey.random(rng[]).tryGet()
|
|
|
|
peers.add(PeerID.init(seckey.getPublicKey().tryGet()).tryGet())
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
peersCtx.add(BlockExcPeerCtx(
|
2021-02-26 00:23:22 +00:00
|
|
|
id: peers[i]
|
|
|
|
))
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore.add(peersCtx[i])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-05-10 10:47:45 +00:00
|
|
|
engine.pricing = Pricing.example.some
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
test "Should send want-blocks in priority order":
|
|
|
|
proc sendBlocks(
|
|
|
|
id: PeerID,
|
2022-07-29 16:19:34 +00:00
|
|
|
blks: seq[bt.Block]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
check blks.len == 2
|
|
|
|
check:
|
|
|
|
blks[1].cid == blocks[0].cid
|
|
|
|
blks[0].cid == blocks[1].cid
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
for blk in blocks:
|
2022-07-28 00:39:17 +00:00
|
|
|
(await engine.localStore.putBlock(blk)).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
engine.network.request.sendBlocks = sendBlocks
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# second block to send by priority
|
|
|
|
peersCtx[0].peerWants.add(
|
|
|
|
Entry(
|
|
|
|
`block`: blocks[0].cid.data.buffer,
|
|
|
|
priority: 49,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantBlock,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
)
|
|
|
|
|
|
|
|
# first block to send by priority
|
|
|
|
peersCtx[0].peerWants.add(
|
|
|
|
Entry(
|
|
|
|
`block`: blocks[1].cid.data.buffer,
|
|
|
|
priority: 50,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantBlock,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
)
|
|
|
|
|
|
|
|
await engine.taskHandler(peersCtx[0])
|
|
|
|
|
|
|
|
test "Should send presence":
|
2021-05-10 09:20:37 +00:00
|
|
|
let present = blocks
|
2022-03-18 19:50:53 +00:00
|
|
|
let missing = @[bt.Block.new("missing".toBytes).tryGet()]
|
2021-05-10 10:47:45 +00:00
|
|
|
let price = (!engine.pricing).price
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
proc sendPresence(id: PeerID, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-05-10 09:20:37 +00:00
|
|
|
check presence.mapIt(!Presence.init(it)) == @[
|
2021-05-10 10:47:45 +00:00
|
|
|
Presence(cid: present[0].cid, have: true, price: price),
|
|
|
|
Presence(cid: present[1].cid, have: true, price: price),
|
2021-05-10 09:20:37 +00:00
|
|
|
Presence(cid: missing[0].cid, have: false)
|
|
|
|
]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
for blk in blocks:
|
2022-07-28 00:39:17 +00:00
|
|
|
(await engine.localStore.putBlock(blk)).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
engine.network.request.sendPresence = sendPresence
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# have block
|
|
|
|
peersCtx[0].peerWants.add(
|
|
|
|
Entry(
|
2021-05-10 09:20:37 +00:00
|
|
|
`block`: present[0].cid.data.buffer,
|
2021-02-26 00:23:22 +00:00
|
|
|
priority: 1,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
)
|
|
|
|
|
|
|
|
# have block
|
|
|
|
peersCtx[0].peerWants.add(
|
|
|
|
Entry(
|
2021-05-10 09:20:37 +00:00
|
|
|
`block`: present[1].cid.data.buffer,
|
2021-02-26 00:23:22 +00:00
|
|
|
priority: 1,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
)
|
|
|
|
|
|
|
|
# don't have block
|
|
|
|
peersCtx[0].peerWants.add(
|
|
|
|
Entry(
|
2021-05-10 09:20:37 +00:00
|
|
|
`block`: missing[0].cid.data.buffer,
|
2021-02-26 00:23:22 +00:00
|
|
|
priority: 1,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
)
|
|
|
|
|
|
|
|
await engine.taskHandler(peersCtx[0])
|