From 4de8eaa81d19620bc4dc195365ae5c40b51fd32b Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 28 Apr 2020 16:58:16 +0700 Subject: [PATCH] implement account code encoding and decoding --- stateless/test_witness_keys.nim | 58 ++++++++++++++++----------------- stateless/tree_from_witness.nim | 12 +++++-- stateless/witness_from_tree.nim | 6 ++-- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/stateless/test_witness_keys.nim b/stateless/test_witness_keys.nim index e3c201c3f..904194fcb 100644 --- a/stateless/test_witness_keys.nim +++ b/stateless/test_witness_keys.nim @@ -1,9 +1,12 @@ import randutils, stew/byteutils, random, - eth/[common, rlp], eth/trie/[hexary, db], + eth/[common, rlp], eth/trie/[hexary, db, trie_defs], faststreams/input_stream, nimcrypto/[utils, sysrand], ../stateless/[witness_from_tree, tree_from_witness] +type + DB = TrieDatabaseRef + proc randU256(): UInt256 = var bytes: array[32, byte] discard randomBytes(bytes[0].addr, sizeof(result)) @@ -12,56 +15,53 @@ proc randU256(): UInt256 = proc randNonce(): AccountNonce = discard randomBytes(result.addr, sizeof(result)) +proc randCode(db: DB): Hash256 = + if rand(0..1) == 0: + result = blankStringHash + else: + let codeLen = rand(1..150) + let code = randList(byte, rng(0, 255), codeLen, unique = false) + result = hexary.keccak(code) + db.put(result.data, code) + proc randHash(): Hash256 = discard randomBytes(result.data[0].addr, sizeof(result)) -proc randAccount(): Account = +proc randAccount(db: DB): Account = result.nonce = randNonce() result.balance = randU256() - result.codeHash = randHash() + result.codeHash = randCode(db) result.storageRoot = randHash() -proc runTest(keyBytes: int, valBytes: int, numPairs: int) = +proc runTest(numPairs: int) = var memDB = newMemoryDB() - var trie = initHexaryTrie(memDB) - - var - keys = newSeq[Bytes](numPairs) - vals = newSeq[Bytes](numPairs) + var trie = initSecureHexaryTrie(memDB) + var addrs = newSeq[Bytes](numPairs) for i in 0..