Skip to content

Commit

Permalink
Fix readOnlyLedger
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Dec 21, 2024
1 parent 30ef4c3 commit 5e89361
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion nimbus/core/executor/process_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ proc processTransactionImpl(

let
fork = vmState.fork
roDB = vmState.ReadOnlyLedger
roDB = vmState.readOnlyLedger
baseFee256 = header.eip1559BaseFee(fork)
baseFee = baseFee256.truncate(GasInt)
priorityFee = min(tx.maxPriorityFeePerGasNorm(), tx.maxFeePerGasNorm() - baseFee)
Expand Down
8 changes: 4 additions & 4 deletions nimbus/core/tx_pool/tx_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ func excessBlobGas*(xp: TxPoolRef): GasInt =
xp.vmState.blockCtx.excessBlobGas

proc getBalance*(xp: TxPoolRef; account: Address): UInt256 =
## Wrapper around `vmState.ReadOnlyLedger.getBalance()` for a `vmState`
## Wrapper around `vmState.readOnlyLedger.getBalance()` for a `vmState`
## descriptor positioned at the `dh.head`. This might differ from the
## `dh.vmState.ReadOnlyLedger.getBalance()` which returnes the current
## `dh.vmState.readOnlyLedger.getBalance()` which returnes the current
## balance relative to what has been accumulated by the current packing
## procedure.
xp.vmState.ledger.getBalance(account)

proc getNonce*(xp: TxPoolRef; account: Address): AccountNonce =
## Wrapper around `vmState.ReadOnlyLedger.getNonce()` for a `vmState`
## Wrapper around `vmState.readOnlyLedger.getNonce()` for a `vmState`
## descriptor positioned at the `dh.head`. This might differ from the
## `dh.vmState.ReadOnlyLedger.getNonce()` which returnes the current balance
## `dh.vmState.readOnlyLedger.getNonce()` which returnes the current balance
## relative to what has been accumulated by the current packing procedure.
xp.vmState.ledger.getNonce(account)

Expand Down
2 changes: 1 addition & 1 deletion nimbus/core/tx_pool/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ proc classifyValidatePacked(vmState: BaseVMState; item: TxItemRef): bool =
## is a wrapper around the `verifyTransaction()` call to be used in a similar
## fashion as in `asyncProcessTransactionImpl()`.
let
roDB = vmState.ReadOnlyLedger
roDB = vmState.readOnlyLedger
baseFee = vmState.blockCtx.baseFeePerGas.get(0.u256)
fork = vmState.fork
gasLimit = vmState.blockCtx.gasLimit
Expand Down
22 changes: 11 additions & 11 deletions nimbus/evm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,34 @@ template accountExists*(c: Computation, address: Address): bool =
c.host.accountExists(address)
else:
if c.fork >= FkSpurious:
not c.vmState.ReadOnlyLedger.isDeadAccount(address)
not c.vmState.readOnlyLedger.isDeadAccount(address)
else:
c.vmState.ReadOnlyLedger.accountExists(address)
c.vmState.readOnlyLedger.accountExists(address)

template getStorage*(c: Computation, slot: UInt256): UInt256 =
when evmc_enabled:
c.host.getStorage(c.msg.contractAddress, slot)
else:
c.vmState.ReadOnlyLedger.getStorage(c.msg.contractAddress, slot)
c.vmState.readOnlyLedger.getStorage(c.msg.contractAddress, slot)

template getBalance*(c: Computation, address: Address): UInt256 =
when evmc_enabled:
c.host.getBalance(address)
else:
c.vmState.ReadOnlyLedger.getBalance(address)
c.vmState.readOnlyLedger.getBalance(address)

template getCodeSize*(c: Computation, address: Address): uint =
when evmc_enabled:
c.host.getCodeSize(address)
else:
uint(c.vmState.ReadOnlyLedger.getCodeSize(address))
uint(c.vmState.readOnlyLedger.getCodeSize(address))

template getCodeHash*(c: Computation, address: Address): Hash32 =
when evmc_enabled:
c.host.getCodeHash(address)
else:
let
db = c.vmState.ReadOnlyLedger
db = c.vmState.readOnlyLedger
if not db.accountExists(address) or db.isEmptyAccount(address):
default(Hash32)
else:
Expand All @@ -197,7 +197,7 @@ template getCode*(c: Computation, address: Address): CodeBytesRef =
when evmc_enabled:
CodeBytesRef.init(c.host.copyCode(address))
else:
c.vmState.ReadOnlyLedger.getCode(address)
c.vmState.readOnlyLedger.getCode(address)

template setTransientStorage*(c: Computation, slot, val: UInt256) =
when evmc_enabled:
Expand All @@ -210,7 +210,7 @@ template getTransientStorage*(c: Computation, slot: UInt256): UInt256 =
when evmc_enabled:
c.host.getTransientStorage(c.msg.contractAddress, slot)
else:
c.vmState.ReadOnlyLedger.
c.vmState.readOnlyLedger.
getTransientStorage(c.msg.contractAddress, slot)

template resolveCodeSize*(c: Computation, address: Address): uint =
Expand All @@ -221,7 +221,7 @@ template resolveCodeSize*(c: Computation, address: Address): uint =
else:
c.host.getCodeSize(delegateTo)
else:
uint(c.vmState.ReadOnlyLedger.resolveCodeSize(address))
uint(c.vmState.readOnlyLedger.resolveCodeSize(address))

template resolveCodeHash*(c: Computation, address: Address): Hash32=
when evmc_enabled:
Expand All @@ -232,7 +232,7 @@ template resolveCodeHash*(c: Computation, address: Address): Hash32=
c.host.getCodeHash(delegateTo)
else:
let
db = c.vmState.ReadOnlyLedger
db = c.vmState.readOnlyLedger
if not db.accountExists(address) or db.isEmptyAccount(address):
default(Hash32)
else:
Expand All @@ -246,7 +246,7 @@ template resolveCode*(c: Computation, address: Address): CodeBytesRef =
else:
CodeBytesRef.init(c.host.copyCode(delegateTo))
else:
c.vmState.ReadOnlyLedger.resolveCode(address)
c.vmState.readOnlyLedger.resolveCode(address)

func newComputation*(vmState: BaseVMState,
keepStack: bool,
Expand Down
2 changes: 1 addition & 1 deletion nimbus/evm/interpreter/op_handlers/oph_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ proc gasEip7702CodeCheck*(c: Computation; address: Address): GasInt =
let code = when defined(evmc_enabled):
CodeBytesRef.init(c.host.copyCode(address))
else:
c.vmState.ReadOnlyLedger.getCode(address)
c.vmState.readOnlyLedger.getCode(address)
let delegateTo = parseDelegationAddress(code).valueOr:
return 0
c.delegateResolutionCost(delegateTo)
Expand Down
2 changes: 1 addition & 1 deletion nimbus/evm/interpreter/op_handlers/oph_memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ else:

proc sstoreNetGasMeteringImpl(c: Computation; slot, newValue: UInt256, coldAccess = 0.GasInt): EvmResultVoid =
let
ledger = c.vmState.ReadOnlyLedger
ledger = c.vmState.readOnlyLedger
currentValue = c.getStorage(slot)

gasParam = GasParamsSs(
Expand Down
2 changes: 1 addition & 1 deletion nimbus/evm/interpreter_dispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ proc beforeExecCreate(c: Computation): bool =

c.snapshot()

if c.vmState.ReadOnlyLedger().contractCollision(c.msg.contractAddress):
if c.vmState.readOnlyLedger().contractCollision(c.msg.contractAddress):
let blurb = c.msg.contractAddress.toHex
c.setError("Address collision when creating contract address=" & blurb, true)
c.rollback()
Expand Down
6 changes: 3 additions & 3 deletions nimbus/evm/message.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ proc generateContractAddress*(vmState: BaseVMState,
salt = ZERO_CONTRACTSALT,
code = CodeBytesRef(nil)): Address =
if kind == EVMC_CREATE:
let creationNonce = vmState.ReadOnlyLedger().getNonce(sender)
let creationNonce = vmState.readOnlyLedger().getNonce(sender)
generateAddress(sender, creationNonce)
else:
generateSafeAddress(sender, salt, code.bytes)
Expand All @@ -37,6 +37,6 @@ proc getCallCode*(vmState: BaseVMState, codeAddress: Address): CodeBytesRef =
return CodeBytesRef(nil)

if vmState.fork >= FkPrague:
vmState.ReadOnlyLedger.resolveCode(codeAddress)
vmState.readOnlyLedger.resolveCode(codeAddress)
else:
vmState.ReadOnlyLedger.getCode(codeAddress)
vmState.readOnlyLedger.getCode(codeAddress)
2 changes: 1 addition & 1 deletion nimbus/evm/state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ method getAncestorHash*(
return default(Hash32)
blockHash

proc ReadOnlyLedger*(vmState: BaseVMState): ReadOnlyLedger {.inline.} =
proc readOnlyLedger*(vmState: BaseVMState): ReadOnlyLedger {.inline.} =
ReadOnlyLedger(vmState.ledger)

template mutateLedger*(vmState: BaseVMState, body: untyped) =
Expand Down
2 changes: 1 addition & 1 deletion nimbus/transaction/call_evm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ proc rpcEstimateGas*(args: TransactionArgs,
if args.source.isNone:
return err(evmErr(EvmInvalidParam))

let balance = vmState.ReadOnlyLedger.getBalance(args.source.get)
let balance = vmState.readOnlyLedger.getBalance(args.source.get)
var available = balance
if args.value.isSome:
let value = args.value.get
Expand Down
20 changes: 10 additions & 10 deletions nimbus/transaction/host_services.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ when use_evmc_glue:

proc accountExists(host: TransactionHost, address: HostAddress): bool {.show.} =
if host.vmState.fork >= FkSpurious:
not host.vmState.ReadOnlyLedger.isDeadAccount(address)
not host.vmState.readOnlyLedger.isDeadAccount(address)
else:
host.vmState.ReadOnlyLedger.accountExists(address)
host.vmState.readOnlyLedger.accountExists(address)

# TODO: Why is `address` an argument in `getStorage`, `setStorage` and
# `selfDestruct`, if an EVM is only allowed to do these things to its own
# contract account and the host always knows which account?

proc getStorage(host: TransactionHost, address: HostAddress, key: HostKey): HostValue {.show.} =
host.vmState.ReadOnlyLedger.getStorage(address, key)
host.vmState.readOnlyLedger.getStorage(address, key)

proc setStorage(host: TransactionHost, address: HostAddress,
key: HostKey, newVal: HostValue): EvmcStorageStatus {.show.} =
let
db = host.vmState.ReadOnlyLedger
db = host.vmState.readOnlyLedger
currentVal = db.getStorage(address, key)

if currentVal == newVal:
Expand Down Expand Up @@ -174,15 +174,15 @@ proc setStorage(host: TransactionHost, address: HostAddress,
return EVMC_STORAGE_ASSIGNED

proc getBalance(host: TransactionHost, address: HostAddress): HostBalance {.show.} =
host.vmState.ReadOnlyLedger.getBalance(address)
host.vmState.readOnlyLedger.getBalance(address)

proc getCodeSize(host: TransactionHost, address: HostAddress): HostSize {.show.} =
# TODO: Check this `HostSize`, it was copied as `uint` from other code.
# Note: Old `evmc_host` uses `getCode(address).len` instead.
host.vmState.ReadOnlyLedger.getCodeSize(address).HostSize
host.vmState.readOnlyLedger.getCodeSize(address).HostSize

proc getCodeHash(host: TransactionHost, address: HostAddress): HostHash {.show.} =
let db = host.vmState.ReadOnlyLedger
let db = host.vmState.readOnlyLedger
# TODO: Copied from `Computation`, but check if that code is wrong with
# `FkSpurious`, as it has different calls from `accountExists` above.
if not db.accountExists(address) or db.isEmptyAccount(address):
Expand All @@ -205,7 +205,7 @@ proc copyCode(host: TransactionHost, address: HostAddress,
#
# Note, when there is no code, `getCode` result is empty `seq`. It was `nil`
# when the DB was first implemented, due to Nim language changes since then.
let code = host.vmState.ReadOnlyLedger.getCode(address)
let code = host.vmState.readOnlyLedger.getCode(address)
var safe_len: int = code.len # It's safe to assume >= 0.

if code_offset >= safe_len.HostSize:
Expand Down Expand Up @@ -295,15 +295,15 @@ proc accessStorage(host: TransactionHost, address: HostAddress,

proc getTransientStorage(host: TransactionHost,
address: HostAddress, key: HostKey): HostValue {.show.} =
host.vmState.ReadOnlyLedger.getTransientStorage(address, key)
host.vmState.readOnlyLedger.getTransientStorage(address, key)

proc setTransientStorage(host: TransactionHost, address: HostAddress,
key: HostKey, newVal: HostValue) {.show.} =
host.vmState.mutateLedger:
db.setTransientStorage(address, key, newVal)

proc getDelegateAddress(host: TransactionHost, address: HostAddress): HostAddress {.show.} =
let db = host.vmState.ReadOnlyLedger
let db = host.vmState.readOnlyLedger
db.getDelegateAddress(address)

when use_evmc_glue:
Expand Down
2 changes: 1 addition & 1 deletion nimbus/utils/debug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ proc debugAccounts*(vmState: BaseVMState): string =
accountList.add address

let res = %{
"stateRoot": %($vmState.ReadOnlyLedger.getStateRoot()),
"stateRoot": %($vmState.readOnlyLedger.getStateRoot()),
"accounts": %dumpAccounts(vmState.ledger, accountList),
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_generalstate_json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ proc testFixtureIndexes(ctx: var TestCtx, testStatusIMPL: var TestStatus) =
coinbaseStateClearing(vmState, miner)

block post:
let obtainedHash = vmState.ReadOnlyLedger.getStateRoot()
let obtainedHash = vmState.readOnlyLedger.getStateRoot()
check obtainedHash == ctx.expectedHash
let logEntries = vmState.getAndClearLogEntries()
let actualLogsHash = rlpHash(logEntries)
Expand Down
2 changes: 1 addition & 1 deletion tools/evmstate/evmstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ proc runExecution(ctx: var StateContext, conf: StateConf, pre: JsonNode): StateR
db.persist(clearEmptyAccount = false) # settle accounts storage

defer:
let stateRoot = vmState.ReadOnlyLedger.getStateRoot()
let stateRoot = vmState.readOnlyLedger.getStateRoot()
ctx.verifyResult(vmState, stateRoot)
result = StateResult(
name : ctx.name,
Expand Down

0 comments on commit 5e89361

Please sign in to comment.