diff --git a/CHANGELOG.md b/CHANGELOG.md index fadc2f9a..92c8104f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes * (evm) [\#661](https://github.com/cosmos/ethermint/pull/661) Set nonce to the EVM account on genesis initialization. +* (rpc) [\#648](https://github.com/cosmos/ethermint/issues/648) Fix block cumulative gas used value. * (evm) [\#621](https://github.com/cosmos/ethermint/issues/621) EVM `GenesisAccount` fields now share the same format as the auth module `Account`. * (evm) [\#618](https://github.com/cosmos/ethermint/issues/618) Add missing EVM `Context` `GetHash` field that retrieves a the header hash from a given block height. * (app) [\#617](https://github.com/cosmos/ethermint/issues/617) Fix genesis export functionality. diff --git a/rpc/namespaces/eth/api.go b/rpc/namespaces/eth/api.go index 66104dd1..57e8a143 100644 --- a/rpc/namespaces/eth/api.go +++ b/rpc/namespaces/eth/api.go @@ -677,6 +677,11 @@ func (api *PublicEthereumAPI) GetTransactionReceipt(hash common.Hash) (map[strin return nil, err } + cumulativeGasUsed := uint64(tx.TxResult.GasUsed) + if tx.Index != 0 { + cumulativeGasUsed += rpctypes.GetBlockCumulativeGas(api.clientCtx.Codec, block.Block, int(tx.Index)) + } + // Set status codes based on tx result var status hexutil.Uint if tx.TxResult.IsOK() { @@ -699,7 +704,7 @@ func (api *PublicEthereumAPI) GetTransactionReceipt(hash common.Hash) (map[strin receipt := map[string]interface{}{ // Consensus fields: These fields are defined by the Yellow Paper "status": status, - "cumulativeGasUsed": nil, // ignore until needed + "cumulativeGasUsed": hexutil.Uint64(cumulativeGasUsed), "logsBloom": data.Bloom, "logs": data.Logs, @@ -715,7 +720,7 @@ func (api *PublicEthereumAPI) GetTransactionReceipt(hash common.Hash) (map[strin "blockNumber": hexutil.Uint64(tx.Height), "transactionIndex": hexutil.Uint64(tx.Index), - // sender and receiver (contract or EOA) addreses + // sender and receiver (contract or EOA) addresses "from": from, "to": ethTx.To(), } diff --git a/rpc/types/utils.go b/rpc/types/utils.go index e1bbaec7..f5a1ed67 100644 --- a/rpc/types/utils.go +++ b/rpc/types/utils.go @@ -10,7 +10,9 @@ import ( tmtypes "github.com/tendermint/tendermint/types" clientcontext "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/ethermint/crypto/ethsecp256k1" evmtypes "github.com/cosmos/ethermint/x/evm/types" @@ -189,3 +191,26 @@ func GetKeyByAddress(keys []ethsecp256k1.PrivKey, address common.Address) (key * } return nil, false } + +// GetBlockCumulativeGas returns the cumulative gas used on a block up to a given +// transaction index. The returned gas used includes the gas from both the SDK and +// EVM module transactions. +func GetBlockCumulativeGas(cdc *codec.Codec, block *tmtypes.Block, idx int) uint64 { + var gasUsed uint64 + txDecoder := evmtypes.TxDecoder(cdc) + + for i := 0; i < idx && i < len(block.Txs[i]); i++ { + txi, err := txDecoder(block.Txs[i]) + if err != nil { + continue + } + + switch tx := txi.(type) { + case authtypes.StdTx: + gasUsed += tx.GetGas() + case evmtypes.MsgEthereumTx: + gasUsed += tx.GetGas() + } + } + return gasUsed +}