2023-12-22 12:04:01 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2023 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import std/tables
|
|
|
|
import std/sugar
|
|
|
|
|
|
|
|
import pkg/libp2p/multicodec
|
|
|
|
import pkg/libp2p/multihash
|
|
|
|
import pkg/libp2p/cid
|
|
|
|
import pkg/results
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
import ./units
|
|
|
|
import ./errors
|
|
|
|
|
|
|
|
export tables
|
|
|
|
|
|
|
|
const
|
|
|
|
# Size of blocks for storage / network exchange,
|
2024-02-06 16:20:15 +00:00
|
|
|
DefaultBlockSize* = NBytes 1024 * 64
|
2023-12-22 12:04:01 +00:00
|
|
|
DefaultCellSize* = NBytes 2048
|
|
|
|
|
2024-02-09 20:51:22 +00:00
|
|
|
# Proving defaults
|
|
|
|
DefaultMaxSlotDepth* = 32
|
|
|
|
DefaultMaxDatasetDepth* = 8
|
|
|
|
DefaultBlockDepth* = 5
|
|
|
|
DefaultCellElms* = 67
|
|
|
|
DefaultSamplesNum* = 5
|
|
|
|
|
2023-12-22 12:04:01 +00:00
|
|
|
# hashes
|
|
|
|
Sha256HashCodec* = multiCodec("sha2-256")
|
|
|
|
Sha512HashCodec* = multiCodec("sha2-512")
|
|
|
|
Pos2Bn128SpngCodec* = multiCodec("poseidon2-alt_bn_128-sponge-r2")
|
|
|
|
Pos2Bn128MrklCodec* = multiCodec("poseidon2-alt_bn_128-merkle-2kb")
|
|
|
|
|
|
|
|
ManifestCodec* = multiCodec("codex-manifest")
|
|
|
|
DatasetRootCodec* = multiCodec("codex-root")
|
|
|
|
BlockCodec* = multiCodec("codex-block")
|
|
|
|
SlotRootCodec* = multiCodec("codex-slot-root")
|
|
|
|
SlotProvingRootCodec* = multiCodec("codex-proving-root")
|
2024-01-11 16:45:23 +00:00
|
|
|
CodexSlotCellCodec* = multiCodec("codex-slot-cell")
|
2023-12-22 12:04:01 +00:00
|
|
|
|
|
|
|
CodexHashesCodecs* = [
|
|
|
|
Sha256HashCodec,
|
|
|
|
Pos2Bn128SpngCodec,
|
|
|
|
Pos2Bn128MrklCodec
|
|
|
|
]
|
|
|
|
|
|
|
|
CodexPrimitivesCodecs* = [
|
|
|
|
ManifestCodec,
|
|
|
|
DatasetRootCodec,
|
|
|
|
BlockCodec,
|
|
|
|
SlotRootCodec,
|
|
|
|
SlotProvingRootCodec,
|
2024-01-11 16:45:23 +00:00
|
|
|
CodexSlotCellCodec,
|
2023-12-22 12:04:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
proc initEmptyCidTable(): ?!Table[(CidVersion, MultiCodec, MultiCodec), Cid] =
|
|
|
|
## Initialize padding blocks table
|
|
|
|
##
|
|
|
|
## TODO: Ideally this is done at compile time, but for now
|
|
|
|
## we do it at runtime because of an `importc` error that is
|
2024-01-08 22:52:46 +00:00
|
|
|
## coming from somewhere in MultiHash that I can't track down.
|
2023-12-22 12:04:01 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
let
|
|
|
|
emptyData: seq[byte] = @[]
|
|
|
|
PadHashes = {
|
|
|
|
Sha256HashCodec: ? MultiHash.digest($Sha256HashCodec, emptyData).mapFailure,
|
|
|
|
Sha512HashCodec: ? MultiHash.digest($Sha512HashCodec, emptyData).mapFailure,
|
|
|
|
}.toTable
|
|
|
|
|
|
|
|
var
|
|
|
|
table = initTable[(CidVersion, MultiCodec, MultiCodec), Cid]()
|
|
|
|
|
|
|
|
for hcodec, mhash in PadHashes.pairs:
|
|
|
|
table[(CIDv1, hcodec, BlockCodec)] = ? Cid.init(CIDv1, BlockCodec, mhash).mapFailure
|
|
|
|
|
|
|
|
success table
|
|
|
|
|
|
|
|
proc emptyCid*(
|
|
|
|
version: CidVersion,
|
|
|
|
hcodec: MultiCodec,
|
|
|
|
dcodec: MultiCodec): ?!Cid =
|
|
|
|
## Returns cid representing empty content,
|
|
|
|
## given cid version, hash codec and data codec
|
|
|
|
##
|
|
|
|
|
|
|
|
var
|
|
|
|
table {.global, threadvar.}: Table[(CidVersion, MultiCodec, MultiCodec), Cid]
|
|
|
|
|
|
|
|
once:
|
|
|
|
table = ? initEmptyCidTable()
|
|
|
|
|
|
|
|
table[(version, hcodec, dcodec)].catch
|
|
|
|
|
|
|
|
proc emptyDigest*(
|
|
|
|
version: CidVersion,
|
|
|
|
hcodec: MultiCodec,
|
|
|
|
dcodec: MultiCodec): ?!MultiHash =
|
|
|
|
## Returns hash representing empty content,
|
|
|
|
## given cid version, hash codec and data codec
|
|
|
|
##
|
|
|
|
emptyCid(version, hcodec, dcodec)
|
|
|
|
.flatMap((cid: Cid) => cid.mhash.mapFailure)
|