Merge pull request #15 from status-im/testing
Adds Tests to the Verkle Tree
This commit is contained in:
commit
6f5aef83eb
|
@ -29,6 +29,9 @@ proc run(args, path: string) =
|
|||
if (NimMajor, NimMinor) > (1, 6):
|
||||
build args & " --mm:refc -r", path
|
||||
|
||||
task test, "Run all tests":
|
||||
task testAll, "Run all tests":
|
||||
for threads in ["--threads:off", "--threads:on"]:
|
||||
run threads, "tests/test_all"
|
||||
|
||||
task testTree, "Run Tree Tests":
|
||||
run "", "tests/test_tree"
|
||||
|
|
|
@ -106,7 +106,7 @@ proc getValue*(node: BranchesNode, key: Bytes32): ref Bytes32 =
|
|||
inc(depth)
|
||||
|
||||
var vn = current.branches[key[depth]].ValuesNode
|
||||
if vn != nil and vn.values[key[^1]] != nil:
|
||||
if vn != nil and vn.stem == key[0..30] and vn.values[key[^1]] != nil:
|
||||
return vn.values[key[^1]]
|
||||
else: return nil
|
||||
|
||||
|
@ -154,6 +154,8 @@ proc deleteValue(node: BranchesNode, key: Bytes32, depth: int = 0):
|
|||
|
||||
elif child of ValuesNode:
|
||||
var vn = child.ValuesNode
|
||||
if vn.stem != key[0..30]:
|
||||
return (found: false, empty: false, values: nil)
|
||||
var target = vn.values[key[^1]]
|
||||
when TraceLogs: echo " ".repeat(depth+1) & &"At ValuesNode {cast[uint64](vn)}, depth {depth+1}"
|
||||
if target == nil:
|
||||
|
|
|
@ -122,8 +122,10 @@ suite "main":
|
|||
check tree.getValue(key)[] == value
|
||||
var missingKey1 = hexToBytesArray[32]("abcd000000000000000000000000000000000000000000000000000000000000")
|
||||
var missingKey2 = hexToBytesArray[32]("ef01234000000000000000000000000000000000000000000000000000000000")
|
||||
var missingKey3 = hexToBytesArray[32]("0000110000000000000000000000000000000000000000000000000000000000")
|
||||
check tree.getValue(missingKey1) == nil
|
||||
check tree.getValue(missingKey2) == nil
|
||||
check tree.getValue(missingKey3) == nil
|
||||
|
||||
|
||||
test "testDelNonExistingValues":
|
||||
|
|
|
@ -0,0 +1,291 @@
|
|||
# Nimbus
|
||||
# Copyright (c) 2021-2023 Status Research & Development GmbH
|
||||
# Licensed and distributed under either of
|
||||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
||||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
|
||||
import
|
||||
unittest,
|
||||
../eth_verkle/math,
|
||||
../eth_verkle/tree/[tree, operations, commitment],
|
||||
../constantine/constantine/serialization/codecs
|
||||
|
||||
## Values to be used for testing
|
||||
const
|
||||
testValue = fromHex(
|
||||
Bytes32,
|
||||
"0x0123456789abcdef0123456789abcdef"
|
||||
)
|
||||
zeroKeyTest = fromHex(
|
||||
Bytes32,
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
oneKeyTest = fromHex(
|
||||
Bytes32,
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000001"
|
||||
)
|
||||
# forkOneKeyTest = fromHex(
|
||||
# Bytes32,
|
||||
# "0x0001000000000000000000000000000000000000000000000000000000000001"
|
||||
# )
|
||||
fourtyKeyTest = fromHex(
|
||||
Bytes32,
|
||||
"0x4000000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
ffx32KeyTest = fromHex(
|
||||
Bytes32,
|
||||
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
)
|
||||
|
||||
## ################################################################
|
||||
##
|
||||
## Tests for Tree Insertion Correctness
|
||||
##
|
||||
## ################################################################
|
||||
suite "Tree Insertion Tests":
|
||||
## Tests if the Insertion into the root not
|
||||
## is taking place at the expected position
|
||||
test "Insertion Into Root":
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(zeroKeyTest, testValue)
|
||||
check testValue == ((ValuesNode)tree.branches[0]).values[zeroKeyTest[31]][]
|
||||
|
||||
## Tests if the insertion of two leafs
|
||||
## into the root is taking place at the expected position
|
||||
test "Insert Two Leaves":
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(zeroKeyTest, testValue)
|
||||
tree.setValue(ffx32KeyTest, testValue)
|
||||
check testValue == ((ValuesNode)tree.branches[0]).values[zeroKeyTest[31]][]
|
||||
check testValue == ((ValuesNode)tree.branches[255]).values[255][]
|
||||
|
||||
## Tests if the insertion of leafs
|
||||
## into the last level is taking place at the expected position
|
||||
test "Insert Two Leaves Last Level":
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(zeroKeyTest, testValue)
|
||||
tree.setValue(oneKeyTest, testValue)
|
||||
check testValue == ((ValuesNode)tree.branches[0]).values[1][]
|
||||
check testValue == ((ValuesNode)tree.branches[0]).values[0][]
|
||||
|
||||
## ################################################################
|
||||
##
|
||||
## Tests for Correct Commitment Updation
|
||||
##
|
||||
## ################################################################
|
||||
suite "Commitment Tests":
|
||||
## Tests to check is the commitment is updated
|
||||
## after the insertion of a leaf
|
||||
## and also to checks if caching of commitment is
|
||||
## working as expected
|
||||
## TODO: make the nil check work
|
||||
test "Cached Commitment Test":
|
||||
const
|
||||
key1 = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key2 = fromHex(
|
||||
Bytes32,
|
||||
"0x0107000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key3 = fromHex(
|
||||
Bytes32,
|
||||
"0x0405000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key4 = fromHex(
|
||||
Bytes32,
|
||||
"0x0407000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
var tree = newBranchesNode()
|
||||
|
||||
tree.setValue(key1, fourtyKeyTest)
|
||||
tree.setValue(key2, fourtyKeyTest)
|
||||
tree.setValue(key3, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var oldRoot = tree.commitment
|
||||
var oldInternal = ((ValuesNode)tree.branches[4]).commitment
|
||||
|
||||
|
||||
tree.setValue(key4, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var newRoot = tree.commitment
|
||||
var newInternal = ((BranchesNode)tree.branches[4]).commitment
|
||||
|
||||
doAssert oldRoot.serializePoint() != newRoot.serializePoint(), "root has stale commitment"
|
||||
doAssert oldInternal.serializePoint() != newInternal.serializePoint(), "internal node has stale commitment"
|
||||
|
||||
## TODO: Perform the not nil check
|
||||
## currently the not nil check have been checked manually by printing the values
|
||||
# doAssert BranchesNode(tree.branches[1]).commitment != nil, "internal node has mistakenly cleared cached commitment"
|
||||
|
||||
## ################################################################
|
||||
##
|
||||
## Tests for Deletion in Tree
|
||||
##
|
||||
## ################################################################
|
||||
suite "Tree Deletion Tests":
|
||||
## Tests if the deletion of a leaf is taking place
|
||||
## correctly with proper removal of the leaf
|
||||
## and updation of the commitment
|
||||
test "Delete Leaf Node":
|
||||
const
|
||||
key1 = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key1p = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000001"
|
||||
)
|
||||
key1pp = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000081"
|
||||
)
|
||||
key2 = fromHex(
|
||||
Bytes32,
|
||||
"0x0107000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key3 = fromHex(
|
||||
Bytes32,
|
||||
"0x0405000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(key1, fourtyKeyTest)
|
||||
tree.setValue(key1p, fourtyKeyTest)
|
||||
tree.setValue(key1pp, fourtyKeyTest)
|
||||
tree.setValue(key2, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var init = tree.commitment
|
||||
|
||||
tree.setValue(key3, fourtyKeyTest)
|
||||
let err = tree.deleteValue(key3)
|
||||
doAssert err, "Deletion Failed"
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var final = tree.commitment
|
||||
doAssert init.serializePoint() == final.serializePoint(), "Deletion Inconsistent"
|
||||
doAssert tree.getValue(key3).isNil, "leaf hasnt been deleted"
|
||||
|
||||
test "Test Delete Non-Existent Node should fail":
|
||||
## Tests if the deletion of a non-existent leaf is taking place
|
||||
## this test should fail, as the deletion of a non-existent leaf
|
||||
## should not be allowed
|
||||
const
|
||||
key1 = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key2 = fromHex(
|
||||
Bytes32,
|
||||
"0x0107000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key3 = fromHex(
|
||||
Bytes32,
|
||||
"0x0405000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(key1, fourtyKeyTest)
|
||||
tree.setValue(key2, fourtyKeyTest)
|
||||
let err = tree.deleteValue(key3)
|
||||
doAssert (not err), "hould not fail when deleting a non-existent key"
|
||||
|
||||
test "Test Delete Prune":
|
||||
## Tests if the deletion of a leaf is taking place
|
||||
## correctly with proper pruning of the data and commitment
|
||||
const
|
||||
key1 = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key2 = fromHex(
|
||||
Bytes32,
|
||||
"0x0107000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key3 = fromHex(
|
||||
Bytes32,
|
||||
"0x0405000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key4 = fromHex(
|
||||
Bytes32,
|
||||
"0x0407000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key5 = fromHex(
|
||||
Bytes32,
|
||||
"0x04070000000000000000000000000000000000000000000000000000000000FF"
|
||||
)
|
||||
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(key1, fourtyKeyTest)
|
||||
tree.setValue(key2, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var hashPostKey2 = tree.commitment
|
||||
|
||||
tree.setValue(key3, fourtyKeyTest)
|
||||
tree.setValue(key4, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var hashPostKey4 = tree.commitment
|
||||
|
||||
tree.setValue(key5, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var completeTreeHash = tree.commitment
|
||||
|
||||
# Delete Key5
|
||||
doAssert tree.deleteValue(key5), "Unable to delete key5"
|
||||
tree.updateAllCommitments()
|
||||
|
||||
var postHash = tree.commitment
|
||||
|
||||
# Check that the deletion updated the root hash and that it's not
|
||||
# the same as the pre-deletion hash
|
||||
doAssert completeTreeHash.serializePoint() != postHash.serializePoint(), "Deletion did not update root hash"
|
||||
|
||||
# The post deletion hash should be the same as the post key4 hash
|
||||
doAssert hashPostKey4.serializePoint() == postHash.serializePoint(), "deleting leaf #5 resulted in unexpected tree"
|
||||
|
||||
# Delete key4 and key3
|
||||
doAssert tree.deleteValue(key4), "Unable to delete key4"
|
||||
doAssert tree.deleteValue(key3), "Unable to delete key3"
|
||||
tree.updateAllCommitments()
|
||||
|
||||
postHash = tree.commitment
|
||||
|
||||
# The post deletion hash should be different from the post key2 hash
|
||||
doAssert hashPostKey2.serializePoint() == postHash.serializePoint(), "deleting leaf #3 resulted in unexpected tree"
|
||||
|
||||
test "Delete Unequal Path should fail":
|
||||
## Test if deletion of unequal path is taking place
|
||||
## for keys having similar path starting from the root
|
||||
const
|
||||
key1 = fromHex(
|
||||
Bytes32,
|
||||
"0x0105000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key2 = fromHex(
|
||||
Bytes32,
|
||||
"0x0107000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
key3 = fromHex(
|
||||
Bytes32,
|
||||
"0x0405000000000000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
var tree = newBranchesNode()
|
||||
tree.setValue(key1, fourtyKeyTest)
|
||||
tree.setValue(key3, fourtyKeyTest)
|
||||
tree.updateAllCommitments()
|
||||
|
||||
doAssert (not tree.deleteValue(key2)), "errored during the deletion of non-existing key"
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue