2024-08-26 13:18:59 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/random
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p/routing_record
|
|
|
|
import pkg/codexdht/discv5/protocol as discv5
|
|
|
|
|
|
|
|
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/manifest
|
|
|
|
|
|
|
|
import ../../../asynctest
|
|
|
|
import ../../helpers
|
|
|
|
import ../../helpers/mockdiscovery
|
|
|
|
import ../../examples
|
|
|
|
|
|
|
|
asyncchecksuite "Advertiser":
|
|
|
|
var
|
|
|
|
blockDiscovery: MockDiscovery
|
|
|
|
localStore: BlockStore
|
|
|
|
advertiser: Advertiser
|
2024-12-05 09:30:01 +00:00
|
|
|
advertised: seq[Cid]
|
2024-08-26 13:18:59 +00:00
|
|
|
let
|
|
|
|
manifest = Manifest.new(
|
|
|
|
treeCid = Cid.example,
|
|
|
|
blockSize = 123.NBytes,
|
|
|
|
datasetSize = 234.NBytes)
|
|
|
|
manifestBlk = Block.new(data = manifest.encode().tryGet(), codec = ManifestCodec).tryGet()
|
|
|
|
|
|
|
|
setup:
|
|
|
|
blockDiscovery = MockDiscovery.new()
|
|
|
|
localStore = CacheStore.new()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
advertised = newSeq[Cid]()
|
|
|
|
blockDiscovery.publishBlockProvideHandler =
|
|
|
|
proc(d: MockDiscovery, cid: Cid) {.async, gcsafe.} =
|
|
|
|
advertised.add(cid)
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
advertiser = Advertiser.new(
|
|
|
|
localStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
|
|
|
|
await advertiser.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await advertiser.stop()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
proc waitTillQueueEmpty() {.async.} =
|
|
|
|
check eventually advertiser.advertiseQueue.len == 0
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
test "blockStored should queue manifest Cid for advertising":
|
|
|
|
(await localStore.putBlock(manifestBlk)).tryGet()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
await waitTillQueueEmpty()
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
check:
|
2024-12-05 09:30:01 +00:00
|
|
|
manifestBlk.cid in advertised
|
2024-08-26 13:18:59 +00:00
|
|
|
|
|
|
|
test "blockStored should queue tree Cid for advertising":
|
|
|
|
(await localStore.putBlock(manifestBlk)).tryGet()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
await waitTillQueueEmpty()
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
check:
|
2024-12-05 09:30:01 +00:00
|
|
|
manifest.treeCid in advertised
|
2024-08-26 13:18:59 +00:00
|
|
|
|
|
|
|
test "blockStored should not queue non-manifest non-tree CIDs for discovery":
|
|
|
|
let blk = bt.Block.example
|
|
|
|
|
|
|
|
(await localStore.putBlock(blk)).tryGet()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
await waitTillQueueEmpty()
|
|
|
|
|
2024-08-26 13:18:59 +00:00
|
|
|
check:
|
2024-12-05 09:30:01 +00:00
|
|
|
blk.cid notin advertised
|
2024-08-26 13:18:59 +00:00
|
|
|
|
|
|
|
test "Should not queue if there is already an inflight advertise request":
|
|
|
|
(await localStore.putBlock(manifestBlk)).tryGet()
|
|
|
|
(await localStore.putBlock(manifestBlk)).tryGet()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
await waitTillQueueEmpty()
|
|
|
|
|
|
|
|
check eventually advertised.len == 2
|
|
|
|
check manifestBlk.cid in advertised
|
|
|
|
check manifest.treeCid in advertised
|
2024-08-26 13:18:59 +00:00
|
|
|
|
|
|
|
test "Should advertise existing manifests and their trees":
|
|
|
|
let
|
|
|
|
newStore = CacheStore.new([manifestBlk])
|
|
|
|
|
|
|
|
await advertiser.stop()
|
|
|
|
advertiser = Advertiser.new(
|
|
|
|
newStore,
|
|
|
|
blockDiscovery
|
|
|
|
)
|
|
|
|
await advertiser.start()
|
|
|
|
|
2024-12-05 09:30:01 +00:00
|
|
|
check eventually manifestBlk.cid in advertised
|
|
|
|
check eventually manifest.treeCid in advertised
|
2024-08-26 13:18:59 +00:00
|
|
|
|
|
|
|
test "Stop should clear onBlockStored callback":
|
|
|
|
await advertiser.stop()
|
|
|
|
|
|
|
|
check:
|
|
|
|
localStore.onBlockStored.isNone()
|