Skip to content

Commit

Permalink
Fix StoreStream so it doesn't return parity bytes (#838)
Browse files Browse the repository at this point in the history
* fix storestream so it doesn\'t return parity bits for protected/verifiable manifests

* use Cid.example instead of creating a mock manually
  • Loading branch information
gmega authored Jun 21, 2024
1 parent ec7faa2 commit 4619260
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
7 changes: 0 additions & 7 deletions codex/manifest/manifest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions codex/streams/storestream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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".} =
Expand Down
48 changes: 44 additions & 4 deletions tests/codex/teststorestream.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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 ./examples
import ./helpers

asyncchecksuite "StoreStream":
Expand Down Expand Up @@ -99,3 +102,40 @@ 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 = Cid.example,
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 protectedManifest = Manifest.new(
treeCid = Cid.example,
datasetSize = 120.NBytes, # size including parity bytes
blockSize = 10.NBytes,
version = CIDv1,
hcodec = Sha256HashCodec,
codec = BlockCodec,
ecK = 2,
ecM = 1,
originalTreeCid = Cid.example,
originalDatasetSize = 80.NBytes, # size without parity bytes
strategy = StrategyType.SteppedStrategy
)

stream = StoreStream.new(CacheStore.new(), protectedManifest)

check stream.size == 80

0 comments on commit 4619260

Please sign in to comment.