From d902be66eeed0b3d146fd04c8bc7eb562688dc14 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 8 Aug 2018 15:48:34 +0300 Subject: [PATCH] Fix the genesis state hash computation The account database needed to use the secure flavour of the Hexary Trie. This means all keys are SHA3 hashed before being inserted. --- nimbus/db/state_db.nim | 13 ++++++++----- nimbus/genesis.nim | 21 ++++++++------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/nimbus/db/state_db.nim b/nimbus/db/state_db.nim index ff04a8045..71fc5a69b 100644 --- a/nimbus/db/state_db.nim +++ b/nimbus/db/state_db.nim @@ -12,7 +12,7 @@ import type AccountStateDB* = ref object - trie: HexaryTrie + trie: SecureHexaryTrie proc rootHash*(accountDb: AccountStateDB): KeccakHash = accountDb.trie.rootHash @@ -23,7 +23,7 @@ proc rootHash*(accountDb: AccountStateDB): KeccakHash = proc newAccountStateDB*(backingStore: TrieDatabaseRef, root: KeccakHash, readOnly: bool = false): AccountStateDB = result.new() - result.trie = initHexaryTrie(backingStore, root) + result.trie = initSecureHexaryTrie(backingStore, root) proc logger*(db: AccountStateDB): Logger = logging.getLogger("db.State") @@ -42,7 +42,7 @@ proc getAccount(db: AccountStateDB, address: EthAddress): Account = else: result = newAccount() -proc setAccount(db: AccountStateDB, address: EthAddress, account: Account) = +proc setAccount*(db: AccountStateDB, address: EthAddress, account: Account) = db.trie.put createRangeFromAddress(address), rlp.encode(account) proc getCodeHash*(db: AccountStateDB, address: EthAddress): Hash256 = @@ -69,6 +69,9 @@ template createTrieKeyFromSlot(slot: UInt256): ByteRange = # Original py-evm code: # pad32(int_to_big_endian(slot)) +template getAccountTrie(stateDb: AccountStateDB, account: Account): auto = + initSecureHexaryTrie(HexaryTrie(stateDb.trie).db, account.storageRoot) + proc setStorage*(db: var AccountStateDB, address: EthAddress, slot: UInt256, value: UInt256) = @@ -76,7 +79,7 @@ proc setStorage*(db: var AccountStateDB, #validateGte(slot, 0, title="Storage Slot") var account = db.getAccount(address) - var accountTrie = initHexaryTrie(db.trie.db, account.storageRoot) + var accountTrie = getAccountTrie(db, account) let slotAsKey = createTrieKeyFromSlot slot if value > 0: @@ -94,7 +97,7 @@ proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt2 let account = db.getAccount(address) slotAsKey = createTrieKeyFromSlot slot - accountTrie = initHexaryTrie(db.trie.db, account.storageRoot) + accountTrie = getAccountTrie(db, account) let foundRecord = accountTrie.get(slotAsKey) diff --git a/nimbus/genesis.nim b/nimbus/genesis.nim index 44c92917c..168f88b19 100644 --- a/nimbus/genesis.nim +++ b/nimbus/genesis.nim @@ -70,22 +70,17 @@ proc toBlock*(g: Genesis): BlockHeader = for address, account in g.alloc: sdb.setBalance(address, account.balance) - sdb.setCode(address, account.code.toRange) - sdb.setNonce(address, account.nonce) - for k, v in account.storage: - sdb.setStorage(address, k, v) + when false: + # These properties are empty in all genesis blocks so far + sdb.setCode(address, account.code.toRange) + sdb.setNonce(address, account.nonce) + + for k, v in account.storage: + sdb.setStorage(address, k, v) var root = sdb.rootHash - - block tempRootHashStub: # TODO: Remove this block when we calculate the root hash correctly - if g.config.chainId == 1: - const correctMainnetRootHash = toDigest("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544") - if root != correctMainnetRootHash: - error "Root hash incorrect. Stubbing it out." - root = correctMainnetRootHash - else: - error "Yay! Root hash is correct. Please remove the block where this message comes from." + doAssert $root == "D7F8974FB5AC78D9AC099B9AD5018BEDC2CE0A72DAD1827A1709DA30580F0544" result = BlockHeader( nonce: g.nonce,