From cd19291d73c8eb177f30e5bc440ee09aab14f277 Mon Sep 17 00:00:00 2001 From: dongsam Date: Thu, 24 Aug 2023 07:03:47 +0900 Subject: [PATCH] fix: set epoch duration same with staking unbonding time --- app/upgrades/v8/upgrades.go | 10 ++++++++-- app/upgrades/v8/upgrades_test.go | 16 +++++++++++++--- x/liquidstaking/genesis.go | 31 +++++++++++++++++-------------- x/liquidstaking/keeper/epoch.go | 9 ++++++++- x/liquidstaking/types/epoch.go | 7 +++---- x/liquidstaking/types/genesis.go | 7 ++++--- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/app/upgrades/v8/upgrades.go b/app/upgrades/v8/upgrades.go index 74c58ce3..52ece3a5 100644 --- a/app/upgrades/v8/upgrades.go +++ b/app/upgrades/v8/upgrades.go @@ -1,11 +1,12 @@ package v8 import ( - liquidstakingkeeper "github.com/Canto-Network/Canto/v7/x/liquidstaking/keeper" - liquidstakingtypes "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + + liquidstakingkeeper "github.com/Canto-Network/Canto/v7/x/liquidstaking/keeper" + liquidstakingtypes "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" ) // CreateUpgradeHandler creates an SDK upgrade handler for v8 @@ -26,6 +27,11 @@ func CreateUpgradeHandler( liquidstakingKeeper.SetParams(ctx, params) liquidstakingKeeper.SetLiquidBondDenom(ctx, liquidstakingtypes.DefaultLiquidBondDenom) + // epoch duration must be same with staking module's unbonding time + epoch := liquidstakingKeeper.GetEpoch(ctx) + epoch.Duration = liquidstakingKeeper.GetUnbondingTime(ctx) + liquidstakingKeeper.SetEpoch(ctx, epoch) + // Leave modules are as-is to avoid running InitGenesis. logger.Debug("running module migrations ...") return newVM, nil diff --git a/app/upgrades/v8/upgrades_test.go b/app/upgrades/v8/upgrades_test.go index 36cec52a..e3503a38 100644 --- a/app/upgrades/v8/upgrades_test.go +++ b/app/upgrades/v8/upgrades_test.go @@ -4,9 +4,6 @@ import ( "testing" "time" - chain "github.com/Canto-Network/Canto/v7/app" - v8 "github.com/Canto-Network/Canto/v7/app/upgrades/v8" - "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" sdk "github.com/cosmos/cosmos-sdk/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -16,6 +13,10 @@ import ( "github.com/stretchr/testify/suite" abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + chain "github.com/Canto-Network/Canto/v7/app" + v8 "github.com/Canto-Network/Canto/v7/app/upgrades/v8" + "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" ) type UpgradeTestSuite struct { @@ -90,6 +91,15 @@ func (s *UpgradeTestSuite) TestUpgradeV8() { liquidBondDenom := s.app.LiquidStakingKeeper.GetLiquidBondDenom(s.ctx) s.Require().EqualValues( liquidBondDenom, types.DefaultLiquidBondDenom) + + epoch := s.app.LiquidStakingKeeper.GetEpoch(s.ctx) + s.Require().EqualValues( + epoch, types.Epoch{ + CurrentNumber: 0, + StartTime: time.Time{}, + Duration: s.app.StakingKeeper.UnbondingTime(s.ctx), + StartHeight: 0, + }) }, true, }, diff --git a/x/liquidstaking/genesis.go b/x/liquidstaking/genesis.go index 5f245e60..f65090ed 100644 --- a/x/liquidstaking/genesis.go +++ b/x/liquidstaking/genesis.go @@ -1,9 +1,10 @@ package liquidstaking import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/Canto-Network/Canto/v7/x/liquidstaking/keeper" "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" - sdk "github.com/cosmos/cosmos-sdk/types" ) // InitGenesis initializes the capability module's state from a provided genesis @@ -12,6 +13,10 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) if err := genState.Validate(); err != nil { panic(err) } + stakingUnbondingTime := k.GetUnbondingTime(ctx) + if genState.Epoch.Duration != stakingUnbondingTime { + panic(types.ErrInvalidEpochDuration) + } k.SetParams(ctx, genState.Params) k.SetEpoch(ctx, genState.Epoch) k.SetLiquidBondDenom(ctx, genState.LiquidBondDenom) @@ -36,17 +41,15 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) // ExportGenesis returns the capability module's exported genesis. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { - genesis := types.DefaultGenesisState() - genesis.LiquidBondDenom = k.GetLiquidBondDenom(ctx) - genesis.Params = k.GetParams(ctx) - genesis.Epoch = k.GetEpoch(ctx) - genesis.LastChunkId = k.GetLastChunkId(ctx) - genesis.LastInsuranceId = k.GetLastInsuranceId(ctx) - genesis.Chunks = k.GetAllChunks(ctx) - genesis.Insurances = k.GetAllInsurances(ctx) - genesis.UnpairingForUnstakingChunkInfos = k.GetAllUnpairingForUnstakingChunkInfos(ctx) - genesis.WithdrawInsuranceRequests = k.GetAllWithdrawInsuranceRequests(ctx) - genesis.RedelegationInfos = k.GetAllRedelegationInfos(ctx) - - return genesis + return types.NewGenesisState( + k.GetLiquidBondDenom(ctx), + k.GetParams(ctx), + k.GetEpoch(ctx), + k.GetLastChunkId(ctx), + k.GetLastInsuranceId(ctx), + k.GetAllChunks(ctx), + k.GetAllInsurances(ctx), + k.GetAllUnpairingForUnstakingChunkInfos(ctx), + k.GetAllWithdrawInsuranceRequests(ctx), + k.GetAllRedelegationInfos(ctx)) } diff --git a/x/liquidstaking/keeper/epoch.go b/x/liquidstaking/keeper/epoch.go index 8ee76ee3..6764bd7b 100644 --- a/x/liquidstaking/keeper/epoch.go +++ b/x/liquidstaking/keeper/epoch.go @@ -1,8 +1,11 @@ package keeper import ( - "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/Canto-Network/Canto/v7/x/liquidstaking/types" ) func (k Keeper) GetEpoch(ctx sdk.Context) types.Epoch { @@ -31,3 +34,7 @@ func (k Keeper) IsEpochReached(ctx sdk.Context) bool { epoch := k.GetEpoch(ctx) return !ctx.BlockTime().Before(epoch.StartTime.Add(epoch.Duration)) } + +func (k Keeper) GetUnbondingTime(ctx sdk.Context) time.Duration { + return k.stakingKeeper.UnbondingTime(ctx) +} diff --git a/x/liquidstaking/types/epoch.go b/x/liquidstaking/types/epoch.go index b673298c..f61103d6 100644 --- a/x/liquidstaking/types/epoch.go +++ b/x/liquidstaking/types/epoch.go @@ -1,14 +1,13 @@ package types import ( + "fmt" "time" - - "github.com/cosmos/cosmos-sdk/x/staking/types" ) func (e *Epoch) Validate() error { - if e.Duration != types.DefaultUnbondingTime { - return ErrInvalidEpochDuration + if e.Duration <= 0 { + return fmt.Errorf("duration must be positive: %d", e.Duration) } // Comment the following lines checking StartTime when enable advance epoch mode. if !e.StartTime.Before(time.Now()) { diff --git a/x/liquidstaking/types/genesis.go b/x/liquidstaking/types/genesis.go index 24f7e282..b356bdfc 100644 --- a/x/liquidstaking/types/genesis.go +++ b/x/liquidstaking/types/genesis.go @@ -8,6 +8,7 @@ import ( // NewGenesisState creates a new GenesisState instance. func NewGenesisState( + liquidBondDenom string, params Params, epoch Epoch, lastChunkId, lastInsuranceId uint64, @@ -16,9 +17,9 @@ func NewGenesisState( infos []UnpairingForUnstakingChunkInfo, reqs []WithdrawInsuranceRequest, reDelInfos []RedelegationInfo, -) GenesisState { - return GenesisState{ - LiquidBondDenom: DefaultLiquidBondDenom, +) *GenesisState { + return &GenesisState{ + LiquidBondDenom: liquidBondDenom, Params: params, Epoch: epoch, LastChunkId: lastChunkId,