From 18a33374980aea40c57eae836a2ba684373a3de3 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Fri, 13 Dec 2024 14:39:20 +0700 Subject: [PATCH] fix tests --- tests/integration/protocolpool/app_config.go | 30 ---- tests/integration/protocolpool/module_test.go | 137 ------------------ .../v2/protocolpool/module_test.go | 31 ++-- 3 files changed, 17 insertions(+), 181 deletions(-) delete mode 100644 tests/integration/protocolpool/app_config.go delete mode 100644 tests/integration/protocolpool/module_test.go 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/protocolpool/module_test.go b/tests/integration/v2/protocolpool/module_test.go index 54bbd3fea7f4..0289b64020d5 100644 --- a/tests/integration/v2/protocolpool/module_test.go +++ b/tests/integration/v2/protocolpool/module_test.go @@ -1,7 +1,6 @@ package protocolpool import ( - "context" "fmt" "math/rand" "testing" @@ -51,8 +50,6 @@ var moduleConfigs = []configurator.ModuleOption{ } type fixture struct { - ctx context.Context - accountKeeper authkeeper.AccountKeeper protocolpoolKeeper protocolpoolkeeper.Keeper bankKeeper bankkeeper.Keeper @@ -95,9 +92,12 @@ func TestWithdrawAnytime(t *testing.T) { // 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) - headerInfo.Height++ ctx = integration.SetHeaderInfo(ctx, headerInfo) // withdraw funds randomly, but it must always land on the same end balance @@ -140,21 +140,21 @@ func TestExpireInTheMiddle(t *testing.T) { startupCfg, &res.accountKeeper, &res.protocolpoolKeeper, &res.bankKeeper, &res.stakingKeeper) require.NoError(t, err) - res.ctx = app.StateLatestContext(t) + ctx := app.StateLatestContext(t) - acc := res.accountKeeper.GetAccount(res.ctx, authtypes.NewModuleAddress(types.ModuleName)) + acc := res.accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName)) require.NotNil(t, acc) - testAddrs := simtestutil.AddTestAddrs(res.bankKeeper, res.stakingKeeper, res.ctx, 5, math.NewInt(1)) + 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(res.ctx) + headerInfo := integration.HeaderInfoFromContext(ctx) expirationTime := headerInfo.Time.Add(time.Minute * 2) _, err = msgServer.CreateContinuousFund( - res.ctx, + ctx, &protocolpooltypes.MsgCreateContinuousFund{ Authority: res.protocolpoolKeeper.GetAuthority(), Recipient: testAddr0Str, @@ -166,18 +166,21 @@ func TestExpireInTheMiddle(t *testing.T) { // increase the community pool by a bunch for i := 0; i < 30; i++ { - headerInfo := integration.HeaderInfoFromContext(res.ctx) + _, 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) - headerInfo.Height++ - res.ctx = integration.SetHeaderInfo(res.ctx, headerInfo) + ctx = integration.SetHeaderInfo(ctx, headerInfo) require.NoError(t, err) } - _, err = msgServer.WithdrawContinuousFund(res.ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ + _, err = msgServer.WithdrawContinuousFund(ctx, &protocolpooltypes.MsgWithdrawContinuousFund{ RecipientAddress: testAddr0Str, }) require.NoError(t, err) - endBalance := res.bankKeeper.GetBalance(res.ctx, testAddrs[0], sdk.DefaultBondDenom) + endBalance := res.bankKeeper.GetBalance(ctx, testAddrs[0], sdk.DefaultBondDenom) require.Equal(t, "237661stake", endBalance.String()) }