diff --git a/app/rpc/namespaces/eth/api.go b/app/rpc/namespaces/eth/api.go index 6611b73184..1e02c811ca 100644 --- a/app/rpc/namespaces/eth/api.go +++ b/app/rpc/namespaces/eth/api.go @@ -679,6 +679,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() { @@ -701,7 +706,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, @@ -717,7 +722,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/app/rpc/types/utils.go b/app/rpc/types/utils.go index a9a0f16641..fcce48f92c 100644 --- a/app/rpc/types/utils.go +++ b/app/rpc/types/utils.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "math/big" tmbytes "github.com/tendermint/tendermint/libs/bytes" @@ -15,6 +16,7 @@ import ( "github.com/okex/okexchain/app/crypto/ethsecp256k1" evmtypes "github.com/okex/okexchain/x/evm/types" + "github.com/cosmos/cosmos-sdk/codec" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/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++ { + 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 +} \ No newline at end of file