Skip to content

Commit

Permalink
feat: use values from keeper (#49)
Browse files Browse the repository at this point in the history
* 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 <dongsamb@gmail.com>
  • Loading branch information
zsystm and dongsam authored Aug 23, 2023
1 parent c32d3e8 commit 42cd98f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 31 deletions.
11 changes: 9 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 @@ -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 ...")
Expand Down
20 changes: 17 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 @@ -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,
},
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
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)
}
3 changes: 2 additions & 1 deletion x/liquidstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
),
),
})
Expand Down
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 42cd98f

Please sign in to comment.