mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-12 05:14:14 +00:00
correcting account storage retrieval
This commit is contained in:
parent
c954dc7d62
commit
7a3ddfec87
@ -8,7 +8,8 @@
|
||||
import
|
||||
sequtils, strformat, tables,
|
||||
chronicles, eth_common, nimcrypto, rlp, eth_trie/[hexary, db],
|
||||
../constants, ../errors, ../validation
|
||||
../constants, ../errors, ../validation,
|
||||
storage_types
|
||||
|
||||
logScope:
|
||||
topics = "state_db"
|
||||
@ -88,7 +89,7 @@ proc setStorageRoot*(db: var AccountStateDB, address: EthAddress, storageRoot: H
|
||||
account.storageRoot = storageRoot
|
||||
db.setAccount(address, account)
|
||||
|
||||
proc getStorageRoot*(db: var AccountStateDB, address: EthAddress): Hash256 =
|
||||
proc getStorageRoot*(db: AccountStateDB, address: EthAddress): Hash256 =
|
||||
var account = db.getAccount(address)
|
||||
account.storageRoot
|
||||
|
||||
@ -105,9 +106,28 @@ proc setStorage*(db: var AccountStateDB,
|
||||
else:
|
||||
accountTrie.del(slotAsKey)
|
||||
|
||||
# map slothash back to slot value
|
||||
# see iterator storage below
|
||||
var
|
||||
triedb = HexaryTrie(db.trie).db
|
||||
# slotHash can be obtained from accountTrie.put?
|
||||
slotHash = keccak256.digest(slot.toByteArrayBE)
|
||||
triedb.put(slotHashToSlotKey(slotHash.data).toOpenArray, rlp.encode(slot))
|
||||
|
||||
account.storageRoot = accountTrie.rootHash
|
||||
db.setAccount(address, account)
|
||||
|
||||
iterator storage*(db: AccountStateDB, address: EthAddress): (UInt256, UInt256) =
|
||||
let
|
||||
storageRoot = db.getStorageRoot(address)
|
||||
triedb = HexaryTrie(db.trie).db
|
||||
var trie = initHexaryTrie(triedb, storageRoot)
|
||||
|
||||
for key, value in trie:
|
||||
if key.len != 0:
|
||||
var keyData = triedb.get(slotHashToSlotKey(key.toOpenArray).toOpenArray).toRange
|
||||
yield (rlp.decode(keyData, UInt256), rlp.decode(value, UInt256))
|
||||
|
||||
proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) =
|
||||
let
|
||||
account = db.getAccount(address)
|
||||
|
@ -8,6 +8,7 @@ type
|
||||
blockHashToScore
|
||||
transactionHashToBlock
|
||||
canonicalHeadHash
|
||||
slotHashToSlot
|
||||
|
||||
DbKey* = object
|
||||
# The first byte stores the key type. The rest are key-specific values
|
||||
@ -41,6 +42,12 @@ proc canonicalHeadHashKey*(): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(canonicalHeadHash)
|
||||
result.dataEndPos = 1
|
||||
|
||||
proc slotHashToSlotKey*(h: openArray[byte]): DbKey {.inline.} =
|
||||
assert(h.len == 32)
|
||||
result.data[0] = byte ord(slotHashToSlot)
|
||||
result.data[1 .. 32] = h
|
||||
result.dataEndPos = uint8 32
|
||||
|
||||
const hashHolderKinds = {genericHash, blockHashToScore, transactionHashToBlock}
|
||||
|
||||
template toOpenArray*(k: DbKey): openarray[byte] =
|
||||
|
@ -46,23 +46,18 @@ proc traceOpCodeStarted*(tracer: var TransactionTracer, c: BaseComputation, op:
|
||||
mem.add(%c.memory.bytes.toOpenArray(i * chunkLen, (i + 1) * chunkLen - 1).toHex())
|
||||
j["memory"] = mem
|
||||
|
||||
# TODO: this seems very inefficient
|
||||
# could we improve it?
|
||||
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) =
|
||||
let j = tracer.trace["structLogs"].elems[^1]
|
||||
|
||||
# TODO: figure out how to get storage
|
||||
# when contract excecution interrupted by exception
|
||||
if TracerFlags.DisableStorage notin tracer.flags:
|
||||
var storage = newJObject()
|
||||
var stateDB = c.vmState.chaindb.getStateDb(c.vmState.blockHeader.stateRoot, readOnly = true)
|
||||
let storageRoot = stateDB.getStorageRoot(c.msg.storageAddress)
|
||||
var trie = initHexaryTrie(c.vmState.chaindb.db, storageRoot)
|
||||
for k, v in trie:
|
||||
var key = k.toOpenArray.toHex
|
||||
if key.len != 0:
|
||||
storage[key] = %(v.toOpenArray.toHex)
|
||||
for key, value in stateDB.storage(c.msg.storageAddress):
|
||||
storage[key.dumpHex] = %(value.dumpHex)
|
||||
j["storage"] = storage
|
||||
|
||||
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) =
|
||||
let j = tracer.trace["structLogs"].elems[^1]
|
||||
j["gasCost"] = %(tracer.gasRemaining - c.gasMeter.gasRemaining)
|
||||
|
||||
proc traceError*(tracer: var TransactionTracer, c: BaseComputation) =
|
||||
|
Loading…
x
Reference in New Issue
Block a user