From 615226093e5dee327167ef8d98c7bc4b83865e77 Mon Sep 17 00:00:00 2001 From: son trinh Date: Sun, 15 Dec 2024 18:13:55 +0700 Subject: [PATCH] refactor(tests/integration): Migrate mint and protocolpool integration tests to server v2 (#22859) --- tests/integration/mint/app_config.go | 26 --- tests/integration/mint/module_test.go | 30 --- tests/integration/protocolpool/app_config.go | 30 --- tests/integration/protocolpool/module_test.go | 137 -------------- tests/integration/v2/mint/module_test.go | 50 +++++ .../v2/protocolpool/module_test.go | 178 ++++++++++++++++++ 6 files changed, 228 insertions(+), 223 deletions(-) delete mode 100644 tests/integration/mint/app_config.go delete mode 100644 tests/integration/mint/module_test.go delete mode 100644 tests/integration/protocolpool/app_config.go delete mode 100644 tests/integration/protocolpool/module_test.go create mode 100644 tests/integration/v2/mint/module_test.go create mode 100644 tests/integration/v2/protocolpool/module_test.go diff --git a/tests/integration/mint/app_config.go b/tests/integration/mint/app_config.go deleted file mode 100644 index b074d723d9cb..000000000000 --- a/tests/integration/mint/app_config.go +++ /dev/null @@ -1,26 +0,0 @@ -package mint - -import ( - _ "cosmossdk.io/x/accounts" // import as blank for app wiring - _ "cosmossdk.io/x/bank" // import as blank for app wiring - _ "cosmossdk.io/x/consensus" // import as blank for app wiring - _ "cosmossdk.io/x/mint" // import as blank for app wiring - _ "cosmossdk.io/x/staking" // import as blank for app wiring - - "github.com/cosmos/cosmos-sdk/testutil/configurator" - _ "github.com/cosmos/cosmos-sdk/x/auth" // import as blank for app wiring - _ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring - _ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring -) - -var AppConfig = configurator.NewAppConfig( - configurator.AccountsModule(), - configurator.AuthModule(), - configurator.BankModule(), - configurator.StakingModule(), - configurator.TxModule(), - configurator.ValidateModule(), - configurator.ConsensusModule(), - configurator.GenutilModule(), - configurator.MintModule(), -) diff --git a/tests/integration/mint/module_test.go b/tests/integration/mint/module_test.go deleted file mode 100644 index e0d9896ac149..000000000000 --- a/tests/integration/mint/module_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package mint - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "cosmossdk.io/depinject" - "cosmossdk.io/log" - "cosmossdk.io/x/mint/types" - - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -func TestItCreatesModuleAccountOnInitBlock(t *testing.T) { - var accountKeeper authkeeper.AccountKeeper - - app, err := simtestutil.SetupAtGenesis( - depinject.Configs( - AppConfig, - depinject.Supply(log.NewNopLogger()), - ), &accountKeeper) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false) - acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) - require.NotNil(t, acc) -} diff --git a/tests/integration/protocolpool/app_config.go b/tests/integration/protocolpool/app_config.go deleted file mode 100644 index 34f39a3111ac..000000000000 --- a/tests/integration/protocolpool/app_config.go +++ /dev/null @@ -1,30 +0,0 @@ -package protocolpool - -import ( - _ "cosmossdk.io/x/accounts" // import as blank for app wiring - _ "cosmossdk.io/x/bank" // import as blank for app wiring - _ "cosmossdk.io/x/consensus" // import as blank for app wiring - _ "cosmossdk.io/x/distribution" // import as blank for app wiring - _ "cosmossdk.io/x/mint" // import as blank for app wiring - _ "cosmossdk.io/x/protocolpool" // import as blank for app wiring - _ "cosmossdk.io/x/staking" // import as blank for app wiring - - "github.com/cosmos/cosmos-sdk/testutil/configurator" - _ "github.com/cosmos/cosmos-sdk/x/auth" // import as blank for app wiring - _ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring - _ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring -) - -var AppConfig = configurator.NewAppConfig( - configurator.AccountsModule(), - configurator.AuthModule(), - configurator.BankModule(), - configurator.StakingModule(), - configurator.TxModule(), - configurator.ValidateModule(), - configurator.ConsensusModule(), - configurator.GenutilModule(), - configurator.MintModule(), - configurator.DistributionModule(), - configurator.ProtocolPoolModule(), -) diff --git a/tests/integration/protocolpool/module_test.go b/tests/integration/protocolpool/module_test.go deleted file mode 100644 index 8a7afd427918..000000000000 --- a/tests/integration/protocolpool/module_test.go +++ /dev/null @@ -1,137 +0,0 @@ -package protocolpool - -import ( - "math/rand" - "testing" - "time" - - "github.com/stretchr/testify/require" - - "cosmossdk.io/core/header" - "cosmossdk.io/depinject" - "cosmossdk.io/log" - "cosmossdk.io/math" - bankkeeper "cosmossdk.io/x/bank/keeper" - "cosmossdk.io/x/mint/types" - protocolpoolkeeper "cosmossdk.io/x/protocolpool/keeper" - protocolpooltypes "cosmossdk.io/x/protocolpool/types" - stakingkeeper "cosmossdk.io/x/staking/keeper" - - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - sdk "github.com/cosmos/cosmos-sdk/types" - authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -// TestWithdrawAnytime tests if withdrawing funds many times vs withdrawing funds once -// yield the same end balance. -func TestWithdrawAnytime(t *testing.T) { - var accountKeeper authkeeper.AccountKeeper - var protocolpoolKeeper protocolpoolkeeper.Keeper - var bankKeeper bankkeeper.Keeper - var stakingKeeper *stakingkeeper.Keeper - - app, err := simtestutil.SetupAtGenesis( - depinject.Configs( - AppConfig, - depinject.Supply(log.NewNopLogger()), - ), &accountKeeper, &protocolpoolKeeper, &bankKeeper, &stakingKeeper) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Height: 1}) - acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) - require.NotNil(t, acc) - - testAddrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 5, math.NewInt(1)) - testAddr0Str, err := accountKeeper.AddressCodec().BytesToString(testAddrs[0]) - require.NoError(t, err) - - msgServer := protocolpoolkeeper.NewMsgServerImpl(protocolpoolKeeper) - _, err = msgServer.CreateContinuousFund( - ctx, - &protocolpooltypes.MsgCreateContinuousFund{ - Authority: protocolpoolKeeper.GetAuthority(), - Recipient: testAddr0Str, - Percentage: math.LegacyMustNewDecFromStr("0.5"), - }, - ) - require.NoError(t, err) - - // increase the community pool by a bunch - for i := 0; i < 30; i++ { - ctx, err = simtestutil.NextBlock(app, ctx, time.Minute) - require.NoError(t, err) - - // withdraw funds randomly, but it must always land on the same end balance - if rand.Intn(100) > 50 { - _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ - RecipientAddress: testAddr0Str, - }) - require.NoError(t, err) - } - } - - pool, err := protocolpoolKeeper.GetCommunityPool(ctx) - require.NoError(t, err) - require.True(t, pool.IsAllGT(sdk.NewCoins(sdk.NewInt64Coin("stake", 100000)))) - - _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ - RecipientAddress: testAddr0Str, - }) - require.NoError(t, err) - - endBalance := bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom) - require.Equal(t, "11883031stake", endBalance.String()) -} - -// TestExpireInTheMiddle tests if a continuous fund that expires without anyone -// calling the withdraw function, the funds are still distributed correctly. -func TestExpireInTheMiddle(t *testing.T) { - var accountKeeper authkeeper.AccountKeeper - var protocolpoolKeeper protocolpoolkeeper.Keeper - var bankKeeper bankkeeper.Keeper - var stakingKeeper *stakingkeeper.Keeper - - app, err := simtestutil.SetupAtGenesis( - depinject.Configs( - AppConfig, - depinject.Supply(log.NewNopLogger()), - ), &accountKeeper, &protocolpoolKeeper, &bankKeeper, &stakingKeeper) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Height: 1}) - acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) - require.NotNil(t, acc) - - testAddrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 5, math.NewInt(1)) - testAddr0Str, err := accountKeeper.AddressCodec().BytesToString(testAddrs[0]) - require.NoError(t, err) - - msgServer := protocolpoolkeeper.NewMsgServerImpl(protocolpoolKeeper) - - expirationTime := ctx.BlockTime().Add(time.Minute * 2) - _, err = msgServer.CreateContinuousFund( - ctx, - &protocolpooltypes.MsgCreateContinuousFund{ - Authority: protocolpoolKeeper.GetAuthority(), - Recipient: testAddr0Str, - Percentage: math.LegacyMustNewDecFromStr("0.1"), - Expiry: &expirationTime, - }, - ) - require.NoError(t, err) - - // increase the community pool by a bunch - for i := 0; i < 30; i++ { - ctx, err = simtestutil.NextBlock(app, ctx, time.Minute) - require.NoError(t, err) - } - - _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ - RecipientAddress: testAddr0Str, - }) - require.NoError(t, err) - - endBalance := bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom) - require.Equal(t, "237661stake", endBalance.String()) -} diff --git a/tests/integration/v2/mint/module_test.go b/tests/integration/v2/mint/module_test.go new file mode 100644 index 000000000000..97177f5a2bae --- /dev/null +++ b/tests/integration/v2/mint/module_test.go @@ -0,0 +1,50 @@ +package mint + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "cosmossdk.io/depinject" + "cosmossdk.io/log" + _ "cosmossdk.io/x/accounts" // import as blank for app wiring + _ "cosmossdk.io/x/bank" // import as blank for app wiring + _ "cosmossdk.io/x/consensus" // import as blank for app wiring + _ "cosmossdk.io/x/mint" // import as blank for app wiring + "cosmossdk.io/x/mint/types" + _ "cosmossdk.io/x/staking" // import as blank for app wiring + + "github.com/cosmos/cosmos-sdk/tests/integration/v2" + "github.com/cosmos/cosmos-sdk/testutil/configurator" + _ "github.com/cosmos/cosmos-sdk/x/auth" // import as blank for app wiring + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + _ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + _ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring +) + +func TestItCreatesModuleAccountOnInitBlock(t *testing.T) { + var accountKeeper authkeeper.AccountKeeper + + moduleConfigs := []configurator.ModuleOption{ + configurator.AccountsModule(), + configurator.AuthModule(), + configurator.BankModule(), + configurator.StakingModule(), + configurator.TxModule(), + configurator.ValidateModule(), + configurator.ConsensusModule(), + configurator.GenutilModule(), + configurator.MintModule(), + } + + startupCfg := integration.DefaultStartUpConfig(t) + app, err := integration.NewApp( + depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(log.NewNopLogger())), + startupCfg, &accountKeeper) + require.NoError(t, err) + + ctx := app.StateLatestContext(t) + acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) + require.NotNil(t, acc) +} diff --git a/tests/integration/v2/protocolpool/module_test.go b/tests/integration/v2/protocolpool/module_test.go new file mode 100644 index 000000000000..be74371dac43 --- /dev/null +++ b/tests/integration/v2/protocolpool/module_test.go @@ -0,0 +1,178 @@ +package protocolpool + +import ( + "math/rand" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "cosmossdk.io/depinject" + "cosmossdk.io/log" + "cosmossdk.io/math" + _ "cosmossdk.io/x/accounts" // import as blank for app wiring + _ "cosmossdk.io/x/bank" // import as blank for app wiring + bankkeeper "cosmossdk.io/x/bank/keeper" + _ "cosmossdk.io/x/consensus" // import as blank for app wiring + _ "cosmossdk.io/x/distribution" // import as blank for app wiring + _ "cosmossdk.io/x/mint" // import as blank for app wiring + "cosmossdk.io/x/mint/types" + _ "cosmossdk.io/x/protocolpool" // import as blank for app wiring + protocolpoolkeeper "cosmossdk.io/x/protocolpool/keeper" + protocolpooltypes "cosmossdk.io/x/protocolpool/types" + _ "cosmossdk.io/x/staking" // import as blank for app wiring + stakingkeeper "cosmossdk.io/x/staking/keeper" + + "github.com/cosmos/cosmos-sdk/tests/integration/v2" + "github.com/cosmos/cosmos-sdk/testutil/configurator" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/cosmos-sdk/x/auth" // import as blank for app wiring + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + _ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import as blank for app wiring + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + _ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring +) + +var moduleConfigs = []configurator.ModuleOption{ + configurator.AccountsModule(), + configurator.AuthModule(), + configurator.BankModule(), + configurator.StakingModule(), + configurator.TxModule(), + configurator.ValidateModule(), + configurator.ConsensusModule(), + configurator.GenutilModule(), + configurator.MintModule(), + configurator.DistributionModule(), + configurator.ProtocolPoolModule(), +} + +type fixture struct { + accountKeeper authkeeper.AccountKeeper + protocolpoolKeeper protocolpoolkeeper.Keeper + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper +} + +// TestWithdrawAnytime tests if withdrawing funds many times vs withdrawing funds once +// yield the same end balance. +func TestWithdrawAnytime(t *testing.T) { + res := fixture{} + + startupCfg := integration.DefaultStartUpConfig(t) + startupCfg.HeaderService = &integration.HeaderService{} + + app, err := integration.NewApp( + depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(log.NewNopLogger())), + startupCfg, &res.accountKeeper, &res.protocolpoolKeeper, &res.bankKeeper, &res.stakingKeeper) + require.NoError(t, err) + + ctx := app.StateLatestContext(t) + acc := res.accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) + require.NotNil(t, acc) + + testAddrs := simtestutil.AddTestAddrs(res.bankKeeper, res.stakingKeeper, ctx, 5, math.NewInt(1)) + testAddr0Str, err := res.accountKeeper.AddressCodec().BytesToString(testAddrs[0]) + require.NoError(t, err) + + msgServer := protocolpoolkeeper.NewMsgServerImpl(res.protocolpoolKeeper) + _, err = msgServer.CreateContinuousFund( + ctx, + &protocolpooltypes.MsgCreateContinuousFund{ + Authority: res.protocolpoolKeeper.GetAuthority(), + Recipient: testAddr0Str, + Percentage: math.LegacyMustNewDecFromStr("0.5"), + }, + ) + require.NoError(t, err) + + // increase the community pool by a bunch + for i := 0; i < 30; i++ { + _, state := app.Deliver(t, ctx, nil) + _, err = app.Commit(state) + require.NoError(t, err) + + headerInfo := integration.HeaderInfoFromContext(ctx) + headerInfo.Time = headerInfo.Time.Add(time.Minute) + ctx = integration.SetHeaderInfo(ctx, headerInfo) + + // withdraw funds randomly, but it must always land on the same end balance + if rand.Intn(100) > 50 { + _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ + RecipientAddress: testAddr0Str, + }) + require.NoError(t, err) + } + } + + pool, err := res.protocolpoolKeeper.GetCommunityPool(ctx) + require.NoError(t, err) + require.True(t, pool.IsAllGT(sdk.NewCoins(sdk.NewInt64Coin("stake", 100000)))) + + _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ + RecipientAddress: testAddr0Str, + }) + require.NoError(t, err) + + endBalance := res.bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom) + require.Equal(t, "11883031stake", endBalance.String()) +} + +// TestExpireInTheMiddle tests if a continuous fund that expires without anyone +// calling the withdraw function, the funds are still distributed correctly. +func TestExpireInTheMiddle(t *testing.T) { + res := fixture{} + + startupCfg := integration.DefaultStartUpConfig(t) + startupCfg.HeaderService = &integration.HeaderService{} + + app, err := integration.NewApp( + depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(log.NewNopLogger())), + startupCfg, &res.accountKeeper, &res.protocolpoolKeeper, &res.bankKeeper, &res.stakingKeeper) + require.NoError(t, err) + + ctx := app.StateLatestContext(t) + + acc := res.accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) + require.NotNil(t, acc) + + testAddrs := simtestutil.AddTestAddrs(res.bankKeeper, res.stakingKeeper, ctx, 5, math.NewInt(1)) + testAddr0Str, err := res.accountKeeper.AddressCodec().BytesToString(testAddrs[0]) + require.NoError(t, err) + + msgServer := protocolpoolkeeper.NewMsgServerImpl(res.protocolpoolKeeper) + + headerInfo := integration.HeaderInfoFromContext(ctx) + expirationTime := headerInfo.Time.Add(time.Minute * 2) + _, err = msgServer.CreateContinuousFund( + ctx, + &protocolpooltypes.MsgCreateContinuousFund{ + Authority: res.protocolpoolKeeper.GetAuthority(), + Recipient: testAddr0Str, + Percentage: math.LegacyMustNewDecFromStr("0.1"), + Expiry: &expirationTime, + }, + ) + require.NoError(t, err) + + // increase the community pool by a bunch + for i := 0; i < 30; i++ { + _, state := app.Deliver(t, ctx, nil) + _, err = app.Commit(state) + require.NoError(t, err) + + headerInfo := integration.HeaderInfoFromContext(ctx) + headerInfo.Time = headerInfo.Time.Add(time.Minute) + ctx = integration.SetHeaderInfo(ctx, headerInfo) + require.NoError(t, err) + } + + _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ + RecipientAddress: testAddr0Str, + }) + require.NoError(t, err) + + endBalance := res.bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom) + require.Equal(t, "237661stake", endBalance.String()) +}