fix at various places related to missing accounts_cache.persist call

This commit is contained in:
jangko 2020-06-01 13:45:32 +07:00
parent 079579c1cb
commit 844071033a
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 33 additions and 23 deletions

View File

@ -416,18 +416,22 @@ proc persist*(ac: var AccountsCache) =
iterator storage*(ac: AccountsCache, address: EthAddress): (UInt256, UInt256) =
# beware that if the account not persisted,
# the storage root will not be updated
let storageRoot = ac.getAccount(address).account.storageRoot
var trie = initHexaryTrie(ac.db, storageRoot)
let acc = ac.getAccount(address, false)
if not acc.isNil:
let storageRoot = acc.account.storageRoot
var trie = initHexaryTrie(ac.db, storageRoot)
for slot, value in trie:
if slot.len != 0:
var keyData = ac.db.get(slotHashToSlotKey(slot).toOpenArray)
yield (rlp.decode(keyData, UInt256), rlp.decode(value, UInt256))
for slot, value in trie:
if slot.len != 0:
var keyData = ac.db.get(slotHashToSlotKey(slot).toOpenArray)
yield (rlp.decode(keyData, UInt256), rlp.decode(value, UInt256))
proc getStorageRoot*(ac: AccountsCache, address: EthAddress): Hash256 =
# beware that if the account not persisted,
# the storage root will not be updated
result = ac.getAccount(address).account.storageRoot
let acc = ac.getAccount(address, false)
if acc.isNil: emptyAcc.storageRoot
else: acc.account.storageRoot
proc rootHash*(db: ReadOnlyStateDB): KeccakHash {.borrow.}
proc getCodeHash*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.}

View File

@ -143,6 +143,7 @@ proc processBlock*(chainDB: BaseChainDB, header: BlockHeader, body: BlockBody, v
# Reward beneficiary
vmState.mutateStateDB:
db.addBalance(header.coinbase, mainReward)
db.persist()
let stateDb = vmState.accountDb
if header.stateRoot != stateDb.rootHash:

View File

@ -13,7 +13,7 @@ import
eth/[common, keys, rlp, p2p], nimcrypto,
../transaction, ../config, ../vm_state, ../constants, ../vm_types,
../vm_state_transactions, ../utils,
../db/[db_chain, state_db],
../db/[db_chain, accounts_cache],
rpc_types, rpc_utils, ../vm/[message, computation],
../vm/interpreter/vm_forks
@ -166,9 +166,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
let
accountDb = accountDbFromTag(quantityTag)
addrBytes = data.toAddress
storage = accountDb.getStorage(addrBytes, quantity.u256)
if storage[1]:
result = storage[0]
result = accountDb.getStorage(addrBytes, quantity.u256)
rpcsrv.rpc("eth_getTransactionCount") do(data: EthAddressStr, quantityTag: string) -> AccountNonce:
## Returns the number of transactions sent from an address.

View File

@ -1,5 +1,5 @@
import
db/[db_chain, state_db, capturedb], eth/common, utils, json,
db/[db_chain, accounts_cache, capturedb], eth/common, utils, json,
constants, vm_state, vm_types, transaction, p2p/executor,
eth/trie/db, nimcrypto, strutils,
chronicles, rpc/hexstrings, launcher,
@ -33,18 +33,23 @@ proc toJson*(receipts: seq[Receipt]): JsonNode =
for receipt in receipts:
result.add receipt.toJson
proc captureAccount(n: JsonNode, db: AccountStateDB, address: EthAddress, name: string) =
proc captureAccount(n: JsonNode, db: AccountsCache, address: EthAddress, name: string) =
var jaccount = newJObject()
jaccount["name"] = %name
jaccount["address"] = %("0x" & $address)
let account = db.getAccount(address)
jaccount["nonce"] = %(encodeQuantity(account.nonce).toLowerAscii)
jaccount["balance"] = %("0x" & account.balance.toHex)
let nonce = db.getNonce(address)
let balance = db.getBalance(address)
let codeHash = db.getCodeHash(address)
let storageRoot = db.getStorageRoot(address)
jaccount["nonce"] = %(encodeQuantity(nonce).toLowerAscii)
jaccount["balance"] = %("0x" & balance.toHex)
let code = db.getCode(address)
jaccount["codeHash"] = %("0x" & ($account.codeHash).toLowerAscii)
jaccount["codeHash"] = %("0x" & ($codeHash).toLowerAscii)
jaccount["code"] = %("0x" & toHex(code, true))
jaccount["storageRoot"] = %("0x" & ($account.storageRoot).toLowerAscii)
jaccount["storageRoot"] = %("0x" & ($storageRoot).toLowerAscii)
var storage = newJObject()
for key, value in db.storage(address):
@ -102,6 +107,7 @@ proc traceTransaction*(chainDB: BaseChainDB, header: BlockHeader,
before.captureAccount(stateDb, sender, senderName)
before.captureAccount(stateDb, recipient, recipientName)
before.captureAccount(stateDb, header.coinbase, minerName)
stateDb.persist()
stateDiff["beforeRoot"] = %($stateDb.rootHash)
beforeRoot = stateDb.rootHash
@ -112,11 +118,12 @@ proc traceTransaction*(chainDB: BaseChainDB, header: BlockHeader,
after.captureAccount(stateDb, recipient, recipientName)
after.captureAccount(stateDb, header.coinbase, minerName)
vmState.removeTracedAccounts(sender, recipient, header.coinbase)
stateDb.persist()
stateDiff["afterRoot"] = %($stateDb.rootHash)
break
# internal transactions:
var stateBefore = newAccountStateDB(captureTrieDB, beforeRoot, chainDB.pruneTrie)
var stateBefore = AccountsCache.init(captureTrieDB, beforeRoot, chainDB.pruneTrie)
for idx, acc in tracedAccountsPairs(vmState):
before.captureAccount(stateBefore, acc, internalTxName & $idx)
@ -146,7 +153,7 @@ proc dumpBlockState*(db: BaseChainDB, header: BlockHeader, body: BlockBody, dump
var
before = newJArray()
after = newJArray()
stateBefore = newAccountStateDB(captureTrieDB, parent.stateRoot, db.pruneTrie)
stateBefore = AccountsCache.init(captureTrieDB, parent.stateRoot, db.pruneTrie)
for idx, tx in body.transactions:
let sender = tx.getSender

View File

@ -7,7 +7,7 @@ import
import
options, json, os, eth/trie/[db, hexary],
../nimbus/[vm_state, vm_types, transaction, utils],
../nimbus/db/[db_chain, state_db],
../nimbus/db/[db_chain, accounts_cache],
../nimbus/vm_state_transactions,
../nimbus/vm/interpreter/vm_forks,
../nimbus/vm/[message, computation, memory]
@ -303,8 +303,8 @@ proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool =
var
stateDB = computation.vmState.accountDb
account = stateDB.getAccount(computation.msg.contractAddress)
trie = initSecureHexaryTrie(chainDB.db, account.storageRoot)
storageRoot = stateDB.getStorageRoot(computation.msg.contractAddress)
trie = initSecureHexaryTrie(chainDB.db, storageRoot)
for kv in boa.storage:
let key = kv[0].toHex()