2022-05-26 02:29:31 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p/errors
|
|
|
|
import pkg/contractabi as ca
|
|
|
|
|
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/storageproofs
|
|
|
|
import pkg/codex/discovery
|
|
|
|
import pkg/codex/manifest
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/storageproofs as st
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/streams
|
|
|
|
|
|
|
|
import ../examples
|
|
|
|
import ../helpers
|
|
|
|
|
|
|
|
const
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize = 31'nb * 64
|
2022-05-26 02:29:31 +00:00
|
|
|
DataSetSize = BlockSize * 100
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Storage Proofs Network":
|
2022-05-26 02:29:31 +00:00
|
|
|
let
|
2023-03-09 11:23:45 +00:00
|
|
|
hostAddr = ca.Address.example
|
2022-05-26 02:29:31 +00:00
|
|
|
blocks = toSeq([1, 5, 10, 14, 20, 12, 22]) # TODO: maybe make them random
|
|
|
|
|
|
|
|
var
|
|
|
|
stpNetwork1: StpNetwork
|
|
|
|
stpNetwork2: StpNetwork
|
|
|
|
switch1: Switch
|
|
|
|
switch2: Switch
|
|
|
|
discovery1: MockDiscovery
|
|
|
|
discovery2: MockDiscovery
|
|
|
|
|
|
|
|
chunker: RandomChunker
|
|
|
|
manifest: Manifest
|
|
|
|
store: BlockStore
|
|
|
|
ssk: st.SecretKey
|
|
|
|
spk: st.PublicKey
|
|
|
|
porMsg: PorMessage
|
|
|
|
cid: Cid
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream: StoreStream
|
2022-05-26 02:29:31 +00:00
|
|
|
por: PoR
|
|
|
|
tags: seq[Tag]
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
setup:
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = DataSetSize.int, chunkSize = BlockSize)
|
2022-05-26 02:29:31 +00:00
|
|
|
store = CacheStore.new(cacheSize = DataSetSize, chunkSize = BlockSize)
|
|
|
|
manifest = Manifest.new(blockSize = BlockSize).tryGet()
|
|
|
|
(spk, ssk) = st.keyGen()
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
2022-05-26 02:29:31 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-07-28 00:39:17 +00:00
|
|
|
(await store.putBlock(blk)).tryGet()
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
cid = manifest.cid.tryGet()
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream = StoreStream.new(store, manifest)
|
2022-05-26 02:29:31 +00:00
|
|
|
por = await PoR.init(
|
2023-06-22 18:01:21 +00:00
|
|
|
porStream,
|
2022-05-26 02:29:31 +00:00
|
|
|
ssk, spk,
|
2023-07-06 23:23:27 +00:00
|
|
|
BlockSize.int)
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
porMsg = por.toMessage()
|
|
|
|
tags = blocks.mapIt(
|
2023-06-22 18:01:21 +00:00
|
|
|
Tag(idx: it, tag: porMsg.authenticators[it]))
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
switch2 = newStandardSwitch()
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
discovery1 = MockDiscovery.new()
|
|
|
|
discovery2 = MockDiscovery.new()
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
stpNetwork1 = StpNetwork.new(switch1, discovery1)
|
|
|
|
stpNetwork2 = StpNetwork.new(switch2, discovery2)
|
|
|
|
|
|
|
|
switch1.mount(stpNetwork1)
|
|
|
|
switch2.mount(stpNetwork2)
|
|
|
|
|
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await switch1.stop()
|
|
|
|
await switch2.stop()
|
2023-06-22 18:01:21 +00:00
|
|
|
await close(porStream)
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
test "Should upload to host":
|
|
|
|
var
|
|
|
|
done = newFuture[void]()
|
|
|
|
|
|
|
|
discovery1.findHostProvidersHandler = proc(d: MockDiscovery, host: ca.Address):
|
|
|
|
Future[seq[SignedPeerRecord]] {.async, gcsafe.} =
|
2023-03-09 11:23:45 +00:00
|
|
|
check hostAddr == host
|
2022-05-26 02:29:31 +00:00
|
|
|
return @[switch2.peerInfo.signedPeerRecord]
|
|
|
|
|
|
|
|
proc tagsHandler(msg: TagsMessage) {.async, gcsafe.} =
|
|
|
|
check:
|
|
|
|
Cid.init(msg.cid).tryGet() == cid
|
|
|
|
msg.tags == tags
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
stpNetwork2.tagsHandle = tagsHandler
|
|
|
|
(await stpNetwork1.uploadTags(
|
|
|
|
cid,
|
|
|
|
blocks,
|
|
|
|
porMsg.authenticators,
|
2023-03-09 11:23:45 +00:00
|
|
|
hostAddr)).tryGet()
|
2022-05-26 02:29:31 +00:00
|
|
|
|
|
|
|
await done.wait(1.seconds)
|