Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
rpc: fix cumulative gas (#662)
Browse files Browse the repository at this point in the history
* rpc: fix cumulative gas

* changelog
  • Loading branch information
fedekunze committed Dec 15, 2020
1 parent a1386ee commit 602e61a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,

Expand All @@ -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(),
}
Expand Down
25 changes: 25 additions & 0 deletions rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 602e61a

Please sign in to comment.