Adding storage proofs store (#109)

* add por store

* rename por store to stp store

* add stp tests
This commit is contained in:
Dmitriy Ryajov 2022-05-24 18:42:49 -06:00 committed by GitHub
parent 56b80d6f6d
commit 738738c3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 145 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import ./storageproofs/por import ./storageproofs/por
import ./storageproofs/timing import ./storageproofs/timing
import ./storageproofs/stpstore
export por, timing export por, timing, stpstore

View File

@ -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)

View File

@ -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()

View File

@ -1,3 +1,4 @@
import ./storageproofs/teststpstore
import ./storageproofs/testpor import ./storageproofs/testpor
{.warning[UnusedImport]: off.} {.warning[UnusedImport]: off.}