From 17f52bbfc0f4e74190ca648b8e24ff0ad7f493c5 Mon Sep 17 00:00:00 2001 From: Advaita Saha Date: Sat, 20 Jan 2024 18:16:04 +0530 Subject: [PATCH] feat: tests added for insertion correctness + commitment caching --- eth_verkle.nimble | 5 ++- tests/test_tree.nim | 97 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/test_tree.nim diff --git a/eth_verkle.nimble b/eth_verkle.nimble index ec70666..98f7c4a 100644 --- a/eth_verkle.nimble +++ b/eth_verkle.nimble @@ -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" diff --git a/tests/test_tree.nim b/tests/test_tree.nim new file mode 100644 index 0000000..e03201e --- /dev/null +++ b/tests/test_tree.nim @@ -0,0 +1,97 @@ +# 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. + +## The main module. Provides some tests. + +import + unittest, + ../eth_verkle/math, + ../eth_verkle/tree/[tree, operations, commitment], + ../constantine/constantine/serialization/codecs + +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" + ) + + +suite "Tree Insertion Tests": + + test "Insertion Into Root": + var tree = newBranchesNode() + tree.setValue(zeroKeyTest, testValue) + check testValue == ((ValuesNode)tree.branches[0]).values[zeroKeyTest[31]][] + + 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][] + + 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][] + +suite "Commitment Tests": + + test "Cached Commitment Test": + + var + key1: Bytes32 = fromHex(Bytes32, "0x0105000000000000000000000000000000000000000000000000000000000000") + key2: Bytes32 = fromHex(Bytes32, "0x0107000000000000000000000000000000000000000000000000000000000000") + key3: Bytes32 = fromHex(Bytes32, "0x0405000000000000000000000000000000000000000000000000000000000000") + key4: Bytes32 = 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: make the nil check work + # doAssert isNil(BranchesNode(tree.branches[1]).commitment), "internal node has mistakenly cleared cached commitment" + +