Skip to content

Commit

Permalink
Added additional tests to cover HexaryTrie MPT proofs. (#650)
Browse files Browse the repository at this point in the history
* Added additional tests to cover HexaryTrie MPT proofs.

* Minor updates based on PR comments.
  • Loading branch information
web3-developer committed Dec 22, 2023
1 parent e5c2b17 commit 5aaeb67
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion tests/trie/test_hexary_proof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
{.push raises: [].}

import
std/sequtils,
unittest2,
stint,
std/sequtils,
nimcrypto/hash,
stew/byteutils,
../../eth/trie/[hexary, db, trie_defs, hexary_proof_verification]

proc getKeyBytes(i: int): seq[byte] =
Expand Down Expand Up @@ -59,3 +60,87 @@ suite "MPT trie proof verification":

check:
res.isMissing()

# The following test cases were copied from the Rust hexary trie implementation.
# See here: https://github.com/citahub/cita_trie/blob/master/src/tests/mod.rs#L554
test "Validate proof for empty trie":
let db = newMemoryDB()
var trie = initHexaryTrie(db)

let
proof = trie.getBranch("not-exist".toBytes)
res = verifyMptProof(proof, trie.rootHash, "not-exist".toBytes, "not-exist".toBytes)

check:
trie.rootHash == keccakHash(emptyRlp)
proof.len() == 1 # Note that the Rust implementation returns an empty list for this scenario
proof == @[emptyRlp]
res.kind == InvalidProof

test "Validate proof for one element trie":
let db = newMemoryDB()
var trie = initHexaryTrie(db)

trie.put("k".toBytes, "v".toBytes)

let
rootHash = trie.rootHash
proof = trie.getBranch("k".toBytes)
res = verifyMptProof(proof, rootHash, "k".toBytes, "v".toBytes)

check:
proof.len() == 1
res.isValid()

# removing key does not affect the verify process
trie.del("k".toBytes)
check verifyMptProof(proof, rootHash, "k".toBytes, "v".toBytes).isValid()

test "Validate proof bytes":
let db = newMemoryDB()
var trie = initHexaryTrie(db)

trie.put("doe".toBytes, "reindeer".toBytes)
trie.put("dog".toBytes, "puppy".toBytes)
trie.put("dogglesworth".toBytes, "cat".toBytes)

block:
let
rootHash = trie.rootHash
proof = trie.getBranch("doe".toBytes)
res = verifyMptProof(proof, rootHash, "doe".toBytes, "reindeer".toBytes)

check:
to0xHex(rootHash.data) == "0x8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3"
proof.len() == 3
proof[0] == "e5831646f6a0db6ae1fda66890f6693f36560d36b4dca68b4d838f17016b151efe1d4c95c453".hexToSeqByte
proof[1] == "f83b8080808080ca20887265696e6465657280a037efd11993cb04a54048c25320e9f29c50a432d28afdf01598b2978ce1ca3068808080808080808080".hexToSeqByte
res.isValid()

block:
let
proof = trie.getBranch("dogg".toBytes)
res = verifyMptProof(proof, trie.rootHash, "dogg".toBytes, "puppy".toBytes)

check:
proof.len() == 4
proof[0] == "e5831646f6a0db6ae1fda66890f6693f36560d36b4dca68b4d838f17016b151efe1d4c95c453".hexToSeqByte
proof[1] == "f83b8080808080ca20887265696e6465657280a037efd11993cb04a54048c25320e9f29c50a432d28afdf01598b2978ce1ca3068808080808080808080".hexToSeqByte
proof[2] == "e4808080808080ce89376c6573776f72746883636174808080808080808080857075707079".hexToSeqByte
res.isMissing()

block:
let
proof = newSeq[seq[byte]]()
res = verifyMptProof(proof, trie.rootHash, "doe".toBytes, "reindeer".toBytes)

check res.kind == InvalidProof

block:
let
proof = @["aaa".toBytes, "ccc".toBytes]
res = verifyMptProof(proof, trie.rootHash, "doe".toBytes, "reindeer".toBytes)

check res.kind == InvalidProof


0 comments on commit 5aaeb67

Please sign in to comment.