Skip to content

Commit

Permalink
fix: set epoch duration same with staking unbonding time
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed Aug 23, 2023
1 parent b1a16de commit cd19291
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
10 changes: 8 additions & 2 deletions app/upgrades/v8/upgrades.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions app/upgrades/v8/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
},
Expand Down
31 changes: 17 additions & 14 deletions x/liquidstaking/genesis.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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))
}
9 changes: 8 additions & 1 deletion x/liquidstaking/keeper/epoch.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
7 changes: 3 additions & 4 deletions x/liquidstaking/types/epoch.go
Original file line number Diff line number Diff line change
@@ -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()) {
Expand Down
7 changes: 4 additions & 3 deletions x/liquidstaking/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// NewGenesisState creates a new GenesisState instance.
func NewGenesisState(
liquidBondDenom string,
params Params,
epoch Epoch,
lastChunkId, lastInsuranceId uint64,
Expand All @@ -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,
Expand Down

0 comments on commit cd19291

Please sign in to comment.