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
|
2021-05-03 08:07:56 +00:00
|
|
|
eth/common/eth_types, stint, options, stew/byteutils,
|
2021-05-03 09:04:15 +00:00
|
|
|
".."/[vm_types, vm_types2, vm_state, vm_computation, utils],
|
2021-05-03 08:08:11 +00:00
|
|
|
".."/[db/db_chain, config, vm_state_transactions, rpc/hexstrings],
|
RPC: Change rpcEstimateGas to use the EVM without a fake transaction
Change `rpcEstimateGas` to setup and execute a computation directly, in a
similar way to `rpcDoCall` and `rpcMakeCall`, instead of constructing a fake
transaction and then validating it.
This patch does not (or should not) change any behaviour.
Although this looks a bit messy as it duplicates parts of `validateTransaction`
and `processTransaction`, proc names have been used to hopefully keep the
meanings clear, and it's just a stepping stone as those transaction functions
will be changed next. Also the behaviour of RPC `estimateGas` may not be
correct (although this patch is careful not to change it), so it's good to make
it explicit so we can see how it differs from other RPCs.
Doing this change exposed some interesting behaviour differences between the
`call` RPC and `estimateGas` RPC, which may be bugs, or may be intentional.
These differences are now obvious and explicit.
The unclear areas are not well documented by any of the clients, even Infura
which says a bit more than the others. So to find out if they are intended,
we'll have to run tests against other Ethereum services.
Guessing, on the face of it, it looks likely that RPC `call` should:
- Setup EIP2929 access lists
- Account for intrinsic gas (maybe not because zero-gas transactions are ok)
And it looks likely that RPC `estimateGas` should:
- Not return zero when an account has insufficient balance
- Maybe use a different gas cost estimate when one isn't supplied in the RPC
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-03 06:58:45 +00:00
|
|
|
".."/[db/accounts_cache, transaction, vm_precompiles, vm_gas_costs], eth/trie/db
|
2021-04-30 09:33:14 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RpcCallData* = object
|
|
|
|
source*: EthAddress
|
|
|
|
to*: EthAddress
|
|
|
|
gas*: GasInt
|
|
|
|
gasPrice*: GasInt
|
|
|
|
value*: UInt256
|
|
|
|
data*: seq[byte]
|
|
|
|
contractCreation*: bool
|
|
|
|
|
2021-05-03 09:04:15 +00:00
|
|
|
proc rpcSetupComputation*(vmState: BaseVMState, call: RpcCallData,
|
|
|
|
fork: Fork, gasLimit: GasInt): Computation =
|
2021-04-30 09:33:14 +00:00
|
|
|
vmState.setupTxContext(
|
|
|
|
origin = call.source,
|
|
|
|
gasPrice = call.gasPrice,
|
|
|
|
forkOverride = some(fork)
|
|
|
|
)
|
|
|
|
|
2021-05-03 09:04:15 +00:00
|
|
|
var msg = Message(
|
|
|
|
kind: if call.contractCreation: evmcCreate else: evmcCall,
|
2021-04-30 09:33:14 +00:00
|
|
|
depth: 0,
|
2021-05-03 09:04:15 +00:00
|
|
|
gas: gasLimit,
|
2021-04-30 09:33:14 +00:00
|
|
|
sender: call.source,
|
2021-05-03 09:04:15 +00:00
|
|
|
contractAddress:
|
|
|
|
if not call.contractCreation:
|
|
|
|
call.to
|
|
|
|
else:
|
|
|
|
generateAddress(call.source, vmState.readOnlyStateDB.getNonce(call.source)),
|
2021-04-30 09:33:14 +00:00
|
|
|
codeAddress: call.to,
|
|
|
|
value: call.value,
|
|
|
|
data: call.data
|
2021-05-03 09:04:15 +00:00
|
|
|
)
|
2021-04-30 09:33:14 +00:00
|
|
|
|
|
|
|
return newComputation(vmState, msg)
|
2021-05-03 08:08:03 +00:00
|
|
|
|
|
|
|
proc rpcDoCall*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB): HexDataStr =
|
|
|
|
# TODO: handle revert and error
|
|
|
|
# TODO: handle contract ABI
|
|
|
|
var
|
|
|
|
# we use current header stateRoot, unlike block validation
|
|
|
|
# which use previous block stateRoot
|
|
|
|
vmState = newBaseVMState(header.stateRoot, header, chain)
|
|
|
|
fork = toFork(chain.config, header.blockNumber)
|
2021-05-03 09:04:15 +00:00
|
|
|
comp = rpcSetupComputation(vmState, call, fork, call.gas)
|
2021-05-03 08:08:03 +00:00
|
|
|
|
|
|
|
comp.execComputation()
|
|
|
|
result = hexDataStr(comp.output)
|
2021-05-03 08:08:11 +00:00
|
|
|
|
2021-05-03 08:07:56 +00:00
|
|
|
proc rpcMakeCall*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB): (string, GasInt, bool) =
|
|
|
|
# TODO: handle revert
|
|
|
|
var
|
|
|
|
# we use current header stateRoot, unlike block validation
|
|
|
|
# which use previous block stateRoot
|
|
|
|
vmState = newBaseVMState(header.stateRoot, header, chain)
|
|
|
|
fork = toFork(chain.config, header.blockNumber)
|
2021-05-03 09:04:15 +00:00
|
|
|
comp = rpcSetupComputation(vmState, call, fork, call.gas)
|
2021-05-03 08:07:56 +00:00
|
|
|
|
|
|
|
let gas = comp.gasMeter.gasRemaining
|
|
|
|
comp.execComputation()
|
|
|
|
return (comp.output.toHex, gas - comp.gasMeter.gasRemaining, comp.isError)
|
|
|
|
|
RPC: Change rpcEstimateGas to use the EVM without a fake transaction
Change `rpcEstimateGas` to setup and execute a computation directly, in a
similar way to `rpcDoCall` and `rpcMakeCall`, instead of constructing a fake
transaction and then validating it.
This patch does not (or should not) change any behaviour.
Although this looks a bit messy as it duplicates parts of `validateTransaction`
and `processTransaction`, proc names have been used to hopefully keep the
meanings clear, and it's just a stepping stone as those transaction functions
will be changed next. Also the behaviour of RPC `estimateGas` may not be
correct (although this patch is careful not to change it), so it's good to make
it explicit so we can see how it differs from other RPCs.
Doing this change exposed some interesting behaviour differences between the
`call` RPC and `estimateGas` RPC, which may be bugs, or may be intentional.
These differences are now obvious and explicit.
The unclear areas are not well documented by any of the clients, even Infura
which says a bit more than the others. So to find out if they are intended,
we'll have to run tests against other Ethereum services.
Guessing, on the face of it, it looks likely that RPC `call` should:
- Setup EIP2929 access lists
- Account for intrinsic gas (maybe not because zero-gas transactions are ok)
And it looks likely that RPC `estimateGas` should:
- Not return zero when an account has insufficient balance
- Maybe use a different gas cost estimate when one isn't supplied in the RPC
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-03 06:58:45 +00:00
|
|
|
func rpcIntrinsicGas(call: RpcCallData, fork: Fork): GasInt =
|
|
|
|
var intrinsicGas = call.data.intrinsicGas(fork)
|
|
|
|
if call.contractCreation:
|
|
|
|
intrinsicGas = intrinsicGas + gasFees[fork][GasTXCreate]
|
|
|
|
return intrinsicGas
|
|
|
|
|
|
|
|
func rpcValidateCall(call: RpcCallData, vmState: BaseVMState, gasLimit: GasInt,
|
|
|
|
fork: Fork, intrinsicGas: var GasInt, gasCost: var UInt256): bool =
|
|
|
|
# This behaviour matches `validateTransaction`, used by `processTransaction`.
|
|
|
|
if vmState.cumulativeGasUsed + gasLimit > vmState.blockHeader.gasLimit:
|
|
|
|
return false
|
|
|
|
let balance = vmState.readOnlyStateDB.getBalance(call.source)
|
|
|
|
gasCost = gasLimit.u256 * call.gasPrice.u256
|
|
|
|
if gasCost > balance or call.value > balance - gasCost:
|
|
|
|
return false
|
|
|
|
intrinsicGas = rpcIntrinsicGas(call, fork)
|
|
|
|
if intrinsicGas > gasLimit:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc rpcInitialAccessListEIP2929(call: RpcCallData, vmState: BaseVMState, fork: Fork) =
|
|
|
|
# EIP2929 initial access list.
|
|
|
|
if fork >= FkBerlin:
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.accessList(call.source)
|
|
|
|
# For contract creations the EVM will add the contract address to the
|
|
|
|
# access list itself, after calculating the new contract address.
|
|
|
|
if not call.contractCreation:
|
|
|
|
db.accessList(call.to)
|
|
|
|
for c in activePrecompiles():
|
|
|
|
db.accessList(c)
|
|
|
|
|
2021-05-03 08:08:11 +00:00
|
|
|
proc rpcEstimateGas*(call: RpcCallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): GasInt =
|
|
|
|
# TODO: handle revert and error
|
|
|
|
var
|
|
|
|
# we use current header stateRoot, unlike block validation
|
|
|
|
# which use previous block stateRoot
|
|
|
|
vmState = newBaseVMState(header.stateRoot, header, chain)
|
|
|
|
fork = toFork(chain.config, header.blockNumber)
|
RPC: Change rpcEstimateGas to use the EVM without a fake transaction
Change `rpcEstimateGas` to setup and execute a computation directly, in a
similar way to `rpcDoCall` and `rpcMakeCall`, instead of constructing a fake
transaction and then validating it.
This patch does not (or should not) change any behaviour.
Although this looks a bit messy as it duplicates parts of `validateTransaction`
and `processTransaction`, proc names have been used to hopefully keep the
meanings clear, and it's just a stepping stone as those transaction functions
will be changed next. Also the behaviour of RPC `estimateGas` may not be
correct (although this patch is careful not to change it), so it's good to make
it explicit so we can see how it differs from other RPCs.
Doing this change exposed some interesting behaviour differences between the
`call` RPC and `estimateGas` RPC, which may be bugs, or may be intentional.
These differences are now obvious and explicit.
The unclear areas are not well documented by any of the clients, even Infura
which says a bit more than the others. So to find out if they are intended,
we'll have to run tests against other Ethereum services.
Guessing, on the face of it, it looks likely that RPC `call` should:
- Setup EIP2929 access lists
- Account for intrinsic gas (maybe not because zero-gas transactions are ok)
And it looks likely that RPC `estimateGas` should:
- Not return zero when an account has insufficient balance
- Maybe use a different gas cost estimate when one isn't supplied in the RPC
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-03 06:58:45 +00:00
|
|
|
gasLimit = if haveGasLimit: call.gas else: header.gasLimit - vmState.cumulativeGasUsed
|
|
|
|
intrinsicGas: GasInt
|
|
|
|
gasCost: UInt256
|
|
|
|
|
|
|
|
# Nimbus `estimateGas` has historically checked against remaining gas in the
|
|
|
|
# current block, balance in the sender account (even if the sender is default
|
|
|
|
# account 0x00), and other limits, and returned 0 as the gas estimate if any
|
|
|
|
# checks failed. This behaviour came from how it used `processTransaction`
|
|
|
|
# which calls `validateTransaction`. For now, keep this behaviour the same.
|
|
|
|
# Compare this code with `validateTransaction`.
|
|
|
|
#
|
|
|
|
# TODO: This historically differs from `rpcDoCall` and `rpcMakeCall`. There
|
|
|
|
# are other differences in rpc_utils.nim `callData` too. Are the different
|
|
|
|
# behaviours intended, and is 0 the correct return value to mean "not enough
|
|
|
|
# gas to start"? Probably not.
|
|
|
|
if not rpcValidateCall(call, vmState, gasLimit, fork, intrinsicGas, gasCost):
|
|
|
|
return 0
|
2021-05-03 08:08:11 +00:00
|
|
|
|
|
|
|
var dbTx = chain.db.beginTransaction()
|
|
|
|
defer: dbTx.dispose()
|
RPC: Change rpcEstimateGas to use the EVM without a fake transaction
Change `rpcEstimateGas` to setup and execute a computation directly, in a
similar way to `rpcDoCall` and `rpcMakeCall`, instead of constructing a fake
transaction and then validating it.
This patch does not (or should not) change any behaviour.
Although this looks a bit messy as it duplicates parts of `validateTransaction`
and `processTransaction`, proc names have been used to hopefully keep the
meanings clear, and it's just a stepping stone as those transaction functions
will be changed next. Also the behaviour of RPC `estimateGas` may not be
correct (although this patch is careful not to change it), so it's good to make
it explicit so we can see how it differs from other RPCs.
Doing this change exposed some interesting behaviour differences between the
`call` RPC and `estimateGas` RPC, which may be bugs, or may be intentional.
These differences are now obvious and explicit.
The unclear areas are not well documented by any of the clients, even Infura
which says a bit more than the others. So to find out if they are intended,
we'll have to run tests against other Ethereum services.
Guessing, on the face of it, it looks likely that RPC `call` should:
- Setup EIP2929 access lists
- Account for intrinsic gas (maybe not because zero-gas transactions are ok)
And it looks likely that RPC `estimateGas` should:
- Not return zero when an account has insufficient balance
- Maybe use a different gas cost estimate when one isn't supplied in the RPC
Signed-off-by: Jamie Lokier <jamie@shareable.org>
2021-05-03 06:58:45 +00:00
|
|
|
|
|
|
|
# TODO: EIP2929 setup also historically differs from `rpcDoCall` and `rpcMakeCall`.
|
|
|
|
rpcInitialAccessListEIP2929(call, vmState, fork)
|
|
|
|
|
|
|
|
# TODO: Deduction of `intrinsicGas` also differs from `rpcDoCall` and `rpcMakeCall`.
|
|
|
|
var c = rpcSetupComputation(vmState, call, fork, gasLimit - intrinsicGas)
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.subBalance(call.source, gasCost)
|
|
|
|
|
|
|
|
execComputation(c)
|
|
|
|
|
|
|
|
if c.shouldBurnGas:
|
|
|
|
return gasLimit
|
|
|
|
let maxRefund = (gasLimit - c.gasMeter.gasRemaining) div 2
|
|
|
|
let refund = min(c.getGasRefund(), maxRefund)
|
|
|
|
return gasLimit - c.gasMeter.gasRemaining - refund
|