Skip to content

Commit

Permalink
upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lacsomot committed Oct 18, 2024
1 parent dc78498 commit ea841b6
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 14 deletions.
47 changes: 43 additions & 4 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,46 @@ package app
import (
"fmt"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

storetypes "cosmossdk.io/store/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/realiotech/realio-network/app/upgrades/commission"
<<<<<<< Updated upstream

Check failure on line 11 in app/upgrades.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

missing import path
"github.com/realiotech/realio-network/app/upgrades/sdk50"
=======
v2 "github.com/realiotech/realio-network/app/upgrades/v2"
>>>>>>> Stashed changes

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

// BaseAppParamManager defines an interrace that BaseApp is expected to fullfil
// that allows upgrade handlers to modify BaseApp parameters.
type BaseAppParamManager interface {
GetConsensusParams(ctx sdk.Context) *tmproto.ConsensusParams
StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusParams)
}

// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal
// must have written, in order for the state migration to go smoothly.
// An upgrade must implement this struct, and then set it in the app.go.
// The app.go will then define the handler.
type Upgrade struct {
// Upgrade version name, for the upgrade handler, e.g. `v3`
UpgradeName string

// CreateUpgradeHandler defines the function that creates an upgrade handler
CreateUpgradeHandler func(*module.Manager, module.Configurator, BaseAppParamManager) upgradetypes.UpgradeHandler

// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades storetypes.StoreUpgrades
}

func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
// commission
app.UpgradeKeeper.SetUpgradeHandler(
commission.UpgradeName,
commission.CreateUpgradeHandler(
Expand All @@ -22,10 +53,20 @@ func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
)

app.UpgradeKeeper.SetUpgradeHandler(
<<<<<<< Updated upstream
sdk50.UpgradeName,
sdk50.CreateUpgradeHandler(
app.mm,
app.configurator,
=======
v2.UpgradeName,
v2.CreateUpgradeHandler(
app.mm,
app.configurator,
app.ParamsKeeper,
app.ConsensusParamsKeeper,
*app.IBCKeeper,
>>>>>>> Stashed changes
),
)

Expand All @@ -38,9 +79,7 @@ func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
return
}

var storeUpgrades *storetypes.StoreUpgrades

if storeUpgrades != nil {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades))
if upgradeInfo.Name == v2.UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &v2.V2StoreUpgrades))
}
}
23 changes: 17 additions & 6 deletions app/upgrades/v2/constants.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package v2

import (
storetypes "cosmossdk.io/store/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
bridgetypes "github.com/realiotech/realio-network/x/bridge/types"
)

const (
// UpgradeName is the shared upgrade plan name for mainnet and testnet
// UpgradeName = "v2.0.0"
// MainnetUpgradeHeight defines the Realio Network mainnet block height on which the upgrade will take place
// MainnetUpgradeHeight =
// UpgradeInfo defines the binaries that will be used for the upgrade
// UpgradeInfo = `'{"binaries":{"darwin/arm64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Darwin_arm64.tar.gz","darwin/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Darwin_x86_64.tar.gz","linux/arm64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Linux_arm64.tar.gz","linux/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Linux_x86_64.tar.gz","windows/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Windows_x86_64.zip"}}'`
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "V2"
)

var V2StoreUpgrades = storetypes.StoreUpgrades{
Added: []string{
consensustypes.ModuleName,
crisistypes.ModuleName,
bridgetypes.ModuleName,
},
}
59 changes: 56 additions & 3 deletions app/upgrades/v2/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,71 @@ import (
"context"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
)

// CreateUpgradeHandler creates an SDK upgrade handler for v2
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
paramskeeper paramskeeper.Keeper,
consensuskeeper consensusparamkeeper.Keeper,
IBCKeeper ibckeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// Refs:
// - https://docs.cosmos.network/master/building-modules/upgrade.html#registering-migrations
// - https://docs.cosmos.network/master/migrations/chain-upgrade-guide-044.html#chain-upgrade

for _, subspace := range paramskeeper.GetSubspaces() {
subspace := subspace

var keyTable paramstypes.KeyTable
switch subspace.Name() {
case authtypes.ModuleName:
keyTable = authtypes.ParamKeyTable() //nolint:staticcheck
case banktypes.ModuleName:
keyTable = banktypes.ParamKeyTable() //nolint:staticcheck
case stakingtypes.ModuleName:
keyTable = stakingtypes.ParamKeyTable() //nolint:staticcheck
case minttypes.ModuleName:
keyTable = minttypes.ParamKeyTable() //nolint:staticcheck
case distrtypes.ModuleName:
keyTable = distrtypes.ParamKeyTable() //nolint:staticcheck
case slashingtypes.ModuleName:
keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable() //nolint:staticcheck
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck
}

if !subspace.HasKeyTable() {
subspace.WithKeyTable(keyTable)
}
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
legacyBaseAppSubspace := paramskeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
baseapp.MigrateParams(sdkCtx, legacyBaseAppSubspace, consensuskeeper.ParamsStore)

params := IBCKeeper.ClientKeeper.GetParams(sdkCtx)
params.AllowedClients = append(params.AllowedClients, exported.Localhost)
IBCKeeper.ClientKeeper.SetParams(sdkCtx, params)

return mm.RunMigrations(ctx, configurator, vm)
}
}
17 changes: 16 additions & 1 deletion x/bridge/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package bridge_test

import (
"fmt"
"testing"

"cosmossdk.io/math"
"github.com/stretchr/testify/require"

abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/realiotech/realio-network/app"
"github.com/realiotech/realio-network/x/bridge/types"
)

func TestRateLimitTrigger(t *testing.T) {
// init app
realio := app.Setup(false, nil, 1)
ctx := realio.BaseApp.NewContext(true)
err := realio.BridgeKeeper.RegisteredCoins.Set(ctx, app.MultiStakingCoinA.Denom, types.RateLimit{
Ratelimit: math.NewInt(1000000000),
CurrentInflow: math.NewInt(100000000),
})
rateLimit, err := realio.BridgeKeeper.RegisteredCoins.Get(ctx, app.MultiStakingCoinA.Denom)

fmt.Println("realio.BridgeKeeper :", rateLimit.CurrentInflow)
fmt.Println("realio.BridgeKeeper :", rateLimit.Ratelimit)

ver0 := realio.LastBlockHeight()
// commit 10 blocks
Expand All @@ -31,8 +43,11 @@ func TestRateLimitTrigger(t *testing.T) {

require.Equal(t, ver0+10, realio.LastBlockHeight())

ctx := realio.BaseApp.NewContext(true)
ctx = realio.BaseApp.NewContext(true)
epochInfo, err := realio.BridgeKeeper.EpochInfo.Get(ctx)
require.NoError(t, err)
require.True(t, epochInfo.EpochCountingStarted)
rateLimit, err = realio.BridgeKeeper.RegisteredCoins.Get(ctx, app.MultiStakingCoinA.Denom)
fmt.Println("realio.BridgeKeeper :", rateLimit.CurrentInflow)
fmt.Println("realio.BridgeKeeper :", rateLimit.Ratelimit)
}

0 comments on commit ea841b6

Please sign in to comment.