Skip to content

Commit

Permalink
Migrate to audit tag version with upgrade (#459)
Browse files Browse the repository at this point in the history
* added migration functions to add new fields to bridge keeper. Set upgrade name to match software upgrade plan name

* pushing up a change that was missed

* made changes to deal with circular import

* branch is now building with no circular import errors

* removed migrations.go file in keeper and am just importing the v2 migration package into module.go

* fixed migration function to use right context

* fixed typo

* changed format around to desired format

* saving changes before branching to old tag to test

* rebased from main to hopefully solve some git issues with merge

* lint fixes
  • Loading branch information
CJPotter10 authored Dec 9, 2024
1 parent 09cb346 commit 9129e14
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build/*
secrets.yaml
.env
prod-sim
/layerd_new

dist/

Expand Down
6 changes: 4 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,9 @@ func New(

// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
// Make sure it's called after `app.ModuleManager` and `app.configurator` are set.
app.RegisterUpgradeHandlers()
// app.RegisterUpgradeHandlers()
app.setupUpgradeHandlers()
app.setupUpgradeStoreLoaders()

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.mm.Modules))
reflectionSvc, err := runtimeservices.NewReflectionService()
Expand Down Expand Up @@ -1018,7 +1020,7 @@ func (app *App) preBlocker(ph *ProposalHandler) func(sdk.Context, *abci.RequestF
}

func (app *App) RegisterUpgradeHandlers() {
const UpgradeName = "v1.6.0"
const UpgradeName = "v2.0.0-audit"

app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
Expand Down
53 changes: 53 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app

import (
"fmt"

"github.com/tellor-io/layer/app/upgrades"
v_2_0 "github.com/tellor-io/layer/app/upgrades/v2.0.0-alpha2"

upgradetypes "cosmossdk.io/x/upgrade/types"
)

var (
// `Upgrades` defines the upgrade handlers and store loaders for the application.
// New upgrades should be added to this slice after they are implemented.
Upgrades = []*upgrades.Upgrade{
&v_2_0.Upgrade,
}
Forks = []upgrades.Fork{}
)

// setupUpgradeHandlers registers the upgrade handlers to perform custom upgrade
// logic and state migrations for software upgrades.
func (app *App) setupUpgradeHandlers() {
if app.UpgradeKeeper.HasHandler(v_2_0.UpgradeName) {
panic(fmt.Sprintf("Cannot register duplicate upgrade handler '%s'", v_2_0.UpgradeName))
}
app.UpgradeKeeper.SetUpgradeHandler(
v_2_0.UpgradeName,
v_2_0.CreateUpgradeHandler(
app.ModuleManager(),
app.configurator,
),
)
}

// setUpgradeStoreLoaders sets custom store loaders to customize the rootMultiStore
// initialization for software upgrades.
func (app *App) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}

for _, upgrade := range Upgrades {
if upgradeInfo.Name == upgrade.UpgradeName {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgrade.StoreUpgrades))
}
}
}
26 changes: 26 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package upgrades

import (
store "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"

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

type Upgrade struct {
UpgradeName string
CreateUpgradeHandler func(*module.Manager, module.Configurator) upgradetypes.UpgradeHandler
// Store Upgrades, should be used for any new modules introduced, deleted, or store names renamed
StoreUpgrades store.StoreUpgrades
}

type Fork struct {
// Upgrade version name, for the upgrade handler, e.g. `v2.0.0-audit`
UpgradeName string

// Height the upgrade occurs at
UpgradeHeight func(*module.Manager, module.Configurator) upgradetypes.UpgradeHandler

// Upgrade info for this fork
UpgradeInfo string
}
16 changes: 16 additions & 0 deletions app/upgrades/v2.0.0-alpha2/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v_2_0_0_alpha2

import (
"github.com/tellor-io/layer/app/upgrades"

store "cosmossdk.io/store/types"
)

const (
UpgradeName = "v2.0.0-audit"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
StoreUpgrades: store.StoreUpgrades{},
}
23 changes: 23 additions & 0 deletions app/upgrades/v2.0.0-alpha2/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v_2_0_0_alpha2

import (
"context"
"fmt"

upgradetypes "cosmossdk.io/x/upgrade/types"

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

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.Logger().Info(fmt.Sprintf("Running %s Upgrade...", UpgradeName))

return mm.RunMigrations(ctx, configurator, vm)
}
}
2 changes: 1 addition & 1 deletion start_scripts/start_one_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ echo "Collecting gentxs..."


echo "Starting chain for alice..."
./layerd start --home ~/.layer/alice --key-name alice --api.enable --api.swagger --keyring-backend test
#./layerd start --home ~/.layer/alice --key-name alice --api.enable --api.swagger --keyring-backend test
42 changes: 42 additions & 0 deletions x/bridge/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package v2

import (
"context"
"fmt"

"github.com/tellor-io/layer/x/bridge/keeper"
bridgetypes "github.com/tellor-io/layer/x/bridge/types"
)

type SnapshotLimit struct {
Limit uint64 `protobuf:"varint,1,opt,name=limit,proto3"`
}

// func MigrateStoreFromV1ToV2(ctx context.Context, storeService store.KVStoreService) error {
// kvStore := storeService.OpenKVStore(ctx)

// limit := bridgetypes.SnapshotLimit{Limit: 1000}
// data, err := json.Marshal(limit)
// if err != nil {
// return err
// }

// key := []byte("SnapshotLimit")
// err = kvStore.Set(key, data)
// if err != nil {
// return err
// }

// return nil
// }

func MigrateStoreFromV1ToV2(ctx context.Context, keeper *keeper.Keeper) error {
limit := bridgetypes.SnapshotLimit{Limit: 1000}
err := keeper.SnapshotLimit.Set(ctx, limit)
if err != nil {
return err
}
fmt.Println("Set snapshot limit in migration")

return nil
}
9 changes: 9 additions & 0 deletions x/bridge/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v2_test

// import (
// "testing"
// )

// func TestMigrateStoreFromV1ToV2(t *testing.T) {

// }
8 changes: 7 additions & 1 deletion x/bridge/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/runtime"
bridgemodulev1 "github.com/tellor-io/layer/api/layer/bridge/module"
"github.com/tellor-io/layer/x/bridge/keeper"
v2 "github.com/tellor-io/layer/x/bridge/migrations/v2"
"github.com/tellor-io/layer/x/bridge/types"

"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -114,6 +115,11 @@ func NewAppModule(
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
if err := cfg.RegisterMigration("bridge", 1, func(ctx sdk.Context) error {
return v2.MigrateStoreFromV1ToV2(ctx, &am.keeper)
}); err != nil {
panic(fmt.Sprintf("Could not migrate store from v1 to v2: %v", err))
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -137,7 +143,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(ctx context.Context) error {
Expand Down

0 comments on commit 9129e14

Please sign in to comment.