From 2609f40c43e278f3e9815867c4c135b593bb9f24 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Fri, 14 Jan 2022 13:31:01 +0800 Subject: [PATCH] fix cumulativeGasUsed and gasUsed --- app/ante/eth.go | 4 ++-- rpc/ethereum/backend/backend.go | 2 +- rpc/ethereum/namespaces/eth/api.go | 19 ++++++++++++++----- rpc/ethereum/types/utils.go | 19 ++++++++++--------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/ante/eth.go b/app/ante/eth.go index b430acc2e6..f3457d4ef6 100644 --- a/app/ante/eth.go +++ b/app/ante/eth.go @@ -431,11 +431,11 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu fmt.Println("fee compare", authInfo.Fee.Amount, txFee) if !authInfo.Fee.Amount.IsEqual(txFee) { - return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid eth tx AuthInfo Fee Amount") + return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid AuthInfo Fee Amount (%s != %s)", authInfo.Fee.Amount, txFee) } if authInfo.Fee.GasLimit != txGasLimit { - return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid eth tx AuthInfo Fee GasLimit") + return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid AuthInfo Fee GasLimit (%d != %d)", authInfo.Fee.GasLimit, txGasLimit) } sigs := protoTx.Signatures diff --git a/rpc/ethereum/backend/backend.go b/rpc/ethereum/backend/backend.go index 445607b770..d209d94fa1 100644 --- a/rpc/ethereum/backend/backend.go +++ b/rpc/ethereum/backend/backend.go @@ -706,7 +706,7 @@ func (e *EVMBackend) GetTransactionByHash(txHash common.Hash) (*types.RPCTransac // Try to find txIndex from events found := false - txIndex, err := types.TxIndexFromAttributes(attrs) + txIndex, err := types.GetUint64Attribute(attrs, evmtypes.AttributeKeyTxIndex) if err == nil { found = true } else { diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index 8557300856..a35daead28 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -818,11 +818,20 @@ func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interfac return nil, nil } - for i := 0; i <= int(res.Index) && i < len(blockRes.TxsResults); i++ { + for i := 0; i < int(res.Index) && i < len(blockRes.TxsResults); i++ { cumulativeGasUsed += uint64(blockRes.TxsResults[i].GasUsed) } - for i := 0; i < msgIndex; i++ { - cumulativeGasUsed += rpctypes.AccumulateGasUsedBeforeMsg(res.TxResult.Events, msgIndex) + cumulativeGasUsed += rpctypes.AccumulativeGasUsedOfMsg(res.TxResult.Events, msgIndex) + + var gasUsed uint64 + if len(tx.GetMsgs()) == 1 { + // backward compatibility + gasUsed = uint64(res.TxResult.GasUsed) + } else { + gasUsed, err = rpctypes.GetUint64Attribute(attrs, evmtypes.AttributeKeyTxGasUsed) + if err != nil { + return nil, err + } } // Get the transaction result from the log @@ -847,7 +856,7 @@ func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interfac // Try to find txIndex from events found = false - txIndex, err := rpctypes.TxIndexFromAttributes(attrs) + txIndex, err := rpctypes.GetUint64Attribute(attrs, evmtypes.AttributeKeyTxIndex) if err == nil { found = true } else { @@ -876,7 +885,7 @@ func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interfac // They are stored in the chain database. "transactionHash": hash, "contractAddress": nil, - "gasUsed": hexutil.Uint64(res.TxResult.GasUsed), + "gasUsed": hexutil.Uint64(gasUsed), "type": hexutil.Uint(txData.TxType()), // Inclusion information: These fields provide information about the inclusion of the diff --git a/rpc/ethereum/types/utils.go b/rpc/ethereum/types/utils.go index 7a57adf041..afc2bfc86e 100644 --- a/rpc/ethereum/types/utils.go +++ b/rpc/ethereum/types/utils.go @@ -259,12 +259,13 @@ func BaseFeeFromEvents(events []abci.Event) *big.Int { // FindTxAttributes returns the msg index of the eth tx in cosmos tx, and the attributes, // returns -1 and nil if not found. func FindTxAttributes(events []abci.Event, txHash string) (int, map[string]string) { - var counter int + msgIndex := -1 for _, event := range events { if event.Type != evmtypes.EventTypeEthereumTx { continue } + msgIndex++ value := FindAttribute(event.Attributes, []byte(evmtypes.AttributeKeyEthereumTxHash)) if bytes.Equal(value, []byte(txHash)) { // convert attributes to map for later lookup @@ -272,9 +273,8 @@ func FindTxAttributes(events []abci.Event, txHash string) (int, map[string]strin for _, attr := range event.Attributes { attrs[string(attr.Key)] = string(attr.Value) } - return counter, attrs + return msgIndex, attrs } - counter++ } // not found return -1, nil @@ -291,9 +291,9 @@ func FindAttribute(attrs []abci.EventAttribute, key []byte) []byte { return nil } -// TxIndexFromAttributes parses the tx index from cosmos event attributes -func TxIndexFromAttributes(attrs map[string]string) (uint64, error) { - value, found := attrs[evmtypes.AttributeKeyTxIndex] +// GetUint64Attribute parses the uint64 value from event attributes +func GetUint64Attribute(attrs map[string]string, key string) (uint64, error) { + value, found := attrs[key] if !found { return 0, errors.New("tx index attribute not found") } @@ -306,16 +306,17 @@ func TxIndexFromAttributes(attrs map[string]string) (uint64, error) { return 0, errors.New("negative tx index") } return uint64(result), nil + } -// AccumulateGasUsedBeforeMsg accumulate the gas used by msgs before `msgIndex`. -func AccumulateGasUsedBeforeMsg(events []abci.Event, msgIndex int) (gasUsed uint64) { +// AccumulativeGasUsedOfMsg accumulate the gas used by msgs before `msgIndex`. +func AccumulativeGasUsedOfMsg(events []abci.Event, msgIndex int) (gasUsed uint64) { for _, event := range events { if event.Type != evmtypes.EventTypeEthereumTx { continue } - if msgIndex == 0 { + if msgIndex < 0 { break } msgIndex--