diff --git a/.gitignore b/.gitignore index f697b8831..25d182aeb 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ build/* secrets.yaml .env prod-sim +/layerd_new dist/ diff --git a/app/app.go b/app/app.go index bb530b1c7..37591d447 100644 --- a/app/app.go +++ b/app/app.go @@ -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() @@ -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, diff --git a/app/upgrades.go b/app/upgrades.go new file mode 100644 index 000000000..22ece715f --- /dev/null +++ b/app/upgrades.go @@ -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)) + } + } +} diff --git a/app/upgrades/types.go b/app/upgrades/types.go new file mode 100644 index 000000000..542578f45 --- /dev/null +++ b/app/upgrades/types.go @@ -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 +} diff --git a/app/upgrades/v2.0.0-alpha2/constants.go b/app/upgrades/v2.0.0-alpha2/constants.go new file mode 100644 index 000000000..035fcc44c --- /dev/null +++ b/app/upgrades/v2.0.0-alpha2/constants.go @@ -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{}, +} diff --git a/app/upgrades/v2.0.0-alpha2/upgrade.go b/app/upgrades/v2.0.0-alpha2/upgrade.go new file mode 100644 index 000000000..6f7508400 --- /dev/null +++ b/app/upgrades/v2.0.0-alpha2/upgrade.go @@ -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) + } +} diff --git a/start_scripts/start_one_node.sh b/start_scripts/start_one_node.sh index bd8580559..d64e3c2f1 100755 --- a/start_scripts/start_one_node.sh +++ b/start_scripts/start_one_node.sh @@ -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 \ No newline at end of file +#./layerd start --home ~/.layer/alice --key-name alice --api.enable --api.swagger --keyring-backend test \ No newline at end of file diff --git a/x/bridge/migrations/v2/store.go b/x/bridge/migrations/v2/store.go new file mode 100644 index 000000000..063e74e16 --- /dev/null +++ b/x/bridge/migrations/v2/store.go @@ -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 +} diff --git a/x/bridge/migrations/v2/store_test.go b/x/bridge/migrations/v2/store_test.go new file mode 100644 index 000000000..a0b58127a --- /dev/null +++ b/x/bridge/migrations/v2/store_test.go @@ -0,0 +1,9 @@ +package v2_test + +// import ( +// "testing" +// ) + +// func TestMigrateStoreFromV1ToV2(t *testing.T) { + +// } diff --git a/x/bridge/module.go b/x/bridge/module.go index 57b9351bf..9dc5d1413 100644 --- a/x/bridge/module.go +++ b/x/bridge/module.go @@ -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" @@ -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) @@ -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 {