2022-05-12 21:52:03 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/sugar
|
|
|
|
import std/tables
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
|
|
|
|
import pkg/libp2p
|
|
|
|
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
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/blocktype as bt
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
import ../../helpers/mockdiscovery
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
import ../../helpers
|
|
|
|
import ../../examples
|
|
|
|
|
|
|
|
suite "Block Advertising and Discovery":
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
localStore = CacheStore.new(blocks.mapIt( it ))
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
manifest = Manifest.new( blocks.mapIt( it.cid ) ).tryGet()
|
|
|
|
manifestBlock = bt.Block.new(
|
|
|
|
manifest.encode().tryGet(), codec = DagPBCodec).tryGet()
|
|
|
|
|
|
|
|
(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.} =
|
2022-07-28 00:39:17 +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()
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
test "Should advertise both manifests and blocks":
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
|
|
|
advertised = initTable.collect:
|
2022-12-03 00:00:55 +00:00
|
|
|
for b in (blocks & manifestBlock): {b.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
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
discovery.advertiseType = BlockType.Both
|
2022-05-12 21:52:03 +00:00
|
|
|
await engine.start() # fire up advertise loop
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(toSeq(advertised.values)))
|
2022-12-03 00:00:55 +00:00
|
|
|
await engine.stop()
|
|
|
|
|
|
|
|
test "Should advertise local manifests":
|
|
|
|
let
|
|
|
|
advertised = newFuture[Cid]()
|
|
|
|
|
|
|
|
blockDiscovery
|
|
|
|
.publishBlockProvideHandler = proc(d: MockDiscovery, cid: Cid) {.async.} =
|
|
|
|
check manifestBlock.cid == cid
|
|
|
|
advertised.complete(cid)
|
|
|
|
|
|
|
|
discovery.advertiseType = BlockType.Manifest
|
|
|
|
await engine.start() # fire up advertise loop
|
|
|
|
check (await advertised.wait(10.millis)) == manifestBlock.cid
|
|
|
|
await engine.stop()
|
|
|
|
|
|
|
|
test "Should advertise local blocks":
|
|
|
|
let
|
|
|
|
advertised = initTable.collect:
|
|
|
|
for b in blocks: {b.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()
|
|
|
|
|
|
|
|
discovery.advertiseType = BlockType.Block
|
|
|
|
await engine.start() # fire up advertise loop
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(toSeq(advertised.values)))
|
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:
|
|
|
|
{ blk.cid: Presence(cid: blk.cid, 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
|
|
|
|
|
|
|
|
await engine.start() # fire up discovery loop
|
|
|
|
engine.pendingBlocks.resolve(blocks)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks))
|
|
|
|
|
|
|
|
await engine.stop()
|
|
|
|
|
|
|
|
suite "E2E - Multiple Nodes Discovery":
|
|
|
|
let chunker = RandomChunker.new(Rng.instance(), size = 4096, chunkSize = 256)
|
|
|
|
|
|
|
|
var
|
|
|
|
switch: seq[Switch]
|
|
|
|
blockexc: seq[NetworkStore]
|
|
|
|
blocks: seq[bt.Block]
|
|
|
|
|
|
|
|
setup:
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
|
|
|
|
|
|
|
for _ in 0..<4:
|
|
|
|
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 = @[]
|
|
|
|
|
|
|
|
test "E2E - Should advertise and discover blocks":
|
|
|
|
# Distribute the blocks amongst 1..3
|
|
|
|
# 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
|
|
|
|
|
|
|
await blockexc[1].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[0..5])
|
|
|
|
await blockexc[2].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[4..10])
|
|
|
|
await blockexc[3].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[10..15])
|
|
|
|
|
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):
|
|
|
|
for b in blocks:
|
|
|
|
blockexc[0].engine.requestBlock(b.cid)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch.mapIt( it.start() ) &
|
2022-05-19 02:29:15 +00:00
|
|
|
blockexc.mapIt( it.engine.start() ))
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFutures(futs)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
blockexc.mapIt( it.engine.stop() ) &
|
2022-05-19 02:29:15 +00:00
|
|
|
switch.mapIt( it.stop() ))
|
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
|
|
|
|
# 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.} =
|
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
|
|
|
|
|
|
|
await blockexc[1].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[0..5])
|
|
|
|
await blockexc[2].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[4..10])
|
|
|
|
await blockexc[3].engine.blocksHandler(switch[0].peerInfo.peerId, blocks[10..15])
|
|
|
|
|
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
|
|
|
|
futs = blocks.mapIt( blockexc[0].engine.requestBlock( it.cid ) )
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch.mapIt( it.start() ) &
|
2022-05-19 02:29:15 +00:00
|
|
|
blockexc.mapIt( it.engine.start() ))
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFutures(futs).wait(10.seconds)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
blockexc.mapIt( it.engine.stop() ) &
|
2022-05-19 02:29:15 +00:00
|
|
|
switch.mapIt( it.stop() ))
|