2022-12-10 12:53:24 +00:00
|
|
|
# Nimbus
|
2024-02-24 02:38:50 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-12-10 12:53:24 +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.
|
|
|
|
|
2022-10-15 15:58:23 +00:00
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[json, strutils, tables, os, streams],
|
2022-12-08 05:17:14 +00:00
|
|
|
eth/[rlp, trie, eip1559],
|
2024-10-04 14:34:31 +00:00
|
|
|
eth/common/transaction_utils,
|
2024-05-30 12:54:03 +00:00
|
|
|
stint, results,
|
2022-10-15 15:58:23 +00:00
|
|
|
"."/[config, types, helpers],
|
2023-01-09 14:39:36 +00:00
|
|
|
../common/state_clearing,
|
2024-06-17 07:56:39 +00:00
|
|
|
../../nimbus/[evm/types, evm/state, transaction],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/common/common,
|
2023-12-12 19:12:56 +00:00
|
|
|
../../nimbus/db/ledger,
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/utils/utils,
|
|
|
|
../../nimbus/core/pow/difficulty,
|
|
|
|
../../nimbus/core/dao,
|
2023-06-24 13:56:44 +00:00
|
|
|
../../nimbus/core/executor/[process_transaction, executor_helpers],
|
2023-08-02 10:17:40 +00:00
|
|
|
../../nimbus/core/eip4844,
|
2024-09-12 03:51:13 +00:00
|
|
|
../../nimbus/core/eip6110,
|
2023-08-02 10:17:40 +00:00
|
|
|
../../nimbus/evm/tracer/json_tracer
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
wrapExceptionEnabled* {.booldefine.} = true
|
|
|
|
stdinSelector = "stdin"
|
|
|
|
|
|
|
|
type
|
|
|
|
Dispatch = object
|
|
|
|
stdout: JsonNode
|
|
|
|
stderr: JsonNode
|
|
|
|
|
|
|
|
ExecOutput = object
|
|
|
|
result: ExecutionResult
|
|
|
|
alloc: GenesisAlloc
|
|
|
|
|
|
|
|
TestVMState = ref object of BaseVMState
|
2024-10-08 05:52:32 +00:00
|
|
|
blockHashes: Table[uint64, Hash32]
|
2022-10-15 15:58:23 +00:00
|
|
|
hashError: string
|
|
|
|
|
|
|
|
proc init(_: type Dispatch): Dispatch =
|
|
|
|
result.stdout = newJObject()
|
|
|
|
result.stderr = newJObject()
|
|
|
|
|
|
|
|
proc dispatch(dis: var Dispatch, baseDir, fName, name: string, obj: JsonNode) =
|
|
|
|
case fName
|
|
|
|
of "stdout":
|
|
|
|
dis.stdout[name] = obj
|
|
|
|
of "stderr":
|
|
|
|
dis.stderr[name] = obj
|
|
|
|
of "":
|
|
|
|
# don't save
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
# save to file
|
|
|
|
let path = if baseDir.len > 0:
|
|
|
|
baseDir / fName
|
|
|
|
else:
|
|
|
|
fName
|
|
|
|
writeFile(path, obj.pretty)
|
|
|
|
|
2024-10-22 02:07:58 +00:00
|
|
|
proc dispatchOutput(ctx: TransContext, conf: T8NConf, res: ExecOutput) =
|
2022-10-15 15:58:23 +00:00
|
|
|
var dis = Dispatch.init()
|
|
|
|
createDir(conf.outputBaseDir)
|
|
|
|
|
|
|
|
dis.dispatch(conf.outputBaseDir, conf.outputAlloc, "alloc", @@(res.alloc))
|
|
|
|
dis.dispatch(conf.outputBaseDir, conf.outputResult, "result", @@(res.result))
|
|
|
|
|
2022-12-08 05:17:14 +00:00
|
|
|
let chainId = conf.stateChainId.ChainId
|
2024-10-22 02:07:58 +00:00
|
|
|
let txList = ctx.filterGoodTransactions()
|
2022-12-08 05:17:14 +00:00
|
|
|
|
|
|
|
let body = @@(rlp.encode(txList))
|
2022-10-15 15:58:23 +00:00
|
|
|
dis.dispatch(conf.outputBaseDir, conf.outputBody, "body", body)
|
|
|
|
|
|
|
|
if dis.stdout.len > 0:
|
|
|
|
stdout.write(dis.stdout.pretty)
|
|
|
|
stdout.write("\n")
|
|
|
|
|
|
|
|
if dis.stderr.len > 0:
|
|
|
|
stderr.write(dis.stderr.pretty)
|
|
|
|
stderr.write("\n")
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc calcWithdrawalsRoot(w: Opt[seq[Withdrawal]]): Opt[Hash32] =
|
2023-03-21 11:36:22 +00:00
|
|
|
if w.isNone:
|
2024-10-08 05:52:32 +00:00
|
|
|
return Opt.none(Hash32)
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some calcWithdrawalsRoot(w.get)
|
2023-03-21 11:36:22 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc envToHeader(env: EnvStruct): Header =
|
|
|
|
Header(
|
2022-10-15 15:58:23 +00:00
|
|
|
coinbase : env.currentCoinbase,
|
|
|
|
difficulty : env.currentDifficulty.get(0.u256),
|
2024-10-03 11:42:24 +00:00
|
|
|
mixHash : env.currentRandom.get(default(Bytes32)),
|
2024-06-14 07:31:08 +00:00
|
|
|
number : env.currentNumber,
|
2022-10-15 15:58:23 +00:00
|
|
|
gasLimit : env.currentGasLimit,
|
|
|
|
timestamp : env.currentTimestamp,
|
|
|
|
stateRoot : emptyRlpHash,
|
2024-06-14 07:31:08 +00:00
|
|
|
baseFeePerGas : env.currentBaseFee,
|
2023-08-04 12:43:30 +00:00
|
|
|
withdrawalsRoot: env.withdrawals.calcWithdrawalsRoot(),
|
2024-06-14 07:31:08 +00:00
|
|
|
blobGasUsed : env.currentBlobGasUsed,
|
|
|
|
excessBlobGas : env.currentExcessBlobGas,
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc postState(db: LedgerRef, alloc: var GenesisAlloc) =
|
2022-10-15 15:58:23 +00:00
|
|
|
for accAddr in db.addresses():
|
|
|
|
var acc = GenesisAccount(
|
2024-06-21 07:44:10 +00:00
|
|
|
code: db.getCode(accAddr).bytes(),
|
2022-10-15 15:58:23 +00:00
|
|
|
balance: db.getBalance(accAddr),
|
|
|
|
nonce: db.getNonce(accAddr)
|
|
|
|
)
|
|
|
|
|
|
|
|
for k, v in db.storage(accAddr):
|
|
|
|
acc.storage[k] = v
|
|
|
|
alloc[accAddr] = acc
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc genAddress(tx: Transaction, sender: Address): Address =
|
2022-10-15 15:58:23 +00:00
|
|
|
if tx.to.isNone:
|
2023-03-23 02:38:42 +00:00
|
|
|
result = generateAddress(sender, tx.nonce)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
2023-05-11 07:36:32 +00:00
|
|
|
proc toTxReceipt(rec: Receipt,
|
2022-10-15 15:58:23 +00:00
|
|
|
tx: Transaction,
|
2024-10-16 01:34:12 +00:00
|
|
|
sender: Address,
|
2022-10-15 15:58:23 +00:00
|
|
|
txIndex: int,
|
|
|
|
gasUsed: GasInt): TxReceipt =
|
|
|
|
|
2023-05-11 07:36:32 +00:00
|
|
|
let contractAddress = genAddress(tx, sender)
|
2022-10-15 15:58:23 +00:00
|
|
|
TxReceipt(
|
|
|
|
txType: tx.txType,
|
2024-10-08 05:52:32 +00:00
|
|
|
root: if rec.isHash: rec.hash else: default(Hash32),
|
2022-10-15 15:58:23 +00:00
|
|
|
status: rec.status,
|
|
|
|
cumulativeGasUsed: rec.cumulativeGasUsed,
|
2024-06-14 07:31:08 +00:00
|
|
|
logsBloom: rec.logsBloom,
|
2022-10-15 15:58:23 +00:00
|
|
|
logs: rec.logs,
|
|
|
|
transactionHash: rlpHash(tx),
|
|
|
|
contractAddress: contractAddress,
|
|
|
|
gasUsed: gasUsed,
|
2024-10-08 05:52:32 +00:00
|
|
|
blockHash: default(Hash32),
|
2022-10-15 15:58:23 +00:00
|
|
|
transactionIndex: txIndex
|
|
|
|
)
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc calcLogsHash(receipts: openArray[Receipt]): Hash32 =
|
2022-10-15 15:58:23 +00:00
|
|
|
var logs: seq[Log]
|
|
|
|
for rec in receipts:
|
|
|
|
logs.add rec.logs
|
|
|
|
rlpHash(logs)
|
|
|
|
|
2024-06-19 01:58:08 +00:00
|
|
|
proc defaultTraceStreamFilename(conf: T8NConf,
|
|
|
|
txIndex: int,
|
2024-10-08 05:52:32 +00:00
|
|
|
txHash: Hash32): (string, string) =
|
2023-08-20 04:15:11 +00:00
|
|
|
let
|
|
|
|
txHash = "0x" & toLowerAscii($txHash)
|
|
|
|
baseDir = if conf.outputBaseDir.len > 0:
|
|
|
|
conf.outputBaseDir
|
|
|
|
else:
|
|
|
|
"."
|
|
|
|
fName = "$1/trace-$2-$3.jsonl" % [baseDir, $txIndex, txHash]
|
2024-06-19 01:58:08 +00:00
|
|
|
(baseDir, fName)
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
proc defaultTraceStream(conf: T8NConf, txIndex: int, txHash: Hash32): Stream =
|
2024-06-19 01:58:08 +00:00
|
|
|
let (baseDir, fName) = defaultTraceStreamFilename(conf, txIndex, txHash)
|
|
|
|
createDir(baseDir)
|
2023-08-20 04:15:11 +00:00
|
|
|
newFileStream(fName, fmWrite)
|
2023-03-23 02:38:42 +00:00
|
|
|
|
2023-08-20 04:15:11 +00:00
|
|
|
proc traceToFileStream(path: string, txIndex: int): Stream =
|
|
|
|
# replace whatever `.ext` to `-${txIndex}.jsonl`
|
|
|
|
let
|
|
|
|
file = path.splitFile
|
|
|
|
fName = "$1/$2-$3.jsonl" % [file.dir, file.name, $txIndex]
|
2024-06-19 01:58:08 +00:00
|
|
|
createDir(file.dir)
|
2023-08-20 04:15:11 +00:00
|
|
|
newFileStream(fName, fmWrite)
|
2023-03-23 02:38:42 +00:00
|
|
|
|
2024-10-10 13:25:27 +00:00
|
|
|
proc setupTrace(conf: T8NConf, txIndex: int, txHash: Hash32, vmState: BaseVMState): bool =
|
2023-08-02 10:17:40 +00:00
|
|
|
var tracerFlags = {
|
|
|
|
TracerFlags.DisableMemory,
|
|
|
|
TracerFlags.DisableStorage,
|
|
|
|
TracerFlags.DisableState,
|
|
|
|
TracerFlags.DisableStateDiff,
|
|
|
|
TracerFlags.DisableReturnData
|
2023-03-23 02:38:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 04:15:11 +00:00
|
|
|
if conf.traceMemory: tracerFlags.excl TracerFlags.DisableMemory
|
|
|
|
if conf.traceNostack: tracerFlags.incl TracerFlags.DisableStack
|
|
|
|
if conf.traceReturnData: tracerFlags.excl TracerFlags.DisableReturnData
|
|
|
|
|
2024-10-10 13:25:27 +00:00
|
|
|
var closeStream = true
|
2023-08-20 04:15:11 +00:00
|
|
|
let traceMode = conf.traceEnabled.get
|
|
|
|
let stream = if traceMode == "stdout":
|
2024-10-10 13:25:27 +00:00
|
|
|
# don't close stdout or stderr
|
|
|
|
closeStream = false
|
2023-08-20 04:15:11 +00:00
|
|
|
newFileStream(stdout)
|
|
|
|
elif traceMode == "stderr":
|
2024-10-10 13:25:27 +00:00
|
|
|
closeStream = false
|
2023-08-20 04:15:11 +00:00
|
|
|
newFileStream(stderr)
|
|
|
|
elif traceMode.len > 0:
|
|
|
|
traceToFileStream(traceMode, txIndex)
|
|
|
|
else:
|
|
|
|
defaultTraceStream(conf, txIndex, txHash)
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
if stream.isNil:
|
|
|
|
let traceLoc =
|
|
|
|
if traceMode.len > 0:
|
|
|
|
traceMode
|
|
|
|
else:
|
|
|
|
defaultTraceStreamFilename(conf, txIndex, txHash)[1]
|
|
|
|
raise newError(ErrorConfig, "Unable to open tracer stream: " & traceLoc)
|
|
|
|
|
2023-08-20 04:15:11 +00:00
|
|
|
vmState.tracer = newJsonTracer(stream, tracerFlags, false)
|
2024-10-10 13:25:27 +00:00
|
|
|
closeStream
|
2023-03-23 02:38:42 +00:00
|
|
|
|
2024-10-10 13:25:27 +00:00
|
|
|
proc closeTrace(vmState: BaseVMState, closeStream: bool) =
|
2023-08-02 10:17:40 +00:00
|
|
|
let tracer = JsonTracer(vmState.tracer)
|
2024-10-10 13:25:27 +00:00
|
|
|
if tracer.isNil.not and closeStream:
|
2023-08-02 10:17:40 +00:00
|
|
|
tracer.close()
|
2022-10-15 15:58:23 +00:00
|
|
|
|
2024-10-22 02:07:58 +00:00
|
|
|
proc exec(ctx: TransContext,
|
2022-10-15 15:58:23 +00:00
|
|
|
vmState: BaseVMState,
|
2022-11-23 06:30:05 +00:00
|
|
|
stateReward: Option[UInt256],
|
2024-10-08 05:52:32 +00:00
|
|
|
header: Header,
|
2023-08-02 10:17:40 +00:00
|
|
|
conf: T8NConf): ExecOutput =
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
var
|
2024-10-22 02:07:58 +00:00
|
|
|
receipts = newSeqOfCap[TxReceipt](ctx.txList.len)
|
2022-10-15 15:58:23 +00:00
|
|
|
rejected = newSeq[RejectedTx]()
|
|
|
|
includedTx = newSeq[Transaction]()
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
if vmState.com.daoForkSupport and
|
|
|
|
vmState.com.daoForkBlock.get == vmState.blockNumber:
|
2022-10-15 15:58:23 +00:00
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.applyDAOHardFork()
|
|
|
|
|
2024-10-22 02:07:58 +00:00
|
|
|
vmState.receipts = newSeqOfCap[Receipt](ctx.txList.len)
|
2022-10-15 15:58:23 +00:00
|
|
|
vmState.cumulativeGasUsed = 0
|
|
|
|
|
2023-08-30 16:29:48 +00:00
|
|
|
if ctx.env.parentBeaconBlockRoot.isSome:
|
|
|
|
vmState.processBeaconBlockRoot(ctx.env.parentBeaconBlockRoot.get).isOkOr:
|
|
|
|
raise newError(ErrorConfig, error)
|
|
|
|
|
2024-09-10 09:52:03 +00:00
|
|
|
if vmState.com.isPragueOrLater(ctx.env.currentTimestamp) and
|
|
|
|
ctx.env.blockHashes.len > 0:
|
|
|
|
let
|
|
|
|
prevNumber = ctx.env.currentNumber - 1
|
|
|
|
prevHash = ctx.env.blockHashes.getOrDefault(prevNumber)
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
if prevHash == static(default(Hash32)):
|
2024-09-10 09:52:03 +00:00
|
|
|
raise newError(ErrorConfig, "previous block hash not found for block number: " & $prevNumber)
|
|
|
|
|
|
|
|
vmState.processParentBlockHash(prevHash).isOkOr:
|
|
|
|
raise newError(ErrorConfig, error)
|
|
|
|
|
2024-10-22 02:07:58 +00:00
|
|
|
for txIndex, txRes in ctx.txList:
|
2022-12-08 05:17:14 +00:00
|
|
|
if txRes.isErr:
|
|
|
|
rejected.add RejectedTx(
|
|
|
|
index: txIndex,
|
|
|
|
error: txRes.error
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
let tx = txRes.get
|
2024-10-04 14:34:31 +00:00
|
|
|
let sender = tx.recoverSender().valueOr:
|
2022-10-15 15:58:23 +00:00
|
|
|
rejected.add RejectedTx(
|
|
|
|
index: txIndex,
|
|
|
|
error: "Could not get sender"
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2024-10-10 13:25:27 +00:00
|
|
|
var closeStream = true
|
2023-08-20 04:15:11 +00:00
|
|
|
if conf.traceEnabled.isSome:
|
2024-10-10 13:25:27 +00:00
|
|
|
closeStream = setupTrace(conf, txIndex, rlpHash(tx), vmState)
|
2023-08-02 10:17:40 +00:00
|
|
|
|
2022-10-15 15:58:23 +00:00
|
|
|
let rc = vmState.processTransaction(tx, sender, header)
|
|
|
|
|
2023-08-20 04:15:11 +00:00
|
|
|
if conf.traceEnabled.isSome:
|
2024-10-10 13:25:27 +00:00
|
|
|
closeTrace(vmState, closeStream)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
if rc.isErr:
|
|
|
|
rejected.add RejectedTx(
|
|
|
|
index: txIndex,
|
2023-06-12 05:01:48 +00:00
|
|
|
error: rc.error
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
let gasUsed = rc.get()
|
|
|
|
let rec = vmState.makeReceipt(tx.txType)
|
|
|
|
vmState.receipts.add rec
|
|
|
|
receipts.add toTxReceipt(
|
2023-05-11 07:36:32 +00:00
|
|
|
rec, tx, sender, txIndex, gasUsed
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
includedTx.add tx
|
|
|
|
|
2022-12-08 05:17:14 +00:00
|
|
|
# Add mining reward? (-1 means rewards are disabled)
|
2022-12-07 16:11:33 +00:00
|
|
|
if stateReward.isSome and stateReward.get >= 0:
|
2022-12-08 05:17:14 +00:00
|
|
|
# Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
|
|
|
|
# where
|
|
|
|
# - the coinbase suicided, or
|
|
|
|
# - there are only 'bad' transactions, which aren't executed. In those cases,
|
|
|
|
# the coinbase gets no txfee, so isn't created, and thus needs to be touched
|
2022-11-23 06:30:05 +00:00
|
|
|
let blockReward = stateReward.get()
|
2022-10-15 15:58:23 +00:00
|
|
|
var mainReward = blockReward
|
|
|
|
for uncle in ctx.env.ommers:
|
|
|
|
var uncleReward = 8.u256 - uncle.delta.u256
|
|
|
|
uncleReward = uncleReward * blockReward
|
|
|
|
uncleReward = uncleReward div 8.u256
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.addBalance(uncle.address, uncleReward)
|
|
|
|
mainReward += blockReward div 32.u256
|
|
|
|
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.addBalance(ctx.env.currentCoinbase, mainReward)
|
|
|
|
|
2023-03-21 11:36:22 +00:00
|
|
|
if ctx.env.withdrawals.isSome:
|
|
|
|
for withdrawal in ctx.env.withdrawals.get:
|
2023-10-04 03:47:18 +00:00
|
|
|
vmState.stateDB.addBalance(withdrawal.address, withdrawal.weiAmount)
|
2023-03-21 11:36:22 +00:00
|
|
|
|
2022-12-07 16:11:33 +00:00
|
|
|
let miner = ctx.env.currentCoinbase
|
2024-07-04 13:48:36 +00:00
|
|
|
coinbaseStateClearing(vmState, miner, stateReward.isSome())
|
2022-12-07 16:11:33 +00:00
|
|
|
|
2024-09-12 06:56:13 +00:00
|
|
|
var
|
2024-10-18 09:38:18 +00:00
|
|
|
withdrawalReqs: seq[byte]
|
|
|
|
consolidationReqs: seq[byte]
|
2024-09-12 06:56:13 +00:00
|
|
|
|
|
|
|
if vmState.com.isPragueOrLater(ctx.env.currentTimestamp):
|
2024-10-27 18:56:28 +00:00
|
|
|
# Execute EIP-7002 and EIP-7251 before calculating stateRoot
|
2024-09-12 06:56:13 +00:00
|
|
|
withdrawalReqs = processDequeueWithdrawalRequests(vmState)
|
|
|
|
consolidationReqs = processDequeueConsolidationRequests(vmState)
|
|
|
|
|
2022-10-15 15:58:23 +00:00
|
|
|
let stateDB = vmState.stateDB
|
|
|
|
stateDB.postState(result.alloc)
|
|
|
|
result.result = ExecutionResult(
|
2024-10-27 18:56:28 +00:00
|
|
|
stateRoot : stateDB.getStateRoot(),
|
2022-10-15 15:58:23 +00:00
|
|
|
txRoot : includedTx.calcTxRoot,
|
2024-06-14 07:31:08 +00:00
|
|
|
receiptsRoot: calcReceiptsRoot(vmState.receipts),
|
2022-10-15 15:58:23 +00:00
|
|
|
logsHash : calcLogsHash(vmState.receipts),
|
2024-06-14 07:31:08 +00:00
|
|
|
logsBloom : createBloom(vmState.receipts),
|
2022-10-15 15:58:23 +00:00
|
|
|
receipts : system.move(receipts),
|
|
|
|
rejected : system.move(rejected),
|
|
|
|
# geth using both vmContext.Difficulty and vmContext.Random
|
|
|
|
# therefore we cannot use vmState.difficulty
|
|
|
|
currentDifficulty: ctx.env.currentDifficulty,
|
2024-06-14 07:31:08 +00:00
|
|
|
gasUsed : vmState.cumulativeGasUsed,
|
|
|
|
currentBaseFee : ctx.env.currentBaseFee,
|
|
|
|
withdrawalsRoot : header.withdrawalsRoot
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
|
2024-07-17 10:05:53 +00:00
|
|
|
if vmState.com.isCancunOrLater(ctx.env.currentTimestamp):
|
2024-06-24 05:56:24 +00:00
|
|
|
result.result.blobGasUsed = Opt.some vmState.blobGasUsed
|
2023-09-22 00:36:06 +00:00
|
|
|
if ctx.env.currentExcessBlobGas.isSome:
|
|
|
|
result.result.currentExcessBlobGas = ctx.env.currentExcessBlobGas
|
|
|
|
elif ctx.env.parentExcessBlobGas.isSome and ctx.env.parentBlobGasUsed.isSome:
|
2024-06-14 07:31:08 +00:00
|
|
|
result.result.currentExcessBlobGas = Opt.some calcExcessBlobGas(vmState.parent)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-09-12 03:51:13 +00:00
|
|
|
if vmState.com.isPragueOrLater(ctx.env.currentTimestamp):
|
|
|
|
var allLogs: seq[Log]
|
|
|
|
for rec in result.result.receipts:
|
|
|
|
allLogs.add rec.logs
|
2024-10-18 09:38:18 +00:00
|
|
|
let
|
|
|
|
depositReqs = parseDepositLogs(allLogs).valueOr:
|
|
|
|
raise newError(ErrorEVM, error)
|
|
|
|
requestsHash = calcRequestsHashInsertType(depositReqs, withdrawalReqs, consolidationReqs)
|
|
|
|
result.result.requestsHash = Opt.some(requestsHash)
|
2024-09-12 06:56:13 +00:00
|
|
|
|
2022-10-15 15:58:23 +00:00
|
|
|
template wrapException(body: untyped) =
|
|
|
|
when wrapExceptionEnabled:
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except IOError as e:
|
|
|
|
raise newError(ErrorIO, e.msg)
|
|
|
|
except RlpError as e:
|
|
|
|
raise newError(ErrorRlp, e.msg)
|
|
|
|
except ValueError as e:
|
|
|
|
raise newError(ErrorJson, e.msg)
|
|
|
|
else:
|
|
|
|
body
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc setupAlloc(stateDB: LedgerRef, alloc: GenesisAlloc) =
|
2022-10-15 15:58:23 +00:00
|
|
|
for accAddr, acc in alloc:
|
|
|
|
stateDB.setNonce(accAddr, acc.nonce)
|
|
|
|
stateDB.setCode(accAddr, acc.code)
|
|
|
|
stateDB.setBalance(accAddr, acc.balance)
|
|
|
|
|
|
|
|
for slot, value in acc.storage:
|
|
|
|
stateDB.setStorage(accAddr, slot, value)
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
method getAncestorHash(vmState: TestVMState; blockNumber: BlockNumber): Hash32 =
|
2022-10-15 15:58:23 +00:00
|
|
|
# we can't raise exception here, it'll mess with EVM exception handler.
|
|
|
|
# so, store the exception for later using `hashError`
|
2024-10-08 05:52:32 +00:00
|
|
|
var h = default(Hash32)
|
2022-10-15 15:58:23 +00:00
|
|
|
if vmState.blockHashes.len == 0:
|
2024-06-07 08:24:32 +00:00
|
|
|
vmState.hashError = "getAncestorHash(" &
|
2024-06-14 07:31:08 +00:00
|
|
|
$blockNumber & ") invoked, no blockhashes provided"
|
2022-10-15 15:58:23 +00:00
|
|
|
return h
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
vmState.blockHashes.withValue(blockNumber, val) do:
|
2022-12-15 10:31:47 +00:00
|
|
|
h = val[]
|
|
|
|
do:
|
2024-06-07 08:24:32 +00:00
|
|
|
vmState.hashError = "getAncestorHash(" &
|
2024-06-14 07:31:08 +00:00
|
|
|
$blockNumber & ") invoked, blockhash for that block not provided"
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
return h
|
|
|
|
|
2022-11-25 05:26:29 +00:00
|
|
|
proc parseChainConfig(network: string): ChainConfig =
|
|
|
|
try:
|
|
|
|
result = getChainConfig(network)
|
|
|
|
except ValueError as e:
|
|
|
|
raise newError(ErrorConfig, e.msg)
|
|
|
|
|
2022-12-08 05:17:14 +00:00
|
|
|
proc calcBaseFee(env: EnvStruct): UInt256 =
|
|
|
|
if env.parentGasUsed.isNone:
|
|
|
|
raise newError(ErrorConfig,
|
|
|
|
"'parentBaseFee' exists but missing 'parentGasUsed' in env section")
|
|
|
|
|
|
|
|
if env.parentGasLimit.isNone:
|
|
|
|
raise newError(ErrorConfig,
|
|
|
|
"'parentBaseFee' exists but missing 'parentGasLimit' in env section")
|
|
|
|
|
|
|
|
calcEip1599BaseFee(
|
|
|
|
env.parentGasLimit.get,
|
|
|
|
env.parentGasUsed.get,
|
|
|
|
env.parentBaseFee.get)
|
|
|
|
|
2022-10-15 15:58:23 +00:00
|
|
|
proc transitionAction*(ctx: var TransContext, conf: T8NConf) =
|
|
|
|
wrapException:
|
|
|
|
if conf.inputAlloc.len == 0 and conf.inputEnv.len == 0 and conf.inputTxs.len == 0:
|
|
|
|
raise newError(ErrorConfig, "either one of input is needeed(alloc, txs, or env)")
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
let config = parseChainConfig(conf.stateFork)
|
|
|
|
config.chainId = conf.stateChainId.ChainId
|
|
|
|
|
2024-05-20 10:17:51 +00:00
|
|
|
let com = CommonRef.new(newCoreDbRef DefaultDbMemory, config)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
# We need to load three things: alloc, env and transactions.
|
|
|
|
# May be either in stdin input or in files.
|
|
|
|
|
|
|
|
if conf.inputAlloc == stdinSelector or
|
|
|
|
conf.inputEnv == stdinSelector or
|
|
|
|
conf.inputTxs == stdinSelector:
|
2022-12-08 05:17:14 +00:00
|
|
|
ctx.parseInputFromStdin()
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
if conf.inputAlloc != stdinSelector and conf.inputAlloc.len > 0:
|
2024-10-22 02:07:58 +00:00
|
|
|
ctx.parseAlloc(conf.inputAlloc)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
if conf.inputEnv != stdinSelector and conf.inputEnv.len > 0:
|
2024-10-22 02:07:58 +00:00
|
|
|
ctx.parseEnv(conf.inputEnv)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
if conf.inputTxs != stdinSelector and conf.inputTxs.len > 0:
|
|
|
|
if conf.inputTxs.endsWith(".rlp"):
|
|
|
|
let data = readFile(conf.inputTxs)
|
|
|
|
ctx.parseTxsRlp(data.strip(chars={'"'}))
|
|
|
|
else:
|
2024-10-22 02:07:58 +00:00
|
|
|
ctx.parseTxsJson(conf.inputTxs)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
let uncleHash = if ctx.env.parentUncleHash == default(Hash32):
|
2022-10-15 15:58:23 +00:00
|
|
|
EMPTY_UNCLE_HASH
|
|
|
|
else:
|
|
|
|
ctx.env.parentUncleHash
|
|
|
|
|
2024-10-08 05:52:32 +00:00
|
|
|
let parent = Header(
|
2022-10-15 15:58:23 +00:00
|
|
|
stateRoot: emptyRlpHash,
|
|
|
|
timestamp: ctx.env.parentTimestamp,
|
|
|
|
difficulty: ctx.env.parentDifficulty.get(0.u256),
|
|
|
|
ommersHash: uncleHash,
|
2024-06-14 07:31:08 +00:00
|
|
|
number: ctx.env.currentNumber - 1'u64,
|
2023-08-04 12:43:30 +00:00
|
|
|
blobGasUsed: ctx.env.parentBlobGasUsed,
|
2023-08-30 16:29:48 +00:00
|
|
|
excessBlobGas: ctx.env.parentExcessBlobGas,
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Sanity check, to not `panic` in state_transition
|
2024-06-27 05:54:36 +00:00
|
|
|
if com.isLondonOrLater(ctx.env.currentNumber):
|
2022-12-08 05:17:14 +00:00
|
|
|
if ctx.env.currentBaseFee.isSome:
|
|
|
|
# Already set, currentBaseFee has precedent over parentBaseFee.
|
|
|
|
discard
|
|
|
|
elif ctx.env.parentBaseFee.isSome:
|
2024-06-14 07:31:08 +00:00
|
|
|
ctx.env.currentBaseFee = Opt.some(calcBaseFee(ctx.env))
|
2022-12-08 05:17:14 +00:00
|
|
|
else:
|
2022-10-15 15:58:23 +00:00
|
|
|
raise newError(ErrorConfig, "EIP-1559 config but missing 'currentBaseFee' in env section")
|
|
|
|
|
2023-03-21 11:36:22 +00:00
|
|
|
if com.isShanghaiOrLater(ctx.env.currentTimestamp) and ctx.env.withdrawals.isNone:
|
|
|
|
raise newError(ErrorConfig, "Shanghai config but missing 'withdrawals' in env section")
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
if com.isCancunOrLater(ctx.env.currentTimestamp):
|
2023-08-30 16:29:48 +00:00
|
|
|
if ctx.env.parentBeaconBlockRoot.isNone:
|
|
|
|
raise newError(ErrorConfig, "Cancun config but missing 'parentBeaconBlockRoot' in env section")
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
let res = loadKzgTrustedSetup()
|
|
|
|
if res.isErr:
|
|
|
|
raise newError(ErrorConfig, res.error)
|
2023-08-30 16:29:48 +00:00
|
|
|
else:
|
|
|
|
# un-set it if it has been set too early
|
2024-10-08 05:52:32 +00:00
|
|
|
ctx.env.parentBeaconBlockRoot = Opt.none(Hash32)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-07-17 10:05:53 +00:00
|
|
|
let isMerged = config.terminalTotalDifficulty.isSome and
|
|
|
|
config.terminalTotalDifficulty.value == 0.u256
|
|
|
|
if isMerged:
|
2022-10-15 15:58:23 +00:00
|
|
|
if ctx.env.currentRandom.isNone:
|
|
|
|
raise newError(ErrorConfig, "post-merge requires currentRandom to be defined in env")
|
|
|
|
|
|
|
|
if ctx.env.currentDifficulty.isSome and ctx.env.currentDifficulty.get() != 0:
|
|
|
|
raise newError(ErrorConfig, "post-merge difficulty must be zero (or omitted) in env")
|
2024-06-14 07:31:08 +00:00
|
|
|
ctx.env.currentDifficulty = Opt.none(DifficultyInt)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
elif ctx.env.currentDifficulty.isNone:
|
|
|
|
if ctx.env.parentDifficulty.isNone:
|
|
|
|
raise newError(ErrorConfig, "currentDifficulty was not provided, and cannot be calculated due to missing parentDifficulty")
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
if ctx.env.currentNumber == 0.BlockNumber:
|
2022-10-15 15:58:23 +00:00
|
|
|
raise newError(ErrorConfig, "currentDifficulty needs to be provided for block number 0")
|
|
|
|
|
|
|
|
if ctx.env.currentTimestamp <= ctx.env.parentTimestamp:
|
|
|
|
raise newError(ErrorConfig,
|
|
|
|
"currentDifficulty cannot be calculated -- currentTime ($1) needs to be after parent time ($2)" %
|
|
|
|
[$ctx.env.currentTimestamp, $ctx.env.parentTimestamp])
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
ctx.env.currentDifficulty = Opt.some(calcDifficulty(com,
|
2022-10-15 15:58:23 +00:00
|
|
|
ctx.env.currentTimestamp, parent))
|
|
|
|
|
2024-06-30 07:39:45 +00:00
|
|
|
# Calculate the excessBlobGas
|
|
|
|
if ctx.env.currentExcessBlobGas.isNone:
|
|
|
|
# If it is not explicitly defined, but we have the parent values, we try
|
|
|
|
# to calculate it ourselves.
|
|
|
|
if parent.excessBlobGas.isSome and parent.blobGasUsed.isSome:
|
|
|
|
ctx.env.currentExcessBlobGas = Opt.some calcExcessBlobGas(parent)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
let header = envToHeader(ctx.env)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
let vmState = TestVMState(
|
2022-12-15 10:31:47 +00:00
|
|
|
blockHashes: ctx.env.blockHashes,
|
2022-10-15 15:58:23 +00:00
|
|
|
hashError: ""
|
|
|
|
)
|
|
|
|
|
|
|
|
vmState.init(
|
|
|
|
parent = parent,
|
|
|
|
header = header,
|
2024-08-20 13:23:24 +00:00
|
|
|
com = com,
|
|
|
|
storeSlotHash = true
|
2022-10-15 15:58:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.setupAlloc(ctx.alloc)
|
2024-06-02 19:21:29 +00:00
|
|
|
db.persist(clearEmptyAccount = false)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
2024-10-22 02:07:58 +00:00
|
|
|
ctx.parseTxs(com.chainId)
|
2023-08-02 10:17:40 +00:00
|
|
|
let res = exec(ctx, vmState, conf.stateReward, header, conf)
|
2022-10-15 15:58:23 +00:00
|
|
|
|
|
|
|
if vmState.hashError.len > 0:
|
|
|
|
raise newError(ErrorMissingBlockhash, vmState.hashError)
|
|
|
|
|
|
|
|
ctx.dispatchOutput(conf, res)
|