nimbus-eth1/nimbus/p2p/executor/process_transaction.nim
Jordan Hrycaj 261c0b51a7
Redesign of BaseVMState descriptor (#923)
* Redesign of BaseVMState descriptor

why:
  BaseVMState provides an environment for executing transactions. The
  current descriptor also provides data that cannot generally be known
  within the execution environment, e.g. the total gasUsed which is
  available not before after all transactions have finished.

  Also, the BaseVMState constructor has been replaced by a constructor
  that does not need pre-initialised input of the account database.

also:
  Previous constructor and some fields are provided with a deprecated
  annotation (producing a lot of noise.)

* Replace legacy directives in production sources

* Replace legacy directives in unit test sources

* fix CI (missing premix update)

* Remove legacy directives

* chase CI problem

* rebased

* Re-introduce 'AccountsCache' constructor optimisation for 'BaseVmState' re-initialisation

why:
  Constructing a new 'AccountsCache' descriptor can be avoided sometimes
  when the current state root is properly positioned already. Such a
  feature existed already as the update function 'initStateDB()' for the
  'BaseChanDB' where the accounts cache was linked into this desctiptor.

  The function 'initStateDB()' was removed and re-implemented into the
  'BaseVmState' constructor without optimisation. The old version was of
  restricted use as a wrong accounts cache state would unconditionally
  throw an exception rather than conceptually ask for a remedy.

  The optimised 'BaseVmState' re-initialisation has been implemented for
  the 'persistBlocks()' function.

also:
  moved some test helpers to 'test/replay' folder

* Remove unused & undocumented fields from Chain descriptor

why:
  Reduces attack surface in general & improves reading the code.
2022-01-18 16:19:32 +00:00

151 lines
5.8 KiB
Nim

# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# 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.
import
std/[sets],
../../db/accounts_cache,
../../forks,
../../transaction/call_evm,
../../vm_state,
../../vm_types,
../validate,
./executor_helpers,
chronicles,
eth/common,
stew/results
{.push raises: [Defect].}
# ------------------------------------------------------------------------------
# Private functions
# ------------------------------------------------------------------------------
proc eip1559TxNormalization(tx: Transaction): Transaction =
result = tx
if tx.txType < TxEip1559:
result.maxPriorityFee = tx.gasPrice
result.maxFee = tx.gasPrice
proc eip1559BaseFee(header: BlockHeader; fork: Fork): UInt256 =
## 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
proc processTransactionImpl(
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
fork: Fork): Result[GasInt,void]
# wildcard exception, wrapped below
{.gcsafe, raises: [Exception].} =
## Modelled after `https://eips.ethereum.org/EIPS/eip-1559#specification`_
## which provides a backward compatible framwork for EIP1559.
#trace "Sender", sender
#trace "txHash", rlpHash = ty.rlpHash
var
tx = eip1559TxNormalization(tx)
let
roDB = vmState.readOnlyStateDB
baseFee256 = header.eip1559BaseFee(fork)
baseFee = baseFee256.truncate(int64)
priorityFee = min(tx.maxPriorityFee, tx.maxFee - baseFee)
miner = vmState.coinbase()
if FkLondon <= fork:
# The signer pays both the priority fee and the base fee. So tx.gasPrice
# now is the effective gasPrice which also effects the `stateRoot` value.
tx.gasPrice = priorityFee + baseFee
# Return failure unless explicitely set `ok()`
result = err()
# 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.
if roDB.validateTransaction(tx, sender, header.gasLimit, baseFee256, fork):
# Execute the transaction.
let
accTx = vmState.stateDB.beginSavepoint
gasBurned = tx.txCallEvm(sender, vmState, fork)
# 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.
if header.gasLimit < vmState.cumulativeGasUsed + gasBurned:
vmState.stateDB.rollback(accTx)
debug "invalid tx: block header gasLimit reached",
maxLimit = header.gasLimit,
gasUsed = vmState.cumulativeGasUsed,
addition = gasBurned
else:
# Accept transaction and collect mining fee.
vmState.stateDB.commit(accTx)
vmState.stateDB.addBalance(miner, gasBurned.u256 * priorityFee.u256)
vmState.cumulativeGasUsed += gasBurned
result = ok(gasBurned)
vmState.mutateStateDB:
for deletedAccount in vmState.selfDestructs:
db.deleteAccount deletedAccount
if fork >= FkSpurious:
vmState.touchedAccounts.incl(miner)
# EIP158/161 state clearing
for account in vmState.touchedAccounts:
if db.accountExists(account) and db.isEmptyAccount(account):
debug "state clearing", account
db.deleteAccount(account)
if vmState.generateWitness:
vmState.stateDB.collectWitnessData()
vmState.stateDB.persist(clearCache = false)
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
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
fork: Fork): Result[GasInt,void]
{.gcsafe, raises: [Defect,CatchableError].} =
## Process the transaction, write the results to accounts db. The function
## returns the amount of gas burned if executed.
safeExecutor("processTransaction"):
result = vmState.processTransactionImpl(tx, sender, header, fork)
proc processTransaction*(
vmState: BaseVMState; ## Parent accounts environment for transaction
tx: Transaction; ## Transaction to validate
sender: EthAddress; ## tx.getSender or tx.ecRecover
header: BlockHeader): Result[GasInt,void]
{.gcsafe, raises: [Defect,CatchableError].} =
## Variant of `processTransaction()` with `*fork* derived
## from the `vmState` argument.
var fork: Fork
safeExecutor("processTransaction"):
fork = vmState.getForkUnsafe
vmState.processTransaction(tx, sender, header, fork)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------