2021-07-06 13:14:45 +00:00
|
|
|
# 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,
|
2022-04-05 10:22:46 +00:00
|
|
|
../../transaction,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../vm_state,
|
|
|
|
../../vm_types,
|
|
|
|
../validate,
|
|
|
|
./executor_helpers,
|
|
|
|
chronicles,
|
2022-01-10 09:04:06 +00:00
|
|
|
eth/common,
|
|
|
|
stew/results
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
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.
|
2021-07-06 13:14:45 +00:00
|
|
|
|
Tracing: Remove some trace messages that occur a lot during sync
Disable some trace messages which appeared a lot in the output and probably
aren't so useful any more, when block processing is functioning well at high
speed.
Turning on the trace level globally is useful to get a feel for what's
happening, but only if each category is kept to a reasonable amount.
As well as overwhelming the output so that it's hard to see general activity,
some of these messages happen so much they severely slow down processing. Ones
called every time an EVM opcode uses some gas are particularly extreme.
These messages have all been chosen as things which are probably not useful any
more (the relevant functionality has been debugged and is tested plenty).
These have been commented out rather than removed. It may be that turning
trace topics on/off, or other selection, is a better longer term solution, but
that will require better command line options and good defaults for sure.
(I think higher levels `tracev` and `tracevv` levels (extra verbose) would be
more useful for this sort of deep tracing on request.)
For now, enabling `--log-level:TRACE` on the command line is quite useful as
long as we keep each category reasonable, and this patch tries to keep that
balance.
- Don't show "has transactions" on virtually every block imported.
- Don't show "Sender" and "txHash" lines on every transaction processed.
- Don't show "GAS CONSUMPTION" on every opcode executed", this is way too much.
- Don't show "GAS RETURNED" and "GAS REFUND" on each contract call.
- Don't show "op: Stop" on every Stop opcode, which means every transaction.
- Don't show "Insufficient funds" whenever a contract can't call another.
- Don't show "ECRecover", "SHA256 precompile", "RIPEMD160", "Identity"
or even "Call precompile" every time a precompile is called. These are
very well tested now.
- Don't show "executeOpcodes error" whenever a contract returns an error.
(This is changed to `trace` too, it's a normal event that is well tested.)
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-07-22 13:35:41 +00:00
|
|
|
#trace "Sender", sender
|
2022-01-10 09:04:06 +00:00
|
|
|
#trace "txHash", rlpHash = ty.rlpHash
|
|
|
|
|
|
|
|
let
|
|
|
|
roDB = vmState.readOnlyStateDB
|
|
|
|
baseFee256 = header.eip1559BaseFee(fork)
|
2022-04-05 10:22:46 +00:00
|
|
|
baseFee = baseFee256.truncate(GasInt)
|
|
|
|
tx = eip1559TxNormalization(tx, baseFee, fork)
|
2021-07-06 13:14:45 +00:00
|
|
|
priorityFee = min(tx.maxPriorityFee, tx.maxFee - baseFee)
|
2022-01-10 09:04:06 +00:00
|
|
|
miner = vmState.coinbase()
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
# 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
|
2021-07-06 13:14:45 +00:00
|
|
|
else:
|
2022-01-10 09:04:06 +00:00
|
|
|
# Accept transaction and collect mining fee.
|
|
|
|
vmState.stateDB.commit(accTx)
|
|
|
|
vmState.stateDB.addBalance(miner, gasBurned.u256 * priorityFee.u256)
|
|
|
|
vmState.cumulativeGasUsed += gasBurned
|
|
|
|
result = ok(gasBurned)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
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:
|
2021-10-28 09:42:39 +00:00
|
|
|
vmState.stateDB.collectWitnessData()
|
|
|
|
vmState.stateDB.persist(clearCache = false)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
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.
|
2021-07-14 15:13:27 +00:00
|
|
|
safeExecutor("processTransaction"):
|
2022-01-10 09:04:06 +00:00
|
|
|
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
|
2021-07-14 15:13:27 +00:00
|
|
|
safeExecutor("processTransaction"):
|
2022-01-10 09:04:06 +00:00
|
|
|
fork = vmState.getForkUnsafe
|
|
|
|
vmState.processTransaction(tx, sender, header, fork)
|
|
|
|
|
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
|
|
|
# ------------------------------------------------------------------------------
|