Skip to content

Commit d3a00eb

Browse files
authored
Add test covering HashKey rlp encoding when data contains embedded trie node (#3578)
* Fix rlp encoding of HashKey type. * Add test which covers trie root calculations for embedded trie node in HashKey scenario. * Revert change to Hashkey type.
1 parent de8585c commit d3a00eb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/test_ledger.nim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,31 @@ proc runLedgerBasicOperationsTests() =
814814
ac.clearBlockHashesCache()
815815
check ac.getBlockHashesCache().len() == 0
816816

817+
test "Verify stateRoot and storageRoot for large storage trie":
818+
# We use a small slot value and a large number of iterations so that
819+
# the test also covers the scenario when the HashKey type contains
820+
# an embedded trie node (when the data length is less than 32 bytes) which,
821+
# when rlp encoded impacts the stateRoot and storageRoot calculation.
822+
const
823+
iterations = 110000
824+
address = Address.fromHex("0x123456cf1046e68e36E1aA2E0E07105eDDD1f08E")
825+
slotValue = u256(1)
826+
827+
let
828+
coreDb = newCoreDbRef(DefaultDbMemory)
829+
ledger = LedgerRef.init(coreDb.baseTxFrame())
830+
831+
for i in 1..iterations:
832+
ledger.setStorage(address, u256(i), slotValue)
833+
ledger.persist()
834+
835+
let
836+
stateRoot = ledger.getStateRoot()
837+
storageRoot = ledger.getStorageRoot(address)
838+
check:
839+
stateRoot == Hash32.fromHex("0xc70266a02ed1531d8e2c47c7ead2a6e8b1f01bf7dbee3b36f8d5dc543ad09658")
840+
storageRoot == Hash32.fromHex("0x619034bd7de60f9f8a08cdf277dc55da1984aae08c79e190faeafba27bc3c9d2")
841+
817842
# ------------------------------------------------------------------------------
818843
# Main function(s)
819844
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)