From 0d777d2a3d1a353e8feed94216cee6310bbf6dac Mon Sep 17 00:00:00 2001 From: puneetmahajan Date: Wed, 29 May 2024 16:39:54 +0400 Subject: [PATCH] move rebalancing to day epoch. --- x/liquidstake/abci.go | 21 --------------------- x/liquidstake/keeper/hooks.go | 7 ++++++- x/liquidstake/keeper/keeper_test.go | 11 +++-------- x/liquidstake/module.go | 10 ++++------ x/liquidstake/types/keys.go | 1 + 5 files changed, 14 insertions(+), 36 deletions(-) delete mode 100644 x/liquidstake/abci.go diff --git a/x/liquidstake/abci.go b/x/liquidstake/abci.go deleted file mode 100644 index 79f08e949..000000000 --- a/x/liquidstake/abci.go +++ /dev/null @@ -1,21 +0,0 @@ -package liquidstake - -import ( - "time" - - "github.com/cosmos/cosmos-sdk/telemetry" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/persistenceOne/pstake-native/v2/x/liquidstake/keeper" - "github.com/persistenceOne/pstake-native/v2/x/liquidstake/types" -) - -// BeginBlocker updates liquid validator set changes for the current block -func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - - if !k.GetParams(ctx).ModulePaused { - // return value of UpdateLiquidValidatorSet is useful only in testing - _ = k.UpdateLiquidValidatorSet(ctx) - } -} diff --git a/x/liquidstake/keeper/hooks.go b/x/liquidstake/keeper/hooks.go index 776b48752..9b2f7a5ff 100644 --- a/x/liquidstake/keeper/hooks.go +++ b/x/liquidstake/keeper/hooks.go @@ -26,12 +26,17 @@ func (h EpochHooks) AfterEpochEnd(_ sdk.Context, _ string, _ int64) error { return nil } -func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error { +func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, _ int64) error { if !k.GetParams(ctx).ModulePaused { // Update the liquid validator set at the start of each epoch if epochIdentifier == liquidstake.AutocompoundEpoch { k.AutocompoundStakingRewards(ctx, liquidstake.GetWhitelistedValsMap(k.GetParams(ctx).WhitelistedValidators)) } + + if epochIdentifier == liquidstake.RebalanceEpoch { + // return value of UpdateLiquidValidatorSet is useful only in testing + _ = k.UpdateLiquidValidatorSet(ctx) + } } return nil diff --git a/x/liquidstake/keeper/keeper_test.go b/x/liquidstake/keeper/keeper_test.go index cfd1da6cd..22478bdd5 100644 --- a/x/liquidstake/keeper/keeper_test.go +++ b/x/liquidstake/keeper/keeper_test.go @@ -23,7 +23,6 @@ import ( chain "github.com/persistenceOne/pstake-native/v2/app" testhelpers "github.com/persistenceOne/pstake-native/v2/app/helpers" - "github.com/persistenceOne/pstake-native/v2/x/liquidstake" "github.com/persistenceOne/pstake-native/v2/x/liquidstake/keeper" "github.com/persistenceOne/pstake-native/v2/x/liquidstake/types" ) @@ -83,7 +82,7 @@ func (s *KeeperTestSuite) CreateValidators(powers []int64) ([]sdk.AccAddress, [] pks := testhelpers.CreateTestPubKeys(num) skParams := s.app.StakingKeeper.GetParams(s.ctx) skParams.ValidatorLiquidStakingCap = sdk.OneDec() - s.app.StakingKeeper.SetParams(s.ctx, skParams) + _ = s.app.StakingKeeper.SetParams(s.ctx, skParams) for i, power := range powers { val, err := stakingtypes.NewValidator(valAddrs[i], pks[i], stakingtypes.Description{}) s.Require().NoError(err) @@ -91,7 +90,7 @@ func (s *KeeperTestSuite) CreateValidators(powers []int64) ([]sdk.AccAddress, [] err = s.app.StakingKeeper.SetValidatorByConsAddr(s.ctx, val) s.Require().NoError(err) s.app.StakingKeeper.SetNewValidatorByPowerIndex(s.ctx, val) - s.app.StakingKeeper.Hooks().AfterValidatorCreated(s.ctx, val.GetOperator()) + _ = s.app.StakingKeeper.Hooks().AfterValidatorCreated(s.ctx, val.GetOperator()) newShares, err := s.app.StakingKeeper.Delegate(s.ctx, addrs[i], math.NewInt(power), stakingtypes.Unbonded, val, true) s.Require().NoError(err) s.Require().Equal(newShares.TruncateInt(), math.NewInt(power)) @@ -302,7 +301,7 @@ func (s *KeeperTestSuite) printRedelegationsLiquidTokens() { } } -func (s *KeeperTestSuite) advanceHeight(height int, withBeginBlock bool) { +func (s *KeeperTestSuite) advanceHeight(height int, _ bool) { feeCollector := s.app.AccountKeeper.GetModuleAddress( authtypes.FeeCollectorName, ) @@ -373,10 +372,6 @@ func (s *KeeperTestSuite) advanceHeight(height int, withBeginBlock bool) { ) s.app.DistrKeeper.SetFeePool(s.ctx, feePool) - if withBeginBlock { - // liquid validator set update and rebalancing - liquidstake.BeginBlocker(s.ctx, s.app.LiquidStakeKeeper) - } staking.EndBlocker(s.ctx, s.app.StakingKeeper) } diff --git a/x/liquidstake/module.go b/x/liquidstake/module.go index df1082fcc..adb3eaf9c 100644 --- a/x/liquidstake/module.go +++ b/x/liquidstake/module.go @@ -45,7 +45,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { } // ValidateGenesis performs genesis state validation for the liquidstake module. -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config sdkclient.TxEncodingConfig, bz json.RawMessage) error { +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) @@ -102,7 +102,7 @@ func (AppModule) Name() string { } // RegisterInvariants registers the liquidstake module invariants. -func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // QuerierRoute returns the liquidstake module's querier route name. func (AppModule) QuerierRoute() string { @@ -135,12 +135,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw func (AppModule) ConsensusVersion() uint64 { return 1 } // BeginBlock returns the begin blocker for the liquidstake module. -func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { - BeginBlocker(ctx, am.keeper) -} +func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} // EndBlock returns the end blocker for the liquidstake module. It returns no validator // updates. -func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } diff --git a/x/liquidstake/types/keys.go b/x/liquidstake/types/keys.go index e1cd7cd08..9935daa2b 100644 --- a/x/liquidstake/types/keys.go +++ b/x/liquidstake/types/keys.go @@ -21,6 +21,7 @@ const ( // Epoch identifiers AutocompoundEpoch = "hour" + RebalanceEpoch = "day" ) var (