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

Problem: handle for integer overflow is messy #517

Merged
merged 1 commit into from
Sep 10, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ante) [#497](https://github.com/crypto-org-chain/ethermint/pull/497) Enforce positive value check in eth transaction.
* (deps) [#505](https://github.com/crypto-org-chain/ethermint/pull/505) Update cometbft to v0.38.10.
* (ante) [#504](https://github.com/crypto-org-chain/ethermint/pull/504) Optimize AnteHandle method to skip checks if disabledMsgs is empty.
* [#517](https://github.com/crypto-org-chain/ethermint/pull/517) Add check for integer overflow to ensure safe conversion.

## v0.21.x-cronos

Expand Down
9 changes: 6 additions & 3 deletions app/ante/fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@
}

feeCoins := feeTx.GetFee()
gas := feeTx.GetGas()
gas, err := ethermint.SafeInt64(feeTx.GetGas())
if err != nil {
return nil, 0, err

Check warning on line 123 in app/ante/fee_checker.go

View check run for this annotation

Codecov / codecov/patch

app/ante/fee_checker.go#L123

Added line #L123 was not covered by tests
}
minGasPrices := ctx.MinGasPrices()

// Ensure that the provided fees meet a minimum threshold for the validator,
Expand All @@ -129,7 +132,7 @@

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
glDec := sdkmath.LegacyNewDec(int64(gas))
glDec := sdkmath.LegacyNewDec(gas)

for i, gp := range minGasPrices {
fee := gp.Amount.Mul(glDec)
Expand All @@ -141,7 +144,7 @@
}
}

priority := getTxPriority(feeCoins, int64(gas))
priority := getTxPriority(feeCoins, gas)
return feeCoins, priority, nil
}

Expand Down
4 changes: 2 additions & 2 deletions ethereum/eip712/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
)

// createEIP712Domain creates the typed data domain for the given chainID.
func createEIP712Domain(chainID uint64) apitypes.TypedDataDomain {
func createEIP712Domain(chainID int64) apitypes.TypedDataDomain {
domain := apitypes.TypedDataDomain{
Name: "Cosmos Web3",
Version: "1.0.0",
ChainId: math.NewHexOrDecimal256(int64(chainID)), // #nosec G701
ChainId: math.NewHexOrDecimal256(chainID),
VerifyingContract: "cosmos",
Salt: "0",
}
Expand Down
7 changes: 6 additions & 1 deletion ethereum/eip712/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import (
"github.com/ethereum/go-ethereum/signer/core/apitypes"
ethermint "github.com/evmos/ethermint/types"
)

// WrapTxToTypedData wraps an Amino-encoded Cosmos Tx JSON SignDoc
Expand All @@ -36,7 +37,11 @@
return apitypes.TypedData{}, err
}

domain := createEIP712Domain(chainID)
value, err := ethermint.SafeInt64(chainID)
if err != nil {
return apitypes.TypedData{}, err

Check warning on line 42 in ethereum/eip712/eip712.go

View check run for this annotation

Codecov / codecov/patch

ethereum/eip712/eip712.go#L42

Added line #L42 was not covered by tests
}
domain := createEIP712Domain(value)

typedData := apitypes.TypedData{
Types: types,
Expand Down
7 changes: 6 additions & 1 deletion ethereum/eip712/eip712_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
ethermint "github.com/evmos/ethermint/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
Expand All @@ -52,6 +53,10 @@
data []byte,
feeDelegation *FeeDelegationOptions,
) (apitypes.TypedData, error) {
value, err := ethermint.SafeInt64(chainID)
if err != nil {
return apitypes.TypedData{}, err

Check warning on line 58 in ethereum/eip712/eip712_legacy.go

View check run for this annotation

Codecov / codecov/patch

ethereum/eip712/eip712_legacy.go#L58

Added line #L58 was not covered by tests
}
txData := make(map[string]interface{})

if err := json.Unmarshal(data, &txData); err != nil {
Expand All @@ -61,7 +66,7 @@
domain := apitypes.TypedDataDomain{
Name: "Cosmos Web3",
Version: "1.0.0",
ChainId: math.NewHexOrDecimal256(int64(chainID)),
ChainId: math.NewHexOrDecimal256(value),
VerifyingContract: "cosmos",
Salt: "0",
}
Expand Down
14 changes: 11 additions & 3 deletions indexer/kv_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
// record index of valid eth tx during the iteration
var ethTxIndex int32
for txIndex, tx := range block.Txs {
txIdx, err := ethermint.SafeUint32(txIndex)
if err != nil {
return err

Check warning on line 75 in indexer/kv_indexer.go

View check run for this annotation

Codecov / codecov/patch

indexer/kv_indexer.go#L75

Added line #L75 was not covered by tests
}
result := txResults[txIndex]
if !rpctypes.TxSuccessOrExceedsBlockGasLimit(result) {
continue
Expand All @@ -93,13 +97,17 @@

var cumulativeGasUsed uint64
for msgIndex, msg := range tx.GetMsgs() {
msgIdx, err := ethermint.SafeUint32(msgIndex)
if err != nil {
return err

Check warning on line 102 in indexer/kv_indexer.go

View check run for this annotation

Codecov / codecov/patch

indexer/kv_indexer.go#L102

Added line #L102 was not covered by tests
}
ethMsg := msg.(*evmtypes.MsgEthereumTx)
var txHash common.Hash

txResult := ethermint.TxResult{
Height: height,
TxIndex: uint32(txIndex),
MsgIndex: uint32(msgIndex),
TxIndex: txIdx,
MsgIndex: msgIdx,
EthTxIndex: ethTxIndex,
}
if result.Code != abci.CodeTypeOK {
Expand Down Expand Up @@ -243,5 +251,5 @@
return 0, fmt.Errorf("wrong tx index key length, expect: %d, got: %d", TxIndexKeyLength, len(key))
}

return int64(sdk.BigEndianToUint64(key[1:9])), nil
return ethermint.SafeInt64(sdk.BigEndianToUint64(key[1:9]))
}
15 changes: 8 additions & 7 deletions rpc/backend/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package backend

import (
"fmt"
"math"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand All @@ -29,6 +27,7 @@
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
rpctypes "github.com/evmos/ethermint/rpc/types"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -74,11 +73,10 @@
return nil, err
}

if bn > math.MaxInt64 {
return nil, fmt.Errorf("not able to query block number greater than MaxInt64")
height, err = ethermint.SafeHexToInt64(bn)
if err != nil {
return nil, err

Check warning on line 78 in rpc/backend/account_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/account_info.go#L76-L78

Added lines #L76 - L78 were not covered by tests
}

height = int64(bn)
}

clientCtx := b.clientCtx.WithHeight(height)
Expand Down Expand Up @@ -195,8 +193,11 @@
if err != nil {
return &n, err
}
currentHeight, err := ethermint.SafeHexToInt64(bn)
if err != nil {
return nil, err

Check warning on line 198 in rpc/backend/account_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/account_info.go#L198

Added line #L198 was not covered by tests
}
height := blockNum.Int64()
currentHeight := int64(bn)
if height > currentHeight {
return &n, errorsmod.Wrapf(
sdkerrors.ErrInvalidHeight,
Expand Down
6 changes: 5 additions & 1 deletion rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
rpctypes "github.com/evmos/ethermint/rpc/types"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -179,7 +180,10 @@
if err != nil {
return nil, err
}
height = int64(n)
height, err = ethermint.SafeHexToInt64(n)
if err != nil {
return nil, err

Check warning on line 185 in rpc/backend/blocks.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/blocks.go#L185

Added line #L185 was not covered by tests
}
}
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
if err != nil {
Expand Down
23 changes: 17 additions & 6 deletions rpc/backend/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,25 @@
return nil, fmt.Errorf("%w: #%d:%f > #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p)
}
}
blockNumber, err := b.BlockNumber()
blkNumber, err := b.BlockNumber()
if err != nil {
return nil, err
}
blockNumber, err := ethermint.SafeHexToInt64(blkNumber)
if err != nil {
return nil, err
}
blockEnd := int64(lastBlock)
if blockEnd < 0 {
blockEnd = int64(blockNumber)
} else if int64(blockNumber) < blockEnd {
return nil, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, blockEnd, int64(blockNumber))
blockEnd = blockNumber
} else if blockNumber < blockEnd {
return nil, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, blockEnd, blockNumber)

Check warning on line 197 in rpc/backend/chain_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/chain_info.go#L197

Added line #L197 was not covered by tests
}

blocks := int64(userBlockCount)
blocks, err := ethermint.SafeInt64(uint64(userBlockCount))
if err != nil {
return nil, err

Check warning on line 202 in rpc/backend/chain_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/chain_info.go#L202

Added line #L202 was not covered by tests
}
maxBlockCount := int64(b.cfg.JSONRPC.FeeHistoryCap)
if blocks > maxBlockCount {
return nil, fmt.Errorf("FeeHistory user block count %d higher than %d", blocks, maxBlockCount)
Expand Down Expand Up @@ -226,6 +233,10 @@
if blockID+int64(i) >= blockEnd+1 {
break
}
value := blockID - blockStart + int64(i)
if value > math.MaxInt32 || value < math.MinInt32 {
return nil, fmt.Errorf("integer overflow: calculated value %d exceeds int32 limits", value)

Check warning on line 238 in rpc/backend/chain_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/chain_info.go#L238

Added line #L238 was not covered by tests
}
wg.Add(1)
go func(index int32) {
defer func() {
Expand Down Expand Up @@ -282,7 +293,7 @@
}
}
}
}(int32(blockID - blockStart + int64(i)))
}(int32(value)) //nolint:gosec // checked
}
go func() {
wg.Wait()
Expand Down
8 changes: 6 additions & 2 deletions rpc/backend/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
rpctypes "github.com/evmos/ethermint/rpc/types"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/pkg/errors"
)
Expand All @@ -48,9 +49,12 @@
b.logger.Debug("block not found", "height", transaction.Height)
return nil, err
}

total, err := ethermint.SafeUint32(len(blk.Block.Txs))
if err != nil {
return nil, err

Check warning on line 54 in rpc/backend/tracing.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tracing.go#L54

Added line #L54 was not covered by tests
}
// check tx index is not out of bound
if uint32(len(blk.Block.Txs)) < transaction.TxIndex {
if total < transaction.TxIndex {
b.logger.Debug("tx index out of bounds", "index", transaction.TxIndex, "hash", hash.String(), "height", blk.Block.Height)
return nil, fmt.Errorf("transaction not included in block %v", blk.Block.Height)
}
Expand Down
31 changes: 25 additions & 6 deletions rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@
// Fallback to find tx index by iterating all valid eth transactions
msgs := b.EthMsgsFromTendermintBlock(block, blockRes)
for i := range msgs {
idx, err := ethermint.SafeIntToInt32(i)
if err != nil {
return nil, err

Check warning on line 70 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L68-L70

Added lines #L68 - L70 were not covered by tests
}
if msgs[i].Hash() == txHash {
res.EthTxIndex = int32(i)
res.EthTxIndex = idx

Check warning on line 73 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L73

Added line #L73 was not covered by tests
break
}
}
Expand Down Expand Up @@ -209,8 +213,12 @@
// Fallback to find tx index by iterating all valid eth transactions
msgs := b.EthMsgsFromTendermintBlock(resBlock, blockRes)
for i := range msgs {
idx, err := ethermint.SafeIntToInt32(i)
if err != nil {
return nil, err

Check warning on line 218 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L216-L218

Added lines #L216 - L218 were not covered by tests
}
if msgs[i].Hash() == hash {
res.EthTxIndex = int32(i)
res.EthTxIndex = idx

Check warning on line 221 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L221

Added line #L221 was not covered by tests
break
}
}
Expand Down Expand Up @@ -328,9 +336,17 @@
}

// GetTxByTxIndex uses `/tx_query` to find transaction by tx index of valid ethereum txs
func (b *Backend) GetTxByTxIndex(height int64, index uint) (*ethermint.TxResult, error) {
func (b *Backend) GetTxByTxIndex(height int64, i uint) (*ethermint.TxResult, error) {
index, err := ethermint.SafeUintToInt32(i)
if err != nil {
return nil, err

Check warning on line 342 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L342

Added line #L342 was not covered by tests
}
idx, err := ethermint.SafeInt(i)
if err != nil {
return nil, err

Check warning on line 346 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L346

Added line #L346 was not covered by tests
}
if b.indexer != nil {
return b.indexer.GetByBlockAndIndex(height, int32(index))
return b.indexer.GetByBlockAndIndex(height, index)
}

// fallback to tendermint tx indexer
Expand All @@ -339,7 +355,7 @@
evmtypes.AttributeKeyTxIndex, index,
)
txResult, err := b.queryTendermintTxIndexer(query, func(txs *rpctypes.ParsedTxs) *rpctypes.ParsedTx {
return txs.GetTxByTxIndex(int(index))
return txs.GetTxByTxIndex(idx)
})
if err != nil {
return nil, errorsmod.Wrapf(err, "GetTxByTxIndex %d %d", height, index)
Expand Down Expand Up @@ -398,7 +414,10 @@
return nil, nil
}
} else {
i := int(idx)
i, err := ethermint.SafeHexToInt(idx)
if err != nil {
return nil, err

Check warning on line 419 in rpc/backend/tx_info.go

View check run for this annotation

Codecov / codecov/patch

rpc/backend/tx_info.go#L419

Added line #L419 was not covered by tests
}
ethMsgs := b.EthMsgsFromTendermintBlock(block, blockRes)
if i >= len(ethMsgs) {
b.logger.Debug("block txs index out of bound", "index", i)
Expand Down
Loading
Loading