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.
This commit is contained in:
Zahary Karadjov 2018-08-08 15:48:34 +03:00
parent 21fcaeb8ef
commit d902be66ee
2 changed files with 16 additions and 18 deletions

View File

@ -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)

View File

@ -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,