From 4e253279a7dbacbf7235140e2a39f1fde8a56843 Mon Sep 17 00:00:00 2001 From: Narangde <0xhanslee@gmail.com> Date: Tue, 17 Sep 2024 18:21:06 +0900 Subject: [PATCH] fix(evmstaking): fix tests with implementation of epoched staking --- client/x/evmstaking/keeper/abci_test.go | 6 +--- client/x/evmstaking/keeper/deposit_test.go | 11 ++++++- client/x/evmstaking/keeper/keeper_test.go | 32 +++++++++++++++++-- client/x/evmstaking/keeper/params_test.go | 2 +- client/x/evmstaking/keeper/redelegate_test.go | 11 ++++++- client/x/evmstaking/keeper/validator_test.go | 11 ++++++- client/x/evmstaking/keeper/withdraw_test.go | 11 ++++++- 7 files changed, 71 insertions(+), 13 deletions(-) diff --git a/client/x/evmstaking/keeper/abci_test.go b/client/x/evmstaking/keeper/abci_test.go index ccf26b94..16f89b4c 100644 --- a/client/x/evmstaking/keeper/abci_test.go +++ b/client/x/evmstaking/keeper/abci_test.go @@ -242,8 +242,6 @@ func (s *TestSuite) TestEndBlock() { pastHeader.Time = pastHeader.Time.Add(-ubdTime).Add(-time.Minute) s.setupUnbonding(sdkCtx.WithBlockHeader(pastHeader), delAddr, valAddr1, "10") - // Mock staking.EndBlocker - s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil) // Mock evmstaking.EndBlocker s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(errors.New("failed to send coins to module")) @@ -268,8 +266,6 @@ func (s *TestSuite) TestEndBlock() { pastHeader.Time = pastHeader.Time.Add(-ubdTime).Add(-time.Minute) s.setupUnbonding(sdkCtx.WithBlockHeader(pastHeader), delAddr, valAddr1, "10") - // Mock staking.EndBlocker - s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil) // Mock evmstaking.EndBlocker s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(nil) s.BankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(errors.New("failed to burn coins")) @@ -370,7 +366,7 @@ func (s *TestSuite) setupMatureUnbondingDelegation(ctx sdk.Context, delAddr sdk. s.setupUnbonding(pastCtx, delAddr, valAddr, amt) // Mock staking.EndBlocker - s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil) + s.BankKeeper.EXPECT().UndelegateCoinsFromModuleToAccount(gomock.Any(), stypes.NotBondedPoolName, delAddr, gomock.Any()).Return(nil).AnyTimes() // Mock evmstaking.EndBlocker s.BankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), delAddr, types.ModuleName, gomock.Any()).Return(nil) s.BankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil) diff --git a/client/x/evmstaking/keeper/deposit_test.go b/client/x/evmstaking/keeper/deposit_test.go index 06c5b324..12bf28bc 100644 --- a/client/x/evmstaking/keeper/deposit_test.go +++ b/client/x/evmstaking/keeper/deposit_test.go @@ -207,7 +207,16 @@ func (s *TestSuite) TestProcessDeposit() { tc.settingMock() } cachedCtx, _ := ctx.CacheContext() - err := keeper.HandleDepositEvent(cachedCtx, tc.deposit) + var err error + err = keeper.HandleDepositEvent(cachedCtx, tc.deposit) + if !keeper.MessageQueue.IsEmpty(cachedCtx) { + var queuedMsgs []*types.QueuedMessage + queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx) + + for _, msg := range queuedMsgs { + err = keeper.ProcessMsg(cachedCtx, msg) + } + } if tc.expectedErr != "" { require.ErrorContains(err, tc.expectedErr) } else { diff --git a/client/x/evmstaking/keeper/keeper_test.go b/client/x/evmstaking/keeper/keeper_test.go index 4ddb8d30..fe6c9aaf 100644 --- a/client/x/evmstaking/keeper/keeper_test.go +++ b/client/x/evmstaking/keeper/keeper_test.go @@ -32,6 +32,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/stretchr/testify/suite" + epochstypes "github.com/piplabs/story/client/x/epochs/types" evmenginetypes "github.com/piplabs/story/client/x/evmengine/types" "github.com/piplabs/story/client/x/evmstaking/keeper" "github.com/piplabs/story/client/x/evmstaking/module" @@ -46,7 +47,11 @@ import ( ) var ( - PKs = simtestutil.CreateTestPubKeys(3) + PKs = simtestutil.CreateTestPubKeys(3) + TestEVMStakingParams = types.NewParams(types.DefaultMaxWithdrawalPerBlock, + types.DefaultMaxSweepPerBlock, + types.DefaultMinPartialWithdrawalAmount, + "second") ) type TestSuite struct { @@ -111,6 +116,16 @@ func (s *TestSuite) SetupTest() { slashingKeeper := estestutil.NewMockSlashingKeeper(ctrl) s.SlashingKeeper = slashingKeeper epochsKeeper := estestutil.NewMockEpochsKeeper(ctrl) + secondEpochInfo := epochstypes.EpochInfo{ + Identifier: "second", + StartTime: time.Time{}, + Duration: time.Second, + CurrentEpoch: 1, // to update validator set + CurrentEpochStartHeight: s.Ctx.BlockHeight(), + CurrentEpochStartTime: time.Time{}, + EpochCountingStarted: true, + } + epochsKeeper.EXPECT().GetEpochInfo(gomock.Any(), gomock.Any()).Return(secondEpochInfo, nil).AnyTimes() // staking keeper stakingKeeper := skeeper.NewKeeper( @@ -141,7 +156,8 @@ func (s *TestSuite) SetupTest() { ethCl, address.NewBech32Codec("storyvaloper"), ) - s.Require().NoError(evmstakingKeeper.SetParams(s.Ctx, types.DefaultParams())) + s.Require().NoError(evmstakingKeeper.SetParams(s.Ctx, TestEVMStakingParams)) + s.Require().NoError(evmstakingKeeper.SetEpochNumber(s.Ctx, 0)) s.EVMStakingKeeper = evmstakingKeeper s.msgServer = keeper.NewMsgServerImpl(evmstakingKeeper) } @@ -617,12 +633,22 @@ func (s *TestSuite) TestProcessStakingEvents() { if tc.setup != nil { tc.setup(cachedCtx) } - evmLogs, err := tc.evmEvents() + var err error + var evmLogs []*evmenginetypes.EVMEvent + evmLogs, err = tc.evmEvents() require.NoError(err) if tc.stateCheck != nil { tc.stateCheck(cachedCtx) } err = evmstakingKeeper.ProcessStakingEvents(cachedCtx, 1, evmLogs) + if !evmstakingKeeper.MessageQueue.IsEmpty(cachedCtx) { + var queuedMsgs []*types.QueuedMessage + queuedMsgs, err = evmstakingKeeper.DequeueAllMsgs(cachedCtx) + + for _, msg := range queuedMsgs { + err = evmstakingKeeper.ProcessMsg(cachedCtx, msg) + } + } if tc.expectedError != "" { require.Error(err) require.Contains(err.Error(), tc.expectedError) diff --git a/client/x/evmstaking/keeper/params_test.go b/client/x/evmstaking/keeper/params_test.go index 58446a1a..a5fc0735 100644 --- a/client/x/evmstaking/keeper/params_test.go +++ b/client/x/evmstaking/keeper/params_test.go @@ -7,7 +7,7 @@ func (s *TestSuite) TestGetParams() { ctx, keeper := s.Ctx, s.EVMStakingKeeper params, err := keeper.GetParams(ctx) require.NoError(err) - require.Equal(types.DefaultParams(), params) + require.Equal(TestEVMStakingParams, params) } func (s *TestSuite) TestSetParams() { diff --git a/client/x/evmstaking/keeper/redelegate_test.go b/client/x/evmstaking/keeper/redelegate_test.go index ca4807ea..5f40d6ae 100644 --- a/client/x/evmstaking/keeper/redelegate_test.go +++ b/client/x/evmstaking/keeper/redelegate_test.go @@ -182,7 +182,16 @@ func (s *TestSuite) TestRedelegation() { s.Run(tc.name, func() { cachedCtx, _ := ctx.CacheContext() input := tc.input() - err := keeper.HandleRedelegateEvent(cachedCtx, &input) + var err error + err = keeper.HandleRedelegateEvent(cachedCtx, &input) + if !keeper.MessageQueue.IsEmpty(cachedCtx) { + var queuedMsgs []*types.QueuedMessage + queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx) + + for _, msg := range queuedMsgs { + err = keeper.ProcessMsg(cachedCtx, msg) + } + } if tc.expectedError != "" { require.ErrorContains(err, tc.expectedError) } else { diff --git a/client/x/evmstaking/keeper/validator_test.go b/client/x/evmstaking/keeper/validator_test.go index 2620b1d0..52424367 100644 --- a/client/x/evmstaking/keeper/validator_test.go +++ b/client/x/evmstaking/keeper/validator_test.go @@ -211,7 +211,8 @@ func (s *TestSuite) TestProcessCreateValidator() { if moniker == "" { moniker = "testing" } - err := keeper.HandleCreateValidatorEvent(cachedCtx, &bindings.IPTokenStakingCreateValidator{ + var err error + err = keeper.HandleCreateValidatorEvent(cachedCtx, &bindings.IPTokenStakingCreateValidator{ ValidatorUncmpPubkey: nil, ValidatorCmpPubkey: tc.valPubKeyBytes, Moniker: moniker, @@ -221,6 +222,14 @@ func (s *TestSuite) TestProcessCreateValidator() { MaxCommissionChangeRate: 500, // 5% Raw: gethtypes.Log{}, }) + if !keeper.MessageQueue.IsEmpty(cachedCtx) { + var queuedMsgs []*types.QueuedMessage + queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx) + + for _, msg := range queuedMsgs { + err = keeper.ProcessMsg(cachedCtx, msg) + } + } if tc.expectedError != "" { require.Error(err, tc.expectedError) } else { diff --git a/client/x/evmstaking/keeper/withdraw_test.go b/client/x/evmstaking/keeper/withdraw_test.go index f1b72c01..4ad11901 100644 --- a/client/x/evmstaking/keeper/withdraw_test.go +++ b/client/x/evmstaking/keeper/withdraw_test.go @@ -393,10 +393,19 @@ func (s *TestSuite) TestProcessWithdraw() { } cachedCtx, _ := ctx.CacheContext() // check undelegation does not exist - _, err := s.StakingKeeper.GetUnbondingDelegation(cachedCtx, delAddr1, valAddr) + var err error + _, err = s.StakingKeeper.GetUnbondingDelegation(cachedCtx, delAddr1, valAddr) require.ErrorContains(err, "no unbonding delegation found") err = keeper.HandleWithdrawEvent(cachedCtx, tc.withdraw) + if !keeper.MessageQueue.IsEmpty(cachedCtx) { + var queuedMsgs []*types.QueuedMessage + queuedMsgs, err = keeper.DequeueAllMsgs(cachedCtx) + + for _, msg := range queuedMsgs { + err = keeper.ProcessMsg(cachedCtx, msg) + } + } if tc.expectedErr != "" { require.ErrorContains(err, tc.expectedErr) } else {