2021-07-06 13:14:45 +00:00
|
|
|
# Nimbus
|
2023-05-10 16:04:35 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-07-06 13:14:45 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
import
|
2023-06-12 05:01:48 +00:00
|
|
|
std/strutils,
|
2022-12-02 04:35:41 +00:00
|
|
|
../../common/common,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../db/accounts_cache,
|
|
|
|
../../transaction/call_evm,
|
2023-08-30 16:29:48 +00:00
|
|
|
../../transaction/call_common,
|
2022-04-05 10:22:46 +00:00
|
|
|
../../transaction,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../vm_state,
|
|
|
|
../../vm_types,
|
2023-04-12 12:39:11 +00:00
|
|
|
../../evm/async/operations,
|
2023-08-30 16:29:48 +00:00
|
|
|
../../constants,
|
2021-07-06 13:14:45 +00:00
|
|
|
../validate,
|
2023-04-12 12:39:11 +00:00
|
|
|
chronos,
|
2022-01-10 09:04:06 +00:00
|
|
|
stew/results
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc eip1559BaseFee(header: BlockHeader; fork: EVMFork): UInt256 =
|
2022-01-10 09:04:06 +00:00
|
|
|
## Actually, `baseFee` should be 0 for pre-London headers already. But this
|
|
|
|
## function just plays safe. In particular, the `test_general_state_json.nim`
|
|
|
|
## module modifies this block header `baseFee` field unconditionally :(.
|
|
|
|
if FkLondon <= fork:
|
|
|
|
result = header.baseFee
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc commitOrRollbackDependingOnGasUsed(
|
|
|
|
vmState: BaseVMState, accTx: SavePoint,
|
|
|
|
header: BlockHeader, tx: Transaction,
|
|
|
|
gasBurned: GasInt, priorityFee: GasInt):
|
2023-06-12 05:01:48 +00:00
|
|
|
Result[GasInt, string] {.raises: [].} =
|
2023-04-12 12:39:11 +00:00
|
|
|
# Make sure that the tx does not exceed the maximum cumulative limit as
|
|
|
|
# set in the block header. Again, the eip-1559 reference does not mention
|
|
|
|
# an early stop. It would rather detect differing values for the block
|
|
|
|
# header `gasUsed` and the `vmState.cumulativeGasUsed` at a later stage.
|
2023-04-11 08:28:45 +00:00
|
|
|
if header.gasLimit < vmState.cumulativeGasUsed + gasBurned:
|
2023-06-12 05:01:48 +00:00
|
|
|
try:
|
|
|
|
vmState.stateDB.rollback(accTx)
|
|
|
|
return err("invalid tx: block header gasLimit reached. gasLimit=$1, gasUsed=$2, addition=$3" % [
|
|
|
|
$header.gasLimit, $vmState.cumulativeGasUsed, $gasBurned])
|
|
|
|
except ValueError as ex:
|
|
|
|
return err(ex.msg)
|
2023-04-12 12:39:11 +00:00
|
|
|
else:
|
|
|
|
# Accept transaction and collect mining fee.
|
|
|
|
vmState.stateDB.commit(accTx)
|
|
|
|
vmState.stateDB.addBalance(vmState.coinbase(), gasBurned.u256 * priorityFee.u256)
|
|
|
|
vmState.cumulativeGasUsed += gasBurned
|
2023-04-11 08:28:45 +00:00
|
|
|
|
|
|
|
# Return remaining gas to the block gas counter so it is
|
|
|
|
# available for the next transaction.
|
|
|
|
vmState.gasPool += tx.gasLimit - gasBurned
|
2023-04-12 12:39:11 +00:00
|
|
|
return ok(gasBurned)
|
|
|
|
|
|
|
|
proc asyncProcessTransactionImpl(
|
2022-01-10 09:04:06 +00:00
|
|
|
vmState: BaseVMState; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## Transaction to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
|
|
|
header: BlockHeader; ## Header for the block containing the current tx
|
2023-06-12 05:01:48 +00:00
|
|
|
fork: EVMFork): Future[Result[GasInt, string]]
|
2022-01-10 09:04:06 +00:00
|
|
|
# wildcard exception, wrapped below
|
2023-04-12 12:39:11 +00:00
|
|
|
{.async, gcsafe.} =
|
2022-01-10 09:04:06 +00:00
|
|
|
## Modelled after `https://eips.ethereum.org/EIPS/eip-1559#specification`_
|
|
|
|
## which provides a backward compatible framwork for EIP1559.
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
let
|
|
|
|
roDB = vmState.readOnlyStateDB
|
|
|
|
baseFee256 = header.eip1559BaseFee(fork)
|
2022-04-05 10:22:46 +00:00
|
|
|
baseFee = baseFee256.truncate(GasInt)
|
2023-08-22 06:05:10 +00:00
|
|
|
tx = eip1559TxNormalization(tx, baseFee)
|
2021-07-06 13:14:45 +00:00
|
|
|
priorityFee = min(tx.maxPriorityFee, tx.maxFee - baseFee)
|
2023-09-24 15:25:41 +00:00
|
|
|
excessBlobGas = header.excessBlobGas.get(0'u64)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
# Return failure unless explicitely set `ok()`
|
2023-06-12 05:01:48 +00:00
|
|
|
var res: Result[GasInt, string] = err("")
|
2023-04-12 12:39:11 +00:00
|
|
|
|
|
|
|
await ifNecessaryGetAccounts(vmState, @[sender, vmState.coinbase()])
|
|
|
|
if tx.to.isSome:
|
|
|
|
await ifNecessaryGetCode(vmState, tx.to.get)
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2023-04-11 08:28:45 +00:00
|
|
|
# buy gas, then the gas goes into gasMeter
|
|
|
|
if vmState.gasPool < tx.gasLimit:
|
2023-06-12 05:01:48 +00:00
|
|
|
return err("gas limit reached. gasLimit=$1, gasNeeded=$2" % [
|
|
|
|
$vmState.gasPool, $tx.gasLimit])
|
|
|
|
|
2023-04-11 08:28:45 +00:00
|
|
|
vmState.gasPool -= tx.gasLimit
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
# Actually, the eip-1559 reference does not mention an early exit.
|
|
|
|
#
|
|
|
|
# Even though database was not changed yet but, a `persist()` directive
|
|
|
|
# before leaving is crucial for some unit tests that us a direct/deep call
|
|
|
|
# of the `processTransaction()` function. So there is no `return err()`
|
|
|
|
# statement, here.
|
2023-08-04 12:43:30 +00:00
|
|
|
let txRes = roDB.validateTransaction(tx, sender, header.gasLimit, baseFee256, excessBlobGas, fork)
|
2023-06-12 05:01:48 +00:00
|
|
|
if txRes.isOk:
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2023-03-21 13:27:12 +00:00
|
|
|
# EIP-1153
|
|
|
|
vmState.stateDB.clearTransientStorage()
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
# Execute the transaction.
|
2023-08-25 09:07:20 +00:00
|
|
|
vmState.captureTxStart(tx.gasLimit)
|
2022-01-10 09:04:06 +00:00
|
|
|
let
|
|
|
|
accTx = vmState.stateDB.beginSavepoint
|
|
|
|
gasBurned = tx.txCallEvm(sender, vmState, fork)
|
2023-08-25 09:07:20 +00:00
|
|
|
vmState.captureTxEnd(tx.gasLimit - gasBurned)
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2023-04-11 08:28:45 +00:00
|
|
|
res = commitOrRollbackDependingOnGasUsed(vmState, accTx, header, tx, gasBurned, priorityFee)
|
2023-06-12 05:01:48 +00:00
|
|
|
else:
|
|
|
|
res = err(txRes.error)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
if vmState.generateWitness:
|
2021-10-28 09:42:39 +00:00
|
|
|
vmState.stateDB.collectWitnessData()
|
2023-04-24 20:59:38 +00:00
|
|
|
vmState.stateDB.persist(
|
|
|
|
clearEmptyAccount = fork >= FkSpurious,
|
|
|
|
clearCache = false)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2023-04-12 12:39:11 +00:00
|
|
|
return res
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-30 16:29:48 +00:00
|
|
|
proc processBeaconBlockRoot*(vmState: BaseVMState, beaconRoot: Hash256):
|
|
|
|
Result[void, string] {.raises: [CatchableError].} =
|
|
|
|
## processBeaconBlockRoot applies the EIP-4788 system call to the
|
|
|
|
## beacon block root contract. This method is exported to be used in tests.
|
|
|
|
## If EIP-4788 is enabled, we need to invoke the beaconroot storage
|
|
|
|
## contract with the new root.
|
|
|
|
let
|
|
|
|
statedb = vmState.stateDB
|
|
|
|
call = CallParams(
|
2023-09-20 13:10:16 +00:00
|
|
|
vmState : vmState,
|
2023-10-19 00:50:07 +00:00
|
|
|
sender : SYSTEM_ADDRESS,
|
2023-09-20 13:10:16 +00:00
|
|
|
gasLimit : 30_000_000.GasInt,
|
|
|
|
gasPrice : 0.GasInt,
|
2023-10-19 00:50:07 +00:00
|
|
|
to : BEACON_ROOTS_ADDRESS,
|
2023-09-20 13:10:16 +00:00
|
|
|
input : @(beaconRoot.data),
|
|
|
|
|
|
|
|
# It's a systemCall, no need for other knicks knacks
|
|
|
|
sysCall : true,
|
|
|
|
noAccessList: true,
|
|
|
|
noIntrinsic : true,
|
|
|
|
noGasCharge : true,
|
|
|
|
noRefund : true,
|
2023-08-30 16:29:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# runComputation a.k.a syscall/evm.call
|
2023-10-31 05:54:57 +00:00
|
|
|
let res = call.runComputation()
|
|
|
|
if res.isError:
|
|
|
|
return err("processBeaconBlockRoot: " & res.error)
|
2023-08-30 16:29:48 +00:00
|
|
|
|
2023-09-20 13:10:16 +00:00
|
|
|
statedb.persist(clearEmptyAccount = true, clearCache = false)
|
2023-08-30 16:29:48 +00:00
|
|
|
ok()
|
|
|
|
|
2023-04-12 12:39:11 +00:00
|
|
|
proc asyncProcessTransaction*(
|
|
|
|
vmState: BaseVMState; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## Transaction to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
|
|
|
header: BlockHeader; ## Header for the block containing the current tx
|
2023-06-12 05:01:48 +00:00
|
|
|
fork: EVMFork): Future[Result[GasInt,string]]
|
2023-04-12 12:39:11 +00:00
|
|
|
{.async, gcsafe.} =
|
|
|
|
## Process the transaction, write the results to accounts db. The function
|
|
|
|
## returns the amount of gas burned if executed.
|
|
|
|
return await vmState.asyncProcessTransactionImpl(tx, sender, header, fork)
|
|
|
|
|
|
|
|
# FIXME-duplicatedForAsync
|
|
|
|
proc asyncProcessTransaction*(
|
|
|
|
vmState: BaseVMState; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## Transaction to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
2023-06-12 05:01:48 +00:00
|
|
|
header: BlockHeader): Future[Result[GasInt,string]]
|
2023-04-12 12:39:11 +00:00
|
|
|
{.async, gcsafe.} =
|
|
|
|
## Variant of `asyncProcessTransaction()` with `*fork* derived
|
|
|
|
## from the `vmState` argument.
|
2023-10-24 10:39:19 +00:00
|
|
|
let fork = vmState.com.toEVMFork(header.forkDeterminationInfo)
|
2023-04-12 12:39:11 +00:00
|
|
|
return await vmState.asyncProcessTransaction(tx, sender, header, fork)
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc processTransaction*(
|
|
|
|
vmState: BaseVMState; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## Transaction to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
|
|
|
header: BlockHeader; ## Header for the block containing the current tx
|
2023-06-12 05:01:48 +00:00
|
|
|
fork: EVMFork): Result[GasInt,string]
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2023-04-12 12:39:11 +00:00
|
|
|
return waitFor(vmState.asyncProcessTransaction(tx, sender, header, fork))
|
2022-01-10 09:04:06 +00:00
|
|
|
|
|
|
|
proc processTransaction*(
|
|
|
|
vmState: BaseVMState; ## Parent accounts environment for transaction
|
|
|
|
tx: Transaction; ## Transaction to validate
|
|
|
|
sender: EthAddress; ## tx.getSender or tx.ecRecover
|
2023-06-12 05:01:48 +00:00
|
|
|
header: BlockHeader): Result[GasInt,string]
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2023-04-12 12:39:11 +00:00
|
|
|
return waitFor(vmState.asyncProcessTransaction(tx, sender, header))
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-06 13:14:45 +00:00
|
|
|
# End
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|