From 738738c3c6c82212f392839c9acfe8e836800577 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 24 May 2022 18:42:49 -0600 Subject: [PATCH] Adding storage proofs store (#109) * add por store * rename por store to stp store * add stp tests --- codex/storageproofs.nim | 3 +- codex/storageproofs/stpstore.nim | 74 ++++++++++++++++++++++ tests/codex/storageproofs/teststpstore.nim | 68 ++++++++++++++++++++ tests/codex/teststorageproofs.nim | 1 + 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 codex/storageproofs/stpstore.nim create mode 100644 tests/codex/storageproofs/teststpstore.nim diff --git a/codex/storageproofs.nim b/codex/storageproofs.nim index e23ecfbf..7ad106ea 100644 --- a/codex/storageproofs.nim +++ b/codex/storageproofs.nim @@ -1,4 +1,5 @@ import ./storageproofs/por import ./storageproofs/timing +import ./storageproofs/stpstore -export por, timing +export por, timing, stpstore diff --git a/codex/storageproofs/stpstore.nim b/codex/storageproofs/stpstore.nim new file mode 100644 index 00000000..43a8ca14 --- /dev/null +++ b/codex/storageproofs/stpstore.nim @@ -0,0 +1,74 @@ +## Nim-Dagger +## Copyright (c) 2022 Status Research & Development GmbH +## Licensed under either of +## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +## * MIT license ([LICENSE-MIT](LICENSE-MIT)) +## at your option. +## This file may not be copied, modified, or distributed except according to +## those terms. + +import std/os +import std/strformat + +import pkg/libp2p +import pkg/chronos +import pkg/chronicles +import pkg/stew/io2 +import pkg/questionable +import pkg/questionable/results +import pkg/protobuf_serialization + +import ./por + +type + StpStore* = object + authDir*: string + postfixLen*: int + +template stpPath*(self: StpStore, cid: Cid): string = + self.authDir / ($cid)[^self.postfixLen..^1] / $cid + +proc retrieve*(self: StpStore, cid: Cid): Future[?!PorMessage] {.async.} = + ## Retrieve authenticators from data store + ## + + let path = self.stpPath(cid) + var data: seq[byte] + if ( + let res = io2.readFile(path, data); + res.isErr): + let error = io2.ioErrorMsg(res.error) + trace "Cannot retrieve authenticators from fs", path , error + return failure("Cannot retrieve authenticators from fs") + + return Protobuf.decode(data, PorMessage).success + +proc store*(self: StpStore, por: PoR, cid: Cid): Future[?!void] {.async.} = + ## Persist storage proofs + ## + + let + dir = self.stpPath(cid).parentDir + + if io2.createPath(dir).isErr: + trace "Unable to create storage proofs prefix dir", dir + return failure(&"Unable to create storage proofs prefix dir ${dir}") + + let path = self.stpPath(cid) + if ( + let res = io2.writeFile(path, Protobuf.encode(por.toMessage())); + res.isErr): + let error = io2.ioErrorMsg(res.error) + trace "Unable to store storage proofs", path, cid = cid, error + return failure( + &"Unable to store storage proofs path = ${path} cid = ${$cid} error = ${error}") + + return success() + +proc init*( + T: type StpStore, + authDir: string, + postfixLen: int = 2): StpStore = + T( + authDir: authDir, + postfixLen: postfixLen) diff --git a/tests/codex/storageproofs/teststpstore.nim b/tests/codex/storageproofs/teststpstore.nim new file mode 100644 index 00000000..c2839c1e --- /dev/null +++ b/tests/codex/storageproofs/teststpstore.nim @@ -0,0 +1,68 @@ +import std/os + +import pkg/chronos +import pkg/asynctest + +import pkg/codex/rng +import pkg/codex/streams +import pkg/codex/storageproofs as st +import pkg/codex/blocktype as bt + +import ../helpers + +const + SectorSize = 31 + SectorsPerBlock = BlockSize div SectorSize + DataSetSize = BlockSize * 100 + +suite "Test PoR store": + let + (path, _, _) = instantiationInfo(-2, fullPaths = true) # get this file's name + + var + chunker: RandomChunker + manifest: Manifest + store: BlockStore + ssk: st.SecretKey + spk: st.PublicKey + repoDir: string + stpstore: st.StpStore + por: PoR + cid: Cid + + setupAll: + chunker = RandomChunker.new(Rng.instance(), size = DataSetSize, chunkSize = BlockSize) + 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): + + let + blk = bt.Block.new(chunk).tryGet() + + manifest.add(blk.cid) + if not (await store.putBlock(blk)): + raise newException(CatchableError, "Unable to store block " & $blk.cid) + + cid = manifest.cid.tryGet() + por = await PoR.init( + StoreStream.new(store, manifest), + ssk, spk, + BlockSize) + + repoDir = path.parentDir / "stp" + createDir(repoDir) + stpstore = st.StpStore.init(repoDir) + + teardownAll: + removeDir(repoDir) + + test "Should store Storage Proofs": + check (await stpstore.store(por, cid)).isOk + check fileExists(stpstore.stpPath(cid)) + + test "Should retrieve Storage Proofs": + discard (await stpstore.retrieve(cid)).tryGet() diff --git a/tests/codex/teststorageproofs.nim b/tests/codex/teststorageproofs.nim index 25a85538..71b2577a 100644 --- a/tests/codex/teststorageproofs.nim +++ b/tests/codex/teststorageproofs.nim @@ -1,3 +1,4 @@ +import ./storageproofs/teststpstore import ./storageproofs/testpor {.warning[UnusedImport]: off.}