2021-04-08 14:52:10 +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],
|
|
|
|
../config,
|
|
|
|
../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
|
|
|
./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)
|
|
|
|
|
|
|
|
proc getMinerAddress(chainDB: BaseChainDB; header: BlockHeader): EthAddress
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
if not chainDB.config.poaEngine:
|
|
|
|
return header.coinbase
|
|
|
|
|
|
|
|
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;
|
|
|
|
fee: Option[Uint256];
|
2022-02-01 08:55:51 +00:00
|
|
|
random: Hash256;
|
2022-01-18 16:19:32 +00:00
|
|
|
miner: EthAddress;
|
|
|
|
chainDB: BaseChainDB;
|
|
|
|
tracer: TransactionTracer)
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Initialisation helper
|
2021-04-08 14:52:10 +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-01 08:55:51 +00:00
|
|
|
self.random = random
|
2021-04-08 14:52:10 +00:00
|
|
|
self.chaindb = chainDB
|
2022-01-18 16:19:32 +00:00
|
|
|
self.tracer = tracer
|
2021-04-08 14:52:10 +00:00
|
|
|
self.logEntries = @[]
|
2021-10-28 09:42:39 +00:00
|
|
|
self.stateDB = ac
|
2021-04-08 14:52:10 +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;
|
|
|
|
fee: Option[Uint256];
|
2022-02-01 08:55:51 +00:00
|
|
|
random: Hash256;
|
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-01 08:55:51 +00:00
|
|
|
random = random,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = miner,
|
|
|
|
chainDB = chainDB,
|
|
|
|
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
|
|
|
|
fee: Option[Uint256]; ## tx env: optional base fee
|
2022-02-01 08:55:51 +00:00
|
|
|
random: Hash256; ## tx env: POS block randomness
|
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-01 08:55:51 +00:00
|
|
|
random = random,
|
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
|
|
|
|
fee: Option[Uint256]; ## tx env: optional base fee
|
2022-02-01 08:55:51 +00:00
|
|
|
random: Hash256; ## tx env: POS block randomness
|
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-01 08:55:51 +00:00
|
|
|
random = random,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = miner,
|
|
|
|
chainDB = db,
|
|
|
|
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()`.
|
|
|
|
self.reinit(
|
|
|
|
parent = parent,
|
|
|
|
timestamp = header.timestamp,
|
|
|
|
gasLimit = header.gasLimit,
|
|
|
|
fee = header.fee,
|
2022-02-01 08:55:51 +00:00
|
|
|
random = header.random,
|
2022-01-18 16:19:32 +00:00
|
|
|
miner = self.chainDB.getMinerAddress(header),
|
|
|
|
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()`.
|
|
|
|
self.init(AccountsCache.init(chainDB.db, parent.stateRoot, pruneTrie),
|
|
|
|
parent,
|
|
|
|
header.timestamp,
|
|
|
|
header.gasLimit,
|
|
|
|
header.fee,
|
2022-02-01 08:55:51 +00:00
|
|
|
header.random,
|
2022-01-18 16:19:32 +00:00
|
|
|
chainDB.getMinerAddress(header),
|
|
|
|
chainDB,
|
|
|
|
tracerFlags)
|
|
|
|
|
|
|
|
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-04-08 14:52:10 +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-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
proc consensusEnginePoA*(vmState: BaseVMState): bool =
|
|
|
|
# PoA consensus engine have no reward for miner
|
2021-05-20 06:01:57 +00:00
|
|
|
# TODO: this need to be fixed somehow
|
|
|
|
# using `real` engine configuration
|
|
|
|
vmState.chainDB.config.poaEngine
|
2021-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
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-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
method difficulty*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
2022-01-18 16:19:32 +00:00
|
|
|
vmState.chainDB.config.calcDifficulty(vmState.timestamp, vmState.parent)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2021-06-27 13:19:22 +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:19:22 +00:00
|
|
|
|
2021-04-08 14:52:10 +00:00
|
|
|
when defined(geth):
|
|
|
|
import db/geth_db
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
method getAncestorHash*(vmState: BaseVMState, blockNumber: BlockNumber): Hash256 {.base, gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
var ancestorDepth = vmState.blockNumber - blockNumber - 1
|
2021-04-08 14:52:10 +00:00
|
|
|
if ancestorDepth >= constants.MAX_PREV_HEADER_DEPTH:
|
|
|
|
return
|
2022-01-18 16:19:32 +00:00
|
|
|
if blockNumber >= vmState.blockNumber:
|
2021-04-08 14:52:10 +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-04-08 14:52:10 +00:00
|
|
|
|
|
|
|
template mutateStateDB*(vmState: BaseVMState, body: untyped) =
|
|
|
|
block:
|
2021-10-28 09:42:39 +00:00
|
|
|
var db {.inject.} = vmState.stateDB
|
2021-04-08 14:52:10 +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-04-08 14:52:10 +00:00
|
|
|
vmState.tracer.flags.incl EnableTracing
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc disableTracing*(vmState: BaseVMState) =
|
2021-04-08 14:52:10 +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-04-08 14:52:10 +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-04-08 14:52:10 +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()
|
2021-04-08 14:52:10 +00:00
|
|
|
let flags = if vmState.fork >= FKSpurious: {wfEIP170} else: {}
|
|
|
|
|
|
|
|
# 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)
|