diff --git a/app/ante/account_type_test.go b/app/ante/account_type_test.go index 350c39b8..db71929e 100644 --- a/app/ante/account_type_test.go +++ b/app/ante/account_type_test.go @@ -33,32 +33,32 @@ func isEthPubKey(pubKey cryptotypes.PubKey) bool { _, ok := pubKey.(*ethsecp256k1.PubKey) return ok } +func (suite *AnteTestSuite) testCaseSetup(ctx sdk.Context, addr sdk.AccAddress, isBaseAccount bool) { + var acc authtypes.AccountI + suite.enableFeemarket = false + + if isBaseAccount { + acc = suite.app.AccountKeeper.NewAccountWithAddress(ctx, addr) + suite.Require().True(isCosmosAccount(acc), "account type should be BaseAccount") + } else { + acc = ethtypes.ProtoAccount() + suite.Require().NoError(acc.SetAddress(addr)) + suite.app.AccountKeeper.SetAccount(ctx, acc) + acc = suite.app.AccountKeeper.GetAccount(ctx, addr) + suite.Require().True(isEthAccount(acc), "account type should be EthAccount") + } + evmAddr := common.BytesToAddress(addr.Bytes()) + suite.Require().NoError(suite.app.EvmKeeper.SetBalance(ctx, evmAddr, big.NewInt(10000000000))) + + suite.app.FeemarketKeeper.SetBaseFee(ctx, big.NewInt(100)) +} // Checks that the account types are updated correctly -func (suite AnteTestSuite) TestAccountTypeAnteHandler() { - var acc authtypes.AccountI +func (suite *AnteTestSuite) TestAccountTypeAnteHandler() { to := tests.GenerateAddress() suite.SetupTest() // Setup will create the account as either a BaseAccount or an EthAccount (based on isBaseAccount) and set the balance - setup := func(ctx sdk.Context, addr sdk.AccAddress, isBaseAccount bool) { - suite.enableFeemarket = false - - if isBaseAccount { - acc = suite.app.AccountKeeper.NewAccountWithAddress(ctx, addr) - suite.Require().True(isCosmosAccount(acc), "account type should be BaseAccount") - } else { - acc = ethtypes.ProtoAccount() - suite.Require().NoError(acc.SetAddress(addr)) - suite.app.AccountKeeper.SetAccount(ctx, acc) - acc = suite.app.AccountKeeper.GetAccount(ctx, addr) - suite.Require().True(isEthAccount(acc), "account type should be EthAccount") - } - evmAddr := common.BytesToAddress(addr.Bytes()) - suite.app.EvmKeeper.SetBalance(ctx, evmAddr, big.NewInt(10000000000)) - - suite.app.FeemarketKeeper.SetBaseFee(ctx, big.NewInt(100)) - } testCases := []struct { name string // A name which is printed when the test fails @@ -177,7 +177,7 @@ func (suite AnteTestSuite) TestAccountTypeAnteHandler() { builder, privKey := tc.txPrivKeyFn() addr := sdk.AccAddress(privKey.PubKey().Address()) // Create the account for the test, which will be a BaseAccount or an EthAccount depending on startBaseAcc - setup(ctx, addr, tc.startBaseAcc) + suite.testCaseSetup(ctx, addr, tc.startBaseAcc) acc := suite.app.AccountKeeper.GetAccount(ctx, addr) if tc.signCosmosTx { diff --git a/app/ante/ante_test.go b/app/ante/ante_test.go index f391ccc4..cb05f4ed 100644 --- a/app/ante/ante_test.go +++ b/app/ante/ante_test.go @@ -16,7 +16,7 @@ import ( // These tests have been copied from ethermint but duplicated here to ensure that // our changes to the AnteHandler do not cause conflicts -func (suite AnteTestSuite) TestAnteHandler() { +func (suite *AnteTestSuite) TestAnteHandler() { var acc authtypes.AccountI addr, privKey := tests.NewAddrKey() to := tests.GenerateAddress() @@ -29,7 +29,7 @@ func (suite AnteTestSuite) TestAnteHandler() { suite.Require().NoError(acc.SetSequence(1)) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) - suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000)) + suite.Require().NoError(suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000))) suite.app.FeemarketKeeper.SetBaseFee(suite.ctx, big.NewInt(100)) } @@ -349,8 +349,9 @@ func (suite AnteTestSuite) TestAnteHandler() { amount := sdk.NewCoins(sdk.NewCoin(altheaconfig.BaseDenom, sdk.NewInt(20))) gas := uint64(200000) txBuilder := suite.CreateTestEIP712TxBuilderMsgSend(from, privKey, suite.ctx.ChainID(), gas, amount) + // nolint: exhaustruct sigsV2 := signing.SignatureV2{} - txBuilder.SetSignatures(sigsV2) + suite.Require().NoError(txBuilder.SetSignatures(sigsV2)) return txBuilder.GetTx() }, false, false, false, }, @@ -363,6 +364,7 @@ func (suite AnteTestSuite) TestAnteHandler() { txBuilder := suite.CreateTestEIP712TxBuilderMsgSend(from, privKey, suite.ctx.ChainID(), gas, amount) nonce, err := suite.app.AccountKeeper.GetSequence(suite.ctx, acc.GetAddress()) suite.Require().NoError(err) + // nolint: exhaustruct sigsV2 := signing.SignatureV2{ PubKey: privKey.PubKey(), Data: &signing.SingleSignatureData{ @@ -370,7 +372,7 @@ func (suite AnteTestSuite) TestAnteHandler() { }, Sequence: nonce - 1, } - txBuilder.SetSignatures(sigsV2) + suite.Require().NoError(txBuilder.SetSignatures(sigsV2)) return txBuilder.GetTx() }, false, false, false, }, @@ -383,6 +385,7 @@ func (suite AnteTestSuite) TestAnteHandler() { txBuilder := suite.CreateTestEIP712TxBuilderMsgSend(from, privKey, suite.ctx.ChainID(), gas, amount) nonce, err := suite.app.AccountKeeper.GetSequence(suite.ctx, acc.GetAddress()) suite.Require().NoError(err) + // nolint: exhaustruct sigsV2 := signing.SignatureV2{ PubKey: privKey.PubKey(), Data: &signing.SingleSignatureData{ @@ -390,7 +393,7 @@ func (suite AnteTestSuite) TestAnteHandler() { }, Sequence: nonce, } - txBuilder.SetSignatures(sigsV2) + suite.Require().NoError(txBuilder.SetSignatures(sigsV2)) return txBuilder.GetTx() }, false, false, false, }, diff --git a/app/ante/doc.go b/app/ante/doc.go deleted file mode 100644 index 73b56f74..00000000 --- a/app/ante/doc.go +++ /dev/null @@ -1,11 +0,0 @@ -/*Package ante defines the SDK auth module's AnteHandler as well as an internal -AnteHandler for an Ethereum transaction (i.e MsgEthereumTx). - -During CheckTx, the transaction is passed through a series of -pre-message execution validation checks such as signature and account -verification in addition to minimum fees being checked. Otherwise, during -DeliverTx, the transaction is simply passed to the EVM which will also -perform the same series of checks. The distinction is made in CheckTx to -prevent spam and DoS attacks. -*/ -package ante diff --git a/app/ante/eth_set_pubkey.go b/app/ante/eth_set_pubkey.go index 13885a81..590104bb 100644 --- a/app/ante/eth_set_pubkey.go +++ b/app/ante/eth_set_pubkey.go @@ -128,6 +128,7 @@ func recoverPubKey(config *params.ChainConfig, blockNumber *big.Int, ethTx *etht signer := ethtypes.NewEIP155Signer(config.ChainID) pubkeyUncompressed, err = eip155PubKey(config, ethTx, signer) case config.IsHomestead(blockNumber): + // nolint: exhaustruct signer := ethtypes.HomesteadSigner{} pubkeyUncompressed, err = homesteadPubKey(ethTx, signer) default: @@ -166,6 +167,7 @@ func eip2930PubKey(config *params.ChainConfig, tx *ethtypes.Transaction, signer switch tx.Type() { case LegacyTxType: if !tx.Protected() { + // nolint: exhaustruct return homesteadPubKey(tx, ethtypes.HomesteadSigner{}) } V = new(big.Int).Sub(V, new(big.Int).Mul(config.ChainID, big.NewInt(2))) @@ -191,6 +193,7 @@ func eip155PubKey(config *params.ChainConfig, tx *ethtypes.Transaction, signer e return nil, ErrTxTypeNotSupported } if !tx.Protected() { + // nolint: exhaustruct return homesteadPubKey(tx, ethtypes.HomesteadSigner{}) } if tx.ChainId().Cmp(config.ChainID) != 0 { diff --git a/app/ante/eth_set_pubkey_test.go b/app/ante/eth_set_pubkey_test.go index 20dc4c52..26e43cc4 100644 --- a/app/ante/eth_set_pubkey_test.go +++ b/app/ante/eth_set_pubkey_test.go @@ -7,39 +7,15 @@ import ( "github.com/cosmos/cosmos-sdk/client" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/ethereum/go-ethereum/common" "github.com/evmos/ethermint/tests" - ethtypes "github.com/evmos/ethermint/types" evmtypes "github.com/evmos/ethermint/x/evm/types" ) // Checks that the PubKey types are updated correctly, and that the new ante handler succeeds where the old one failed -func (suite AnteTestSuite) TestEthSetPubkeyHandler() { - var acc authtypes.AccountI +func (suite *AnteTestSuite) TestEthSetPubkeyHandler() { to := tests.GenerateAddress() suite.SetupTest() - // Setup will create the account as either a BaseAccount or an EthAccount (based on isBaseAccount) and set the balance - setup := func(ctx sdk.Context, addr sdk.AccAddress, isBaseAccount bool) { - suite.enableFeemarket = false - - if isBaseAccount { - acc = suite.app.AccountKeeper.NewAccountWithAddress(ctx, addr) - suite.Require().True(isCosmosAccount(acc), "account type should be BaseAccount") - } else { - acc = ethtypes.ProtoAccount() - suite.Require().NoError(acc.SetAddress(addr)) - suite.app.AccountKeeper.SetAccount(ctx, acc) - acc = suite.app.AccountKeeper.GetAccount(ctx, addr) - suite.Require().True(isEthAccount(acc), "account type should be EthAccount") - } - evmAddr := common.BytesToAddress(addr.Bytes()) - suite.app.EvmKeeper.SetBalance(ctx, evmAddr, big.NewInt(10000000000)) - - suite.app.FeemarketKeeper.SetBaseFee(ctx, big.NewInt(100)) - } - testCases := []struct { name string // A name which is printed when the test fails txPrivKeyFn func() (client.TxBuilder, cryptotypes.PrivKey) // generates a tx and privkey for the test @@ -157,7 +133,7 @@ func (suite AnteTestSuite) TestEthSetPubkeyHandler() { builder, privKey := tc.txPrivKeyFn() addr := sdk.AccAddress(privKey.PubKey().Address()) // Create the account for the test, which will be a BaseAccount or an EthAccount depending on startBaseAcc - setup(ctx, addr, tc.startBaseAcc) + suite.testCaseSetup(ctx, addr, tc.startBaseAcc) acc := suite.app.AccountKeeper.GetAccount(ctx, addr) if tc.signCosmosTx { @@ -180,7 +156,8 @@ func (suite AnteTestSuite) TestEthSetPubkeyHandler() { // Now check the old antehandler against the same Tx, see if it would create a pubkey ctx, _ = suite.ctx.CacheContext() - setup(ctx, addr, tc.startBaseAcc) + suite.testCaseSetup(ctx, addr, tc.startBaseAcc) + // nolint: errcheck _, _ = suite.oldAnteHandler(ctx, tx, false) acc = suite.app.AccountKeeper.GetAccount(ctx, addr) pubKey = acc.GetPubKey() diff --git a/app/ante/utils_test.go b/app/ante/utils_test.go index c40d9ef5..7266dc18 100644 --- a/app/ante/utils_test.go +++ b/app/ante/utils_test.go @@ -107,6 +107,7 @@ func (suite *AnteTestSuite) SetupTest() { return genesis }) + // nolint: exhaustruct suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 2, ChainID: "althea_417834-1", Time: time.Now().UTC()}) suite.ctx = suite.ctx.WithMinGasPrices(sdk.NewDecCoins(sdk.NewDecCoin(altheaconfig.BaseDenom, sdk.OneInt()))) suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(1000000000000000000)) @@ -116,12 +117,14 @@ func (suite *AnteTestSuite) SetupTest() { encodingConfig := encoding.MakeConfig(app.ModuleBasics) // We're using TestMsg amino encoding in some tests, so register it here. + + // nolint: exhaustruct encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil) + // nolint: exhaustruct suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) - // altheaconfig.SetBech32Prefixes(sdk.GetConfig()) - + // nolint: exhaustruct anteHandler := ante.NewAnteHandler(ante.HandlerOptions{ AccountKeeper: suite.app.AccountKeeper, BankKeeper: suite.app.BankKeeper, @@ -136,6 +139,7 @@ func (suite *AnteTestSuite) SetupTest() { suite.anteHandler = anteHandler // Also make a copy of the old Canto antehandler we were using to ensure that our changes fix the problem + // nolint: exhaustruct oldAnteHandler := cantoante.NewAnteHandler(cantoante.HandlerOptions{ AccountKeeper: suite.app.AccountKeeper, BankKeeper: suite.app.BankKeeper, @@ -154,6 +158,7 @@ func (suite *AnteTestSuite) SetupTest() { // DefaultConsensusParams defines the default Tendermint consensus params used in // EthermintApp testing. +// nolint: exhaustruct var DefaultConsensusParams = &abci.ConsensusParams{ Block: &abci.BlockParams{ MaxBytes: 200000, @@ -189,6 +194,7 @@ func Setup(isCheckTx bool, patchGenesis func(*althea.AltheaApp, althea.GenesisSt // Initialize the chain app.InitChain( + // nolint: exhaustruct abci.RequestInitChain{ ChainId: "althea_417834-1", Validators: []abci.ValidatorUpdate{}, @@ -202,14 +208,18 @@ func Setup(isCheckTx bool, patchGenesis func(*althea.AltheaApp, althea.GenesisSt } func TestAnteTestSuite(t *testing.T) { + // nolint: exhaustruct suite.Run(t, &AnteTestSuite{ enableLondonHF: true, }) } func (s *AnteTestSuite) NewEthermintPrivkey() *ethsecp256k1.PrivKey { - privkey, _ := ethsecp256k1.GenerateKey() - _, err := privkey.ToECDSA() + privkey, err := ethsecp256k1.GenerateKey() + if err != nil { + return nil + } + _, err = privkey.ToECDSA() if err != nil { return nil } @@ -395,7 +405,7 @@ func (suite *AnteTestSuite) SignTestCosmosTx(ctx sdk.Context, txBuilder client.T Sequence: sequence, } - txBuilder.SetSignatures(sig) + suite.Require().NoError(txBuilder.SetSignatures(sig)) return txBuilder } @@ -428,6 +438,7 @@ func (suite *AnteTestSuite) CreateTestEIP712CosmosTxBuilder( // GenerateTypedData TypedData var ethermintCodec codec.ProtoCodecMarshaler + // nolint: staticcheck fee := legacytx.NewStdFee(gas, gasAmount) accNumber := suite.app.AccountKeeper.GetAccount(suite.ctx, from).GetAccountNumber() @@ -467,7 +478,8 @@ func (suite *AnteTestSuite) CreateTestEIP712CosmosTxBuilder( sigsV2 := signing.SignatureV2{ PubKey: pubKey, Data: &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + Signature: []byte{}, }, Sequence: nonce, } diff --git a/app/utils.go b/app/utils.go deleted file mode 100644 index 86b95c00..00000000 --- a/app/utils.go +++ /dev/null @@ -1,168 +0,0 @@ -package althea - -import ( - "encoding/json" - "math/rand" - "time" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/types/module" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - ethermint "github.com/evmos/ethermint/types" - evmtypes "github.com/evmos/ethermint/x/evm/types" - - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - sdk "github.com/cosmos/cosmos-sdk/types" - simtypes "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/evmos/ethermint/crypto/ethsecp256k1" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - tmtypes "github.com/tendermint/tendermint/types" - dbm "github.com/tendermint/tm-db" -) - -// DefaultConsensusParams defines the default Tendermint consensus params used in -// EthermintApp testing. -var DefaultConsensusParams = &abci.ConsensusParams{ - Block: &abci.BlockParams{ - MaxBytes: 200000, - MaxGas: -1, // no limit - }, - Evidence: &tmproto.EvidenceParams{ - MaxAgeNumBlocks: 302400, - MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration - MaxBytes: 10000, - }, - Validator: &tmproto.ValidatorParams{ - PubKeyTypes: []string{ - tmtypes.ABCIPubKeyTypeEd25519, - }, - }, -} - -// Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp. -func Setup(isCheckTx bool, patchGenesis func(*AltheaApp, GenesisState) GenesisState) *AltheaApp { - db := dbm.NewMemDB() - app := NewAltheaApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, MakeEncodingConfig(), simapp.EmptyAppOptions{}) - if !isCheckTx { - // init chain must be called to stop deliverState from being nil - genesisState := NewDefaultGenesisState() - if patchGenesis != nil { - genesisState = patchGenesis(app, genesisState) - } - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } - - // Initialize the chain - app.InitChain( - abci.RequestInitChain{ - ChainId: "althea_417834-1", - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - } - - return app -} - -// RandomGenesisAccounts is used by the auth module to create random genesis accounts in simulation when a genesis.json is not specified. -// In contrast, the default auth module's RandomGenesisAccounts implementation creates only base accounts and vestings accounts. -func RandomGenesisAccounts(simState *module.SimulationState) authtypes.GenesisAccounts { - emptyCodeHash := crypto.Keccak256(nil) - genesisAccs := make(authtypes.GenesisAccounts, len(simState.Accounts)) - for i, acc := range simState.Accounts { - bacc := authtypes.NewBaseAccountWithAddress(acc.Address) - - ethacc := ðermint.EthAccount{ - BaseAccount: bacc, - CodeHash: common.BytesToHash(emptyCodeHash).String(), - } - genesisAccs[i] = ethacc - } - - return genesisAccs -} - -// RandomAccounts creates random accounts with an ethsecp256k1 private key -// TODO: replace secp256k1.GenPrivKeyFromSecret() with similar function in go-ethereum -func RandomAccounts(r *rand.Rand, n int) []simtypes.Account { - accs := make([]simtypes.Account, n) - - for i := 0; i < n; i++ { - // don't need that much entropy for simulation - privkeySeed := make([]byte, 15) - _, _ = r.Read(privkeySeed) - - prv := secp256k1.GenPrivKeyFromSecret(privkeySeed) - ethPrv := ðsecp256k1.PrivKey{} - _ = ethPrv.UnmarshalAmino(prv.Bytes()) // UnmarshalAmino simply copies the bytes and assigns them to ethPrv.Key - accs[i].PrivKey = ethPrv - accs[i].PubKey = accs[i].PrivKey.PubKey() - accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) - - accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(privkeySeed) - } - - return accs -} - -// StateFn returns the initial application state using a genesis or the simulation parameters. -// It is a wrapper of simapp.AppStateFn to replace evm param EvmDenom with staking param BondDenom. -func StateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn { - return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config, - ) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) { - appStateFn := simapp.AppStateFn(cdc, simManager) - appState, simAccs, chainID, genesisTimestamp = appStateFn(r, accs, config) - - rawState := make(map[string]json.RawMessage) - err := json.Unmarshal(appState, &rawState) - if err != nil { - panic(err) - } - - stakingStateBz, ok := rawState[stakingtypes.ModuleName] - if !ok { - panic("staking genesis state is missing") - } - - stakingState := new(stakingtypes.GenesisState) - cdc.MustUnmarshalJSON(stakingStateBz, stakingState) - - // we should get the BondDenom and make it the evmdenom. - // thus simulation accounts could have positive amount of gas token. - bondDenom := stakingState.Params.BondDenom - - evmStateBz, ok := rawState[evmtypes.ModuleName] - if !ok { - panic("staking genesis state is missing") - } - - evmState := new(evmtypes.GenesisState) - cdc.MustUnmarshalJSON(evmStateBz, evmState) - - // we should replace the EvmDenom with BondDenom - evmState.Params.EvmDenom = bondDenom - - // change appState back - rawState[evmtypes.ModuleName] = cdc.MustMarshalJSON(evmState) - - // replace appstate - appState, err = json.Marshal(rawState) - if err != nil { - panic(err) - } - return appState, simAccs, chainID, genesisTimestamp - } -}