modify estimateGas in rpc_utils.nim and it can be reusable for graphql too

This commit is contained in:
jangko 2021-04-24 10:56:22 +07:00
parent 82d4f6a2d4
commit 3dfc1501f0
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 12 additions and 12 deletions

View File

@ -274,7 +274,8 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB , server: RpcServer) =
let let
header = chain.headerFromTag(quantityTag) header = chain.headerFromTag(quantityTag)
callData = callData(call, false, chain) callData = callData(call, false, chain)
result = estimateGas(callData, header, chain, call.gas.isSome) gasUsed = estimateGas(callData, header, chain, call.gas.isSome)
result = encodeQuantity(gasUsed.uint64)
server.rpc("eth_getBlockByHash") do(data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]: server.rpc("eth_getBlockByHash") do(data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
## Returns information about a block by hash. ## Returns information about a block by hash.

View File

@ -24,13 +24,13 @@ type
contractCreation* {.rlpIgnore.}: bool contractCreation* {.rlpIgnore.}: bool
CallData* = object CallData* = object
source: EthAddress source*: EthAddress
to: EthAddress to*: EthAddress
gas: GasInt gas*: GasInt
gasPrice: GasInt gasPrice*: GasInt
value: UInt256 value*: UInt256
data: seq[byte] data*: seq[byte]
contractCreation: bool contractCreation*: bool
proc read(rlp: var Rlp, t: var UnsignedTx, _: type EthAddress): EthAddress {.inline.} = proc read(rlp: var Rlp, t: var UnsignedTx, _: type EthAddress): EthAddress {.inline.} =
if rlp.blobLen != 0: if rlp.blobLen != 0:
@ -188,7 +188,7 @@ proc callData*(call: EthCall, callMode: bool = true, chain: BaseChainDB): CallDa
if call.data.isSome: if call.data.isSome:
result.data = hexToSeqByte(call.data.get.string) result.data = hexToSeqByte(call.data.get.string)
proc setupComputation(vmState: BaseVMState, call: CallData, fork: Fork) : Computation = proc setupComputation*(vmState: BaseVMState, call: CallData, fork: Fork) : Computation =
vmState.setupTxContext( vmState.setupTxContext(
origin = call.source, origin = call.source,
gasPrice = call.gasPrice, gasPrice = call.gasPrice,
@ -221,7 +221,7 @@ proc doCall*(call: CallData, header: BlockHeader, chain: BaseChainDB): HexDataSt
# TODO: handle revert and error # TODO: handle revert and error
# TODO: handle contract ABI # TODO: handle contract ABI
proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): HexQuantityStr = proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): GasInt =
var var
# we use current header stateRoot, unlike block validation # we use current header stateRoot, unlike block validation
# which use previous block stateRoot # which use previous block stateRoot
@ -239,8 +239,7 @@ proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveG
var dbTx = chain.db.beginTransaction() var dbTx = chain.db.beginTransaction()
defer: dbTx.dispose() defer: dbTx.dispose()
let gasUsed = processTransaction(tx, call.source, vmState, fork) result = processTransaction(tx, call.source, vmState, fork)
result = encodeQuantity(gasUsed.uint64)
dbTx.dispose() dbTx.dispose()
# TODO: handle revert and error # TODO: handle revert and error