Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix cumulativeGasUsed and gasUsed
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jan 14, 2022
1 parent 26b5909 commit 2609f40
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 14 additions & 5 deletions rpc/ethereum/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions rpc/ethereum/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,22 @@ 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
attrs := make(map[string]string, len(event.Attributes))
for _, attr := range event.Attributes {
attrs[string(attr.Key)] = string(attr.Value)
}
return counter, attrs
return msgIndex, attrs
}
counter++
}
// not found
return -1, nil
Expand All @@ -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")
}
Expand All @@ -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--
Expand Down

0 comments on commit 2609f40

Please sign in to comment.