From aa3fbbd95dd386b9acedc9204ab5e2e7288fa211 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Thu, 12 Aug 2021 16:15:02 +0200 Subject: [PATCH] Add proof verification to public api (#390) --- eth/trie/hexary.nim | 14 ++++++++++++++ tests/trie/test_hexary_trie.nim | 13 ------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/eth/trie/hexary.nim b/eth/trie/hexary.nim index 8702875..a3d7fe9 100644 --- a/eth/trie/hexary.nim +++ b/eth/trie/hexary.nim @@ -677,3 +677,17 @@ proc isPruning*(self: SecureHexaryTrie): bool {.borrow.} template contains*(self: HexaryTrie | SecureHexaryTrie; key: openArray[byte]): bool = self.get(key).len > 0 + +# Validates merkle proof against provided root hash +proc isValidBranch*(branch: seq[seq[byte]], rootHash: KeccakHash, key, value: seq[byte]): bool = + # branch must not be empty + doAssert(branch.len != 0) + + var db = newMemoryDB() + for node in branch: + doAssert(node.len != 0) + let nodeHash = hexary.keccak(node) + db.put(nodeHash.data, node) + + var trie = initHexaryTrie(db, rootHash) + result = trie.get(key) == value diff --git a/tests/trie/test_hexary_trie.nim b/tests/trie/test_hexary_trie.nim index 5ddbc93..c47915c 100644 --- a/tests/trie/test_hexary_trie.nim +++ b/tests/trie/test_hexary_trie.nim @@ -310,19 +310,6 @@ suite "hexary trie": echo "ITERATION: ", iteration break - proc isValidBranch(branch: seq[seq[byte]], rootHash: KeccakHash, key, value: seq[byte]): bool = - # branch must not be empty - doAssert(branch.len != 0) - - var db = newMemoryDB() - for node in branch: - doAssert(node.len != 0) - let nodeHash = hexary.keccak(node) - db.put(nodeHash.data, node) - - var trie = initHexaryTrie(db, rootHash) - result = trie.get(key) == value - test "get branch with pruning trie": var memdb = newMemoryDB()