Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: fix cumulative gas #503

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,

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