mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-03 15:55:47 +00:00
Initial commit for eth_estimateGas
This commit is contained in:
parent
ac9fb37465
commit
3f1122702e
@ -14,7 +14,7 @@ import
|
||||
../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types,
|
||||
../vm_state_transactions, ../utils/addresses,
|
||||
../vm/[interpreter_dispatch, computation],
|
||||
rpc_types, rpc_utils, ../vm/[message, computation]
|
||||
rpc_types, rpc_utils, ../vm/[message, computation, interpreter_dispatch]
|
||||
|
||||
#[
|
||||
Note:
|
||||
@ -79,6 +79,52 @@ proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender:
|
||||
maxVal = midPoint
|
||||
result = minVal
|
||||
|
||||
proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress, gasPrice: GasInt, tolerance = 1): GasInt =
|
||||
proc dummyComputation(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress): BaseComputation =
|
||||
# Note that vmState may be altered
|
||||
setupComputation(
|
||||
vmState.blockHeader,
|
||||
vmState,
|
||||
transaction,
|
||||
sender)
|
||||
|
||||
proc dummyTransaction(gasLimit, gasPrice: GasInt, destination: EthAddress, value: UInt256): Transaction =
|
||||
Transaction(
|
||||
accountNonce: 0.AccountNonce,
|
||||
gasPrice: gasPrice,
|
||||
gasLimit: gasLimit,
|
||||
to: destination,
|
||||
value: value
|
||||
)
|
||||
var
|
||||
hiGas = vmState.gasLimit
|
||||
loGas = transaction.intrinsicGas
|
||||
gasPrice = transaction.gasPrice # TODO: Or zero?
|
||||
|
||||
proc tryTransaction(vmState: var BaseVMState, gasLimit: GasInt): bool =
|
||||
var
|
||||
spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value)
|
||||
computation = vmState.dummyComputation(spoofTransaction, sender)
|
||||
computation.executeOpcodes
|
||||
if not computation.isError:
|
||||
return true
|
||||
|
||||
if vmState.tryTransaction(loGas):
|
||||
return loGas
|
||||
if not vmState.tryTransaction(hiGas):
|
||||
return 0.GasInt # TODO: Reraise error from computation
|
||||
|
||||
var
|
||||
minVal = vmState.gasLimit
|
||||
maxVal = transaction.intrinsicGas
|
||||
while loGas - hiGas > tolerance:
|
||||
let midPoint = (loGas + hiGas) div 2
|
||||
if vmState.tryTransaction(midPoint):
|
||||
minVal = midPoint
|
||||
else:
|
||||
maxVal = midPoint
|
||||
result = minVal
|
||||
|
||||
proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||
|
||||
func getAccountDb(header: BlockHeader): ReadOnlyStateDB =
|
||||
|
Loading…
x
Reference in New Issue
Block a user