fix storestream so it doesn\'t return parity bits for protected/verifiable manifests

This commit is contained in:
gmega 2024-06-20 16:52:33 -03:00
parent b50addf0cb
commit ce42bd37cd
No known key found for this signature in database
GPG Key ID: FFD8DAF00660270F
3 changed files with 49 additions and 14 deletions

View File

@ -135,13 +135,6 @@ func isManifest*(mc: MultiCodec): ?!bool =
# Various sizes and verification
############################################################
func bytes*(self: Manifest, pad = true): NBytes =
## Compute how many bytes corresponding StoreStream(Manifest, pad) will return
if pad or self.protected:
self.blocksCount.NBytes * self.blockSize
else:
self.datasetSize
func rounded*(self: Manifest): int =
## Number of data blocks in *protected* manifest including padding at the end
roundUp(self.originalBlocksCount, self.ecK)

View File

@ -38,7 +38,6 @@ type
StoreStream* = ref object of SeekableStream
store*: BlockStore # Store where to lookup block contents
manifest*: Manifest # List of block CIDs
pad*: bool # Pad last block to manifest.blockSize?
method initStream*(s: StoreStream) =
if s.objName.len == 0:
@ -57,13 +56,15 @@ proc new*(
result = StoreStream(
store: store,
manifest: manifest,
pad: pad,
offset: 0)
result.initStream()
method `size`*(self: StoreStream): int =
bytes(self.manifest, self.pad).int
## The size of a StoreStream is the size of the original dataset, without
## padding or parity blocks.
let m = self.manifest
(if m.protected: m.originalDatasetSize else: m.datasetSize).int
proc `size=`*(self: StoreStream, size: int)
{.error: "Setting the size is forbidden".} =

View File

@ -1,10 +1,12 @@
import pkg/chronos
import pkg/questionable/results
import pkg/codex/streams
import pkg/codex/stores
import pkg/codex/manifest
import pkg/codex/blocktype as bt
import pkg/codex/[
streams,
stores,
indexingstrategy,
manifest,
blocktype as bt]
import ../asynctest
import ./helpers
@ -99,3 +101,42 @@ asyncchecksuite "StoreStream":
await stream.readExactly(addr buf[0], 15)
check sequentialBytes(buf,15,0)
suite "StoreStream - Size Tests":
var stream: StoreStream
teardown:
await stream.close()
test "Should return dataset size as stream size":
let manifest = Manifest.new(
treeCid = emptyCid(CIDv1, Sha256HashCodec, BlockCodec).get,
datasetSize = 80.NBytes,
blockSize = 10.NBytes
)
stream = StoreStream.new(CacheStore.new(), manifest)
check stream.size == 80
test "Should not count parity/padding bytes as part of stream size":
let mockCid = emptyCid(CIDv1, Sha256HashCodec, BlockCodec).get
let protectedManifest = Manifest.new(
treeCid = mockCid,
datasetSize = 120.NBytes, # size including parity bytes
blockSize = 10.NBytes,
version = CIDv1,
hcodec = Sha256HashCodec,
codec = BlockCodec,
ecK = 2,
ecM = 1,
originalTreeCid = mockCid,
originalDatasetSize = 80.NBytes, # size without parity bytes
strategy = StrategyType.SteppedStrategy
)
stream = StoreStream.new(CacheStore.new(), protectedManifest)
check stream.size == 80