rpc: fix JSON-RPC gas used (#558)

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
davcrypto 2021-09-14 18:27:20 +08:00 committed by GitHub
parent f90117c982
commit 644ae50410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -177,13 +177,11 @@ func (e *EVMBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]i
return e.EthBlockFromTendermint(resBlock.Block, fullTx) return e.EthBlockFromTendermint(resBlock.Block, fullTx)
} }
// EthBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a given Tendermint block. // EthBlockFromTendermint returns a JSON-RPC compatible Ethereum block from a given Tendermint block and its block result.
func (e *EVMBackend) EthBlockFromTendermint( func (e *EVMBackend) EthBlockFromTendermint(
block *tmtypes.Block, block *tmtypes.Block,
fullTx bool, fullTx bool,
) (map[string]interface{}, error) { ) (map[string]interface{}, error) {
gasUsed := uint64(0)
ethRPCTxs := []interface{}{} ethRPCTxs := []interface{}{}
for i, txBz := range block.Txs { for i, txBz := range block.Txs {
@ -200,8 +198,6 @@ func (e *EVMBackend) EthBlockFromTendermint(
continue continue
} }
// Todo: gasUsed does not consider the refund gas so it is incorrect, we need to extract it from the result
gasUsed += ethMsg.GetGas()
hash := ethMsg.AsTransaction().Hash() hash := ethMsg.AsTransaction().Hash()
if !fullTx { if !fullTx {
ethRPCTxs = append(ethRPCTxs, hash) ethRPCTxs = append(ethRPCTxs, hash)
@ -267,6 +263,19 @@ func (e *EVMBackend) EthBlockFromTendermint(
if err != nil { if err != nil {
e.logger.Error("failed to query consensus params", "error", err.Error()) e.logger.Error("failed to query consensus params", "error", err.Error())
} }
resBlockResult, err := e.clientCtx.Client.BlockResults(e.ctx, &block.Height)
if err != nil {
e.logger.Debug("EthBlockFromTendermint block result not found", "height", block.Height, "error", err.Error())
return nil, err
}
gasUsed := uint64(0)
for _, txsResult := range resBlockResult.TxsResults {
gasUsed += uint64(txsResult.GetGasUsed())
}
formattedBlock := types.FormatBlock(block.Header, block.Size(), gasLimit, new(big.Int).SetUint64(gasUsed), ethRPCTxs, bloom, validatorAddr) formattedBlock := types.FormatBlock(block.Header, block.Size(), gasLimit, new(big.Int).SetUint64(gasUsed), ethRPCTxs, bloom, validatorAddr)
return formattedBlock, nil return formattedBlock, nil
} }