mirror of https://github.com/status-im/nim-eth.git
Added additional tests to cover HexaryTrie MPT proofs. (#650)
* Added additional tests to cover HexaryTrie MPT proofs. * Minor updates based on PR comments.
This commit is contained in:
parent
e5c2b1784e
commit
5aaeb67ebe
|
@ -10,10 +10,11 @@
|
||||||
{.push raises: [].}
|
{.push raises: [].}
|
||||||
|
|
||||||
import
|
import
|
||||||
|
std/sequtils,
|
||||||
unittest2,
|
unittest2,
|
||||||
stint,
|
stint,
|
||||||
std/sequtils,
|
|
||||||
nimcrypto/hash,
|
nimcrypto/hash,
|
||||||
|
stew/byteutils,
|
||||||
../../eth/trie/[hexary, db, trie_defs, hexary_proof_verification]
|
../../eth/trie/[hexary, db, trie_defs, hexary_proof_verification]
|
||||||
|
|
||||||
proc getKeyBytes(i: int): seq[byte] =
|
proc getKeyBytes(i: int): seq[byte] =
|
||||||
|
@ -59,3 +60,87 @@ suite "MPT trie proof verification":
|
||||||
|
|
||||||
check:
|
check:
|
||||||
res.isMissing()
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue