uncomment engine tests

This commit is contained in:
gmega 2024-02-23 17:09:32 -03:00
parent a274d24583
commit 102ecdf571
No known key found for this signature in database
GPG Key ID: FFD8DAF00660270F
1 changed files with 352 additions and 339 deletions

View File

@ -20,110 +20,111 @@ import ../../../asynctest
import ../../helpers import ../../helpers
import ../../examples import ../../examples
# asyncchecksuite "NetworkStore engine basic": asyncchecksuite "NetworkStore engine basic":
# var var
# rng: Rng rng: Rng
# seckey: PrivateKey seckey: PrivateKey
# peerId: PeerId peerId: PeerId
# chunker: Chunker chunker: Chunker
# wallet: WalletRef wallet: WalletRef
# blockDiscovery: Discovery blockDiscovery: Discovery
# peerStore: PeerCtxStore peerStore: PeerCtxStore
# pendingBlocks: PendingBlocksManager pendingBlocks: PendingBlocksManager
# blocks: seq[Block] blocks: seq[Block]
# done: Future[void] done: Future[void]
# setup: setup:
# rng = Rng.instance() rng = Rng.instance()
# seckey = PrivateKey.random(rng[]).tryGet() seckey = PrivateKey.random(rng[]).tryGet()
# peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet() peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
# chunker = RandomChunker.new(Rng.instance(), size = 1024'nb, chunkSize = 256'nb) chunker = RandomChunker.new(Rng.instance(), size = 1024'nb, chunkSize = 256'nb)
# wallet = WalletRef.example wallet = WalletRef.example
# blockDiscovery = Discovery.new() blockDiscovery = Discovery.new()
# peerStore = PeerCtxStore.new() peerStore = PeerCtxStore.new()
# pendingBlocks = PendingBlocksManager.new() pendingBlocks = PendingBlocksManager.new()
# while true: while true:
# let chunk = await chunker.getBytes() let chunk = await chunker.getBytes()
# if chunk.len <= 0: if chunk.len <= 0:
# break break
# blocks.add(Block.new(chunk).tryGet()) blocks.add(Block.new(chunk).tryGet())
# done = newFuture[void]() done = newFuture[void]()
# test "Should send want list to new peers": test "Should send want list to new peers":
# proc sendWantList( proc sendWantList(
# id: PeerId, id: PeerId,
# addresses: seq[BlockAddress], addresses: seq[BlockAddress],
# priority: int32 = 0, priority: int32 = 0,
# cancel: bool = false, cancel: bool = false,
# wantType: WantType = WantType.WantHave, wantType: WantType = WantType.WantHave,
# full: bool = false, full: bool = false,
# sendDontHave: bool = false) {.gcsafe, async: (handleException: true).} = sendDontHave: bool = false) {.gcsafe, async: (handleException: true).} =
# check addresses.mapIt($it.cidOrTreeCid).sorted == blocks.mapIt($it.cid).sorted check addresses.mapIt($it.cidOrTreeCid).sorted == blocks.mapIt( $it.cid ).sorted
# done.complete() done.complete()
# let let
# network = BlockExcNetwork(request: BlockExcRequest( network = BlockExcNetwork(request: BlockExcRequest(
# sendWantList: sendWantList, sendWantList: sendWantList,
# )) ))
# localStore = CacheStore.new(blocks.mapIt( it )) localStore = CacheStore.new(blocks.mapIt( it ))
# discovery = DiscoveryEngine.new( discovery = DiscoveryEngine.new(
# localStore, localStore,
# peerStore, peerStore,
# network, network,
# blockDiscovery, blockDiscovery,
# pendingBlocks) pendingBlocks)
# engine = BlockExcEngine.new( engine = BlockExcEngine.new(
# localStore, localStore,
# wallet, wallet,
# network, network,
# discovery, discovery,
# peerStore, peerStore,
# pendingBlocks) pendingBlocks)
# for b in blocks: for b in blocks:
# discard engine.pendingBlocks.getWantHandle(b.cid) discard engine.pendingBlocks.getWantHandle(b.cid)
# await engine.setupPeer(peerId) await engine.setupPeer(peerId)
# await done.wait(100.millis) await done.wait(100.millis)
# test "Should send account to new peers": test "Should send account to new peers":
# let pricing = Pricing.example let pricing = Pricing.example
# proc sendAccount(peer: PeerId, account: Account) {.gcsafe, async: (handleException: true).} = proc sendAccount(peer: PeerId,
# check account.address == pricing.address account: Account) {.gcsafe, async: (handleException: true).} =
# done.complete() check account.address == pricing.address
done.complete()
# let let
# network = BlockExcNetwork( network = BlockExcNetwork(
# request: BlockExcRequest( request: BlockExcRequest(
# sendAccount: sendAccount sendAccount: sendAccount
# )) ))
# localStore = CacheStore.new() localStore = CacheStore.new()
# discovery = DiscoveryEngine.new( discovery = DiscoveryEngine.new(
# localStore, localStore,
# peerStore, peerStore,
# network, network,
# blockDiscovery, blockDiscovery,
# pendingBlocks) pendingBlocks)
# engine = BlockExcEngine.new( engine = BlockExcEngine.new(
# localStore, localStore,
# wallet, wallet,
# network, network,
# discovery, discovery,
# peerStore, peerStore,
# pendingBlocks) pendingBlocks)
# engine.pricing = pricing.some engine.pricing = pricing.some
# await engine.setupPeer(peerId) await engine.setupPeer(peerId)
# await done.wait(100.millis) await done.wait(100.millis)
asyncchecksuite "NetworkStore engine handlers": asyncchecksuite "NetworkStore engine handlers":
var var
@ -183,90 +184,99 @@ asyncchecksuite "NetworkStore engine handlers":
) )
engine.peers.add(peerCtx) engine.peers.add(peerCtx)
# test "Should schedule block requests": test "Should schedule block requests":
# let let
# wantList = makeWantList( wantList = makeWantList(
# blocks.mapIt( it.cid ), blocks.mapIt( it.cid ),
# wantType = WantType.WantBlock) # only `wantBlock` are stored in `peerWants` wantType = WantType.WantBlock) # only `wantBlock` are stored in `peerWants`
# proc handler() {.async: (handleException: true).} = proc handler() {.async: (handleException: true).} =
# let ctx = await engine.taskQueue.pop() let ctx = await engine.taskQueue.pop()
# check ctx.id == peerId check ctx.id == peerId
# # only `wantBlock` scheduled # only `wantBlock` scheduled
# check ctx.peerWants.mapIt( it.address.cidOrTreeCid ) == blocks.mapIt( it.cid ) check ctx.peerWants.mapIt( it.address.cidOrTreeCid ) == blocks.mapIt( it.cid )
# let done = handler() let done = handler()
# await engine.wantListHandler(peerId, wantList) await engine.wantListHandler(peerId, wantList)
# await done await done
# test "Should handle want list": test "Should handle want list":
# let let
# done = newFuture[void]() done = newFuture[void]()
# wantList = makeWantList(blocks.mapIt( it.cid )) wantList = makeWantList(blocks.mapIt( it.cid ))
# proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async: (handleException: true).} = proc sendPresence(
# check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address ) peerId: PeerId,
# done.complete() presence: seq[BlockPresence]
) {.gcsafe, async: (handleException: true).} =
check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address )
done.complete()
# engine.network = BlockExcNetwork( engine.network = BlockExcNetwork(
# request: BlockExcRequest( request: BlockExcRequest(
# sendPresence: sendPresence sendPresence: sendPresence
# )) ))
# await allFuturesThrowing( await allFuturesThrowing(
# allFinished(blocks.mapIt( localStore.putBlock(it) ))) allFinished(blocks.mapIt( localStore.putBlock(it) )))
# await engine.wantListHandler(peerId, wantList) await engine.wantListHandler(peerId, wantList)
# await done await done
# test "Should handle want list - `dont-have`": test "Should handle want list - `dont-have`":
# let let
# done = newFuture[void]() done = newFuture[void]()
# wantList = makeWantList( wantList = makeWantList(
# blocks.mapIt( it.cid ), blocks.mapIt( it.cid ),
# sendDontHave = true) sendDontHave = true)
# proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async: (handleException: true).} = proc sendPresence(
# check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address ) peerId: PeerId,
# for p in presence: presence: seq[BlockPresence]
# check: ) {.gcsafe, async: (handleException: true).} =
# p.`type` == BlockPresenceType.DontHave check presence.mapIt( it.address ) == wantList.entries.mapIt( it.address )
for p in presence:
check:
p.`type` == BlockPresenceType.DontHave
# done.complete() done.complete()
# engine.network = BlockExcNetwork(request: BlockExcRequest( engine.network = BlockExcNetwork(request: BlockExcRequest(
# sendPresence: sendPresence sendPresence: sendPresence
# )) ))
# await engine.wantListHandler(peerId, wantList) await engine.wantListHandler(peerId, wantList)
# await done await done
# test "Should handle want list - `dont-have` some blocks": test "Should handle want list - `dont-have` some blocks":
# let let
# done = newFuture[void]() done = newFuture[void]()
# wantList = makeWantList( wantList = makeWantList(
# blocks.mapIt( it.cid ), blocks.mapIt( it.cid ),
# sendDontHave = true) sendDontHave = true)
# proc sendPresence(peerId: PeerId, presence: seq[BlockPresence]) {.gcsafe, async: (handleException: true).} = proc sendPresence(
# for p in presence: peerId: PeerId,
# if p.address.cidOrTreeCid != blocks[0].cid and p.address.cidOrTreeCid != blocks[1].cid: presence: seq[BlockPresence]
# check p.`type` == BlockPresenceType.DontHave ) {.gcsafe, async: (handleException: true).} =
# else: for p in presence:
# check p.`type` == BlockPresenceType.Have if p.address.cidOrTreeCid != blocks[0].cid and p.address.cidOrTreeCid != blocks[1].cid:
check p.`type` == BlockPresenceType.DontHave
else:
check p.`type` == BlockPresenceType.Have
# done.complete() done.complete()
# engine.network = BlockExcNetwork( engine.network = BlockExcNetwork(
# request: BlockExcRequest( request: BlockExcRequest(
# sendPresence: sendPresence sendPresence: sendPresence
# )) ))
# (await engine.localStore.putBlock(blocks[0])).tryGet() (await engine.localStore.putBlock(blocks[0])).tryGet()
# (await engine.localStore.putBlock(blocks[1])).tryGet() (await engine.localStore.putBlock(blocks[1])).tryGet()
# await engine.wantListHandler(peerId, wantList) await engine.wantListHandler(peerId, wantList)
# await done await done
test "Should store blocks in local store": test "Should store blocks in local store":
let pending = blocks.mapIt( let pending = blocks.mapIt(
@ -275,8 +285,6 @@ asyncchecksuite "NetworkStore engine handlers":
let blocksDelivery = blocks.mapIt(BlockDelivery(blk: it, address: it.address)) let blocksDelivery = blocks.mapIt(BlockDelivery(blk: it, address: it.address))
echo "BLD: ", $blocksDelivery[0]
await engine.blocksDeliveryHandler(peerId, blocksDelivery) await engine.blocksDeliveryHandler(peerId, blocksDelivery)
let resolved = await allFinished(pending) let resolved = await allFinished(pending)
check resolved.mapIt( it.read ) == blocks check resolved.mapIt( it.read ) == blocks
@ -284,221 +292,226 @@ asyncchecksuite "NetworkStore engine handlers":
let present = await engine.localStore.hasBlock(b.cid) let present = await engine.localStore.hasBlock(b.cid)
check present.tryGet() check present.tryGet()
# test "Should send payments for received blocks": test "Should send payments for received blocks":
# let let
# done = newFuture[void]() done = newFuture[void]()
# account = Account(address: EthAddress.example) account = Account(address: EthAddress.example)
# peerContext = peerStore.get(peerId) peerContext = peerStore.get(peerId)
# peerContext.account = account.some peerContext.account = account.some
# peerContext.blocks = blocks.mapIt( peerContext.blocks = blocks.mapIt(
# (it.address, Presence(address: it.address, price: rand(uint16).u256)) (it.address, Presence(address: it.address, price: rand(uint16).u256))
# ).toTable ).toTable
# engine.network = BlockExcNetwork( engine.network = BlockExcNetwork(
# request: BlockExcRequest( request: BlockExcRequest(
# sendPayment: proc(receiver: PeerId, payment: SignedState) {.gcsafe, async: (handleException: true).} = sendPayment: proc(
# let receiver: PeerId,
# amount = payment: SignedState
# blocks.mapIt( ) {.gcsafe, async: (handleException: true).} =
# peerContext.blocks[it.address].price let
# ).foldl(a + b) amount =
blocks.mapIt(
peerContext.blocks[it.address].price
).foldl(a + b)
# balances = !payment.state.outcome.balances(Asset) balances = !payment.state.outcome.balances(Asset)
# check receiver == peerId check receiver == peerId
# check balances[account.address.toDestination] == amount check balances[account.address.toDestination] == amount
# done.complete() done.complete()
# )) ))
# await engine.blocksDeliveryHandler(peerId, blocks.mapIt(BlockDelivery(blk: it, address: it.address))) await engine.blocksDeliveryHandler(peerId, blocks.mapIt(BlockDelivery(blk: it, address: it.address)))
# await done.wait(100.millis) await done.wait(100.millis)
# test "Should handle block presence": test "Should handle block presence":
# var var
# handles: Table[Cid, Future[Block]] handles: Table[Cid, Future[Block]]
# proc sendWantList( proc sendWantList(
# id: PeerId, id: PeerId,
# addresses: seq[BlockAddress], addresses: seq[BlockAddress],
# priority: int32 = 0, priority: int32 = 0,
# cancel: bool = false, cancel: bool = false,
# wantType: WantType = WantType.WantHave, wantType: WantType = WantType.WantHave,
# full: bool = false, full: bool = false,
# sendDontHave: bool = false) {.gcsafe, async.} = sendDontHave: bool = false) {.gcsafe, async: (handleException: true).} =
# engine.pendingBlocks.resolve(blocks engine.pendingBlocks.resolve(blocks
# .filterIt( it.address in addresses ) .filterIt( it.address in addresses )
# .mapIt(BlockDelivery(blk: it, address: it.address))) .mapIt(BlockDelivery(blk: it, address: it.address)))
# engine.network = BlockExcNetwork( engine.network = BlockExcNetwork(
# request: BlockExcRequest( request: BlockExcRequest(
# sendWantList: sendWantList sendWantList: sendWantList
# )) ))
# # only Cids in peer want lists are requested # only Cids in peer want lists are requested
# handles = blocks.mapIt( handles = blocks.mapIt(
# (it.cid, engine.pendingBlocks.getWantHandle( it.cid ))).toTable (it.cid, engine.pendingBlocks.getWantHandle( it.cid ))).toTable
# let price = UInt256.example let price = UInt256.example
# await engine.blockPresenceHandler( await engine.blockPresenceHandler(
# peerId, peerId,
# blocks.mapIt( blocks.mapIt(
# PresenceMessage.init( PresenceMessage.init(
# Presence( Presence(
# address: it.address, address: it.address,
# have: true, have: true,
# price: price price: price
# )))) ))))
# for a in blocks.mapIt(it.address): for a in blocks.mapIt(it.address):
# check a in peerCtx.peerHave check a in peerCtx.peerHave
# check peerCtx.blocks[a].price == price check peerCtx.blocks[a].price == price
# asyncchecksuite "Task Handler": asyncchecksuite "Task Handler":
# var var
# rng: Rng rng: Rng
# seckey: PrivateKey seckey: PrivateKey
# peerId: PeerId peerId: PeerId
# chunker: Chunker chunker: Chunker
# wallet: WalletRef wallet: WalletRef
# blockDiscovery: Discovery blockDiscovery: Discovery
# peerStore: PeerCtxStore peerStore: PeerCtxStore
# pendingBlocks: PendingBlocksManager pendingBlocks: PendingBlocksManager
# network: BlockExcNetwork network: BlockExcNetwork
# engine: BlockExcEngine engine: BlockExcEngine
# discovery: DiscoveryEngine discovery: DiscoveryEngine
# localStore: BlockStore localStore: BlockStore
# peersCtx: seq[BlockExcPeerCtx] peersCtx: seq[BlockExcPeerCtx]
# peers: seq[PeerId] peers: seq[PeerId]
# blocks: seq[Block] blocks: seq[Block]
# setup: setup:
# rng = Rng.instance() rng = Rng.instance()
# chunker = RandomChunker.new(rng, size = 1024, chunkSize = 256'nb) chunker = RandomChunker.new(rng, size = 1024, chunkSize = 256'nb)
# while true: while true:
# let chunk = await chunker.getBytes() let chunk = await chunker.getBytes()
# if chunk.len <= 0: if chunk.len <= 0:
# break break
# blocks.add(Block.new(chunk).tryGet()) blocks.add(Block.new(chunk).tryGet())
# seckey = PrivateKey.random(rng[]).tryGet() seckey = PrivateKey.random(rng[]).tryGet()
# peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet() peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
# wallet = WalletRef.example wallet = WalletRef.example
# blockDiscovery = Discovery.new() blockDiscovery = Discovery.new()
# peerStore = PeerCtxStore.new() peerStore = PeerCtxStore.new()
# pendingBlocks = PendingBlocksManager.new() pendingBlocks = PendingBlocksManager.new()
# localStore = CacheStore.new() localStore = CacheStore.new()
# network = BlockExcNetwork() network = BlockExcNetwork()
# discovery = DiscoveryEngine.new( discovery = DiscoveryEngine.new(
# localStore, localStore,
# peerStore, peerStore,
# network, network,
# blockDiscovery, blockDiscovery,
# pendingBlocks) pendingBlocks)
# engine = BlockExcEngine.new( engine = BlockExcEngine.new(
# localStore, localStore,
# wallet, wallet,
# network, network,
# discovery, discovery,
# peerStore, peerStore,
# pendingBlocks) pendingBlocks)
# peersCtx = @[] peersCtx = @[]
# for i in 0..3: for i in 0..3:
# let seckey = PrivateKey.random(rng[]).tryGet() let seckey = PrivateKey.random(rng[]).tryGet()
# peers.add(PeerId.init(seckey.getPublicKey().tryGet()).tryGet()) peers.add(PeerId.init(seckey.getPublicKey().tryGet()).tryGet())
# peersCtx.add(BlockExcPeerCtx( peersCtx.add(BlockExcPeerCtx(
# id: peers[i] id: peers[i]
# )) ))
# peerStore.add(peersCtx[i]) peerStore.add(peersCtx[i])
# engine.pricing = Pricing.example.some engine.pricing = Pricing.example.some
# test "Should send want-blocks in priority order": test "Should send want-blocks in priority order":
# proc sendBlocksDelivery( proc sendBlocksDelivery(
# id: PeerId, id: PeerId,
# blocksDelivery: seq[BlockDelivery]) {.gcsafe, async: (handleException: true).} = blocksDelivery: seq[BlockDelivery]
# check blocksDelivery.len == 2 ) {.gcsafe, async: (handleException: true).} =
# check: check blocksDelivery.len == 2
# blocksDelivery[1].address == blocks[0].address check:
# blocksDelivery[0].address == blocks[1].address blocksDelivery[1].address == blocks[0].address
blocksDelivery[0].address == blocks[1].address
# for blk in blocks: for blk in blocks:
# (await engine.localStore.putBlock(blk)).tryGet() (await engine.localStore.putBlock(blk)).tryGet()
# engine.network.request.sendBlocksDelivery = sendBlocksDelivery engine.network.request.sendBlocksDelivery = sendBlocksDelivery
# # second block to send by priority # second block to send by priority
# peersCtx[0].peerWants.add( peersCtx[0].peerWants.add(
# WantListEntry( WantListEntry(
# address: blocks[0].address, address: blocks[0].address,
# priority: 49, priority: 49,
# cancel: false, cancel: false,
# wantType: WantType.WantBlock, wantType: WantType.WantBlock,
# sendDontHave: false) sendDontHave: false)
# ) )
# # first block to send by priority # first block to send by priority
# peersCtx[0].peerWants.add( peersCtx[0].peerWants.add(
# WantListEntry( WantListEntry(
# address: blocks[1].address, address: blocks[1].address,
# priority: 50, priority: 50,
# cancel: false, cancel: false,
# wantType: WantType.WantBlock, wantType: WantType.WantBlock,
# sendDontHave: false) sendDontHave: false)
# ) )
# await engine.taskHandler(peersCtx[0]) await engine.taskHandler(peersCtx[0])
# test "Should send presence": test "Should send presence":
# let present = blocks let present = blocks
# let missing = @[Block.new("missing".toBytes).tryGet()] let missing = @[Block.new("missing".toBytes).tryGet()]
# let price = (!engine.pricing).price let price = (!engine.pricing).price
# proc sendPresence(id: PeerId, presence: seq[BlockPresence]) {.gcsafe, async: (handleException: true).} = proc sendPresence(id: PeerId,
# check presence.mapIt(!Presence.init(it)) == @[ presence: seq[BlockPresence]) {.gcsafe, async: (handleException: true).} =
# Presence(address: present[0].address, have: true, price: price), check presence.mapIt(!Presence.init(it)) == @[
# Presence(address: present[1].address, have: true, price: price), Presence(address: present[0].address, have: true, price: price),
# Presence(address: missing[0].address, have: false) Presence(address: present[1].address, have: true, price: price),
# ] Presence(address: missing[0].address, have: false)
]
# for blk in blocks: for blk in blocks:
# (await engine.localStore.putBlock(blk)).tryGet() (await engine.localStore.putBlock(blk)).tryGet()
# engine.network.request.sendPresence = sendPresence engine.network.request.sendPresence = sendPresence
# # have block # have block
# peersCtx[0].peerWants.add( peersCtx[0].peerWants.add(
# WantListEntry( WantListEntry(
# address: present[0].address, address: present[0].address,
# priority: 1, priority: 1,
# cancel: false, cancel: false,
# wantType: WantType.WantHave, wantType: WantType.WantHave,
# sendDontHave: false) sendDontHave: false)
# ) )
# # have block # have block
# peersCtx[0].peerWants.add( peersCtx[0].peerWants.add(
# WantListEntry( WantListEntry(
# address: present[1].address, address: present[1].address,
# priority: 1, priority: 1,
# cancel: false, cancel: false,
# wantType: WantType.WantHave, wantType: WantType.WantHave,
# sendDontHave: false) sendDontHave: false)
# ) )
# # don't have block # don't have block
# peersCtx[0].peerWants.add( peersCtx[0].peerWants.add(
# WantListEntry( WantListEntry(
# address: missing[0].address, address: missing[0].address,
# priority: 1, priority: 1,
# cancel: false, cancel: false,
# wantType: WantType.WantHave, wantType: WantType.WantHave,
# sendDontHave: false) sendDontHave: false)
# ) )
# await engine.taskHandler(peersCtx[0]) await engine.taskHandler(peersCtx[0])