diff --git a/codex/merkletree/backend.nim b/codex/merkletree/backend.nim new file mode 100644 index 00000000..e69de29b diff --git a/codex/merkletree/backends/dsbackend.nim b/codex/merkletree/backends/dsbackend.nim new file mode 100644 index 00000000..6db98bbf --- /dev/null +++ b/codex/merkletree/backends/dsbackend.nim @@ -0,0 +1,28 @@ +## 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. + +import ./merklestore +import pkg/datastore +import ../../namespaces + +type + DataStoreBackend* = ref object of MerkleStore + store*: Datastore + +method put*( + self: DataStoreBackend, + index, level: Natural, + hash: seq[byte]): Future[?!void] {.async.} = + success await self.store.put(index, hash) + +method get*(self: DataStoreBackend, index, level: Natural): Future[!?seq[byte]] = + raiseAssert("Not implemented!") + +func new*(_: type DataStoreBackend, store: Datastore): DataStoreBackend = + DataStoreBackend(store: store) diff --git a/codex/merkletree/backends/merklestore.nim b/codex/merkletree/backends/merklestore.nim new file mode 100644 index 00000000..eea9a5f0 --- /dev/null +++ b/codex/merkletree/backends/merklestore.nim @@ -0,0 +1,17 @@ +## 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. + +type + MerkleStore* = ref object of RootObj + +method put*(self: MerkleStore, index, level: Natural, hash: seq[byte]): Future[?!void] = + raiseAssert("Not implemented!") + +method get*(self: MerkleStore, index, level: Natural): Future[!?seq[byte]] = + raiseAssert("Not implemented!") diff --git a/codex/merkletree/merkletree.nim b/codex/merkletree/merkletree.nim index 5c96de6f..c67d5bd8 100644 --- a/codex/merkletree/merkletree.nim +++ b/codex/merkletree/merkletree.nim @@ -26,14 +26,14 @@ logScope: topics = "codex merkletree" type - MerkleTree* = object + MerkleTree* = ref object of RootObj mcodec: MultiCodec # multicodec of the hash function height: Natural # current height of the tree (levels - 1) levels: Natural # number of levels in the tree (height + 1) leafs: Natural # total number of leafs, if odd the last leaf will be hashed twice + length: Natural # corrected to even number of leafs in the tree size: Natural # total number of nodes in the tree (corrected for odd leafs) leafsIter: AsyncIter[seq[byte]] # leafs iterator of the tree - nodesIter: AsyncIter[seq[byte]] # nodes iterator of the tree MerkleProof* = object mcodec: MultiCodec @@ -135,6 +135,7 @@ func init*( self = MerkleTree( mcodec: mcodec, leafs: leafs, + length: length, size: size, height: height, levels: height - 1, diff --git a/codex/namespaces.nim b/codex/namespaces.nim index 42f5684a..761ac71a 100644 --- a/codex/namespaces.nim +++ b/codex/namespaces.nim @@ -14,6 +14,9 @@ const CodexBlockTotalNamespace* = CodexMetaNamespace & "/total" # number of blocks in the repo CodexBlocksNamespace* = CodexRepoNamespace & "/blocks" # blocks namespace CodexManifestNamespace* = CodexRepoNamespace & "/manifests" # manifest namespace + CodexMerkleTreeNamespace* = CodexRepoNamespace & "/trees" # merkle tree namespace + CodexMerkleTreeHeader* = # merkle tree header + CodexMerkleTreeNamespace & "/header" CodexBlocksTtlNamespace* = # Cid TTL CodexMetaNamespace & "/ttl" CodexBlockProofNamespace* = # Cid and Proof