Skip to content

Commit

Permalink
Inflation Fix (#951)
Browse files Browse the repository at this point in the history
Co-authored-by: sampocs <[email protected]>
  • Loading branch information
shellvish and sampocs authored Oct 14, 2023
1 parent 80c5db3 commit 876810c
Show file tree
Hide file tree
Showing 18 changed files with 885 additions and 104 deletions.
12 changes: 12 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
v13 "github.com/Stride-Labs/stride/v15/app/upgrades/v13"
v14 "github.com/Stride-Labs/stride/v15/app/upgrades/v14"
v15 "github.com/Stride-Labs/stride/v15/app/upgrades/v15"
v16 "github.com/Stride-Labs/stride/v15/app/upgrades/v16"
v2 "github.com/Stride-Labs/stride/v15/app/upgrades/v2"
v3 "github.com/Stride-Labs/stride/v15/app/upgrades/v3"
v4 "github.com/Stride-Labs/stride/v15/app/upgrades/v4"
Expand Down Expand Up @@ -204,6 +205,17 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
),
)

// v16 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v16.UpgradeName,
v16.CreateUpgradeHandler(
app.mm,
app.configurator,
app.StakeibcKeeper,
app.RatelimitKeeper,
),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("Failed to read upgrade info from disk: %w", err))
Expand Down
26 changes: 19 additions & 7 deletions app/upgrades/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ func (app *StrideApp) setupUpgradeHandlers() {
}
```
# Migrations (Only required if the state changed)
## Store Old Proto Types
## Migrations (Only required if the state changed)
### Store Old Proto Types
```go
// x/{moduleName}/migrations/{oldVersion}/types/{data_type}.pb.go
```
## Increment the Module's Consensus Version
### Increment the Module's Consensus Version
* The consensus version is different from the chain version - it is specific to each module and is incremented every time state is migrated
```go
// x/{moduleName}/module.go
func (AppModule) ConsensusVersion() uint64 { return 2 }
```
## Define Migration Logic
### Define Migration Logic
```go
// x/{moduleName}/migrations/{new-consensus-version}/migrations.go
package {upgradeVersion}
Expand All @@ -93,7 +93,7 @@ func MigrateStore(ctx sdk.Context) error {
}
```
## Specify the Migration in the Upgrade Handler
### Specify the Migration in the Upgrade Handler
```go
// app/upgrades/{upgradeVersion}/upgrades.go

Expand All @@ -118,10 +118,22 @@ func CreateUpgradeHandler(
}
```
## Add Additional Parameters to `CreateUpgradeHandler` Invocation
### Add Additional Parameters to `CreateUpgradeHandler` Invocation
```go
// app/upgrades.go
...
{upgradeVersion}.CreateUpgradeHandler(app.mm, app.configurator, app.appCodec, app.{module}Keeper),
...
```
```
## Import Paths
* Go to GitHub Actions and manually trigger the "Version" job
* This will replace the import path of each file and open a new PR with the changes
* To make the review easier, you can pipe the diffs into a file and open them in an editor which will let you quickly scroll through them (rather than having to wait for each file to render on github)
```
git diff origin/main..origin/actions/update-stride-version-{upgradeVersion} > diffs.txt
```
## Changelog
* Go to GitHub Actions and manually trigger the "Changelog" job
* This will open a PR with the changes
46 changes: 46 additions & 0 deletions app/upgrades/v16/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v16

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

ratelimitkeeper "github.com/Stride-Labs/stride/v15/x/ratelimit/keeper"
stakeibckeeper "github.com/Stride-Labs/stride/v15/x/stakeibc/keeper"
stakeibctypes "github.com/Stride-Labs/stride/v15/x/stakeibc/types"
)

var (
UpgradeName = "v16"

CosmosHubChainId = "cosmoshub-4"
CosmosHubStToken = "stuatom"
)

// CreateUpgradeHandler creates an SDK upgrade handler for v15
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
stakeibcKeeper stakeibckeeper.Keeper,
ratelimitKeeper ratelimitkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade v16...")

// unhalt Cosmos Hub host zone
ctx.Logger().Info("Unhalting Cosmos Hub...")
hostZone, found := stakeibcKeeper.GetHostZone(ctx, CosmosHubChainId)
if !found {
return vm, stakeibctypes.ErrHostZoneNotFound.Wrap(CosmosHubChainId)
}

hostZone.Halted = false
stakeibcKeeper.SetHostZone(ctx, hostZone)

// remove stuatom from rate limits
ctx.Logger().Info("Removing stuatom as a blacklisted asset...")
ratelimitKeeper.RemoveDenomFromBlacklist(ctx, CosmosHubStToken)

return mm.RunMigrations(ctx, configurator, vm)
}
}
85 changes: 85 additions & 0 deletions app/upgrades/v16/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package v16_test

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/suite"

"github.com/Stride-Labs/stride/v15/app/apptesting"
stakeibctypes "github.com/Stride-Labs/stride/v15/x/stakeibc/types"
)

type UpgradeTestSuite struct {
apptesting.AppTestHelper
}

func (s *UpgradeTestSuite) SetupTest() {
s.Setup()
}

var (
CosmosHubChainIdTest = "cosmoshub-4"
)

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

func (s *UpgradeTestSuite) TestUpgrade() {
dummyUpgradeHeight := int64(5)

// Setup the store before the ugprade
checkCosmosHubAfterUpgrade := s.SetupHostZonesBeforeUpgrade()

// Run the upgrade to set the bounds and clear pending queries
s.ConfirmUpgradeSucceededs("v16", dummyUpgradeHeight)

// Check the store after the upgrade
checkCosmosHubAfterUpgrade()
}

func (s *UpgradeTestSuite) SetupHostZonesBeforeUpgrade() func() {

// Create 10 dummy host zones
for i := 0; i < 10; i++ {
chainId := fmt.Sprintf("chain-%d", i)

hostZone := stakeibctypes.HostZone{
ChainId: chainId,
Halted: false,
RedemptionRate: sdk.MustNewDecFromStr("1.0"),
MinInnerRedemptionRate: sdk.MustNewDecFromStr("0.95"),
MinRedemptionRate: sdk.MustNewDecFromStr("0.97"),
MaxInnerRedemptionRate: sdk.MustNewDecFromStr("1.05"),
MaxRedemptionRate: sdk.MustNewDecFromStr("1.10"),
}
s.App.StakeibcKeeper.SetHostZone(s.Ctx, hostZone)
}
// create Cosmos Hub Host Zone
hostZone := stakeibctypes.HostZone{
ChainId: CosmosHubChainIdTest,
Halted: true,
RedemptionRate: sdk.MustNewDecFromStr("1.0"),
MinInnerRedemptionRate: sdk.MustNewDecFromStr("0.95"),
MinRedemptionRate: sdk.MustNewDecFromStr("0.97"),
MaxInnerRedemptionRate: sdk.MustNewDecFromStr("1.05"),
MaxRedemptionRate: sdk.MustNewDecFromStr("1.10"),
}
s.App.StakeibcKeeper.SetHostZone(s.Ctx, hostZone)

return func() {

hostZones := s.App.StakeibcKeeper.GetAllHostZone(s.Ctx)

for _, hostZone := range hostZones {
s.Require().False(hostZone.Halted, fmt.Sprintf("host zone %s should not be halted: %v", hostZone.ChainId, hostZone))
}
// Confirm Cosmos Hub host zone is not unhalted
cosmosHubHostZone, found := s.App.StakeibcKeeper.GetHostZone(s.Ctx, CosmosHubChainIdTest)
s.Require().True(found, "Cosmos Hub host zone not found!")
s.Require().False(cosmosHubHostZone.Halted, "Cosmos Hub host zone should not be halted")
}
}
7 changes: 7 additions & 0 deletions proto/stride/stakeibc/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ service Msg {
rpc UndelegateHost(MsgUndelegateHost) returns (MsgUndelegateHostResponse);
rpc UpdateInnerRedemptionRateBounds(MsgUpdateInnerRedemptionRateBounds)
returns (MsgUpdateInnerRedemptionRateBoundsResponse);
rpc ResumeHostZone(MsgResumeHostZone) returns (MsgResumeHostZoneResponse);
}

message MsgUpdateInnerRedemptionRateBounds {
Expand Down Expand Up @@ -188,3 +189,9 @@ message MsgCalibrateDelegation {
string valoper = 3;
}
message MsgCalibrateDelegationResponse {}

message MsgResumeHostZone {
string creator = 1;
string chain_id = 2;
}
message MsgResumeHostZoneResponse {}
1 change: 1 addition & 0 deletions x/stakeibc/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdClearBalance())
cmd.AddCommand(CmdUndelegateHost())
cmd.AddCommand(CmdUpdateInnerRedemptionRateBounds())
cmd.AddCommand(CmdResumeHostZone())

return cmd
}
39 changes: 39 additions & 0 deletions x/stakeibc/client/cli/tx_resume_host_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"

"github.com/Stride-Labs/stride/v15/x/stakeibc/types"
)

func CmdResumeHostZone() *cobra.Command {
cmd := &cobra.Command{
Use: "resume-host-zone [chainid]",
Short: "Broadcast message resume-host-zone",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argChainId := args[0]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgResumeHostZone(
clientCtx.GetFromAddress().String(),
argChainId,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
3 changes: 3 additions & 0 deletions x/stakeibc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewMessageHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgUpdateInnerRedemptionRateBounds:
res, err := msgServer.UpdateInnerRedemptionRateBounds(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgResumeHostZone:
res, err := msgServer.ResumeHostZone(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
4 changes: 4 additions & 0 deletions x/stakeibc/keeper/msg_server_lsm_liquid_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (k Keeper) StartLSMLiquidStake(ctx sdk.Context, msg types.MsgLSMLiquidStake
}
hostZone := lsmLiquidStake.HostZone

if hostZone.Halted {
return types.LSMLiquidStake{}, errorsmod.Wrapf(types.ErrHaltedHostZone, "host zone %s is halted", hostZone.ChainId)
}

// Check if we already have tokens with this denom in records
_, found := k.RecordsKeeper.GetLSMTokenDeposit(ctx, hostZone.ChainId, lsmLiquidStake.Deposit.Denom)
if found {
Expand Down
40 changes: 40 additions & 0 deletions x/stakeibc/keeper/msg_server_resume_host_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keeper

import (
"context"
"fmt"

"github.com/Stride-Labs/stride/v15/x/stakeibc/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k msgServer) ResumeHostZone(goCtx context.Context, msg *types.MsgResumeHostZone) (*types.MsgResumeHostZoneResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Get Host Zone
hostZone, found := k.GetHostZone(ctx, msg.ChainId)
if !found {
errMsg := fmt.Sprintf("invalid chain id, zone for %s not found", msg.ChainId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrHostZoneNotFound, errMsg)
}

// Check the zone is halted
if !hostZone.Halted {
errMsg := fmt.Sprintf("invalid chain id, zone for %s not halted", msg.ChainId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrHostZoneNotHalted, errMsg)
}

// remove from blacklist
stDenom := types.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)
k.RatelimitKeeper.RemoveDenomFromBlacklist(ctx, stDenom)

// Resume zone
hostZone.Halted = false
k.SetHostZone(ctx, hostZone)

return &types.MsgResumeHostZoneResponse{}, nil
}
Loading

0 comments on commit 876810c

Please sign in to comment.