From 42cd98f48326402e227a9b9ee54ffb50fe9a3ec6 Mon Sep 17 00:00:00 2001 From: Hyunwoo Lee <124245155+zsystm@users.noreply.github.com> Date: Thu, 24 Aug 2023 07:49:04 +0900 Subject: [PATCH] feat: use values from keeper (#49) * set liquid bond denom at upgrade and msg_server * add tc for liquidBondDenom at upgrade * fix: set epoch duration same with staking unbonding time --------- Co-authored-by: dongsam --- app/upgrades/v8/upgrades.go | 11 ++++++++-- app/upgrades/v8/upgrades_test.go | 20 +++++++++++++++--- go.sum | 3 --- x/liquidstaking/genesis.go | 31 +++++++++++++++------------- x/liquidstaking/keeper/epoch.go | 9 +++++++- x/liquidstaking/keeper/msg_server.go | 3 ++- x/liquidstaking/types/epoch.go | 7 +++---- x/liquidstaking/types/genesis.go | 7 ++++--- 8 files changed, 60 insertions(+), 31 deletions(-) diff --git a/app/upgrades/v8/upgrades.go b/app/upgrades/v8/upgrades.go index 4abffed0..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 @@ -24,6 +25,12 @@ func CreateUpgradeHandler( params := liquidstakingtypes.DefaultParams() 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 ...") diff --git a/app/upgrades/v8/upgrades_test.go b/app/upgrades/v8/upgrades_test.go index 9a34c655..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 { @@ -86,6 +87,19 @@ func (s *UpgradeTestSuite) TestUpgradeV8() { params.DynamicFeeRate.MaxFeeRate, types.DefaultMaxFee) s.Require().EqualValues( params.MaximumDiscountRate, types.DefaultMaximumDiscountRate) + + 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/go.sum b/go.sum index 36d22fee..2af4e3b9 100644 --- a/go.sum +++ b/go.sum @@ -315,7 +315,6 @@ github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5Nq github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= @@ -404,7 +403,6 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -829,7 +827,6 @@ github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNG github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= -github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= github.com/tendermint/btcd v0.1.1 h1:0VcxPfflS2zZ3RiOAHkBiFUcPvbtRj5O7zHmcJWHV7s= github.com/tendermint/btcd v0.1.1/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RMWx1aInLzndwxKalgi5rTqgfXxOxbEI= 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/keeper/msg_server.go b/x/liquidstaking/keeper/msg_server.go index 6379af3b..5994a1dd 100644 --- a/x/liquidstaking/keeper/msg_server.go +++ b/x/liquidstaking/keeper/msg_server.go @@ -23,6 +23,7 @@ func (k Keeper) LiquidStake(goCtx context.Context, msg *types.MsgLiquidStake) (* for _, chunk := range chunks { chunkIds = append(chunkIds, strconv.FormatUint(chunk.Id, 10)) } + liquidBondDenom := k.GetLiquidBondDenom(ctx) ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( sdk.EventTypeMessage, @@ -36,7 +37,7 @@ func (k Keeper) LiquidStake(goCtx context.Context, msg *types.MsgLiquidStake) (* sdk.NewAttribute(types.AttributeKeyNewShares, totalNewShares.String()), sdk.NewAttribute( types.AttributeKeyLsTokenMintedAmount, - sdk.Coin{Denom: types.DefaultLiquidBondDenom, Amount: totalLsTokenMintAmount}.String(), + sdk.Coin{Denom: liquidBondDenom, Amount: totalLsTokenMintAmount}.String(), ), ), }) 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,