Skip to content

Commit

Permalink
V9 Upgrade Handler (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Apr 21, 2023
1 parent 62295e3 commit b8d5152
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
path = deps/hermes
url = https://github.com/informalsystems/ibc-rs.git
[submodule "deps/relayer"]
# Commit: v2.3.0-rc3
# Commit: v2.3.1
path = deps/relayer
url = https://github.com/cosmos/relayer.git
[submodule "deps/gaia"]
Expand Down
7 changes: 7 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
v6 "github.com/Stride-Labs/stride/v8/app/upgrades/v6"
v7 "github.com/Stride-Labs/stride/v8/app/upgrades/v7"
v8 "github.com/Stride-Labs/stride/v8/app/upgrades/v8"
v9 "github.com/Stride-Labs/stride/v8/app/upgrades/v9"
autopilottypes "github.com/Stride-Labs/stride/v8/x/autopilot/types"
claimtypes "github.com/Stride-Labs/stride/v8/x/claim/types"
icacallbacktypes "github.com/Stride-Labs/stride/v8/x/icacallbacks/types"
Expand Down Expand Up @@ -97,6 +98,12 @@ func (app *StrideApp) setupUpgradeHandlers() {
),
)

// v9 upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v9.UpgradeName,
v9.CreateUpgradeHandler(app.mm, app.configurator, app.ClaimKeeper),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("Failed to read upgrade info from disk: %w", err))
Expand Down
15 changes: 15 additions & 0 deletions app/upgrades/v9/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v9

var (
UpgradeName = "v9"

EvmosAirdropId = "evmos"
AirdropChainIds = map[string]string{
"stride": "stride-1",
"gaia": "cosmoshub-4",
"osmosis": "osmosis-1",
"juno": "juno-1",
"stars": "stargaze-1",
"evmos": "evmos_9001-2",
}
)
64 changes: 64 additions & 0 deletions app/upgrades/v9/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package v9

import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

claimkeeper "github.com/Stride-Labs/stride/v8/x/claim/keeper"
)

// CreateUpgradeHandler creates an SDK upgrade handler for v29
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
claimKeeper claimkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade v9...")

if err := AddFieldsToAirdropType(ctx, claimKeeper); err != nil {
return vm, errorsmod.Wrapf(err, "unable to update airdrop schema")
}

ctx.Logger().Info("Running module migrations...")
return mm.RunMigrations(ctx, configurator, vm)
}
}

func AddFieldsToAirdropType(ctx sdk.Context, claimKeeper claimkeeper.Keeper) error {
ctx.Logger().Info("Adding additional fields to airdrop struct...")

// Get list of airdrops from claim parameters
claimParams, err := claimKeeper.GetParams(ctx)
if err != nil {
return errorsmod.Wrapf(err, "unable to get claim parameters")
}

for _, airdrop := range claimParams.Airdrops {
// Add the chain ID to each airdrop
chainId, ok := AirdropChainIds[airdrop.AirdropIdentifier]
if !ok {
ctx.Logger().Error(fmt.Sprintf("Chain ID not specified for %s airdrop", chainId))
continue
}
airdrop.ChainId = chainId

// Enable autopilot for evmos only
if airdrop.AirdropIdentifier == EvmosAirdropId {
airdrop.AutopilotEnabled = true
} else {
airdrop.AutopilotEnabled = false
}
}

// Update list of airdrops
if err := claimKeeper.SetParams(ctx, claimParams); err != nil {
return errorsmod.Wrapf(err, "unable to set claim parameters")
}

return nil
}
101 changes: 101 additions & 0 deletions app/upgrades/v9/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package v9_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"

"github.com/Stride-Labs/stride/v8/app"
"github.com/Stride-Labs/stride/v8/app/apptesting"
v9 "github.com/Stride-Labs/stride/v8/app/upgrades/v9"
"github.com/Stride-Labs/stride/v8/utils"

claimtypes "github.com/Stride-Labs/stride/v8/x/claim/types"

// This isn't the exact type host zone schema as the one that's will be in the store
// before the upgrade, but the only thing that matters, for the sake of the test,
// is that it doesn't have min/max redemption rate as attributes
"github.com/Stride-Labs/stride/v8/x/claim/migrations/v2/types"
oldclaimtypes "github.com/Stride-Labs/stride/v8/x/claim/migrations/v2/types"
)

type UpgradeTestSuite struct {
apptesting.AppTestHelper
}

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

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

func (s *UpgradeTestSuite) TestUpgrade() {
s.Setup()

dummyUpgradeHeight := int64(5)

s.SetupAirdropsBeforeUpgrade()
s.ConfirmUpgradeSucceededs("v9", dummyUpgradeHeight)
s.CheckAirdropsAfterUpgrade()
}

func (s *UpgradeTestSuite) SetupAirdropsBeforeUpgrade() {
// Create a list of airdrops of the old data type
airdrops := []*oldclaimtypes.Airdrop{}
for i, identifier := range utils.StringMapKeys(v9.AirdropChainIds) {
airdrops = append(airdrops, &oldclaimtypes.Airdrop{
AirdropIdentifier: identifier,
ClaimDenom: fmt.Sprintf("denom-%d", i),
})
}

// Add in another airdrop that's not in the map
airdrops = append(airdrops, &types.Airdrop{
AirdropIdentifier: "different_airdrop",
})

// Store the airdrops using the old schema
codec := app.MakeEncodingConfig().Marshaler
claimStore := s.Ctx.KVStore(s.App.GetKey(claimtypes.StoreKey))

paramsBz, err := codec.MarshalJSON(&oldclaimtypes.Params{Airdrops: airdrops})
s.Require().NoError(err, "no error expected when marshalling claim params")
claimStore.Set([]byte(claimtypes.ParamsKey), paramsBz)
}

func (s *UpgradeTestSuite) CheckAirdropsAfterUpgrade() {
// Read in the airdrops using the new schema - which should include chainId and AirdropEnabled
claimParams, err := s.App.ClaimKeeper.GetParams(s.Ctx)
s.Require().NoError(err, "no error expected when getting claims params")
s.Require().Len(claimParams.Airdrops, len(v9.AirdropChainIds)+1, "number of airdrops after migration")

// Confirm the new fields were added and the old fields (e.g. ChainDenom) remain the same
for i, identifier := range utils.StringMapKeys(v9.AirdropChainIds) {
expectedChainId := v9.AirdropChainIds[identifier]
expectedDenom := fmt.Sprintf("denom-%d", i)
expectedAutopilotEnabled := identifier == v9.EvmosAirdropId

actual := claimParams.Airdrops[i]
s.Require().Equal(identifier, actual.AirdropIdentifier, "identifier after migration")
s.Require().Equal(expectedChainId, actual.ChainId, "chain-id after migration")
s.Require().Equal(expectedDenom, actual.ClaimDenom, "denom after migration")
s.Require().Equal(expectedAutopilotEnabled, actual.AutopilotEnabled, "autopilot enabled after migration")
}

// Confirm the airdrop that was not in the map
airdropWithoutChainId := claimParams.Airdrops[len(v9.AirdropChainIds)]
s.Require().Equal("different_airdrop", airdropWithoutChainId.AirdropIdentifier, "airdrop id for outsider")
s.Require().Equal("", airdropWithoutChainId.ChainId, "chain-id for outsider")
}

func (s *UpgradeTestSuite) TestAddFieldsToAirdropType() {
s.SetupAirdropsBeforeUpgrade()

err := v9.AddFieldsToAirdropType(s.Ctx, s.App.ClaimKeeper)
s.Require().NoError(err, "no error expected when migrating airdrop schema")

s.CheckAirdropsAfterUpgrade()
}

0 comments on commit b8d5152

Please sign in to comment.