From 942bfb7ea55c90716ba10b41eeefd5ab83357aeb Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 28 Nov 2019 17:02:32 +0700 Subject: [PATCH] support trie with multiple root --- tests/trie/test_hexary_trie.nim | 41 +++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/trie/test_hexary_trie.nim b/tests/trie/test_hexary_trie.nim index 1b30c8f..0fbeae0 100644 --- a/tests/trie/test_hexary_trie.nim +++ b/tests/trie/test_hexary_trie.nim @@ -374,5 +374,42 @@ suite "hexary trie": check nonPruningTrie.isPruning == false check pruningTrie.isPruning == true - check nonPruningSecureTRie.isPruning == false - check pruningSecureTRie.isPruning == true + check nonPruningSecureTrie.isPruning == false + check pruningSecureTrie.isPruning == true + + test "multi-roots pruning trie": + const + numKeyVal = 30 + + var + memdb = newMemoryDB() + pruningTrie = initHexaryTrie(memdb, isPruning = true) + + let + keys = randList(BytesRange, randGen(5, 77), randGen(numKeyVal)) + vals = randList(BytesRange, randGen(1, 57), randGen(numKeyVal)) + newVals = randList(BytesRange, randGen(1, 63), randGen(numKeyVal)) + + var tx1 = memdb.beginTransaction() + for i in 0 ..< numKeyVal: + pruningTrie.put(keys[i], vals[i]) + tx1.commit() + let rootHash1 = pruningTrie.rootHash + + var tx2 = memdb.beginTransaction() + for i in 0 ..< numKeyVal: + pruningTrie.put(keys[i], newVals[i]) + tx2.commit(applyDeletes = false) + let rootHash2 = pruningTrie.rootHash + + check rootHash1 != rootHash2 + + var trie1 = initHexaryTrie(memdb, rootHash1, isPruning = true) + for x in 0 ..< numKeyVal: + var branch = trie1.getBranch(keys[x]) + check isValidBranch(branch, trie1.rootHash, keys[x], vals[x]) + + var trie2 = initHexaryTrie(memdb, rootHash2, isPruning = true) + for x in 0 ..< numKeyVal: + var branch = trie2.getBranch(keys[x]) + check isValidBranch(branch, trie2.rootHash, keys[x], newVals[x])