2022-05-12 21:52:03 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/sugar
|
|
|
|
import std/tables
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
|
|
|
import pkg/libp2p/errors
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blockexchange
|
|
|
|
import pkg/codex/chunker
|
2022-12-03 00:00:55 +00:00
|
|
|
import pkg/codex/manifest
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/codex/merkletree
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/blocktype as bt
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
import ../../../asynctest
|
2022-05-12 21:52:03 +00:00
|
|
|
import ../../helpers
|
2024-01-29 20:03:51 +00:00
|
|
|
import ../../helpers/mockdiscovery
|
2022-05-12 21:52:03 +00:00
|
|
|
import ../../examples
|
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Block Advertising and Discovery":
|
2022-05-12 21:52:03 +00:00
|
|
|
let chunker = RandomChunker.new(Rng.instance(), size = 4096, chunkSize = 256)
|
|
|
|
|
|
|
|
var
|
|
|
|
blocks: seq[bt.Block]
|
2022-12-03 00:00:55 +00:00
|
|
|
manifest: Manifest
|
2023-12-21 06:41:43 +00:00
|
|
|
tree: CodexTree
|
2022-12-03 00:00:55 +00:00
|
|
|
manifestBlock: bt.Block
|
2022-05-12 21:52:03 +00:00
|
|
|
switch: Switch
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore: PeerCtxStore
|
|
|
|
blockDiscovery: MockDiscovery
|
|
|
|
discovery: DiscoveryEngine
|
2022-05-12 21:52:03 +00:00
|
|
|
wallet: WalletRef
|
|
|
|
network: BlockExcNetwork
|
|
|
|
localStore: CacheStore
|
|
|
|
engine: BlockExcEngine
|
2022-05-19 02:29:15 +00:00
|
|
|
pendingBlocks: PendingBlocksManager
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
|
|
|
|
|
|
|
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
|
2022-05-26 02:29:31 +00:00
|
|
|
blockDiscovery = MockDiscovery.new()
|
2022-05-12 21:52:03 +00:00
|
|
|
wallet = WalletRef.example
|
|
|
|
network = BlockExcNetwork.new(switch)
|
2024-05-09 08:03:35 +00:00
|
|
|
localStore = CacheStore.new(blocks.mapIt(it))
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
(manifest, tree) = makeManifestAndTree(blocks).tryGet()
|
2022-12-03 00:00:55 +00:00
|
|
|
manifestBlock = bt.Block.new(
|
2023-12-22 12:04:01 +00:00
|
|
|
manifest.encode().tryGet(), codec = ManifestCodec).tryGet()
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
(await localStore.putBlock(manifestBlock)).tryGet()
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks,
|
|
|
|
minPeersPerBlock = 1)
|
|
|
|
|
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
switch.mount(network)
|
|
|
|
|
|
|
|
test "Should discover want list":
|
|
|
|
let
|
|
|
|
pendingBlocks = blocks.mapIt(
|
|
|
|
engine.pendingBlocks.getWantHandle(it.cid)
|
|
|
|
)
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
await engine.start()
|
|
|
|
|
2022-05-26 02:29:31 +00:00
|
|
|
blockDiscovery.publishBlockProvideHandler =
|
2022-05-19 02:29:15 +00:00
|
|
|
proc(d: MockDiscovery, cid: Cid): Future[void] {.async, gcsafe.} =
|
|
|
|
return
|
|
|
|
|
|
|
|
blockDiscovery.findBlockProvidersHandler =
|
|
|
|
proc(d: MockDiscovery, cid: Cid): Future[seq[SignedPeerRecord]] {.async.} =
|
2024-05-09 08:03:35 +00:00
|
|
|
await engine.resolveBlocks(blocks.filterIt(it.cid == cid))
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks))
|
|
|
|
|
|
|
|
await engine.stop()
|
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
test "Should advertise both manifests and trees":
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
2024-05-09 08:03:35 +00:00
|
|
|
cids = @[manifest.cid.tryGet, manifest.treeCid]
|
2022-05-12 21:52:03 +00:00
|
|
|
advertised = initTable.collect:
|
2024-05-09 08:03:35 +00:00
|
|
|
for cid in cids: {cid: newFuture[void]()}
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
blockDiscovery
|
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid) {.async.} =
|
|
|
|
if cid in advertised and not advertised[cid].finished():
|
|
|
|
advertised[cid].complete()
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
await engine.start()
|
2022-05-12 21:52:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(toSeq(advertised.values)))
|
2022-12-03 00:00:55 +00:00
|
|
|
await engine.stop()
|
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
test "Should not advertise local blocks":
|
2022-12-03 00:00:55 +00:00
|
|
|
let
|
2024-05-09 08:03:35 +00:00
|
|
|
blockCids = blocks.mapIt(it.cid)
|
2022-12-03 00:00:55 +00:00
|
|
|
|
|
|
|
blockDiscovery
|
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid) {.async.} =
|
2024-05-09 08:03:35 +00:00
|
|
|
check:
|
|
|
|
cid notin blockCids
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
await engine.start()
|
|
|
|
await sleepAsync(3.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
await engine.stop()
|
|
|
|
|
|
|
|
test "Should not launch discovery if remote peer has block":
|
|
|
|
let
|
|
|
|
pendingBlocks = blocks.mapIt(
|
|
|
|
engine.pendingBlocks.getWantHandle(it.cid)
|
|
|
|
)
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId = PeerId.example
|
2022-05-12 21:52:03 +00:00
|
|
|
haves = collect(initTable()):
|
2022-11-15 15:46:21 +00:00
|
|
|
for blk in blocks:
|
2023-11-14 12:02:17 +00:00
|
|
|
{ blk.address: Presence(address: blk.address, price: 0.u256) }
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
engine.peers.add(
|
|
|
|
BlockExcPeerCtx(
|
|
|
|
id: peerId,
|
2022-11-15 15:46:21 +00:00
|
|
|
blocks: haves
|
2022-05-12 21:52:03 +00:00
|
|
|
))
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
blockDiscovery.findBlockProvidersHandler =
|
|
|
|
proc(d: MockDiscovery, cid: Cid): Future[seq[SignedPeerRecord]] =
|
2022-05-12 21:52:03 +00:00
|
|
|
check false
|
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
await engine.start()
|
2023-11-14 12:02:17 +00:00
|
|
|
engine.pendingBlocks.resolve(blocks.mapIt(BlockDelivery(blk: it, address: it.address)))
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks))
|
|
|
|
|
|
|
|
await engine.stop()
|
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
proc asBlock(m: Manifest): bt.Block =
|
|
|
|
let mdata = m.encode().tryGet()
|
|
|
|
bt.Block.new(data = mdata, codec = ManifestCodec).tryGet()
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
asyncchecksuite "E2E - Multiple Nodes Discovery":
|
2022-05-12 21:52:03 +00:00
|
|
|
var
|
|
|
|
switch: seq[Switch]
|
|
|
|
blockexc: seq[NetworkStore]
|
2024-05-09 08:03:35 +00:00
|
|
|
manifests: seq[Manifest]
|
|
|
|
mBlocks: seq[bt.Block]
|
|
|
|
trees: seq[CodexTree]
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
for _ in 0..<4:
|
2024-05-09 08:03:35 +00:00
|
|
|
let chunker = RandomChunker.new(Rng.instance(), size = 4096, chunkSize = 256)
|
|
|
|
var blocks = newSeq[bt.Block]()
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
|
|
|
let (manifest, tree) = makeManifestAndTree(blocks).tryGet()
|
|
|
|
manifests.add(manifest)
|
|
|
|
mBlocks.add(manifest.asBlock())
|
|
|
|
trees.add(tree)
|
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
|
|
|
s = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
|
2022-05-26 02:29:31 +00:00
|
|
|
blockDiscovery = MockDiscovery.new()
|
2022-05-12 21:52:03 +00:00
|
|
|
wallet = WalletRef.example
|
|
|
|
network = BlockExcNetwork.new(s)
|
|
|
|
localStore = CacheStore.new()
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
|
|
|
|
discovery = DiscoveryEngine.new(
|
|
|
|
localStore,
|
|
|
|
peerStore,
|
|
|
|
network,
|
|
|
|
blockDiscovery,
|
|
|
|
pendingBlocks,
|
|
|
|
minPeersPerBlock = 1)
|
|
|
|
|
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore,
|
|
|
|
wallet,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
peerStore,
|
|
|
|
pendingBlocks)
|
2022-05-12 21:52:03 +00:00
|
|
|
networkStore = NetworkStore.new(engine, localStore)
|
|
|
|
|
|
|
|
s.mount(network)
|
|
|
|
switch.add(s)
|
|
|
|
blockexc.add(networkStore)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
switch = @[]
|
|
|
|
blockexc = @[]
|
2024-05-09 08:03:35 +00:00
|
|
|
manifests = @[]
|
|
|
|
mBlocks = @[]
|
|
|
|
trees = @[]
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
test "E2E - Should advertise and discover blocks":
|
2024-05-09 08:03:35 +00:00
|
|
|
# Distribute the manifests and trees amongst 1..3
|
2022-05-12 21:52:03 +00:00
|
|
|
# Ask 0 to download everything without connecting him beforehand
|
|
|
|
|
|
|
|
var advertised: Table[Cid, SignedPeerRecord]
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[1].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2023-03-09 11:23:45 +00:00
|
|
|
advertised[cid] = switch[1].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[2].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2023-03-09 11:23:45 +00:00
|
|
|
advertised[cid] = switch[2].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[3].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2023-03-09 11:23:45 +00:00
|
|
|
advertised[cid] = switch[3].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[1].engine.pendingBlocks.getWantHandle(mBlocks[0].cid)
|
|
|
|
await blockexc[1].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[0], address: BlockAddress(leaf: false, cid: mBlocks[0].cid))])
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[2].engine.pendingBlocks.getWantHandle(mBlocks[1].cid)
|
|
|
|
await blockexc[2].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[1], address: BlockAddress(leaf: false, cid: mBlocks[1].cid))])
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[3].engine.pendingBlocks.getWantHandle(mBlocks[2].cid)
|
|
|
|
await blockexc[3].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[2], address: BlockAddress(leaf: false, cid: mBlocks[2].cid))])
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[0].engine.discovery.discovery)
|
|
|
|
.findBlockProvidersHandler = proc(d: MockDiscovery, cid: Cid):
|
|
|
|
Future[seq[SignedPeerRecord]] {.async.} =
|
2022-05-12 21:52:03 +00:00
|
|
|
if cid in advertised:
|
|
|
|
result.add(advertised[cid])
|
|
|
|
|
|
|
|
let futs = collect(newSeq):
|
2024-05-09 08:03:35 +00:00
|
|
|
for m in mBlocks[0..2]:
|
|
|
|
blockexc[0].engine.requestBlock(m.cid)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2024-05-09 08:03:35 +00:00
|
|
|
switch.mapIt(it.start()) &
|
|
|
|
blockexc.mapIt(it.engine.start())).wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
await allFutures(futs).wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2024-05-09 08:03:35 +00:00
|
|
|
blockexc.mapIt(it.engine.stop()) &
|
|
|
|
switch.mapIt(it.stop())).wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
test "E2E - Should advertise and discover blocks with peers already connected":
|
|
|
|
# Distribute the blocks amongst 1..3
|
2024-05-09 08:03:35 +00:00
|
|
|
# Ask 0 to download everything *WITH* connecting him beforehand
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
var advertised: Table[Cid, SignedPeerRecord]
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[1].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
advertised[cid] = switch[1].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[2].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
advertised[cid] = switch[2].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[3].engine.discovery.discovery)
|
2022-05-26 02:29:31 +00:00
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid): Future[void] {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
advertised[cid] = switch[3].peerInfo.signedPeerRecord
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[1].engine.pendingBlocks.getWantHandle(mBlocks[0].cid)
|
|
|
|
await blockexc[1].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[0], address: BlockAddress(leaf: false, cid: mBlocks[0].cid))])
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[2].engine.pendingBlocks.getWantHandle(mBlocks[1].cid)
|
|
|
|
await blockexc[2].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[1], address: BlockAddress(leaf: false, cid: mBlocks[1].cid))])
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-05-09 08:03:35 +00:00
|
|
|
discard blockexc[3].engine.pendingBlocks.getWantHandle(mBlocks[2].cid)
|
|
|
|
await blockexc[3].engine.blocksDeliveryHandler(switch[0].peerInfo.peerId, @[BlockDelivery(blk: mBlocks[2], address: BlockAddress(leaf: false, cid: mBlocks[2].cid))])
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
MockDiscovery(blockexc[0].engine.discovery.discovery)
|
|
|
|
.findBlockProvidersHandler = proc(d: MockDiscovery, cid: Cid):
|
|
|
|
Future[seq[SignedPeerRecord]] {.async.} =
|
2022-05-12 21:52:03 +00:00
|
|
|
if cid in advertised:
|
2022-05-19 02:29:15 +00:00
|
|
|
return @[advertised[cid]]
|
|
|
|
|
|
|
|
let
|
2024-05-09 08:03:35 +00:00
|
|
|
futs = mBlocks[0..2].mapIt(blockexc[0].engine.requestBlock(it.cid))
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2024-05-09 08:03:35 +00:00
|
|
|
switch.mapIt(it.start()) &
|
|
|
|
blockexc.mapIt(it.engine.start())).wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFutures(futs).wait(10.seconds)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2024-05-09 08:03:35 +00:00
|
|
|
blockexc.mapIt(it.engine.stop()) &
|
|
|
|
switch.mapIt(it.stop())).wait(10.seconds)
|