emptyCid: workaround for blocks with all 0s

there was a different CID for something declared an empty block
(used for padding), and the block with all 0s. This becomes a
problem after decode, where the docoder returns a block with all 0s.

This is a workaround, not a real solution.

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-11-15 13:15:50 +01:00
parent 4e72d47c17
commit 05933c6ebd
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 11 additions and 4 deletions

View File

@ -9,6 +9,7 @@
import std/tables
import std/sugar
import std/sequtils
export tables
import pkg/upraises
@ -88,7 +89,9 @@ proc `$`*(b: Block): string =
result &= "cid: " & $b.cid
result &= "\ndata: " & string.fromBytes(b.data)
func new*(
proc emptyCid*(version: CidVersion, hcodec: MultiCodec, dcodec: MultiCodec): ?!Cid
proc new*(
T: type Block,
data: openArray[byte] = [],
version = CIDv1,
@ -98,9 +101,13 @@ func new*(
## creates a new block for both storage and network IO
##
let
hash = ? MultiHash.digest($mcodec, data).mapFailure
cid = ? Cid.init(version, codec, hash).mapFailure
let cid =
if all(data, proc (x: byte): bool = x == 0):
? emptyCid(version, mcodec, codec)
else:
let
hash = ? MultiHash.digest($mcodec, data).mapFailure
? Cid.init(version, codec, hash).mapFailure
# TODO: If the hash is `>=` to the data,
# use the Cid as a container!