From 712a2f75543fa260a076dbee93bd20b8eb838550 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 27 Aug 2021 15:54:23 +0530 Subject: [PATCH 1/6] contralize simapp setup --- simapp/test_helpers.go | 86 +++++++++++++++++++++++++++++++++++----- testutil/mock/privval.go | 50 +++++++++++++++++++++++ 2 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 testutil/mock/privval.go diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 722fc2b142f..4e7fc022de0 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" + tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmtypes "github.com/tendermint/tendermint/types" @@ -21,8 +22,12 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/server/types" "github.com/cosmos/cosmos-sdk/simapp/helpers" + "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/testutil/mock" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/errors" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -50,6 +55,16 @@ var DefaultConsensusParams = &abci.ConsensusParams{ }, } +// SimappOptions defines arguments that are passed into `Simapp` constructor. +type SimappOptions struct { + Logger log.Logger + InvCheckPeriod uint + HomePath string + SkipUpgradeHeights map[int64]bool + EncConfig params.EncodingConfig + AppOpts types.AppOptions +} + func setup(withGenesis bool, invCheckPeriod uint) (*SimApp, GenesisState) { db := dbm.NewMemDB() encCdc := MakeTestEncodingConfig() @@ -60,6 +75,48 @@ func setup(withGenesis bool, invCheckPeriod uint) (*SimApp, GenesisState) { return app, GenesisState{} } +// SetupWithCustomOptions initializes a new SimApp with custom options. +func SetupWithCustomOptions(t *testing.T, isCheckTx bool, options SimappOptions) *SimApp { + t.Helper() + + privVal := mock.NewPV() + pubKey, err := privVal.GetPubKey() + require.NoError(t, err) + // create validator set with single validator + validator := tmtypes.NewValidator(pubKey, 1) + valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) + + // generate genesis account + senderPrivKey := secp256k1.GenPrivKey() + acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) + balance := banktypes.Balance{ + Address: acc.GetAddress().String(), + Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), + } + + db := dbm.NewMemDB() + app := NewSimApp(options.Logger, db, nil, true, options.SkipUpgradeHeights, options.HomePath, options.InvCheckPeriod, options.EncConfig, options.AppOpts) + genesisState := NewDefaultGenesisState(app.appCodec) + genesisState = genesisStateWithValSet(t, app, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance) + + if !isCheckTx { + // init chain must be called to stop deliverState from being nil + stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + // Initialize the chain + app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: DefaultConsensusParams, + AppStateBytes: stateBytes, + }, + ) + } + + return app +} + // Setup initializes a new SimApp. A Nop logger is set in SimApp. func Setup(t *testing.T, isCheckTx bool) *SimApp { t.Helper() @@ -83,12 +140,10 @@ func Setup(t *testing.T, isCheckTx bool) *SimApp { return app } -// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts -// that also act as delegators. For simplicity, each validator is bonded with a delegation -// of one consensus engine unit (10^6) in the default token of the simapp from first genesis -// account. A Nop logger is set in SimApp. -func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { - app, genesisState := setup(true, 5) +func genesisStateWithValSet(t *testing.T, + app *SimApp, genesisState GenesisState, + valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, + balances ...banktypes.Balance) GenesisState { // set genesis accounts authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) @@ -96,7 +151,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - bondAmt := sdk.NewInt(1000000) + bondAmt := sdk.DefaultPowerReduction for _, val := range valSet.Validators { pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) @@ -140,6 +195,17 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) + return genesisState +} + +// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts +// that also act as delegators. For simplicity, each validator is bonded with a delegation +// of one consensus engine unit in the default token of the simapp from first genesis +// account. A Nop logger is set in SimApp. +func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { + app, genesisState := setup(true, 5) + genesisState = genesisStateWithValSet(t, app, genesisState, valSet, genAccs, balances...) + stateBytes, err := json.MarshalIndent(genesisState, "", " ") require.NoError(t, err) @@ -166,7 +232,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs // SetupWithGenesisAccounts initializes a new SimApp with the provided genesis // accounts and possible balances. -func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { +func SetupWithGenesisAccounts(t *testing.T, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { app, genesisState := setup(true, 0) authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) @@ -180,9 +246,7 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } + require.NoError(t, err) app.InitChain( abci.RequestInitChain{ diff --git a/testutil/mock/privval.go b/testutil/mock/privval.go new file mode 100644 index 00000000000..fe46659b3df --- /dev/null +++ b/testutil/mock/privval.go @@ -0,0 +1,50 @@ +package mock + +import ( + "github.com/tendermint/tendermint/crypto" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmtypes "github.com/tendermint/tendermint/types" + + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" +) + +var _ tmtypes.PrivValidator = PV{} + +// MockPV implements PrivValidator without any safety or persistence. +// Only use it for testing. +type PV struct { + PrivKey cryptotypes.PrivKey +} + +func NewPV() PV { + return PV{ed25519.GenPrivKey()} +} + +// GetPubKey implements PrivValidator interface +func (pv PV) GetPubKey() (crypto.PubKey, error) { + return cryptocodec.ToTmPubKeyInterface(pv.PrivKey.PubKey()) +} + +// SignVote implements PrivValidator interface +func (pv PV) SignVote(chainID string, vote *tmproto.Vote) error { + signBytes := tmtypes.VoteSignBytes(chainID, vote) + sig, err := pv.PrivKey.Sign(signBytes) + if err != nil { + return err + } + vote.Signature = sig + return nil +} + +// SignProposal implements PrivValidator interface +func (pv PV) SignProposal(chainID string, proposal *tmproto.Proposal) error { + signBytes := tmtypes.ProposalSignBytes(chainID, proposal) + sig, err := pv.PrivKey.Sign(signBytes) + if err != nil { + return err + } + proposal.Signature = sig + return nil +} From d1058fde50dadcd79fd1989295de18b052a86f60 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 27 Aug 2021 15:54:33 +0530 Subject: [PATCH 2/6] fix: fix tests --- x/bank/app_test.go | 10 ++++---- x/bank/bench_test.go | 4 ++-- x/slashing/app_test.go | 2 +- x/staking/app_test.go | 2 +- x/upgrade/abci_test.go | 54 +++++++++++++++++++----------------------- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 5e134010e48..d1e810277ed 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -92,7 +92,7 @@ func TestSendNotEnoughBalance(t *testing.T) { } genAccs := []authtypes.GenesisAccount{acc} - app := simapp.SetupWithGenesisAccounts(genAccs) + app := simapp.SetupWithGenesisAccounts(t, genAccs) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) @@ -127,7 +127,7 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { } genAccs := []authtypes.GenesisAccount{acc} - app := simapp.SetupWithGenesisAccounts(genAccs) + app := simapp.SetupWithGenesisAccounts(t, genAccs) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) @@ -197,7 +197,7 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { } genAccs := []authtypes.GenesisAccount{acc1, acc2} - app := simapp.SetupWithGenesisAccounts(genAccs) + app := simapp.SetupWithGenesisAccounts(t, genAccs) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) @@ -246,7 +246,7 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) { } genAccs := []authtypes.GenesisAccount{acc1, acc2, acc4} - app := simapp.SetupWithGenesisAccounts(genAccs) + app := simapp.SetupWithGenesisAccounts(t, genAccs) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) @@ -293,7 +293,7 @@ func TestMsgMultiSendDependent(t *testing.T) { require.NoError(t, err) genAccs := []authtypes.GenesisAccount{acc1, acc2} - app := simapp.SetupWithGenesisAccounts(genAccs) + app := simapp.SetupWithGenesisAccounts(t, genAccs) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) diff --git a/x/bank/bench_test.go b/x/bank/bench_test.go index 3550f44f7b2..2b664ca39be 100644 --- a/x/bank/bench_test.go +++ b/x/bank/bench_test.go @@ -27,7 +27,7 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // construct genesis state genAccs := []types.GenesisAccount{&acc} - benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs) + benchmarkApp := simapp.SetupWithGenesisAccounts(&testing.T{}, genAccs) ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{}) // some value conceivably higher than the benchmarks would ever go @@ -69,7 +69,7 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) { // Construct genesis state genAccs := []authtypes.GenesisAccount{&acc} - benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs) + benchmarkApp := simapp.SetupWithGenesisAccounts(&testing.T{}, genAccs) ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{}) // some value conceivably higher than the benchmarks would ever go diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 028d4175560..e9ece2c690a 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -57,7 +57,7 @@ func TestSlashingMsgs(t *testing.T) { }, } - app := simapp.SetupWithGenesisAccounts(accs, balances...) + app := simapp.SetupWithGenesisAccounts(t, accs, balances...) simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin}) description := stakingtypes.NewDescription("foo_moniker", "", "", "", "") diff --git a/x/staking/app_test.go b/x/staking/app_test.go index 65a6e4524eb..a9795425482 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -59,7 +59,7 @@ func TestStakingMsgs(t *testing.T) { }, } - app := simapp.SetupWithGenesisAccounts(accs, balances...) + app := simapp.SetupWithGenesisAccounts(t, accs, balances...) simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin}) simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin}) diff --git a/x/upgrade/abci_test.go b/x/upgrade/abci_test.go index 5e0d1fd7924..2b2adca88b0 100644 --- a/x/upgrade/abci_test.go +++ b/x/upgrade/abci_test.go @@ -1,7 +1,6 @@ package upgrade_test import ( - "encoding/json" "errors" "fmt" "os" @@ -12,7 +11,6 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -34,20 +32,16 @@ type TestSuite struct { var s TestSuite -func setupTest(height int64, skip map[int64]bool) TestSuite { - db := dbm.NewMemDB() - app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, skip, simapp.DefaultNodeHome, 0, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{}) - genesisState := simapp.NewDefaultGenesisState(app.AppCodec()) - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } - app.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - AppStateBytes: stateBytes, - }, - ) +func setupTest(t *testing.T, height int64, skip map[int64]bool) TestSuite { + + app := simapp.SetupWithCustomOptions(t, false, simapp.SimappOptions{ + Logger: log.NewNopLogger(), + SkipUpgradeHeights: skip, + InvCheckPeriod: 0, + HomePath: simapp.DefaultNodeHome, + EncConfig: simapp.MakeTestEncodingConfig(), + AppOpts: simapp.EmptyAppOptions{}, + }) s.keeper = app.UpgradeKeeper s.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: height, Time: time.Now()}) @@ -59,7 +53,7 @@ func setupTest(height int64, skip map[int64]bool) TestSuite { } func TestRequireName(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{}}) require.NotNil(t, err) @@ -67,14 +61,14 @@ func TestRequireName(t *testing.T) { } func TestRequireFutureBlock(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight()}}) require.NotNil(t, err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) } func TestDoHeightUpgrade(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) t.Log("Verify can schedule an upgrade") err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) require.Nil(t, err) @@ -83,7 +77,7 @@ func TestDoHeightUpgrade(t *testing.T) { } func TestCanOverwriteScheduleUpgrade(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) t.Log("Can overwrite plan") err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "bad_test", Height: s.ctx.BlockHeight() + 10}}) require.Nil(t, err) @@ -132,7 +126,7 @@ func VerifyDoUpgradeWithCtx(t *testing.T, newCtx sdk.Context, proposalName strin } func TestHaltIfTooNew(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) t.Log("Verify that we don't panic with registered plan not in database at all") var called int s.keeper.SetUpgradeHandler("future", func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) { @@ -175,7 +169,7 @@ func VerifyCleared(t *testing.T, newCtx sdk.Context) { } func TestCanClear(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) t.Log("Verify upgrade is scheduled") err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 100}}) require.Nil(t, err) @@ -187,7 +181,7 @@ func TestCanClear(t *testing.T) { } func TestCantApplySameUpgradeTwice(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) height := s.ctx.BlockHeader().Height + 1 err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: height}}) require.Nil(t, err) @@ -199,7 +193,7 @@ func TestCantApplySameUpgradeTwice(t *testing.T) { } func TestNoSpuriousUpgrades(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) t.Log("Verify that no upgrade panic is triggered in the BeginBlocker when we haven't scheduled an upgrade") req := abci.RequestBeginBlock{Header: s.ctx.BlockHeader()} require.NotPanics(t, func() { @@ -243,7 +237,7 @@ func TestContains(t *testing.T) { var ( skipOne int64 = 11 ) - s := setupTest(10, map[int64]bool{skipOne: true}) + s := setupTest(t, 10, map[int64]bool{skipOne: true}) VerifySet(t, map[int64]bool{skipOne: true}) t.Log("case where array contains the element") @@ -258,7 +252,7 @@ func TestSkipUpgradeSkippingAll(t *testing.T) { skipOne int64 = 11 skipTwo int64 = 20 ) - s := setupTest(10, map[int64]bool{skipOne: true, skipTwo: true}) + s := setupTest(t, 10, map[int64]bool{skipOne: true, skipTwo: true}) newCtx := s.ctx @@ -295,7 +289,7 @@ func TestUpgradeSkippingOne(t *testing.T) { skipOne int64 = 11 skipTwo int64 = 20 ) - s := setupTest(10, map[int64]bool{skipOne: true}) + s := setupTest(t, 10, map[int64]bool{skipOne: true}) newCtx := s.ctx @@ -330,7 +324,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) { skipTwo int64 = 20 skipThree int64 = 25 ) - s := setupTest(10, map[int64]bool{skipOne: true, skipTwo: true}) + s := setupTest(t, 10, map[int64]bool{skipOne: true, skipTwo: true}) newCtx := s.ctx @@ -369,7 +363,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) { } func TestUpgradeWithoutSkip(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now()) req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) @@ -384,7 +378,7 @@ func TestUpgradeWithoutSkip(t *testing.T) { } func TestDumpUpgradeInfoToFile(t *testing.T) { - s := setupTest(10, map[int64]bool{}) + s := setupTest(t, 10, map[int64]bool{}) require := require.New(t) // require no error when the upgrade info file does not exist From 5f838ecd190e0d4d194229798f6d8df8742a4ca9 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 27 Aug 2021 16:16:13 +0530 Subject: [PATCH 3/6] fix lint errors --- simapp/test_helpers.go | 6 +++--- x/upgrade/abci_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 4e7fc022de0..ef3d1c7fc78 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -55,8 +55,8 @@ var DefaultConsensusParams = &abci.ConsensusParams{ }, } -// SimappOptions defines arguments that are passed into `Simapp` constructor. -type SimappOptions struct { +// SetupOptions defines arguments that are passed into `Simapp` constructor. +type SetupOptions struct { Logger log.Logger InvCheckPeriod uint HomePath string @@ -76,7 +76,7 @@ func setup(withGenesis bool, invCheckPeriod uint) (*SimApp, GenesisState) { } // SetupWithCustomOptions initializes a new SimApp with custom options. -func SetupWithCustomOptions(t *testing.T, isCheckTx bool, options SimappOptions) *SimApp { +func SetupWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions) *SimApp { t.Helper() privVal := mock.NewPV() diff --git a/x/upgrade/abci_test.go b/x/upgrade/abci_test.go index 2b2adca88b0..484d446f545 100644 --- a/x/upgrade/abci_test.go +++ b/x/upgrade/abci_test.go @@ -34,7 +34,7 @@ var s TestSuite func setupTest(t *testing.T, height int64, skip map[int64]bool) TestSuite { - app := simapp.SetupWithCustomOptions(t, false, simapp.SimappOptions{ + app := simapp.SetupWithCustomOptions(t, false, simapp.SetupOptions{ Logger: log.NewNopLogger(), SkipUpgradeHeights: skip, InvCheckPeriod: 0, From ee2dc004688b64be2ce377b661ce8292c2202466 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 27 Aug 2021 18:42:39 +0530 Subject: [PATCH 4/6] update simapp tests --- simapp/app_test.go | 44 ++++++++++++++++++------------------------ simapp/test_helpers.go | 4 ++-- x/upgrade/abci_test.go | 3 +++ 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/simapp/app_test.go b/simapp/app_test.go index 249775349b8..4a69e5578bf 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -39,7 +39,15 @@ import ( func TestSimAppExportAndBlockedAddrs(t *testing.T) { encCfg := MakeTestEncodingConfig() db := dbm.NewMemDB() - app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + app := SetupWithCustomOptions(t, false, SetupOptions{ + Logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), + DB: db, + InvCheckPeriod: 0, + EncConfig: encCfg, + HomePath: DefaultNodeHome, + SkipUpgradeHeights: map[int64]bool{}, + AppOpts: EmptyAppOptions{}, + }) for acc := range maccPerms { require.True( @@ -49,22 +57,11 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) { ) } - genesisState := NewDefaultGenesisState(encCfg.Codec) - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // Initialize the chain - app.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - AppStateBytes: stateBytes, - }, - ) app.Commit() // Making a new app object with the db, so that initchain hasn't been called app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) - _, err = app2.ExportAppStateAndValidators(false, []string{}) + _, err := app2.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err, "ExportAppStateAndValidators should not have an error") } @@ -249,18 +246,15 @@ func TestInitGenesisOnMigration(t *testing.T) { func TestUpgradeStateOnGenesis(t *testing.T) { encCfg := MakeTestEncodingConfig() db := dbm.NewMemDB() - app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) - genesisState := NewDefaultGenesisState(encCfg.Codec) - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // Initialize the chain - app.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - AppStateBytes: stateBytes, - }, - ) + app := SetupWithCustomOptions(t, false, SetupOptions{ + Logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), + DB: db, + InvCheckPeriod: 0, + EncConfig: encCfg, + HomePath: DefaultNodeHome, + SkipUpgradeHeights: map[int64]bool{}, + AppOpts: EmptyAppOptions{}, + }) // make sure the upgrade keeper has version map in state ctx := app.NewContext(false, tmproto.Header{}) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index c1828c6eee2..d4b5387ccf5 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -58,6 +58,7 @@ var DefaultConsensusParams = &abci.ConsensusParams{ // SetupOptions defines arguments that are passed into `Simapp` constructor. type SetupOptions struct { Logger log.Logger + DB *dbm.MemDB InvCheckPeriod uint HomePath string SkipUpgradeHeights map[int64]bool @@ -94,8 +95,7 @@ func SetupWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions) Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), } - db := dbm.NewMemDB() - app := NewSimApp(options.Logger, db, nil, true, options.SkipUpgradeHeights, options.HomePath, options.InvCheckPeriod, options.EncConfig, options.AppOpts) + app := NewSimApp(options.Logger, options.DB, nil, true, options.SkipUpgradeHeights, options.HomePath, options.InvCheckPeriod, options.EncConfig, options.AppOpts) genesisState := NewDefaultGenesisState(app.appCodec) genesisState = genesisStateWithValSet(t, app, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance) diff --git a/x/upgrade/abci_test.go b/x/upgrade/abci_test.go index 484d446f545..4fa0f67ad9a 100644 --- a/x/upgrade/abci_test.go +++ b/x/upgrade/abci_test.go @@ -11,6 +11,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -34,9 +35,11 @@ var s TestSuite func setupTest(t *testing.T, height int64, skip map[int64]bool) TestSuite { + db := dbm.NewMemDB() app := simapp.SetupWithCustomOptions(t, false, simapp.SetupOptions{ Logger: log.NewNopLogger(), SkipUpgradeHeights: skip, + DB: db, InvCheckPeriod: 0, HomePath: simapp.DefaultNodeHome, EncConfig: simapp.MakeTestEncodingConfig(), From 290744415866b76701fce2d4d228df8b3d0d9ce7 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Wed, 1 Sep 2021 16:50:22 +0530 Subject: [PATCH 5/6] review changes --- simapp/app_test.go | 4 ++-- simapp/test_helpers.go | 4 ++-- x/upgrade/abci_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/simapp/app_test.go b/simapp/app_test.go index 4a69e5578bf..0598609de3b 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -39,7 +39,7 @@ import ( func TestSimAppExportAndBlockedAddrs(t *testing.T) { encCfg := MakeTestEncodingConfig() db := dbm.NewMemDB() - app := SetupWithCustomOptions(t, false, SetupOptions{ + app := NewSimappWithCustomOptions(t, false, SetupOptions{ Logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), DB: db, InvCheckPeriod: 0, @@ -246,7 +246,7 @@ func TestInitGenesisOnMigration(t *testing.T) { func TestUpgradeStateOnGenesis(t *testing.T) { encCfg := MakeTestEncodingConfig() db := dbm.NewMemDB() - app := SetupWithCustomOptions(t, false, SetupOptions{ + app := NewSimappWithCustomOptions(t, false, SetupOptions{ Logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)), DB: db, InvCheckPeriod: 0, diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index d4b5387ccf5..c02097209fa 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -76,8 +76,8 @@ func setup(withGenesis bool, invCheckPeriod uint) (*SimApp, GenesisState) { return app, GenesisState{} } -// SetupWithCustomOptions initializes a new SimApp with custom options. -func SetupWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions) *SimApp { +// NewSimappWithCustomOptions initializes a new SimApp with custom options. +func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions) *SimApp { t.Helper() privVal := mock.NewPV() diff --git a/x/upgrade/abci_test.go b/x/upgrade/abci_test.go index 4fa0f67ad9a..16206447ac5 100644 --- a/x/upgrade/abci_test.go +++ b/x/upgrade/abci_test.go @@ -36,7 +36,7 @@ var s TestSuite func setupTest(t *testing.T, height int64, skip map[int64]bool) TestSuite { db := dbm.NewMemDB() - app := simapp.SetupWithCustomOptions(t, false, simapp.SetupOptions{ + app := simapp.NewSimappWithCustomOptions(t, false, simapp.SetupOptions{ Logger: log.NewNopLogger(), SkipUpgradeHeights: skip, DB: db, From 8b380661f17ab44b7c18f238eec7d072e847367b Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Mon, 13 Sep 2021 18:50:29 +0530 Subject: [PATCH 6/6] review changes --- simapp/test_helpers.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index c02097209fa..58e365aed65 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -203,6 +203,8 @@ func genesisStateWithValSet(t *testing.T, // of one consensus engine unit in the default token of the simapp from first genesis // account. A Nop logger is set in SimApp. func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { + t.Helper() + app, genesisState := setup(true, 5) genesisState = genesisStateWithValSet(t, app, genesisState, valSet, genAccs, balances...) @@ -233,6 +235,8 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs // SetupWithGenesisAccounts initializes a new SimApp with the provided genesis // accounts and possible balances. func SetupWithGenesisAccounts(t *testing.T, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { + t.Helper() + app, genesisState := setup(true, 0) authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)