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/chronos
|
2023-08-01 23:47:57 +00:00
|
|
|
import pkg/libp2p/errors
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/libp2p/routing_record
|
2023-08-01 23:47:57 +00:00
|
|
|
import pkg/codexdht/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
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/codex/blocktype
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/utils/asyncheapqueue
|
2024-04-24 07:30:02 +00:00
|
|
|
import pkg/codex/manifest
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
import ../../../asynctest
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../../helpers
|
|
|
|
import ../../examples
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "NetworkStore engine basic":
|
2022-05-19 02:29:15 +00:00
|
|
|
var
|
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId: PeerId
|
2022-05-19 02:29:15 +00:00
|
|
|
chunker: Chunker
|
|
|
|
wallet: WalletRef
|
|
|
|
blockDiscovery: Discovery
|
|
|
|
peerStore: PeerCtxStore
|
|
|
|
pendingBlocks: PendingBlocksManager
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks: seq[Block]
|
2022-05-19 02:29:15 +00:00
|
|
|
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()
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 1024'nb, chunkSize = 256'nb)
|
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
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks.add(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(
|
2023-03-10 07:02:54 +00:00
|
|
|
id: PeerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
addresses: seq[BlockAddress],
|
2021-02-26 00:23:22 +00:00
|
|
|
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.} =
|
2023-11-14 12:02:17 +00:00
|
|
|
check addresses.mapIt($it.cidOrTreeCid).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)
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser = Advertiser.new(
|
|
|
|
localStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
|
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,
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser,
|
2022-05-19 02:29:15 +00:00
|
|
|
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
|
|
|
|
|
2023-03-10 07:02:54 +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)
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser = Advertiser.new(
|
|
|
|
localStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser,
|
2022-05-19 02:29:15 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "NetworkStore engine handlers":
|
2021-02-26 00:23:22 +00:00
|
|
|
var
|
2022-05-19 02:29:15 +00:00
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId: PeerId
|
2022-05-19 02:29:15 +00:00
|
|
|
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
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser: Advertiser
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx: BlockExcPeerCtx
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore: BlockStore
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks: seq[Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2024-02-22 14:54:45 +00:00
|
|
|
const NopSendWantCancellationsProc = proc(
|
|
|
|
id: PeerId,
|
|
|
|
addresses: seq[BlockAddress]
|
|
|
|
) {.gcsafe, async.} = discard
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
setup:
|
2022-05-19 02:29:15 +00:00
|
|
|
rng = Rng.instance()
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(rng, size = 1024'nb, chunkSize = 256'nb)
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks.add(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()
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
|
2022-05-19 02:29:15 +00:00
|
|
|
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)
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser = Advertiser.new(
|
|
|
|
localStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser,
|
2022-05-19 02:29:15 +00:00
|
|
|
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
|
2023-11-14 12:02:17 +00:00
|
|
|
check ctx.peerWants.mapIt( it.address.cidOrTreeCid ) == blocks.mapIt( it.cid )
|
2021-02-26 00:23:22 +00:00
|
|
|
|
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 ))
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2023-11-14 12:02:17 +00:00
|
|
|
check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address )
|
2022-11-15 15:46:21 +00:00
|
|
|
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)
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2023-11-14 12:02:17 +00:00
|
|
|
check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address )
|
2021-02-26 00:23:22 +00:00
|
|
|
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)
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
for p in presence:
|
2023-11-14 12:02:17 +00:00
|
|
|
if p.address.cidOrTreeCid != blocks[0].cid and p.address.cidOrTreeCid != blocks[1].cid:
|
2022-11-15 15:46:21 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
let blocksDelivery = blocks.mapIt(BlockDelivery(blk: it, address: it.address))
|
|
|
|
|
2024-02-22 14:54:45 +00:00
|
|
|
# Install NOP for want list cancellations so they don't cause a crash
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(sendWantCancellations: NopSendWantCancellationsProc))
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
await engine.blocksDeliveryHandler(peerId, blocksDelivery)
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
(it.address, Presence(address: it.address, price: rand(uint16).u256))
|
2022-11-15 15:46:21 +00:00
|
|
|
).toTable
|
|
|
|
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
2023-03-10 07:02:54 +00:00
|
|
|
sendPayment: proc(receiver: PeerId, payment: SignedState) {.gcsafe, async.} =
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
amount =
|
|
|
|
blocks.mapIt(
|
2023-11-14 12:02:17 +00:00
|
|
|
peerContext.blocks[it.address].price
|
2022-11-15 15:46:21 +00:00
|
|
|
).foldl(a + b)
|
|
|
|
|
|
|
|
balances = !payment.state.outcome.balances(Asset)
|
|
|
|
|
|
|
|
check receiver == peerId
|
|
|
|
check balances[account.address.toDestination] == amount
|
2024-02-22 14:54:45 +00:00
|
|
|
done.complete(),
|
|
|
|
|
|
|
|
# Install NOP for want list cancellations so they don't cause a crash
|
|
|
|
sendWantCancellations: NopSendWantCancellationsProc
|
2022-01-10 15:32:56 +00:00
|
|
|
))
|
2021-04-14 14:19:45 +00:00
|
|
|
|
2024-02-22 14:54:45 +00:00
|
|
|
await engine.blocksDeliveryHandler(peerId, blocks.mapIt(
|
|
|
|
BlockDelivery(blk: it, address: it.address)))
|
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
|
2023-11-14 12:02:17 +00:00
|
|
|
handles: Table[Cid, Future[Block]]
|
2022-11-15 15:46:21 +00:00
|
|
|
|
|
|
|
proc sendWantList(
|
2023-03-10 07:02:54 +00:00
|
|
|
id: PeerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
addresses: seq[BlockAddress],
|
2022-11-15 15:46:21 +00:00
|
|
|
priority: int32 = 0,
|
|
|
|
cancel: bool = false,
|
|
|
|
wantType: WantType = WantType.WantHave,
|
|
|
|
full: bool = false,
|
|
|
|
sendDontHave: bool = false) {.gcsafe, async.} =
|
2023-11-14 12:02:17 +00:00
|
|
|
engine.pendingBlocks.resolve(blocks
|
|
|
|
.filterIt( it.address in addresses )
|
|
|
|
.mapIt(BlockDelivery(blk: it, address: it.address)))
|
2022-11-15 15:46:21 +00:00
|
|
|
|
|
|
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
address: it.address,
|
2021-04-26 15:11:11 +00:00
|
|
|
have: true,
|
|
|
|
price: price
|
|
|
|
))))
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
for a in blocks.mapIt(it.address):
|
|
|
|
check a in peerCtx.peerHave
|
|
|
|
check peerCtx.blocks[a].price == price
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2024-02-22 14:54:45 +00:00
|
|
|
test "Should send cancellations for received blocks":
|
|
|
|
let
|
|
|
|
pending = blocks.mapIt(engine.pendingBlocks.getWantHandle(it.cid))
|
|
|
|
blocksDelivery = blocks.mapIt(BlockDelivery(blk: it, address: it.address))
|
|
|
|
cancellations = newTable(
|
|
|
|
blocks.mapIt((it.address, newFuture[void]())).toSeq
|
|
|
|
)
|
|
|
|
|
|
|
|
proc sendWantCancellations(
|
|
|
|
id: PeerId,
|
|
|
|
addresses: seq[BlockAddress]
|
|
|
|
) {.gcsafe, async.} =
|
|
|
|
for address in addresses:
|
|
|
|
cancellations[address].complete()
|
|
|
|
|
|
|
|
engine.network = BlockExcNetwork(
|
|
|
|
request: BlockExcRequest(
|
|
|
|
sendWantCancellations: sendWantCancellations
|
|
|
|
))
|
|
|
|
|
|
|
|
await engine.blocksDeliveryHandler(peerId, blocksDelivery)
|
|
|
|
discard await allFinished(pending)
|
|
|
|
await allFuturesThrowing(cancellations.values().toSeq)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Task Handler":
|
2021-02-26 00:23:22 +00:00
|
|
|
var
|
2022-05-19 02:29:15 +00:00
|
|
|
rng: Rng
|
|
|
|
seckey: PrivateKey
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId: PeerId
|
2022-05-19 02:29:15 +00:00
|
|
|
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
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser: Advertiser
|
2022-05-19 02:29:15 +00:00
|
|
|
localStore: BlockStore
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
peersCtx: seq[BlockExcPeerCtx]
|
2023-03-10 07:02:54 +00:00
|
|
|
peers: seq[PeerId]
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks: seq[Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
setup:
|
2022-05-19 02:29:15 +00:00
|
|
|
rng = Rng.instance()
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(rng, size = 1024, chunkSize = 256'nb)
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks.add(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()
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
|
2022-05-19 02:29:15 +00:00
|
|
|
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)
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser = Advertiser.new(
|
|
|
|
localStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser,
|
2022-05-19 02:29:15 +00:00
|
|
|
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()
|
2023-03-10 07:02:54 +00:00
|
|
|
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":
|
2023-11-14 12:02:17 +00:00
|
|
|
proc sendBlocksDelivery(
|
2023-03-10 07:02:54 +00:00
|
|
|
id: PeerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
blocksDelivery: seq[BlockDelivery]) {.gcsafe, async.} =
|
|
|
|
check blocksDelivery.len == 2
|
2021-02-26 00:23:22 +00:00
|
|
|
check:
|
2023-11-14 12:02:17 +00:00
|
|
|
blocksDelivery[1].address == blocks[0].address
|
|
|
|
blocksDelivery[0].address == blocks[1].address
|
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()
|
2023-11-14 12:02:17 +00:00
|
|
|
engine.network.request.sendBlocksDelivery = sendBlocksDelivery
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# second block to send by priority
|
|
|
|
peersCtx[0].peerWants.add(
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry(
|
|
|
|
address: blocks[0].address,
|
2021-02-26 00:23:22 +00:00
|
|
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry(
|
|
|
|
address: blocks[1].address,
|
2021-02-26 00:23:22 +00:00
|
|
|
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])
|
|
|
|
|
2024-02-29 07:37:12 +00:00
|
|
|
test "Should set in-flight for outgoing blocks":
|
|
|
|
proc sendBlocksDelivery(
|
|
|
|
id: PeerId,
|
|
|
|
blocksDelivery: seq[BlockDelivery]) {.gcsafe, async.} =
|
|
|
|
check peersCtx[0].peerWants[0].inFlight
|
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
(await engine.localStore.putBlock(blk)).tryGet()
|
|
|
|
engine.network.request.sendBlocksDelivery = sendBlocksDelivery
|
|
|
|
|
|
|
|
peersCtx[0].peerWants.add(WantListEntry(
|
|
|
|
address: blocks[0].address,
|
|
|
|
priority: 50,
|
|
|
|
cancel: false,
|
|
|
|
wantType: WantType.WantBlock,
|
|
|
|
sendDontHave: false,
|
|
|
|
inFlight: false)
|
|
|
|
)
|
|
|
|
await engine.taskHandler(peersCtx[0])
|
|
|
|
|
|
|
|
test "Should clear in-flight when local lookup fails":
|
|
|
|
peersCtx[0].peerWants.add(WantListEntry(
|
|
|
|
address: blocks[0].address,
|
|
|
|
priority: 50,
|
|
|
|
cancel: false,
|
|
|
|
wantType: WantType.WantBlock,
|
|
|
|
sendDontHave: false,
|
|
|
|
inFlight: false)
|
|
|
|
)
|
|
|
|
await engine.taskHandler(peersCtx[0])
|
|
|
|
|
|
|
|
check not peersCtx[0].peerWants[0].inFlight
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
test "Should send presence":
|
2021-05-10 09:20:37 +00:00
|
|
|
let present = blocks
|
2023-11-14 12:02:17 +00:00
|
|
|
let missing = @[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
|
|
|
|
2023-03-10 07:02:54 +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)) == @[
|
2023-11-14 12:02:17 +00:00
|
|
|
Presence(address: present[0].address, have: true, price: price),
|
|
|
|
Presence(address: present[1].address, have: true, price: price),
|
|
|
|
Presence(address: missing[0].address, have: false)
|
2021-05-10 09:20:37 +00:00
|
|
|
]
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry(
|
|
|
|
address: present[0].address,
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry(
|
|
|
|
address: present[1].address,
|
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(
|
2023-11-14 12:02:17 +00:00
|
|
|
WantListEntry(
|
|
|
|
address: missing[0].address,
|
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])
|