2021-04-30 09:33:14 +00:00
|
|
|
# Nimbus - Various ways of calling the EVM
|
|
|
|
#
|
|
|
|
# Copyright (c) 2018-2021 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
|
2022-01-18 16:19:32 +00:00
|
|
|
std/times,
|
2021-10-26 15:18:08 +00:00
|
|
|
eth/common/eth_types, stint, options, stew/byteutils, chronicles,
|
|
|
|
".."/[vm_types, vm_state, vm_gas_costs, forks, constants],
|
2021-06-01 10:54:13 +00:00
|
|
|
".."/[db/db_chain, db/accounts_cache, transaction], eth/trie/db,
|
2021-10-14 08:00:02 +00:00
|
|
|
".."/[chain_config, rpc/hexstrings],
|
2021-05-17 11:39:52 +00:00
|
|
|
./call_common
|
2021-04-30 09:33:14 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RpcCallData* = object
|
2021-10-26 15:18:08 +00:00
|
|
|
source* : Option[EthAddress]
|
|
|
|
to* : Option[EthAddress]
|
|
|
|
gasLimit* : Option[GasInt]
|
|
|
|
gasPrice* : Option[GasInt]
|
|
|
|
maxFee* : Option[GasInt]
|
|
|
|
maxPriorityFee*: Option[GasInt]
|
|
|
|
value* : Option[UInt256]
|
|
|
|
data* : seq[byte]
|
|
|
|
accessList* : AccessList
|
|
|
|
|
|
|
|
proc toCallParams(vmState: BaseVMState, cd: RpcCallData,
|
|
|
|
globalGasCap: GasInt, baseFee: Option[Uint256],
|
|
|
|
forkOverride = none(Fork)): CallParams =
|
|
|
|
|
|
|
|
# Reject invalid combinations of pre- and post-1559 fee styles
|
|
|
|
if cd.gasPrice.isSome and (cd.maxFee.isSome or cd.maxPriorityFee.isSome):
|
|
|
|
raise newException(ValueError, "both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
|
|
|
|
|
|
|
# Set default gas & gas price if none were set
|
|
|
|
var gasLimit = globalGasCap
|
|
|
|
if gasLimit == 0:
|
|
|
|
gasLimit = GasInt(high(uint64) div 2)
|
|
|
|
|
|
|
|
if cd.gasLimit.isSome:
|
|
|
|
gasLimit = cd.gasLimit.get()
|
|
|
|
|
|
|
|
if globalGasCap != 0 and globalGasCap < gasLimit:
|
|
|
|
warn "Caller gas above allowance, capping", requested = gasLimit, cap = globalGasCap
|
|
|
|
gasLimit = globalGasCap
|
|
|
|
|
|
|
|
var gasPrice = cd.gasPrice.get(0.GasInt)
|
|
|
|
if baseFee.isSome:
|
|
|
|
# A basefee is provided, necessitating EIP-1559-type execution
|
|
|
|
let maxPriorityFee = cd.maxPriorityFee.get(0.GasInt)
|
|
|
|
let maxFee = cd.maxFee.get(0.GasInt)
|
|
|
|
|
|
|
|
# Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
|
|
|
|
if maxPriorityFee > 0 or maxFee > 0:
|
|
|
|
let baseFee = baseFee.get().truncate(GasInt)
|
|
|
|
let priorityFee = min(maxPriorityFee, maxFee - baseFee)
|
|
|
|
gasPrice = priorityFee + baseFee
|
|
|
|
|
|
|
|
CallParams(
|
2021-05-17 10:03:39 +00:00
|
|
|
vmState: vmState,
|
|
|
|
forkOverride: forkOverride,
|
2021-10-26 15:18:08 +00:00
|
|
|
sender: cd.source.get(ZERO_ADDRESS),
|
|
|
|
to: cd.to.get(ZERO_ADDRESS),
|
|
|
|
isCreate: cd.to.isNone,
|
2021-05-17 10:03:39 +00:00
|
|
|
gasLimit: gasLimit,
|
2021-10-26 15:18:08 +00:00
|
|
|
gasPrice: gasPrice,
|
|
|
|
value: cd.value.get(0.u256),
|
|
|
|
input: cd.data,
|
|
|
|
accessList: cd.accessList
|
|
|
|
)
|
|
|
|
|
|
|
|
proc rpcCallEvm*(call: RpcCallData, header: BlockHeader, chainDB: BaseChainDB): CallResult =
|
|
|
|
const globalGasCap = 0 # TODO: globalGasCap should configurable by user
|
2022-01-18 16:19:32 +00:00
|
|
|
let topHeader = BlockHeader(
|
|
|
|
parentHash: header.blockHash,
|
|
|
|
timestamp: getTime().utc.toTime,
|
|
|
|
gasLimit: 0.GasInt, ## ???
|
|
|
|
fee: Uint256.none()) ## ???
|
|
|
|
let vmState = BaseVMState.new(topHeader, chainDB)
|
2021-10-26 15:18:08 +00:00
|
|
|
let params = toCallParams(vmState, call, globalGasCap, header.fee)
|
|
|
|
|
|
|
|
var dbTx = chainDB.db.beginTransaction()
|
|
|
|
defer: dbTx.dispose() # always dispose state changes
|
|
|
|
|
|
|
|
runComputation(params)
|
|
|
|
|
|
|
|
proc rpcEstimateGas*(cd: RpcCallData, header: BlockHeader, chainDB: BaseChainDB, gasCap: GasInt): GasInt =
|
|
|
|
# Binary search the gas requirement, as it may be higher than the amount used
|
2022-01-18 16:19:32 +00:00
|
|
|
let topHeader = BlockHeader(
|
|
|
|
parentHash: header.blockHash,
|
|
|
|
timestamp: getTime().utc.toTime,
|
|
|
|
gasLimit: 0.GasInt, ## ???
|
|
|
|
fee: Uint256.none()) ## ???
|
|
|
|
let vmState = BaseVMState.new(topHeader, chainDB)
|
2021-10-26 15:18:08 +00:00
|
|
|
let fork = chainDB.config.toFork(header.blockNumber)
|
|
|
|
let txGas = gasFees[fork][GasTransaction] # txGas always 21000, use constants?
|
|
|
|
var params = toCallParams(vmState, cd, gasCap, header.fee)
|
|
|
|
|
2021-05-03 08:08:11 +00:00
|
|
|
var
|
2021-10-26 15:18:08 +00:00
|
|
|
lo : GasInt = txGas - 1
|
|
|
|
hi : GasInt = cd.gasLimit.get(0.GasInt)
|
|
|
|
cap: GasInt
|
|
|
|
|
|
|
|
var dbTx = chainDB.db.beginTransaction()
|
|
|
|
defer: dbTx.dispose() # always dispose state changes
|
|
|
|
|
|
|
|
# Determine the highest gas limit can be used during the estimation.
|
|
|
|
if hi < txGas:
|
|
|
|
# block's gasLimit act as the gas ceiling
|
|
|
|
hi = header.gasLimit
|
|
|
|
|
|
|
|
# Normalize the max fee per gas the call is willing to spend.
|
|
|
|
var feeCap = cd.gasPrice.get(0.GasInt)
|
|
|
|
if cd.gasPrice.isSome and (cd.maxFee.isSome or cd.maxPriorityFee.isSome):
|
|
|
|
raise newException(ValueError, "both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
|
|
|
elif cd.maxFee.isSome:
|
|
|
|
feeCap = cd.maxFee.get
|
|
|
|
|
|
|
|
# Recap the highest gas limit with account's available balance.
|
|
|
|
if feeCap > 0:
|
|
|
|
if cd.source.isNone:
|
|
|
|
raise newException(ValueError, "`from` can't be null")
|
|
|
|
|
|
|
|
let balance = vmState.readOnlyStateDB.getBalance(cd.source.get)
|
|
|
|
var available = balance
|
|
|
|
if cd.value.isSome:
|
|
|
|
let value = cd.value.get
|
|
|
|
if value > available:
|
|
|
|
raise newException(ValueError, "insufficient funds for transfer")
|
|
|
|
available -= value
|
|
|
|
|
|
|
|
let allowance = available div feeCap.u256
|
|
|
|
# If the allowance is larger than maximum GasInt, skip checking
|
|
|
|
if allowance < high(GasInt).u256 and hi > allowance.truncate(GasInt):
|
|
|
|
let transfer = cd.value.get(0.u256)
|
|
|
|
warn "Gas estimation capped by limited funds", original=hi, balance,
|
|
|
|
sent=transfer, maxFeePerGas=feeCap, fundable=allowance
|
|
|
|
hi = allowance.truncate(GasInt)
|
|
|
|
|
|
|
|
# Recap the highest gas allowance with specified gasCap.
|
|
|
|
if gasCap != 0 and hi > gasCap:
|
|
|
|
warn "Caller gas above allowance, capping", requested=hi, cap=gasCap
|
|
|
|
hi = gasCap
|
|
|
|
|
|
|
|
cap = hi
|
|
|
|
let intrinsicGas = intrinsicGas(params, fork)
|
|
|
|
|
|
|
|
# Create a helper to check if a gas allowance results in an executable transaction
|
|
|
|
proc executable(gasLimit: GasInt): bool =
|
|
|
|
if intrinsicGas > gasLimit:
|
|
|
|
# Special case, raise gas limit
|
|
|
|
return true
|
|
|
|
|
|
|
|
params.gasLimit = gasLimit
|
|
|
|
# TODO: bail out on consensus error similar to validateTransaction
|
|
|
|
runComputation(params).isError
|
|
|
|
|
|
|
|
# Execute the binary search and hone in on an executable gas limit
|
|
|
|
while lo+1 < hi:
|
|
|
|
let mid = (hi + lo) div 2
|
|
|
|
let failed = executable(mid)
|
|
|
|
if failed:
|
|
|
|
lo = mid
|
|
|
|
else:
|
|
|
|
hi = mid
|
|
|
|
|
|
|
|
# Reject the transaction as invalid if it still fails at the highest allowance
|
|
|
|
if hi == cap:
|
|
|
|
let failed = executable(hi)
|
|
|
|
if failed:
|
|
|
|
# TODO: provide more descriptive EVM error beside out of gas
|
|
|
|
# e.g. revert and other EVM errors
|
|
|
|
raise newException(ValueError, "gas required exceeds allowance " & $cap)
|
|
|
|
|
|
|
|
hi
|
2021-05-03 16:50:47 +00:00
|
|
|
|
2021-05-17 13:01:41 +00:00
|
|
|
proc txCallEvm*(tx: Transaction, sender: EthAddress, vmState: BaseVMState, fork: Fork): GasInt =
|
|
|
|
var call = CallParams(
|
2021-05-17 09:36:34 +00:00
|
|
|
vmState: vmState,
|
|
|
|
forkOverride: some(fork),
|
|
|
|
gasPrice: tx.gasPrice,
|
2021-05-17 13:01:41 +00:00
|
|
|
gasLimit: tx.gasLimit,
|
2021-05-17 09:36:34 +00:00
|
|
|
sender: sender,
|
2021-06-27 04:19:43 +00:00
|
|
|
to: tx.destination,
|
|
|
|
isCreate: tx.contractCreation,
|
2021-05-17 09:36:34 +00:00
|
|
|
value: tx.value,
|
|
|
|
input: tx.payload
|
2021-05-17 13:01:41 +00:00
|
|
|
)
|
2021-06-27 04:19:43 +00:00
|
|
|
if tx.txType > TxLegacy:
|
|
|
|
shallowCopy(call.accessList, tx.accessList)
|
2021-05-17 13:01:41 +00:00
|
|
|
return runComputation(call).gasUsed
|
2021-05-03 19:35:29 +00:00
|
|
|
|
2021-10-14 06:08:40 +00:00
|
|
|
proc testCallEvm*(tx: Transaction, sender: EthAddress, vmState: BaseVMState, fork: Fork): CallResult =
|
|
|
|
var call = CallParams(
|
2021-05-17 16:27:20 +00:00
|
|
|
vmState: vmState,
|
2021-10-14 06:08:40 +00:00
|
|
|
forkOverride: some(fork),
|
|
|
|
gasPrice: tx.gasPrice,
|
|
|
|
gasLimit: tx.gasLimit,
|
|
|
|
sender: sender,
|
|
|
|
to: tx.destination,
|
|
|
|
isCreate: tx.contractCreation,
|
|
|
|
value: tx.value,
|
|
|
|
input: tx.payload,
|
2021-05-04 14:02:16 +00:00
|
|
|
|
2021-10-14 06:08:40 +00:00
|
|
|
noIntrinsic: true, # Don't charge intrinsic gas.
|
|
|
|
noRefund: true, # Don't apply gas refund/burn rule.
|
|
|
|
)
|
|
|
|
if tx.txType > TxLegacy:
|
|
|
|
shallowCopy(call.accessList, tx.accessList)
|
|
|
|
runComputation(call)
|