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

Commit

Permalink
evm: check height overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Sep 28, 2021
1 parent bc82f3f commit d49a0a6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
10 changes: 10 additions & 0 deletions types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"bytes"
math "math"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -27,3 +28,12 @@ func ValidateAddress(address string) error {
}
return nil
}

// SafeInt64 checks for overflows while casting a uint64 to int64 value.
func SafeInt64(value uint64) (int64, error) {
if value > uint64(math.MaxInt64) {
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "uint64 value %v cannot exceed %v", value, int64(math.MaxInt64))
}

return int64(value), nil
}
58 changes: 58 additions & 0 deletions types/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/tharsis/ethermint/tests"
)

func TestIsEmptyHash(t *testing.T) {
Expand Down Expand Up @@ -52,3 +53,60 @@ func TestIsZeroAddress(t *testing.T) {
require.Equal(t, tc.expEmpty, IsZeroAddress(tc.address), tc.name)
}
}

func TestValidateAddress(t *testing.T) {
testCases := []struct {
name string
address string
expError bool
}{
{
"empty string", "", true,
},
{
"invalid address", "0x", true,
},
{
"zero address", common.Address{}.String(), false,
},
{
"valid address", tests.GenerateAddress().Hex(), false,
},
}

for _, tc := range testCases {
err := ValidateAddress(tc.address)

if tc.expError {
require.Error(t, err, tc.name)
} else {
require.NoError(t, err, tc.name)
}
}
}

func TestSafeInt64(t *testing.T) {
testCases := []struct {
name string
value uint64
expError bool
}{
{
"no overflow", 10, false,
},
{
"overflow", 18446744073709551615, true,
},
}

for _, tc := range testCases {
value, err := SafeInt64(tc.value)
if tc.expError {
require.Error(t, err, tc.name)
continue
}

require.NoError(t, err, tc.name)
require.Equal(t, int64(tc.value), value, tc.name)
}
}
8 changes: 7 additions & 1 deletion x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ func (k Keeper) VMConfig(msg core.Message, params types.Params, tracer vm.Tracer
// 3. The requested height is from a height greater than the latest one
func (k Keeper) GetHashFn() vm.GetHashFunc {
return func(height uint64) common.Hash {
h := int64(height)
ctx := k.Ctx()

h, err := ethermint.SafeInt64(height)
if err != nil {
k.Logger(ctx).Error("failed to cast height to int64", "error", err)
return common.Hash{}
}

switch {
case ctx.BlockHeight() == h:
// Case 1: The requested height matches the one from the context so we can retrieve the header
Expand Down

0 comments on commit d49a0a6

Please sign in to comment.