Skip to content

Commit

Permalink
upgrade go-ethereum (#141)
Browse files Browse the repository at this point in the history
* upgrade to go-ethereum 1.12

* upgrade to go-ethereum 1.13

fix CanTranfer args

* Update expected tracer test output to include instrinsic gas

ethereum/go-ethereum#26048

* fix go-ethereum spc

* always import types as ethermint

* Use Eq to compare uint256

* use uint256.NewInt(0) for clarity

* minimal linter fixes

* add gosec suppressions/fixes

* pass shanghai parameter

* more review feedback

* fix test output

subtracting from 0 balance should result in noop and zero result
  • Loading branch information
gartnera authored Nov 5, 2024
1 parent 8db76d7 commit 1ebf85a
Show file tree
Hide file tree
Showing 77 changed files with 875 additions and 626 deletions.
2 changes: 1 addition & 1 deletion app/ante/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewAuthzLimiterDecorator(disabledMsgTypes []string) AuthzLimiterDecorator {

func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if err := ald.checkDisabledMsgs(tx.GetMsgs(), false, 0); err != nil {
return ctx, errorsmod.Wrapf(errortypes.ErrUnauthorized, err.Error())
return ctx, errorsmod.Wrapf(errortypes.ErrUnauthorized, "%v", err)
}
return next(ctx, tx, simulate)
}
Expand Down
25 changes: 16 additions & 9 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package ante

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

Expand All @@ -24,14 +25,14 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/holiman/uint256"

ethermint "github.com/zeta-chain/ethermint/types"
"github.com/zeta-chain/ethermint/x/evm/keeper"
"github.com/zeta-chain/ethermint/x/evm/statedb"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

// EthAccountVerificationDecorator validates an account balance checks
Expand Down Expand Up @@ -94,7 +95,7 @@ func (avd EthAccountVerificationDecorator) AnteHandle(
"the sender is not EOA: address %s, codeHash <%s>", fromAddr, acct.CodeHash)
}

if err := keeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance), txData); err != nil {
if err := keeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance.ToBig()), txData); err != nil {
return ctx, errorsmod.Wrap(err, "failed to check sender balance")
}
}
Expand Down Expand Up @@ -156,6 +157,7 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
blockHeight := big.NewInt(ctx.BlockHeight())
homestead := ethCfg.IsHomestead(blockHeight)
istanbul := ethCfg.IsIstanbul(blockHeight)
shanghai := ethCfg.IsShanghai(blockHeight, 1)
var events sdk.Events

// Use the lowest priority of all the messages as the final one.
Expand Down Expand Up @@ -186,7 +188,7 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula

evmDenom := evmParams.GetEvmDenom()

fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, homestead, istanbul, ctx.IsCheckTx())
fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, homestead, istanbul, shanghai, ctx.IsCheckTx())
if err != nil {
return ctx, errorsmod.Wrapf(err, "failed to verify the fees")
}
Expand Down Expand Up @@ -258,7 +260,7 @@ func NewCanTransferDecorator(evmKeeper EVMKeeper) CanTransferDecorator {
func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
params := ctd.evmKeeper.GetParams(ctx)
ethCfg := params.ChainConfig.EthereumConfig(ctd.evmKeeper.ChainID())
signer := ethtypes.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))
signer := ethermint.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))

for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
Expand All @@ -283,11 +285,11 @@ func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
"base fee is supported but evm block context value is nil",
)
}
if coreMsg.GasFeeCap().Cmp(baseFee) < 0 {
if coreMsg.GasFeeCap.Cmp(baseFee) < 0 {
return ctx, errorsmod.Wrapf(
errortypes.ErrInsufficientFee,
"max fee per gas less than block base fee (%s < %s)",
coreMsg.GasFeeCap(), baseFee,
coreMsg.GasFeeCap, baseFee,
)
}
}
Expand All @@ -303,14 +305,19 @@ func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
stateDB := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes())))
evm := ctd.evmKeeper.NewEVM(ctx, coreMsg, cfg, evmtypes.NewNoOpTracer(), stateDB)

valueU256, isOverflow := uint256.FromBig(coreMsg.Value)
if isOverflow {
return ctx, fmt.Errorf("%v is not a valid uint256", coreMsg.Value)
}

// check that caller has enough balance to cover asset transfer for **topmost** call
// NOTE: here the gas consumed is from the context with the infinite gas meter
if coreMsg.Value().Sign() > 0 && !evm.Context.CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
if coreMsg.Value.Sign() > 0 && !evm.Context.CanTransfer(stateDB, coreMsg.From, valueU256) {
return ctx, errorsmod.Wrapf(
errortypes.ErrInsufficientFunds,
"failed to transfer %s from address %s using the EVM block context transfer function",
coreMsg.Value(),
coreMsg.From(),
coreMsg.Value,
coreMsg.From,
)
}
}
Expand Down
17 changes: 9 additions & 8 deletions app/ante/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/holiman/uint256"

"github.com/zeta-chain/ethermint/app/ante"
"github.com/zeta-chain/ethermint/server/config"
Expand Down Expand Up @@ -68,7 +69,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
"success new account",
tx,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
true,
Expand All @@ -80,7 +81,7 @@ func (suite AnteTestSuite) TestNewEthAccountVerificationDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
true,
Expand Down Expand Up @@ -241,7 +242,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
0,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
false, true,
0,
Expand All @@ -251,7 +252,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
0,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(1))
},
false, true,
Expand All @@ -262,7 +263,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
tx2GasLimit, // it's capped
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(10000000000000000000))
},
true, false,
Expand All @@ -273,7 +274,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
dynamicFeeTx,
tx2GasLimit, // it's capped
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(10000000000000000000))
},
true, false,
Expand All @@ -284,7 +285,7 @@ func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
dynamicFeeTx,
0, // for reCheckTX mode, gas limit should be set to 0
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithIsReCheckTx(true)
},
true, false,
Expand Down Expand Up @@ -378,7 +379,7 @@ func (suite AnteTestSuite) TestCanTransferDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
},
Expand Down
2 changes: 2 additions & 0 deletions app/ante/fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.FeeTx) (sdk.Coi

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
// #nosec G115 always in range
glDec := sdk.NewDec(int64(gas))

for i, gp := range minGasPrices {
Expand All @@ -132,6 +133,7 @@ func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.FeeTx) (sdk.Coi
}
}

// #nosec G115 always in range
priority := getTxPriority(feeCoins, int64(gas))
return feeCoins, priority, nil
}
Expand Down
2 changes: 1 addition & 1 deletion app/ante/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type EVMKeeper interface {
statedb.Keeper
DynamicFeeEVMKeeper

NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
NewEVM(ctx sdk.Context, msg *core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
Expand Down
1 change: 1 addition & 0 deletions app/ante/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (eeed EthEmitEventDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
ctx.EventManager().EmitEvent(sdk.NewEvent(
evmtypes.EventTypeEthereumTx,
sdk.NewAttribute(evmtypes.AttributeKeyEthereumTxHash, msgEthTx.Hash),
// #nosec G115 index always positive
sdk.NewAttribute(evmtypes.AttributeKeyTxIndex, strconv.FormatUint(txIndex+uint64(i), 10)),
))
}
Expand Down
3 changes: 2 additions & 1 deletion app/ante/sigs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante_test
import (
"math/big"

"github.com/holiman/uint256"
"github.com/zeta-chain/ethermint/tests"
"github.com/zeta-chain/ethermint/x/evm/statedb"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
Expand All @@ -17,7 +18,7 @@ func (suite AnteTestSuite) TestSignatures() {

acc := statedb.NewEmptyAccount()
acc.Nonce = 1
acc.Balance = big.NewInt(10000000000)
acc.Balance = uint256.NewInt(10000000000)

suite.app.EvmKeeper.SetAccount(suite.ctx, addr, *acc)
msgEthereumTx := evmtypes.NewTx(suite.app.EvmKeeper.ChainID(), 1, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil, nil, nil)
Expand Down
4 changes: 2 additions & 2 deletions app/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethermint "github.com/zeta-chain/ethermint/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
)

Expand All @@ -48,7 +48,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
blockNum := big.NewInt(ctx.BlockHeight())
signer := ethtypes.MakeSigner(ethCfg, blockNum)
signer := ethermint.MakeSigner(ethCfg, blockNum)

for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
Expand Down
2 changes: 1 addition & 1 deletion ethereum/eip712/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func createEIP712Domain(chainID uint64) apitypes.TypedDataDomain {
domain := apitypes.TypedDataDomain{
Name: "Cosmos Web3",
Version: "1.0.0",
ChainId: math.NewHexOrDecimal256(int64(chainID)), // #nosec G701
ChainId: math.NewHexOrDecimal256(int64(chainID)), // #nosec G701 G115
VerifyingContract: "cosmos",
Salt: "0",
}
Expand Down
5 changes: 3 additions & 2 deletions ethereum/eip712/eip712_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func LegacyWrapTxToTypedData(
}

domain := apitypes.TypedDataDomain{
Name: "Cosmos Web3",
Version: "1.0.0",
Name: "Cosmos Web3",
Version: "1.0.0",
// #nosec G115 chainID always positive
ChainId: math.NewHexOrDecimal256(int64(chainID)),
VerifyingContract: "cosmos",
Salt: "0",
Expand Down
8 changes: 4 additions & 4 deletions ethereum/eip712/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
txTypes "github.com/cosmos/cosmos-sdk/types/tx"

apitypes "github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/zeta-chain/ethermint/types"
ethermint "github.com/zeta-chain/ethermint/types"

"github.com/cosmos/cosmos-sdk/codec"
)
Expand All @@ -39,7 +39,7 @@ var (
// The process of unmarshaling SignDoc bytes into a SignDoc object requires having a codec
// populated with all relevant message types. As a result, we must call this method on app
// initialization with the app's encoding config.
func SetEncodingConfig(cfg types.EncodingConfig) {
func SetEncodingConfig(cfg ethermint.EncodingConfig) {
aminoCodec = cfg.Amino
protoCodec = codec.NewProtoCodec(cfg.InterfaceRegistry)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func decodeAminoSignDoc(signDocBytes []byte) (apitypes.TypedData, error) {
return apitypes.TypedData{}, err
}

chainID, err := types.ParseChainID(aminoDoc.ChainID)
chainID, err := ethermint.ParseChainID(aminoDoc.ChainID)
if err != nil {
return apitypes.TypedData{}, errors.New("invalid chain ID passed as argument")
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func decodeProtobufSignDoc(signDocBytes []byte) (apitypes.TypedData, error) {

signerInfo := authInfo.SignerInfos[0]

chainID, err := types.ParseChainID(signDoc.ChainId)
chainID, err := ethermint.ParseChainID(signDoc.ChainId)
if err != nil {
return apitypes.TypedData{}, fmt.Errorf("invalid chain ID passed as argument: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions ethereum/eip712/encoding_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
txTypes "github.com/cosmos/cosmos-sdk/types/tx"

apitypes "github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/zeta-chain/ethermint/types"
ethermint "github.com/zeta-chain/ethermint/types"
)

type aminoMessage struct {
Expand Down Expand Up @@ -108,7 +108,7 @@ func legacyDecodeAminoSignDoc(signDocBytes []byte) (apitypes.TypedData, error) {
FeePayer: feePayer,
}

chainID, err := types.ParseChainID(aminoDoc.ChainID)
chainID, err := ethermint.ParseChainID(aminoDoc.ChainID)
if err != nil {
return apitypes.TypedData{}, errors.New("invalid chain ID passed as argument")
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func legacyDecodeProtobufSignDoc(signDocBytes []byte) (apitypes.TypedData, error

signerInfo := authInfo.SignerInfos[0]

chainID, err := types.ParseChainID(signDoc.ChainId)
chainID, err := ethermint.ParseChainID(signDoc.ChainId)
if err != nil {
return apitypes.TypedData{}, fmt.Errorf("invalid chain ID passed as argument: %w", err)
}
Expand Down
Loading

0 comments on commit 1ebf85a

Please sign in to comment.