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
|
import
|
||||||
sequtils, strformat, tables,
|
sequtils, strformat, tables,
|
||||||
chronicles, eth_common, nimcrypto, rlp, eth_trie/[hexary, db],
|
chronicles, eth_common, nimcrypto, rlp, eth_trie/[hexary, db],
|
||||||
../constants, ../errors, ../validation
|
../constants, ../errors, ../validation,
|
||||||
|
storage_types
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
topics = "state_db"
|
topics = "state_db"
|
||||||
@ -88,7 +89,7 @@ proc setStorageRoot*(db: var AccountStateDB, address: EthAddress, storageRoot: H
|
|||||||
account.storageRoot = storageRoot
|
account.storageRoot = storageRoot
|
||||||
db.setAccount(address, account)
|
db.setAccount(address, account)
|
||||||
|
|
||||||
proc getStorageRoot*(db: var AccountStateDB, address: EthAddress): Hash256 =
|
proc getStorageRoot*(db: AccountStateDB, address: EthAddress): Hash256 =
|
||||||
var account = db.getAccount(address)
|
var account = db.getAccount(address)
|
||||||
account.storageRoot
|
account.storageRoot
|
||||||
|
|
||||||
@ -105,9 +106,28 @@ proc setStorage*(db: var AccountStateDB,
|
|||||||
else:
|
else:
|
||||||
accountTrie.del(slotAsKey)
|
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
|
account.storageRoot = accountTrie.rootHash
|
||||||
db.setAccount(address, account)
|
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) =
|
proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) =
|
||||||
let
|
let
|
||||||
account = db.getAccount(address)
|
account = db.getAccount(address)
|
||||||
|
@ -8,6 +8,7 @@ type
|
|||||||
blockHashToScore
|
blockHashToScore
|
||||||
transactionHashToBlock
|
transactionHashToBlock
|
||||||
canonicalHeadHash
|
canonicalHeadHash
|
||||||
|
slotHashToSlot
|
||||||
|
|
||||||
DbKey* = object
|
DbKey* = object
|
||||||
# The first byte stores the key type. The rest are key-specific values
|
# 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.data[0] = byte ord(canonicalHeadHash)
|
||||||
result.dataEndPos = 1
|
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}
|
const hashHolderKinds = {genericHash, blockHashToScore, transactionHashToBlock}
|
||||||
|
|
||||||
template toOpenArray*(k: DbKey): openarray[byte] =
|
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())
|
mem.add(%c.memory.bytes.toOpenArray(i * chunkLen, (i + 1) * chunkLen - 1).toHex())
|
||||||
j["memory"] = mem
|
j["memory"] = mem
|
||||||
|
|
||||||
# TODO: this seems very inefficient
|
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) =
|
||||||
# could we improve it?
|
let j = tracer.trace["structLogs"].elems[^1]
|
||||||
|
|
||||||
# TODO: figure out how to get storage
|
# TODO: figure out how to get storage
|
||||||
# when contract excecution interrupted by exception
|
# when contract excecution interrupted by exception
|
||||||
if TracerFlags.DisableStorage notin tracer.flags:
|
if TracerFlags.DisableStorage notin tracer.flags:
|
||||||
var storage = newJObject()
|
var storage = newJObject()
|
||||||
var stateDB = c.vmState.chaindb.getStateDb(c.vmState.blockHeader.stateRoot, readOnly = true)
|
var stateDB = c.vmState.chaindb.getStateDb(c.vmState.blockHeader.stateRoot, readOnly = true)
|
||||||
let storageRoot = stateDB.getStorageRoot(c.msg.storageAddress)
|
for key, value in stateDB.storage(c.msg.storageAddress):
|
||||||
var trie = initHexaryTrie(c.vmState.chaindb.db, storageRoot)
|
storage[key.dumpHex] = %(value.dumpHex)
|
||||||
for k, v in trie:
|
|
||||||
var key = k.toOpenArray.toHex
|
|
||||||
if key.len != 0:
|
|
||||||
storage[key] = %(v.toOpenArray.toHex)
|
|
||||||
j["storage"] = storage
|
j["storage"] = storage
|
||||||
|
|
||||||
proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation) =
|
|
||||||
let j = tracer.trace["structLogs"].elems[^1]
|
|
||||||
j["gasCost"] = %(tracer.gasRemaining - c.gasMeter.gasRemaining)
|
j["gasCost"] = %(tracer.gasRemaining - c.gasMeter.gasRemaining)
|
||||||
|
|
||||||
proc traceError*(tracer: var TransactionTracer, c: BaseComputation) =
|
proc traceError*(tracer: var TransactionTracer, c: BaseComputation) =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user