2021-03-31 08:58:26 +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
|
2021-07-27 11:28:05 +00:00
|
|
|
std/[json, macros, options, sets, strformat, tables],
|
|
|
|
../../stateless/[witness_from_tree, witness_types],
|
2021-09-11 14:58:01 +00:00
|
|
|
../chain_config,
|
2021-07-27 11:28:05 +00:00
|
|
|
../constants,
|
|
|
|
../db/[db_chain, accounts_cache],
|
|
|
|
../errors,
|
|
|
|
../forks,
|
2022-01-18 16:19:32 +00:00
|
|
|
../utils/[difficulty, ec_recover],
|
2021-07-27 11:28:05 +00:00
|
|
|
./interpreter/gas_costs,
|
|
|
|
./transaction_tracer,
|
|
|
|
./types,
|
|
|
|
eth/[common, keys]
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
const
|
|
|
|
nilHash = block:
|
|
|
|
var rc: Hash256
|
|
|
|
rc
|
|
|
|
|
|
|
|
template safeExecutor(info: string; code: untyped) =
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except CatchableError as e:
|
|
|
|
raise (ref CatchableError)(msg: e.msg)
|
|
|
|
except Defect as e:
|
|
|
|
raise (ref Defect)(msg: e.msg)
|
|
|
|
except:
|
|
|
|
let e = getCurrentException()
|
|
|
|
raise newException(VmStateError, info & "(): " & $e.name & " -- " & e.msg)
|
|
|
|
|
2022-03-15 17:21:41 +00:00
|
|
|
proc isTtdReached(db: BaseChainDB; blockHash: Hash256): bool
|
|
|
|
{.gcsafe, raises: [Defect,RlpError].} =
|
|
|
|
## Returns `true` iff the stored sum of difficulties has reached the
|
|
|
|
## terminal total difficulty, see EIP3675.
|
|
|
|
if db.config.terminalTotalDifficulty.isSome:
|
|
|
|
return db.config.terminalTotalDifficulty.get <= db.getScore(blockHash)
|
|
|
|
|
|
|
|
proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader): EthAddress
|
2022-01-18 16:19:32 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2022-03-15 17:21:41 +00:00
|
|
|
if not chainDB.config.poaEngine or chainDB.isTtdReached(header.parentHash):
|
2022-01-18 16:19:32 +00:00
|
|
|
return header.coinbase
|
2022-03-15 17:21:41 +00:00
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
let account = header.ecRecover
|
2021-07-27 11:28:05 +00:00
|
|
|
if account.isErr:
|
|
|
|
let msg = "Could not recover account address: " & $account.error
|
|
|
|
raise newException(ValidationError, msg)
|
|
|
|
|
|
|
|
account.value
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc init(
|
|
|
|
self: BaseVMState;
|
|
|
|
ac: AccountsCache;
|
|
|
|
parent: BlockHeader;
|
|
|
|
timestamp: EthTime;
|
|
|
|
gasLimit: GasInt;
|
2022-04-08 04:54:11 +00:00
|
|
|
fee: Option[UInt256];
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao: Hash256;
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty: UInt256;
|
2022-01-18 16:19:32 +00:00
|
|
|
miner: EthAddress;
|
|
|
|
chainDB: BaseChainDB;
|
2022-02-08 09:17:59 +00:00
|
|
|
ttdReached: bool;
|
2022-01-18 16:19:32 +00:00
|
|
|
tracer: TransactionTracer)
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Initialisation helper
|
2021-03-31 08:58:26 +00:00
|
|
|
self.prevHeaders = @[]
|
|
|
|
self.name = "BaseVM"
|
2022-01-18 16:19:32 +00:00
|
|
|
self.parent = parent
|
|
|
|
self.timestamp = timestamp
|
|
|
|
self.gasLimit = gasLimit
|
|
|
|
self.fee = fee
|
2022-02-27 05:21:46 +00:00
|
|
|
self.prevRandao = prevRandao
|
2022-06-14 06:01:51 +00:00
|
|
|
self.blockDifficulty = difficulty
|
2022-04-08 04:54:11 +00:00
|
|
|
self.chainDB = chainDB
|
2022-02-08 09:17:59 +00:00
|
|
|
self.ttdReached = ttdReached
|
2022-01-18 16:19:32 +00:00
|
|
|
self.tracer = tracer
|
2021-03-31 08:58:26 +00:00
|
|
|
self.logEntries = @[]
|
2021-10-28 09:42:39 +00:00
|
|
|
self.stateDB = ac
|
2021-03-31 08:58:26 +00:00
|
|
|
self.touchedAccounts = initHashSet[EthAddress]()
|
2022-01-18 16:19:32 +00:00
|
|
|
self.minerAddress = miner
|
|
|
|
|
|
|
|
proc init(
|
|
|
|
self: BaseVMState;
|
|
|
|
ac: AccountsCache;
|
|
|
|
parent: BlockHeader;
|
|
|
|
timestamp: EthTime;
|
|
|
|
gasLimit: GasInt;
|
2022-04-08 04:54:11 +00:00
|
|
|
fee: Option[UInt256];
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao: Hash256;
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty: UInt256;
|
2022-01-18 16:19:32 +00:00
|
|
|
miner: EthAddress;
|
|
|
|
chainDB: BaseChainDB;
|
|
|
|
tracerFlags: set[TracerFlags])
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
var tracer: TransactionTracer
|
|
|
|
tracer.initTracer(tracerFlags)
|
|
|
|
self.init(
|
|
|
|
ac = ac,
|
|
|
|
parent = parent,
|
|
|
|
timestamp = timestamp,
|
|
|
|
gasLimit = gasLimit,
|
|
|
|
fee = fee,
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao= prevRandao,
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty= difficulty,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = miner,
|
|
|
|
chainDB = chainDB,
|
2022-03-15 17:21:41 +00:00
|
|
|
ttdReached= chainDB.isTtdReached(parent.blockHash),
|
2022-01-18 16:19:32 +00:00
|
|
|
tracer = tracer)
|
|
|
|
|
|
|
|
# --------------
|
|
|
|
|
|
|
|
proc `$`*(vmState: BaseVMState): string
|
|
|
|
{.gcsafe, raises: [Defect,ValueError].} =
|
|
|
|
if vmState.isNil:
|
|
|
|
result = "nil"
|
|
|
|
else:
|
|
|
|
result = &"VMState {vmState.name}:"&
|
|
|
|
&"\n blockNumber: {vmState.parent.blockNumber + 1}"&
|
|
|
|
&"\n chaindb: {vmState.chaindb}"
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type BaseVMState;
|
|
|
|
parent: BlockHeader; ## parent header, account sync position
|
|
|
|
timestamp: EthTime; ## tx env: time stamp
|
|
|
|
gasLimit: GasInt; ## tx env: gas limit
|
2022-04-08 04:54:11 +00:00
|
|
|
fee: Option[UInt256]; ## tx env: optional base fee
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao: Hash256; ## tx env: POS block randomness
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty: UInt256, ## tx env: difficulty
|
2022-01-18 16:19:32 +00:00
|
|
|
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
|
|
|
chainDB: BaseChainDB; ## block chain database
|
|
|
|
tracerFlags: set[TracerFlags] = {};
|
|
|
|
pruneTrie: bool = true): T
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Create a new `BaseVMState` descriptor from a parent block header. This
|
|
|
|
## function internally constructs a new account state cache rooted at
|
|
|
|
## `parent.stateRoot`
|
|
|
|
##
|
|
|
|
## This `new()` constructor and its variants (see below) provide a save
|
|
|
|
## `BaseVMState` environment where the account state cache is synchronised
|
|
|
|
## with the `parent` block header.
|
|
|
|
new result
|
|
|
|
result.init(
|
|
|
|
ac = AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
|
|
|
|
parent = parent,
|
|
|
|
timestamp = timestamp,
|
|
|
|
gasLimit = gasLimit,
|
|
|
|
fee = fee,
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao = prevRandao,
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty = difficulty,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = miner,
|
|
|
|
chainDB = chainDB,
|
|
|
|
tracerFlags = tracerFlags)
|
|
|
|
|
|
|
|
proc reinit*(self: BaseVMState; ## Object descriptor
|
|
|
|
parent: BlockHeader; ## parent header, account sync pos.
|
|
|
|
timestamp: EthTime; ## tx env: time stamp
|
|
|
|
gasLimit: GasInt; ## tx env: gas limit
|
2022-04-08 04:54:11 +00:00
|
|
|
fee: Option[UInt256]; ## tx env: optional base fee
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao:Hash256; ## tx env: POS block randomness
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty:UInt256, ## tx env: difficulty
|
2022-01-18 16:19:32 +00:00
|
|
|
miner: EthAddress; ## tx env: coinbase(PoW) or signer(PoA)
|
|
|
|
pruneTrie: bool = true): bool
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Re-initialise state descriptor. The `AccountsCache` database is
|
|
|
|
## re-initilaise only if its `rootHash` doe not point to `parent.stateRoot`,
|
|
|
|
## already. Accumulated state data are reset.
|
|
|
|
##
|
|
|
|
## This function returns `true` unless the `AccountsCache` database could be
|
|
|
|
## queries about its `rootHash`, i.e. `isTopLevelClean` evaluated `true`. If
|
|
|
|
## this function returns `false`, the function argument `self` is left
|
|
|
|
## untouched.
|
|
|
|
if self.stateDB.isTopLevelClean:
|
|
|
|
let
|
|
|
|
tracer = self.tracer
|
|
|
|
db = self.chainDB
|
|
|
|
ac = if self.stateDB.rootHash == parent.stateRoot: self.stateDB
|
|
|
|
else: AccountsCache.init(db.db, parent.stateRoot, pruneTrie)
|
|
|
|
self[].reset
|
|
|
|
self.init(
|
|
|
|
ac = ac,
|
|
|
|
parent = parent,
|
|
|
|
timestamp = timestamp,
|
|
|
|
gasLimit = gasLimit,
|
|
|
|
fee = fee,
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao = prevRandao,
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty = difficulty,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = miner,
|
|
|
|
chainDB = db,
|
2022-03-15 17:21:41 +00:00
|
|
|
ttdReached = db.isTtdReached(parent.blockHash),
|
2022-01-18 16:19:32 +00:00
|
|
|
tracer = tracer)
|
|
|
|
return true
|
|
|
|
# else: false
|
|
|
|
|
|
|
|
proc reinit*(self: BaseVMState; ## Object descriptor
|
|
|
|
parent: BlockHeader; ## parent header, account sync pos.
|
|
|
|
header: BlockHeader; ## header with tx environment data fields
|
|
|
|
pruneTrie: bool = true): bool
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Variant of `reinit()`. The `parent` argument is used to sync the accounts
|
|
|
|
## cache and the `header` is used as a container to pass the `timestamp`,
|
|
|
|
## `gasLimit`, and `fee` values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2022-03-15 17:21:41 +00:00
|
|
|
result = self.reinit(
|
2022-01-18 16:19:32 +00:00
|
|
|
parent = parent,
|
|
|
|
timestamp = header.timestamp,
|
|
|
|
gasLimit = header.gasLimit,
|
|
|
|
fee = header.fee,
|
2022-02-27 05:21:46 +00:00
|
|
|
prevRandao= header.prevRandao,
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty= header.difficulty,
|
2022-03-15 17:21:41 +00:00
|
|
|
miner = self.chainDB.getMinerAddress(header),
|
2022-01-18 16:19:32 +00:00
|
|
|
pruneTrie = pruneTrie)
|
|
|
|
|
|
|
|
proc reinit*(self: BaseVMState; ## Object descriptor
|
|
|
|
header: BlockHeader; ## header with tx environment data fields
|
|
|
|
pruneTrie: bool = true): bool
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## This is a variant of the `reinit()` function above where the field
|
|
|
|
## `header.parentHash`, is used to fetch the `parent` BlockHeader to be
|
|
|
|
## used in the `update()` variant, above.
|
|
|
|
self.reinit(
|
|
|
|
parent = self.chainDB.getBlockHeader(header.parentHash),
|
|
|
|
header = header,
|
|
|
|
pruneTrie = pruneTrie)
|
|
|
|
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
self: BaseVMState; ## Object descriptor
|
|
|
|
parent: BlockHeader; ## parent header, account sync position
|
|
|
|
header: BlockHeader; ## header with tx environment data fields
|
|
|
|
chainDB: BaseChainDB; ## block chain database
|
|
|
|
tracerFlags: set[TracerFlags] = {},
|
|
|
|
pruneTrie: bool = true)
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Variant of `new()` constructor above for in-place initalisation. The
|
|
|
|
## `parent` argument is used to sync the accounts cache and the `header`
|
|
|
|
## is used as a container to pass the `timestamp`, `gasLimit`, and `fee`
|
|
|
|
## values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2022-03-15 17:21:41 +00:00
|
|
|
self.init(
|
|
|
|
ac = AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
|
|
|
|
parent = parent,
|
|
|
|
timestamp = header.timestamp,
|
|
|
|
gasLimit = header.gasLimit,
|
|
|
|
fee = header.fee,
|
|
|
|
prevRandao = header.prevRandao,
|
2022-06-14 06:01:51 +00:00
|
|
|
difficulty = header.difficulty,
|
2022-03-15 17:21:41 +00:00
|
|
|
miner = chainDB.getMinerAddress(header),
|
|
|
|
chainDB = chainDB,
|
|
|
|
tracerFlags = tracerFlags)
|
2022-01-18 16:19:32 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type BaseVMState;
|
|
|
|
parent: BlockHeader; ## parent header, account sync position
|
|
|
|
header: BlockHeader; ## header with tx environment data fields
|
|
|
|
chainDB: BaseChainDB; ## block chain database
|
|
|
|
tracerFlags: set[TracerFlags] = {},
|
|
|
|
pruneTrie: bool = true): T
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## This is a variant of the `new()` constructor above where the `parent`
|
|
|
|
## argument is used to sync the accounts cache and the `header` is used
|
|
|
|
## as a container to pass the `timestamp`, `gasLimit`, and `fee` values.
|
|
|
|
##
|
|
|
|
## It requires the `header` argument properly initalised so that for PoA
|
|
|
|
## networks, the miner address is retrievable via `ecRecover()`.
|
2021-03-31 08:58:26 +00:00
|
|
|
new result
|
2022-01-18 16:19:32 +00:00
|
|
|
result.init(
|
|
|
|
parent = parent,
|
|
|
|
header = header,
|
|
|
|
chainDB = chainDB,
|
|
|
|
tracerFlags = tracerFlags,
|
|
|
|
pruneTrie = pruneTrie)
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type BaseVMState;
|
|
|
|
header: BlockHeader; ## header with tx environment data fields
|
|
|
|
chainDB: BaseChainDB; ## block chain database
|
|
|
|
tracerFlags: set[TracerFlags] = {};
|
|
|
|
pruneTrie: bool = true): T
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## This is a variant of the `new()` constructor above where the field
|
|
|
|
## `header.parentHash`, is used to fetch the `parent` BlockHeader to be
|
|
|
|
## used in the `new()` variant, above.
|
|
|
|
BaseVMState.new(
|
|
|
|
parent = chainDB.getBlockHeader(header.parentHash),
|
|
|
|
header = header,
|
|
|
|
chainDB = chainDB,
|
|
|
|
tracerFlags = tracerFlags,
|
|
|
|
pruneTrie = pruneTrie)
|
|
|
|
|
2021-03-31 08:58:26 +00:00
|
|
|
|
|
|
|
proc setupTxContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt, forkOverride=none(Fork)) =
|
|
|
|
## this proc will be called each time a new transaction
|
|
|
|
## is going to be executed
|
|
|
|
vmState.txOrigin = origin
|
|
|
|
vmState.txGasPrice = gasPrice
|
|
|
|
vmState.fork =
|
|
|
|
if forkOverride.isSome:
|
|
|
|
forkOverride.get
|
|
|
|
else:
|
2022-01-18 16:19:32 +00:00
|
|
|
vmState.chainDB.config.toFork(vmState.parent.blockNumber + 1)
|
2021-03-31 08:58:26 +00:00
|
|
|
vmState.gasCosts = vmState.fork.forkToSchedule
|
|
|
|
|
|
|
|
method coinbase*(vmState: BaseVMState): EthAddress {.base, gcsafe.} =
|
|
|
|
vmState.minerAddress
|
|
|
|
|
|
|
|
method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} =
|
|
|
|
# it should return current block number
|
|
|
|
# and not head.blockNumber
|
2022-01-18 16:19:32 +00:00
|
|
|
vmState.parent.blockNumber + 1
|
2021-03-31 08:58:26 +00:00
|
|
|
|
|
|
|
method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
2022-02-08 09:17:59 +00:00
|
|
|
if vmState.ttdReached:
|
2022-02-05 09:15:50 +00:00
|
|
|
# EIP-4399/EIP-3675
|
2022-02-27 05:21:46 +00:00
|
|
|
UInt256.fromBytesBE(vmState.prevRandao.data, allowPadding = false)
|
2022-02-05 09:15:50 +00:00
|
|
|
else:
|
2022-06-14 06:01:51 +00:00
|
|
|
vmState.blockDifficulty
|
2021-03-31 08:58:26 +00:00
|
|
|
|
2021-07-27 11:28:05 +00:00
|
|
|
method baseFee*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
2022-01-18 16:19:32 +00:00
|
|
|
if vmState.fee.isSome:
|
|
|
|
vmState.fee.get
|
|
|
|
else:
|
|
|
|
0.u256
|
2021-06-27 13:18:17 +00:00
|
|
|
|
2021-03-31 08:58:26 +00:00
|
|
|
when defined(geth):
|
|
|
|
import db/geth_db
|
|
|
|
|
2022-02-14 14:22:39 +00:00
|
|
|
method getAncestorHash*(vmState: BaseVMState, blockNumber: BlockNumber): Hash256 {.base, gcsafe, raises: [Defect,CatchableError,Exception].} =
|
2022-01-18 16:19:32 +00:00
|
|
|
var ancestorDepth = vmState.blockNumber - blockNumber - 1
|
2021-03-31 08:58:26 +00:00
|
|
|
if ancestorDepth >= constants.MAX_PREV_HEADER_DEPTH:
|
|
|
|
return
|
2022-01-18 16:19:32 +00:00
|
|
|
if blockNumber >= vmState.blockNumber:
|
2021-03-31 08:58:26 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
when defined(geth):
|
|
|
|
result = vmState.chainDB.headerHash(blockNumber.truncate(uint64))
|
|
|
|
else:
|
|
|
|
result = vmState.chainDB.getBlockHash(blockNumber)
|
|
|
|
#TODO: should we use deque here?
|
|
|
|
# someday we may revive this code when
|
|
|
|
# we already have working miner
|
|
|
|
when false:
|
|
|
|
let idx = ancestorDepth.toInt
|
|
|
|
if idx >= vmState.prevHeaders.len:
|
|
|
|
return
|
|
|
|
|
|
|
|
var header = vmState.prevHeaders[idx]
|
|
|
|
result = header.hash
|
|
|
|
|
|
|
|
proc readOnlyStateDB*(vmState: BaseVMState): ReadOnlyStateDB {.inline.} =
|
2021-10-28 09:42:39 +00:00
|
|
|
ReadOnlyStateDB(vmState.stateDB)
|
2021-03-31 08:58:26 +00:00
|
|
|
|
|
|
|
template mutateStateDB*(vmState: BaseVMState, body: untyped) =
|
|
|
|
block:
|
2021-10-28 09:42:39 +00:00
|
|
|
var db {.inject.} = vmState.stateDB
|
2021-03-31 08:58:26 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
proc getTracingResult*(vmState: BaseVMState): JsonNode {.inline.} =
|
|
|
|
doAssert(EnableTracing in vmState.tracer.flags)
|
|
|
|
vmState.tracer.trace
|
|
|
|
|
|
|
|
proc getAndClearLogEntries*(vmState: BaseVMState): seq[Log] =
|
|
|
|
shallowCopy(result, vmState.logEntries)
|
|
|
|
vmState.logEntries = @[]
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc enableTracing*(vmState: BaseVMState) =
|
2021-03-31 08:58:26 +00:00
|
|
|
vmState.tracer.flags.incl EnableTracing
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc disableTracing*(vmState: BaseVMState) =
|
2021-03-31 08:58:26 +00:00
|
|
|
vmState.tracer.flags.excl EnableTracing
|
|
|
|
|
|
|
|
iterator tracedAccounts*(vmState: BaseVMState): EthAddress =
|
|
|
|
for acc in vmState.tracer.accounts:
|
|
|
|
yield acc
|
|
|
|
|
|
|
|
iterator tracedAccountsPairs*(vmState: BaseVMState): (int, EthAddress) =
|
|
|
|
var idx = 0
|
|
|
|
for acc in vmState.tracer.accounts:
|
|
|
|
yield (idx, acc)
|
|
|
|
inc idx
|
|
|
|
|
|
|
|
proc removeTracedAccounts*(vmState: BaseVMState, accounts: varargs[EthAddress]) =
|
|
|
|
for acc in accounts:
|
|
|
|
vmState.tracer.accounts.excl acc
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc status*(vmState: BaseVMState): bool =
|
2021-03-31 08:58:26 +00:00
|
|
|
ExecutionOK in vmState.flags
|
|
|
|
|
|
|
|
proc `status=`*(vmState: BaseVMState, status: bool) =
|
|
|
|
if status: vmState.flags.incl ExecutionOK
|
|
|
|
else: vmState.flags.excl ExecutionOK
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc generateWitness*(vmState: BaseVMState): bool =
|
2021-03-31 08:58:26 +00:00
|
|
|
GenerateWitness in vmState.flags
|
|
|
|
|
|
|
|
proc `generateWitness=`*(vmState: BaseVMState, status: bool) =
|
|
|
|
if status: vmState.flags.incl GenerateWitness
|
|
|
|
else: vmState.flags.excl GenerateWitness
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc buildWitness*(vmState: BaseVMState): seq[byte]
|
|
|
|
{.raises: [Defect, CatchableError].} =
|
2021-10-28 09:42:39 +00:00
|
|
|
let rootHash = vmState.stateDB.rootHash
|
|
|
|
let mkeys = vmState.stateDB.makeMultiKeys()
|
2022-04-08 04:54:11 +00:00
|
|
|
let flags = if vmState.fork >= FkSpurious: {wfEIP170} else: {}
|
2021-03-31 08:58:26 +00:00
|
|
|
|
|
|
|
# build witness from tree
|
|
|
|
var wb = initWitnessBuilder(vmState.chainDB.db, rootHash, flags)
|
2022-01-18 16:19:32 +00:00
|
|
|
safeExecutor("buildWitness"):
|
|
|
|
result = wb.buildWitness(mkeys)
|