Add proof verification to public api (#390)

This commit is contained in:
KonradStaniec 2021-08-12 16:15:02 +02:00 committed by GitHub
parent 83e5638212
commit aa3fbbd95d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions

View File

@ -677,3 +677,17 @@ proc isPruning*(self: SecureHexaryTrie): bool {.borrow.}
template contains*(self: HexaryTrie | SecureHexaryTrie; template contains*(self: HexaryTrie | SecureHexaryTrie;
key: openArray[byte]): bool = key: openArray[byte]): bool =
self.get(key).len > 0 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

View File

@ -310,19 +310,6 @@ suite "hexary trie":
echo "ITERATION: ", iteration echo "ITERATION: ", iteration
break 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": test "get branch with pruning trie":
var var
memdb = newMemoryDB() memdb = newMemoryDB()