address review comments

This commit is contained in:
Zahary Karadjov 2018-08-09 12:25:37 +03:00
parent d902be66ee
commit 350188bd29
2 changed files with 15 additions and 26 deletions

View File

@ -92,8 +92,6 @@ proc setStorage*(db: var AccountStateDB,
db.setAccount(address, account) db.setAccount(address, account)
proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) = proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) =
#validateGte(slot, 0, title="Storage Slot")
let let
account = db.getAccount(address) account = db.getAccount(address)
slotAsKey = createTrieKeyFromSlot slot slotAsKey = createTrieKeyFromSlot slot
@ -107,17 +105,13 @@ proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt2
else: else:
result = (0.u256, false) result = (0.u256, false)
proc setNonce*(db: var AccountStateDB, address: EthAddress, nonce: UInt256) = proc setNonce*(db: var AccountStateDB, address: EthAddress, newNonce: UInt256) =
#validateGte(nonce, 0, title="Nonce")
var account = db.getAccount(address) var account = db.getAccount(address)
account.nonce = nonce if newNonce != account.nonce:
account.nonce = newNonce
db.setAccount(address, account) db.setAccount(address, account)
proc getNonce*(db: AccountStateDB, address: EthAddress): UInt256 = proc getNonce*(db: AccountStateDB, address: EthAddress): UInt256 =
# TODO it is very strange that we require a var param here
let account = db.getAccount(address) let account = db.getAccount(address)
account.nonce account.nonce
@ -127,12 +121,13 @@ proc toByteRange_Unnecessary*(h: KeccakHash): ByteRange =
return s.toRange return s.toRange
proc setCode*(db: var AccountStateDB, address: EthAddress, code: ByteRange) = proc setCode*(db: var AccountStateDB, address: EthAddress, code: ByteRange) =
var account = db.getAccount(address) var account = db.getAccount(address)
account.codeHash = keccak256.digest code.toOpenArray let newCodeHash = keccak256.digest code.toOpenArray
# XXX: this uses the journaldb in py-evm if newCodeHash != account.codeHash:
db.trie.put(account.codeHash.toByteRange_Unnecessary, code) account.codeHash = newCodeHash
db.setAccount(address, account) # XXX: this uses the journaldb in py-evm
db.trie.put(account.codeHash.toByteRange_Unnecessary, code)
db.setAccount(address, account)
proc getCode*(db: AccountStateDB, address: EthAddress): ByteRange = proc getCode*(db: AccountStateDB, address: EthAddress): ByteRange =
let codeHash = db.getCodeHash(address) let codeHash = db.getCodeHash(address)

View File

@ -1,6 +1,6 @@
import db/[db_chain, state_db], genesis_alloc, eth_common, tables, stint, import db/[db_chain, state_db], genesis_alloc, eth_common, tables, stint,
byteutils, times, config, rlp, ranges, block_types, eth_trie, byteutils, times, config, rlp, ranges, block_types, eth_trie,
eth_trie/memdb, constants, nimcrypto, chronicles eth_trie/memdb, account, constants, nimcrypto, chronicles
type type
Genesis* = object Genesis* = object
@ -69,18 +69,12 @@ proc toBlock*(g: Genesis): BlockHeader =
var sdb = newAccountStateDB(tdb, trie.rootHash) var sdb = newAccountStateDB(tdb, trie.rootHash)
for address, account in g.alloc: for address, account in g.alloc:
sdb.setBalance(address, account.balance) sdb.setAccount(address, newAccount(account.nonce, account.balance))
sdb.setCode(address, account.code.toRange)
when false: for k, v in account.storage:
# These properties are empty in all genesis blocks so far sdb.setStorage(address, k, v)
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 var root = sdb.rootHash
doAssert $root == "D7F8974FB5AC78D9AC099B9AD5018BEDC2CE0A72DAD1827A1709DA30580F0544"
result = BlockHeader( result = BlockHeader(
nonce: g.nonce, nonce: g.nonce,