From 34ce1b01089cdbe037b72433ae6c0b967287cd2f Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Fri, 28 Apr 2023 15:23:58 -0400 Subject: [PATCH] refactor(migrations): group migrations by upgrade name place all migrations within migrations dir Signed-off-by: Artur Troian --- app/upgrades.go | 4 +- .../upgrade.go | 4 +- app/upgrades/v0.24.0/upgrade.go | 56 +++++- go.mod | 2 +- go.sum | 4 +- migrations/consensus/consensus.go | 91 +++++++++ migrations/consensus/helpers.go | 27 +++ migrations/migrations.go | 9 + migrations/v0.15.0/audit.go | 34 ++++ migrations/v0.15.0/cert.go | 34 ++++ migrations/v0.15.0/deployment.go | 48 +++++ migrations/v0.15.0/escrow.go | 84 ++++++++ .../v015 => migrations/v0.15.0}/helpers.go | 29 +-- migrations/v0.15.0/market.go | 183 ++++++++++++++++++ migrations/v0.15.0/migrations.go | 23 +++ .../v0.15.0}/proto_compatibility_test.go | 107 +++++----- migrations/v0.15.0/provider.go | 46 +++++ migrations/v0.24.0/deployment.go | 47 +++++ migrations/v0.24.0/market.go | 59 ++++++ migrations/v0.24.0/migrations.go | 15 ++ x/audit/keeper/keeper.go | 5 + x/audit/keeper/migrations.go | 22 --- x/audit/legacy/v015/store.go | 18 -- x/audit/module.go | 13 +- x/cert/keeper/keeper.go | 6 + x/cert/keeper/migrations.go | 16 -- x/cert/module.go | 12 +- x/deployment/keeper/keeper.go | 5 + x/deployment/keeper/migrations.go | 16 -- x/deployment/module.go | 12 +- x/escrow/genesis.go | 16 +- x/escrow/keeper/keeper.go | 11 ++ x/escrow/keeper/migrations.go | 22 --- x/escrow/legacy/v015/store.go | 62 ------ x/escrow/module.go | 12 +- x/gov/keeper/keeper.go | 5 + x/gov/module.go | 16 +- x/inflation/keeper/keeper.go | 11 ++ x/inflation/module.go | 11 +- x/market/keeper/grpc_query.go | 4 +- x/market/keeper/keeper.go | 11 +- x/market/keeper/keys/v1beta1/key.go | 92 +++++++++ x/market/keeper/keys/v1beta2/key.go | 164 ++++++++++++++++ x/market/keeper/keys/v1beta2/key_test.go | 34 ++++ x/market/keeper/keys/{ => v1beta3}/key.go | 2 +- .../keeper/keys/{ => v1beta3}/key_test.go | 9 +- x/market/keeper/migrations.go | 21 -- x/market/legacy/v015/store.go | 144 -------------- x/market/module.go | 12 +- x/provider/keeper/keeper.go | 6 + x/provider/keeper/migrations.go | 22 --- x/provider/legacy/v015/store.go | 37 ---- x/provider/module.go | 12 +- x/staking/keeper/keeper.go | 5 + x/staking/module.go | 10 +- 55 files changed, 1259 insertions(+), 523 deletions(-) rename app/upgrades/{akash_v0.15.0_cosmos_v0.44.x => v0.15.0}/upgrade.go (96%) create mode 100644 migrations/consensus/consensus.go create mode 100644 migrations/consensus/helpers.go create mode 100644 migrations/migrations.go create mode 100644 migrations/v0.15.0/audit.go create mode 100644 migrations/v0.15.0/cert.go create mode 100644 migrations/v0.15.0/deployment.go create mode 100644 migrations/v0.15.0/escrow.go rename {util/legacy/v015 => migrations/v0.15.0}/helpers.go (53%) create mode 100644 migrations/v0.15.0/market.go create mode 100644 migrations/v0.15.0/migrations.go rename {util/legacy/v015 => migrations/v0.15.0}/proto_compatibility_test.go (75%) create mode 100644 migrations/v0.15.0/provider.go create mode 100644 migrations/v0.24.0/deployment.go create mode 100644 migrations/v0.24.0/market.go create mode 100644 migrations/v0.24.0/migrations.go delete mode 100644 x/audit/keeper/migrations.go delete mode 100644 x/audit/legacy/v015/store.go delete mode 100644 x/cert/keeper/migrations.go delete mode 100644 x/deployment/keeper/migrations.go delete mode 100644 x/escrow/keeper/migrations.go delete mode 100644 x/escrow/legacy/v015/store.go create mode 100644 x/market/keeper/keys/v1beta1/key.go create mode 100644 x/market/keeper/keys/v1beta2/key.go create mode 100644 x/market/keeper/keys/v1beta2/key_test.go rename x/market/keeper/keys/{ => v1beta3}/key.go (99%) rename x/market/keeper/keys/{ => v1beta3}/key_test.go (90%) delete mode 100644 x/market/keeper/migrations.go delete mode 100644 x/market/legacy/v015/store.go delete mode 100644 x/provider/keeper/migrations.go delete mode 100644 x/provider/legacy/v015/store.go diff --git a/app/upgrades.go b/app/upgrades.go index 4935cb0803..31e49fd8d8 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -7,12 +7,14 @@ import ( apptypes "github.com/akash-network/node/app/types" + // nolint: revive + _ "github.com/akash-network/node/migrations" // nolint: revive _ "github.com/akash-network/node/app/upgrades/v0.24.0" // nolint: revive _ "github.com/akash-network/node/app/upgrades/v0.20.0" // nolint: revive - _ "github.com/akash-network/node/app/upgrades/akash_v0.15.0_cosmos_v0.44.x" + _ "github.com/akash-network/node/app/upgrades/v0.15.0" ) func (app *AkashApp) registerUpgradeHandlers() error { diff --git a/app/upgrades/akash_v0.15.0_cosmos_v0.44.x/upgrade.go b/app/upgrades/v0.15.0/upgrade.go similarity index 96% rename from app/upgrades/akash_v0.15.0_cosmos_v0.44.x/upgrade.go rename to app/upgrades/v0.15.0/upgrade.go index de7e82a469..5dab11154d 100644 --- a/app/upgrades/akash_v0.15.0_cosmos_v0.44.x/upgrade.go +++ b/app/upgrades/v0.15.0/upgrade.go @@ -1,5 +1,5 @@ -// Package akash_v0_15_0_cosmos_v0_44_x -package akash_v0_15_0_cosmos_v0_44_x // nolint revive +// Package v0_15_0 +package v0_15_0 // nolint revive import ( inflationtypes "github.com/akash-network/akash-api/go/node/inflation/v1beta3" diff --git a/app/upgrades/v0.24.0/upgrade.go b/app/upgrades/v0.24.0/upgrade.go index d9893753c1..367fe8c1bb 100644 --- a/app/upgrades/v0.24.0/upgrade.go +++ b/app/upgrades/v0.24.0/upgrade.go @@ -4,6 +4,7 @@ package v0_24_0 // nolint revive import ( "fmt" + "github.com/akash-network/akash-api/go/node/escrow/v1beta3" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -11,6 +12,9 @@ import ( upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/tendermint/tendermint/libs/log" + agovtypes "github.com/akash-network/akash-api/go/node/gov/v1beta3" + astakingtypes "github.com/akash-network/akash-api/go/node/staking/v1beta3" + apptypes "github.com/akash-network/node/app/types" agov "github.com/akash-network/node/x/gov" astaking "github.com/akash-network/node/x/staking" @@ -34,7 +38,7 @@ var _ apptypes.IUpgrade = (*upgrade)(nil) func initUpgrade(log log.Logger, app *apptypes.App) (apptypes.IUpgrade, error) { up := &upgrade{ App: app, - log: log.With("upgrade/v0.22.0"), + log: log.With(fmt.Sprintf("upgrade/%s", UpgradeName)), } if _, exists := up.MM.Modules[agov.ModuleName]; !exists { @@ -62,20 +66,28 @@ func (up *upgrade) StoreLoader() *storetypes.StoreUpgrades { func (up *upgrade) UpgradeHandler() upgradetypes.UpgradeHandler { return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { - ctx.Logger().Info("start to run module migrations...") - fromVM[feegrant.ModuleName] = up.MM.Modules[feegrant.ModuleName].ConsensusVersion() - fromVM[astaking.ModuleName] = up.MM.Modules[astaking.ModuleName].ConsensusVersion() - fromVM[agov.ModuleName] = up.MM.Modules[agov.ModuleName].ConsensusVersion() + ctx.Logger().Info("initializing parameters in astaking module...") + if err := up.Keepers.Akash.Staking.SetParams(ctx, astakingtypes.DefaultParams()); err != nil { + return nil, err + } + + ctx.Logger().Info("initializing parameters in agov module...") + if err := up.Keepers.Akash.Gov.SetDepositParams(ctx, agovtypes.DefaultDepositParams()); err != nil { + return nil, err + } - if err := up.patchValidatorsCommission(ctx); err != nil { + if err := up.enforceMinValidatorCommission(ctx); err != nil { return nil, err } + up.patchDanglingEscrowPayments(ctx) + + ctx.Logger().Info("starting module migrations...") return up.MM.RunMigrations(ctx, up.Configurator, fromVM) } } -func (up *upgrade) patchValidatorsCommission(ctx sdk.Context) error { +func (up *upgrade) enforceMinValidatorCommission(ctx sdk.Context) error { minRate := up.Keepers.Akash.Staking.MinCommissionRate(ctx) validators := up.Keepers.Cosmos.Staking.GetAllValidators(ctx) @@ -109,3 +121,33 @@ func (up *upgrade) patchValidatorsCommission(ctx sdk.Context) error { return nil } + +func (up *upgrade) patchDanglingEscrowPayments(ctx sdk.Context) { + up.Keepers.Akash.Escrow.WithPayments(ctx, func(payment v1beta3.FractionalPayment) bool { + acc, _ := up.Keepers.Akash.Escrow.GetAccount(ctx, payment.AccountID) + if (payment.State == v1beta3.PaymentOpen && acc.State != v1beta3.AccountOpen) || + (payment.State == v1beta3.PaymentOverdrawn && acc.State != v1beta3.AccountOverdrawn) { + + up.log.Info( + fmt.Sprintf("payment id state `%s:%s` does not match account state `%s:%s`. forcing payment state to %[4]s", + payment.PaymentID, + payment.State, + acc.ID, + acc.State, + ), + ) + + switch acc.State { + case v1beta3.AccountOpen: + payment.State = v1beta3.PaymentOpen + case v1beta3.AccountClosed: + payment.State = v1beta3.PaymentClosed + case v1beta3.AccountOverdrawn: + payment.State = v1beta3.PaymentOverdrawn + } + } + + up.Keepers.Akash.Escrow.SavePayment(ctx, payment) + return true + }) +} diff --git a/go.mod b/go.mod index c349f13c90..f1fd0bc18a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/akash-network/node go 1.20 require ( - github.com/akash-network/akash-api v0.0.12 + github.com/akash-network/akash-api v0.0.14 github.com/blang/semver/v4 v4.0.0 github.com/boz/go-lifecycle v0.1.0 github.com/cosmos/cosmos-sdk v0.45.15 diff --git a/go.sum b/go.sum index 6440146398..4d088081ec 100644 --- a/go.sum +++ b/go.sum @@ -97,8 +97,8 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= -github.com/akash-network/akash-api v0.0.12 h1:j3PPC6ouI1PcuKbxyuO8ffyVcaZsT3ZDPprQo3KxPYk= -github.com/akash-network/akash-api v0.0.12/go.mod h1:e1QqkOFwxHKf88I3U5bPOmREdfHHHX2bY27ZZOFnTX4= +github.com/akash-network/akash-api v0.0.14 h1:igyQEWt40svNvEgGA/B3+Fxt5HcMVgf5LE3WxRkmGPk= +github.com/akash-network/akash-api v0.0.14/go.mod h1:e1QqkOFwxHKf88I3U5bPOmREdfHHHX2bY27ZZOFnTX4= github.com/akash-network/cometbft v0.34.27-akash h1:V1dApDOr8Ee7BJzYyQ7Z9VBtrAul4+baMeA6C49dje0= github.com/akash-network/cometbft v0.34.27-akash/go.mod h1:BcCbhKv7ieM0KEddnYXvQZR+pZykTKReJJYf7YC7qhw= github.com/akash-network/ledger-go v0.14.3 h1:LCEFkTfgGA2xFMN2CtiKvXKE7dh0QSM77PJHCpSkaAo= diff --git a/migrations/consensus/consensus.go b/migrations/consensus/consensus.go new file mode 100644 index 0000000000..bc71cdf6ab --- /dev/null +++ b/migrations/consensus/consensus.go @@ -0,0 +1,91 @@ +package consensus + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" +) + +type Migrator interface { + StoreKey() sdk.StoreKey + Codec() codec.BinaryCodec +} + +type Migration interface { + GetHandler() sdkmodule.MigrationHandler +} + +type NewMigrationFn func(Migrator) Migration + +type moduleMigrations map[uint64]NewMigrationFn + +var ( + migrations = map[string]moduleMigrations{} + + currentConsensusVersions = map[string]uint64{ + "audit": 1, // 2 + "cert": 1, // 2 + "deployment": 1, // 3 + "escrow": 1, // 2 + "market": 1, // 2 + "provider": 1, // 2 + // modules below don't have migrations yet as there are in genesis state + // so set consensus version to 1 + "inflation": 1, // 1 + "agov": 1, + "astaking": 1, + } + + // currentConsensusVersions = map[string]uint64{ + // "audit": migrations["audit"].getLatest() + 1, // 2 + // "cert": migrations["cert"].getLatest() + 1, // 2 + // "deployment": migrations["deployment"].getLatest() + 1, // 3 + // "escrow": migrations["escrow"].getLatest() + 1, // 2 + // "market": migrations["market"].getLatest() + 1, // 2 + // "provider": migrations["provider"].getLatest() + 1, // 2 + // + // // modules below don't have migrations yet as there are in genesis state + // // so set consensus version to 1 + // "inflation": 1, // 1 + // "agov": 1, + // "astaking": 1, + // } +) + +func RegisterMigration(module string, version uint64, initFn NewMigrationFn) { + if _, exists := migrations[module]; !exists { + migrations[module] = make(moduleMigrations) + } + + if _, exists := migrations[module][version]; exists { + panic(fmt.Sprintf("migration version (%d) has already been registered for module (%s)", version, module)) + } + + migrations[module][version] = initFn + if val := currentConsensusVersions[module]; val <= version+1 { + currentConsensusVersions[module] = version + 1 + } +} + +func ModuleVersion(module string) uint64 { + ver, exists := currentConsensusVersions[module] + if !exists { + panic(fmt.Sprintf("requested consensus version for non existing module (%s)", module)) + } + + return ver +} + +func ModuleMigrations(module string, migrator Migrator, fn func(string, uint64, sdkmodule.MigrationHandler)) { + moduleMigrations, exists := migrations[module] + if !exists { + return + } + + for version, initFn := range moduleMigrations { + migration := initFn(migrator) + fn(module, version, migration.GetHandler()) + } +} diff --git a/migrations/consensus/helpers.go b/migrations/consensus/helpers.go new file mode 100644 index 0000000000..2db9286962 --- /dev/null +++ b/migrations/consensus/helpers.go @@ -0,0 +1,27 @@ +package consensus + +import ( + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// ValueMigrator migrates a value to the new protobuf type given its old protobuf serialized bytes. +type ValueMigrator func(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler + +// MigrateValue is a helper function that migrates values stored in a KV store for the given +// key prefix using the given value migrator function. +func MigrateValue(store sdk.KVStore, cdc codec.BinaryCodec, prefixBz []byte, migrator ValueMigrator) (err error) { + pStore := prefix.NewStore(store, prefixBz) + + iter := pStore.Iterator(nil, nil) + defer func() { + err = iter.Close() + }() + + for ; iter.Valid(); iter.Next() { + pStore.Set(iter.Key(), cdc.MustMarshal(migrator(iter.Value(), cdc))) + } + + return nil +} diff --git a/migrations/migrations.go b/migrations/migrations.go new file mode 100644 index 0000000000..b953fef989 --- /dev/null +++ b/migrations/migrations.go @@ -0,0 +1,9 @@ +package migrations + +import ( + // ensure init functions called for all migrations + // nolint: revive + _ "github.com/akash-network/node/migrations/v0.15.0" + // nolint: revive + _ "github.com/akash-network/node/migrations/v0.24.0" +) diff --git a/migrations/v0.15.0/audit.go b/migrations/v0.15.0/audit.go new file mode 100644 index 0000000000..dbf25d7159 --- /dev/null +++ b/migrations/v0.15.0/audit.go @@ -0,0 +1,34 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + v043 "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v043" + + av1beta2 "github.com/akash-network/akash-api/go/node/audit/v1beta2" + + "github.com/akash-network/node/migrations/consensus" +) + +type auditMigrations struct { + consensus.Migrator +} + +func newAuditMigration(m consensus.Migrator) consensus.Migration { + return auditMigrations{Migrator: m} +} + +func (m auditMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates provider from version 1 to 2. +func (m auditMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + v043.MigratePrefixAddressAddress(store, av1beta2.PrefixProviderID()) + + return nil +} diff --git a/migrations/v0.15.0/cert.go b/migrations/v0.15.0/cert.go new file mode 100644 index 0000000000..df8911f304 --- /dev/null +++ b/migrations/v0.15.0/cert.go @@ -0,0 +1,34 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + v043 "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v043" + + cv1beta2 "github.com/akash-network/akash-api/go/node/cert/v1beta2" + + "github.com/akash-network/node/migrations/consensus" +) + +type certMigrations struct { + consensus.Migrator +} + +func newCertMigration(m consensus.Migrator) consensus.Migration { + return certMigrations{Migrator: m} +} + +func (m certMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates provider from version 1 to 2. +func (m certMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + v043.MigratePrefixAddressAddress(store, cv1beta2.PrefixCertificateID()) + + return nil +} diff --git a/migrations/v0.15.0/deployment.go b/migrations/v0.15.0/deployment.go new file mode 100644 index 0000000000..9c04400a1a --- /dev/null +++ b/migrations/v0.15.0/deployment.go @@ -0,0 +1,48 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + dv1beta1 "github.com/akash-network/akash-api/go/node/deployment/v1beta1" + dmigrate "github.com/akash-network/akash-api/go/node/deployment/v1beta2/migrate" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/akash-network/node/migrations/consensus" +) + +type deploymentMigrations struct { + consensus.Migrator +} + +func newDeploymentMigration(m consensus.Migrator) consensus.Migration { + return deploymentMigrations{Migrator: m} +} + +func (m deploymentMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates deployment from version 1 to 2. +func (m deploymentMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + migratePrefixBech32AddrBytes(store, dv1beta1.DeploymentPrefix()) + migratePrefixBech32AddrBytes(store, dv1beta1.GroupPrefix()) + + err := consensus.MigrateValue(store, m.Codec(), dv1beta1.GroupPrefix(), migrateDeploymentGroup) + if err != nil { + return err + } + + return nil +} + +func migrateDeploymentGroup(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var from dv1beta1.Group + cdc.MustUnmarshal(fromBz, &from) + + to := dmigrate.GroupFromV1Beta1(from) + return &to +} diff --git a/migrations/v0.15.0/escrow.go b/migrations/v0.15.0/escrow.go new file mode 100644 index 0000000000..d627597a87 --- /dev/null +++ b/migrations/v0.15.0/escrow.go @@ -0,0 +1,84 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + ev1beta1 "github.com/akash-network/akash-api/go/node/escrow/v1beta1" + ev1beta2 "github.com/akash-network/akash-api/go/node/escrow/v1beta2" + + "github.com/akash-network/node/migrations/consensus" +) + +type escrowMigrations struct { + consensus.Migrator +} + +func newEscrowMigration(m consensus.Migrator) consensus.Migration { + return escrowMigrations{Migrator: m} +} + +func (m escrowMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates deployment from version 1 to 2. +func (m escrowMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + err := consensus.MigrateValue(store, m.Codec(), ev1beta2.AccountKeyPrefix(), migrateAccount) + if err != nil { + return err + } + + err = consensus.MigrateValue(store, m.Codec(), ev1beta2.PaymentKeyPrefix(), migratePayment) + if err != nil { + return err + } + + return nil +} + +func migrateAccount(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var from ev1beta1.Account + cdc.MustUnmarshal(fromBz, &from) + to := ev1beta2.Account{ + ID: ev1beta2.AccountID{ + Scope: from.ID.Scope, + XID: from.ID.XID, + }, + Owner: from.Owner, + State: ev1beta2.Account_State(from.State), + Balance: sdk.NewDecCoinFromCoin(from.Balance), + Transferred: sdk.NewDecCoinFromCoin(from.Transferred), + SettledAt: from.SettledAt, + // Correctly initialize the new fields + // - Account.Depositor as Account.Owner + // - Account.Funds as a DecCoin of zero value + Depositor: from.Owner, + Funds: sdk.NewDecCoin(from.Balance.Denom, sdk.ZeroInt()), + } + + return &to +} + +func migratePayment(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var from ev1beta1.Payment + cdc.MustUnmarshal(fromBz, &from) + + return &ev1beta2.FractionalPayment{ + AccountID: ev1beta2.AccountID{ + Scope: from.AccountID.Scope, + XID: from.AccountID.XID, + }, + PaymentID: from.PaymentID, + Owner: from.Owner, + State: ev1beta2.FractionalPayment_State(from.State), + Rate: sdk.NewDecCoinFromCoin(from.Rate), + Balance: sdk.NewDecCoinFromCoin(from.Balance), + Withdrawn: from.Withdrawn, + } +} diff --git a/util/legacy/v015/helpers.go b/migrations/v0.15.0/helpers.go similarity index 53% rename from util/legacy/v015/helpers.go rename to migrations/v0.15.0/helpers.go index 974c9bc4ba..efd54ae40b 100644 --- a/util/legacy/v015/helpers.go +++ b/migrations/v0.15.0/helpers.go @@ -1,7 +1,8 @@ -package v015 +// Package v0_15_0 +// nolint revive +package v0_15_0 import ( - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" @@ -11,15 +12,17 @@ const ( V014Bech32AddrLen = 44 // length of an akash address when encoded as a bech32 string in v0.14 ) -// MigratePrefixBech32AddrBytes is a helper function that migrates all keys of format: +// migratePrefixBech32AddrBytes is a helper function that migrates all keys of format: // prefix_bytes | address_bech32_bytes | arbitrary_bytes // into format: // prefix_bytes | address_len (1 byte) | address_bytes | arbitrary_bytes -func MigratePrefixBech32AddrBytes(store sdk.KVStore, prefixBz []byte) { +func migratePrefixBech32AddrBytes(store sdk.KVStore, prefixBz []byte) { oldStore := prefix.NewStore(store, prefixBz) oldStoreIter := oldStore.Iterator(nil, nil) - defer oldStoreIter.Close() + defer func() { + _ = oldStoreIter.Close() + }() for ; oldStoreIter.Valid(); oldStoreIter.Next() { bech32Addr := string(oldStoreIter.Key()[:V014Bech32AddrLen]) @@ -36,19 +39,3 @@ func MigratePrefixBech32AddrBytes(store sdk.KVStore, prefixBz []byte) { oldStore.Delete(oldStoreIter.Key()) } } - -// ValueMigrator migrates a value to the new protobuf type given its old protobuf serialized bytes. -type ValueMigrator func(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler - -// MigrateValue is a helper function that migrates values stored in a KV store for the given -// key prefix using the given value migrator function. -func MigrateValue(store sdk.KVStore, cdc codec.BinaryCodec, prefixBz []byte, migrator ValueMigrator) { - pStore := prefix.NewStore(store, prefixBz) - - iter := pStore.Iterator(nil, nil) - defer iter.Close() - - for ; iter.Valid(); iter.Next() { - pStore.Set(iter.Key(), cdc.MustMarshal(migrator(iter.Value(), cdc))) - } -} diff --git a/migrations/v0.15.0/market.go b/migrations/v0.15.0/market.go new file mode 100644 index 0000000000..6fc403dd65 --- /dev/null +++ b/migrations/v0.15.0/market.go @@ -0,0 +1,183 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + dmigrate "github.com/akash-network/akash-api/go/node/deployment/v1beta2/migrate" + mv1beta1 "github.com/akash-network/akash-api/go/node/market/v1beta1" + mv1beta2 "github.com/akash-network/akash-api/go/node/market/v1beta2" + + "github.com/akash-network/node/migrations/consensus" + keys "github.com/akash-network/node/x/market/keeper/keys/v1beta2" +) + +type marketMigrations struct { + consensus.Migrator +} + +func newMarketMigration(m consensus.Migrator) consensus.Migration { + return marketMigrations{Migrator: m} +} + +func (m marketMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates market from version 1 to 2. +func (m marketMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + // Change addresses to be length-prefixed + migratePrefixBech32AddrBytes(store, mv1beta1.OrderPrefix()) + err := migratePrefixBech32Uint64Uint32Uint32Bech32(store, mv1beta1.BidPrefix()) + if err != nil { + return err + } + + err = migratePrefixBech32Uint64Uint32Uint32Bech32(store, mv1beta1.LeasePrefix()) + if err != nil { + return err + } + + // Migrate protobuf + err = consensus.MigrateValue(store, m.Codec(), mv1beta1.OrderPrefix(), migrateOrder) + if err != nil { + return err + } + + err = consensus.MigrateValue(store, m.Codec(), mv1beta1.BidPrefix(), migrateBid) + if err != nil { + return err + } + + err = consensus.MigrateValue(store, m.Codec(), mv1beta1.LeasePrefix(), migrateLease) + if err != nil { + return err + } + + // add the mapping of secondary lease key -> lease key + addSecondaryLeaseKeys(store, m.Codec()) + + return nil +} + +func addSecondaryLeaseKeys(baseStore sdk.KVStore, cdc codec.BinaryCodec) { + store := prefix.NewStore(baseStore, mv1beta2.LeasePrefix()) + + iter := sdk.KVStorePrefixIterator(store, mv1beta2.LeasePrefix()) + defer func() { + _ = iter.Close() + }() + + for ; iter.Valid(); iter.Next() { + var from mv1beta2.Lease + cdc.MustUnmarshal(iter.Value(), &from) + + leaseKey := keys.LeaseKey(from.GetLeaseID()) + for _, secondaryKey := range keys.SecondaryKeysForLease(from.GetLeaseID()) { + store.Set(secondaryKey, leaseKey) + } + } +} + +// migratePrefixBech32Uint64Uint32Uint32Bech32 is a helper function that migrates all keys of format: +// prefix_bytes | address1_bech32_bytes | uint64 | uint32 | uint32 | address2_bech32_bytes +// into format: +// prefix_bytes | address1_len (1 byte) | address1_bytes | uint64 | uint32 | uint32 | address2_len (1 byte) | address2_bytes +func migratePrefixBech32Uint64Uint32Uint32Bech32(store sdk.KVStore, prefixBz []byte) error { + oldStore := prefix.NewStore(store, prefixBz) + + oldStoreIter := oldStore.Iterator(nil, nil) + defer func() { + _ = oldStoreIter.Close() + }() + + for ; oldStoreIter.Valid(); oldStoreIter.Next() { + bech32Addr1 := string(oldStoreIter.Key()[:V014Bech32AddrLen]) + addr1, err := sdk.AccAddressFromBech32(bech32Addr1) + if err != nil { + return err + } + + midBz := oldStoreIter.Key()[V014Bech32AddrLen : V014Bech32AddrLen+16] + + bech32Addr2 := string(oldStoreIter.Key()[V014Bech32AddrLen+16:]) + addr2, err := sdk.AccAddressFromBech32(bech32Addr2) + if err != nil { + return err + } + + newStoreKey := append(append(append(prefixBz, address.MustLengthPrefix(addr1)...), midBz...), address.MustLengthPrefix(addr2)...) + + // Set new key on store. Values don't change. + store.Set(newStoreKey, oldStoreIter.Value()) + oldStore.Delete(oldStoreIter.Key()) + } + + return nil +} + +func migrateLease(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var oldObj mv1beta1.Lease + cdc.MustUnmarshal(fromBz, &oldObj) + to := mv1beta2.Lease{ + LeaseID: mv1beta2.LeaseID{ + Owner: oldObj.LeaseID.Owner, + DSeq: oldObj.LeaseID.DSeq, + GSeq: oldObj.LeaseID.GSeq, + OSeq: oldObj.LeaseID.OSeq, + Provider: oldObj.LeaseID.Provider, + }, + State: mv1beta2.Lease_State(oldObj.State), + Price: sdk.NewDecCoinFromCoin(oldObj.Price), + CreatedAt: oldObj.CreatedAt, + ClosedOn: 0, // For leases created prior to this change never report the data + } + + return &to +} + +func migrateBid(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var oldObject mv1beta1.Bid + cdc.MustUnmarshal(fromBz, &oldObject) + + to := mv1beta2.Bid{ + BidID: mv1beta2.BidID{ + Owner: oldObject.BidID.Owner, + DSeq: oldObject.BidID.DSeq, + GSeq: oldObject.BidID.GSeq, + OSeq: oldObject.BidID.OSeq, + Provider: oldObject.BidID.Provider, + }, + State: mv1beta2.Bid_State(oldObject.State), + Price: sdk.NewDecCoinFromCoin(oldObject.Price), + CreatedAt: oldObject.CreatedAt, + } + + return &to +} + +func migrateOrder(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var oldObject mv1beta1.Order + cdc.MustUnmarshal(fromBz, &oldObject) + + to := mv1beta2.Order{ + OrderID: mv1beta2.OrderID{ + Owner: oldObject.OrderID.Owner, + DSeq: oldObject.OrderID.DSeq, + GSeq: oldObject.OrderID.GSeq, + OSeq: oldObject.OrderID.OSeq, + }, + State: mv1beta2.Order_State(oldObject.State), + Spec: dmigrate.GroupSpecFromV1Beta1(oldObject.Spec), + CreatedAt: oldObject.CreatedAt, + } + + return &to +} diff --git a/migrations/v0.15.0/migrations.go b/migrations/v0.15.0/migrations.go new file mode 100644 index 0000000000..287d253c56 --- /dev/null +++ b/migrations/v0.15.0/migrations.go @@ -0,0 +1,23 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + av1beta2 "github.com/akash-network/akash-api/go/node/audit/v1beta2" + cv1beta2 "github.com/akash-network/akash-api/go/node/cert/v1beta2" + dv1beta2 "github.com/akash-network/akash-api/go/node/deployment/v1beta2" + ev1beta2 "github.com/akash-network/akash-api/go/node/escrow/v1beta2" + mv1beta2 "github.com/akash-network/akash-api/go/node/market/v1beta2" + pv1beta2 "github.com/akash-network/akash-api/go/node/provider/v1beta2" + + "github.com/akash-network/node/migrations/consensus" +) + +func init() { + consensus.RegisterMigration(av1beta2.ModuleName, 1, newAuditMigration) + consensus.RegisterMigration(cv1beta2.ModuleName, 1, newCertMigration) + consensus.RegisterMigration(dv1beta2.ModuleName, 1, newDeploymentMigration) + consensus.RegisterMigration(mv1beta2.ModuleName, 1, newMarketMigration) + consensus.RegisterMigration(pv1beta2.ModuleName, 1, newProviderMigration) + consensus.RegisterMigration(ev1beta2.ModuleName, 1, newEscrowMigration) +} diff --git a/util/legacy/v015/proto_compatibility_test.go b/migrations/v0.15.0/proto_compatibility_test.go similarity index 75% rename from util/legacy/v015/proto_compatibility_test.go rename to migrations/v0.15.0/proto_compatibility_test.go index d56533ef61..b460078ca1 100644 --- a/util/legacy/v015/proto_compatibility_test.go +++ b/migrations/v0.15.0/proto_compatibility_test.go @@ -1,22 +1,19 @@ -package v015_test +package v0_15_0_test import ( "testing" - dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta2" - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - dtypesv1beta1 "github.com/akash-network/akash-api/go/node/deployment/v1beta1" - - etypesv1beta1 "github.com/akash-network/akash-api/go/node/escrow/v1beta1" - - etypes "github.com/akash-network/akash-api/go/node/escrow/v1beta2" - mtypes "github.com/akash-network/akash-api/go/node/market/v1beta2" - - mtypesv1beta1 "github.com/akash-network/akash-api/go/node/market/v1beta1" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + dv1beta1 "github.com/akash-network/akash-api/go/node/deployment/v1beta1" + dv1beta2 "github.com/akash-network/akash-api/go/node/deployment/v1beta2" + ev1beta1 "github.com/akash-network/akash-api/go/node/escrow/v1beta1" + ev1beta2 "github.com/akash-network/akash-api/go/node/escrow/v1beta2" + mv1beta1 "github.com/akash-network/akash-api/go/node/market/v1beta1" + mv1beta2 "github.com/akash-network/akash-api/go/node/market/v1beta2" types "github.com/akash-network/akash-api/go/node/types/v1beta1" "github.com/akash-network/node/app" @@ -27,35 +24,35 @@ var ( ) func TestDeployment_DeploymentProto_IsCompatible(t *testing.T) { - oldProto := dtypesv1beta1.Deployment{ - DeploymentID: dtypesv1beta1.DeploymentID{Owner: "A", DSeq: 1}, - State: dtypesv1beta1.DeploymentActive, + oldProto := dv1beta1.Deployment{ + DeploymentID: dv1beta1.DeploymentID{Owner: "A", DSeq: 1}, + State: dv1beta1.DeploymentActive, Version: []byte{1, 2, 3, 4}, CreatedAt: 5, } - expectedProto := dtypes.Deployment{ - DeploymentID: dtypes.DeploymentID{Owner: "A", DSeq: 1}, - State: dtypes.DeploymentActive, + expectedProto := dv1beta2.Deployment{ + DeploymentID: dv1beta2.DeploymentID{Owner: "A", DSeq: 1}, + State: dv1beta2.DeploymentActive, Version: []byte{1, 2, 3, 4}, CreatedAt: 5, } - var actualProto dtypes.Deployment + var actualProto dv1beta2.Deployment cdc.MustUnmarshal(cdc.MustMarshal(&oldProto), &actualProto) require.Equal(t, expectedProto, actualProto) require.Equal(t, cdc.MustMarshal(&oldProto), cdc.MustMarshal(&expectedProto)) } func TestDeployment_GroupProto_IsNotCompatible(t *testing.T) { - oldProto := dtypesv1beta1.Group{ - GroupID: dtypesv1beta1.GroupID{ + oldProto := dv1beta1.Group{ + GroupID: dv1beta1.GroupID{ Owner: "A", DSeq: 1, GSeq: 2, }, - State: dtypesv1beta1.GroupOpen, - GroupSpec: dtypesv1beta1.GroupSpec{ + State: dv1beta1.GroupOpen, + GroupSpec: dv1beta1.GroupSpec{ Name: "A", Requirements: types.PlacementRequirements{ SignedBy: types.SignedBy{ @@ -67,7 +64,7 @@ func TestDeployment_GroupProto_IsNotCompatible(t *testing.T) { Value: "a", }}, }, - Resources: []dtypesv1beta1.Resource{{ + Resources: []dv1beta1.Resource{{ Resources: types.ResourceUnits{ CPU: &types.CPU{ Units: types.ResourceValue{Val: sdk.NewInt(1)}, @@ -99,13 +96,13 @@ func TestDeployment_GroupProto_IsNotCompatible(t *testing.T) { CreatedAt: 5, } - var actualProto dtypes.Group + var actualProto dv1beta2.Group require.Error(t, cdc.Unmarshal(cdc.MustMarshal(&oldProto), &actualProto)) // it doesn't unmarshal } func TestEscrow_AccountProto_IsNotCompatible(t *testing.T) { - oldProto := etypesv1beta1.Account{ - ID: etypesv1beta1.AccountID{ + oldProto := ev1beta1.Account{ + ID: ev1beta1.AccountID{ Scope: "a", XID: "a", }, @@ -116,8 +113,8 @@ func TestEscrow_AccountProto_IsNotCompatible(t *testing.T) { SettledAt: 2, } - expectedProto := etypes.Account{ - ID: etypes.AccountID{ + expectedProto := ev1beta2.Account{ + ID: ev1beta2.AccountID{ Scope: "a", XID: "a", }, @@ -130,15 +127,15 @@ func TestEscrow_AccountProto_IsNotCompatible(t *testing.T) { Funds: sdk.DecCoin{}, } - var actualProto etypes.Account + var actualProto ev1beta2.Account cdc.MustUnmarshal(cdc.MustMarshal(&oldProto), &actualProto) // although it unmarshalls require.NotEqual(t, expectedProto, actualProto) // but the result isn't equal require.NotEqual(t, cdc.MustMarshal(&oldProto), cdc.MustMarshal(&expectedProto)) // neither is marshalled bytes } func TestEscrow_PaymentProto_IsNotCompatible(t *testing.T) { - oldProto := etypesv1beta1.Payment{ - AccountID: etypesv1beta1.AccountID{ + oldProto := ev1beta1.Payment{ + AccountID: ev1beta1.AccountID{ Scope: "a", XID: "a", }, @@ -150,8 +147,8 @@ func TestEscrow_PaymentProto_IsNotCompatible(t *testing.T) { Withdrawn: sdk.NewCoin("uakt", sdk.NewInt(1)), } - expectedProto := etypes.FractionalPayment{ - AccountID: etypes.AccountID{ + expectedProto := ev1beta2.FractionalPayment{ + AccountID: ev1beta2.AccountID{ Scope: "a", XID: "a", }, @@ -163,88 +160,88 @@ func TestEscrow_PaymentProto_IsNotCompatible(t *testing.T) { Withdrawn: sdk.NewCoin("uakt", sdk.NewInt(1)), } - var actualProto etypes.FractionalPayment + var actualProto ev1beta2.FractionalPayment cdc.MustUnmarshal(cdc.MustMarshal(&oldProto), &actualProto) // although it unmarshalls require.NotEqual(t, expectedProto, actualProto) // but the result isn't equal require.NotEqual(t, cdc.MustMarshal(&oldProto), cdc.MustMarshal(&expectedProto)) // neither is marshalled bytes } func TestMarket_BidProto_IsNotCompatible(t *testing.T) { - oldProto := mtypesv1beta1.Bid{ - BidID: mtypesv1beta1.BidID{ + oldProto := mv1beta1.Bid{ + BidID: mv1beta1.BidID{ Owner: "a", DSeq: 1, GSeq: 2, OSeq: 3, Provider: "a", }, - State: mtypesv1beta1.BidActive, + State: mv1beta1.BidActive, Price: sdk.NewCoin("uakt", sdk.NewInt(1)), CreatedAt: 1, } - expectedProto := mtypes.Bid{ - BidID: mtypes.BidID{ + expectedProto := mv1beta2.Bid{ + BidID: mv1beta2.BidID{ Owner: "a", DSeq: 1, GSeq: 2, OSeq: 3, Provider: "a", }, - State: mtypes.BidActive, + State: mv1beta2.BidActive, Price: sdk.NewDecCoin("uakt", sdk.NewInt(1)), CreatedAt: 1, } - var actualProto mtypes.Bid + var actualProto mv1beta2.Bid cdc.MustUnmarshal(cdc.MustMarshal(&oldProto), &actualProto) // although it unmarshalls require.NotEqual(t, expectedProto, actualProto) // but the result isn't equal require.NotEqual(t, cdc.MustMarshal(&oldProto), cdc.MustMarshal(&expectedProto)) // neither is marshalled bytes } func TestMarket_LeaseProto_IsNotCompatible(t *testing.T) { - oldProto := mtypesv1beta1.Lease{ - LeaseID: mtypesv1beta1.LeaseID{ + oldProto := mv1beta1.Lease{ + LeaseID: mv1beta1.LeaseID{ Owner: "a", DSeq: 1, GSeq: 2, OSeq: 3, Provider: "a", }, - State: mtypesv1beta1.LeaseActive, + State: mv1beta1.LeaseActive, Price: sdk.NewCoin("uakt", sdk.NewInt(1)), CreatedAt: 1, } - expectedProto := mtypes.Lease{ - LeaseID: mtypes.LeaseID{ + expectedProto := mv1beta2.Lease{ + LeaseID: mv1beta2.LeaseID{ Owner: "a", DSeq: 1, GSeq: 2, OSeq: 3, Provider: "a", }, - State: mtypes.LeaseActive, + State: mv1beta2.LeaseActive, Price: sdk.NewDecCoin("uakt", sdk.NewInt(1)), CreatedAt: 1, } - var actualProto mtypes.Lease + var actualProto mv1beta2.Lease cdc.MustUnmarshal(cdc.MustMarshal(&oldProto), &actualProto) // although it unmarshalls require.NotEqual(t, expectedProto, actualProto) // but the result isn't equal require.NotEqual(t, cdc.MustMarshal(&oldProto), cdc.MustMarshal(&expectedProto)) // neither is marshalled bytes } func TestMarket_OrderProto_IsNotCompatible(t *testing.T) { - oldProto := mtypesv1beta1.Order{ - OrderID: mtypesv1beta1.OrderID{ + oldProto := mv1beta1.Order{ + OrderID: mv1beta1.OrderID{ Owner: "a", DSeq: 1, GSeq: 2, OSeq: 3, }, - State: mtypesv1beta1.OrderActive, - Spec: dtypesv1beta1.GroupSpec{ + State: mv1beta1.OrderActive, + Spec: dv1beta1.GroupSpec{ Name: "A", Requirements: types.PlacementRequirements{ SignedBy: types.SignedBy{ @@ -256,7 +253,7 @@ func TestMarket_OrderProto_IsNotCompatible(t *testing.T) { Value: "a", }}, }, - Resources: []dtypesv1beta1.Resource{{ + Resources: []dv1beta1.Resource{{ Resources: types.ResourceUnits{ CPU: &types.CPU{ Units: types.ResourceValue{Val: sdk.NewInt(1)}, @@ -288,6 +285,6 @@ func TestMarket_OrderProto_IsNotCompatible(t *testing.T) { CreatedAt: 1, } - var actualProto mtypes.Order + var actualProto mv1beta2.Order require.Error(t, cdc.Unmarshal(cdc.MustMarshal(&oldProto), &actualProto)) // it doesn't unmarshal } diff --git a/migrations/v0.15.0/provider.go b/migrations/v0.15.0/provider.go new file mode 100644 index 0000000000..9ddf447801 --- /dev/null +++ b/migrations/v0.15.0/provider.go @@ -0,0 +1,46 @@ +// Package v0_15_0 +// nolint revive +package v0_15_0 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/akash-network/node/migrations/consensus" +) + +type providerMigrations struct { + consensus.Migrator +} + +func newProviderMigration(m consensus.Migrator) consensus.Migration { + return providerMigrations{Migrator: m} +} + +func (m providerMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates provider from version 1 to 2. +func (m providerMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + // old key is of format: + // ownerAddrBytes (20 bytes) + // new key is of format + // ownerAddrLen (1 byte) || ownerAddrBytes + oldStoreIter := store.Iterator(nil, nil) + defer func() { + _ = oldStoreIter.Close() + }() + + for ; oldStoreIter.Valid(); oldStoreIter.Next() { + newStoreKey := address.MustLengthPrefix(oldStoreIter.Key()) + + // Set new key on store. Values don't change. + store.Set(newStoreKey, oldStoreIter.Value()) + store.Delete(oldStoreIter.Key()) + } + return nil +} diff --git a/migrations/v0.24.0/deployment.go b/migrations/v0.24.0/deployment.go new file mode 100644 index 0000000000..7585a2e968 --- /dev/null +++ b/migrations/v0.24.0/deployment.go @@ -0,0 +1,47 @@ +// Package v0_24_0 +// nolint revive +package v0_24_0 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + dv1beta2 "github.com/akash-network/akash-api/go/node/deployment/v1beta2" + dmigrate "github.com/akash-network/akash-api/go/node/deployment/v1beta3/migrate" + + "github.com/akash-network/node/migrations/consensus" +) + +type deploymentMigrations struct { + consensus.Migrator +} + +func newDeploymentMigration(m consensus.Migrator) consensus.Migration { + return deploymentMigrations{Migrator: m} +} + +func (m deploymentMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates deployment from version 2 to 3. +func (m deploymentMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + err := consensus.MigrateValue(store, m.Codec(), dv1beta2.GroupPrefix(), migrateDeploymentGroup) + + if err != nil { + return err + } + + return nil +} + +func migrateDeploymentGroup(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var from dv1beta2.Group + cdc.MustUnmarshal(fromBz, &from) + + to := dmigrate.GroupFromV1Beta2(from) + return &to +} diff --git a/migrations/v0.24.0/market.go b/migrations/v0.24.0/market.go new file mode 100644 index 0000000000..8560fc550d --- /dev/null +++ b/migrations/v0.24.0/market.go @@ -0,0 +1,59 @@ +// Package v0_24_0 +// nolint revive +package v0_24_0 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkmodule "github.com/cosmos/cosmos-sdk/types/module" + + dmigrate "github.com/akash-network/akash-api/go/node/deployment/v1beta3/migrate" + mv1beta2 "github.com/akash-network/akash-api/go/node/market/v1beta2" + mv1beta3 "github.com/akash-network/akash-api/go/node/market/v1beta3" + + "github.com/akash-network/node/migrations/consensus" +) + +type marketMigrations struct { + consensus.Migrator +} + +func newMarketMigration(m consensus.Migrator) consensus.Migration { + return marketMigrations{Migrator: m} +} + +func (m marketMigrations) GetHandler() sdkmodule.MigrationHandler { + return m.handler +} + +// handler migrates deployment from version 2 to 3. +func (m marketMigrations) handler(ctx sdk.Context) error { + store := ctx.KVStore(m.StoreKey()) + + err := consensus.MigrateValue(store, m.Codec(), mv1beta3.OrderPrefix(), migrateOrder) + + if err != nil { + return err + } + + return nil +} + +func migrateOrder(fromBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { + var oldObject mv1beta2.Order + cdc.MustUnmarshal(fromBz, &oldObject) + + to := mv1beta3.Order{ + OrderID: mv1beta3.OrderID{ + Owner: oldObject.OrderID.Owner, + DSeq: oldObject.OrderID.DSeq, + GSeq: oldObject.OrderID.GSeq, + OSeq: oldObject.OrderID.OSeq, + }, + State: mv1beta3.Order_State(oldObject.State), + Spec: dmigrate.GroupSpecFromV1Beta2(oldObject.Spec), + CreatedAt: oldObject.CreatedAt, + } + + return &to +} diff --git a/migrations/v0.24.0/migrations.go b/migrations/v0.24.0/migrations.go new file mode 100644 index 0000000000..4230d9b82e --- /dev/null +++ b/migrations/v0.24.0/migrations.go @@ -0,0 +1,15 @@ +// Package v0_24_0 +// nolint revive +package v0_24_0 + +import ( + dv1beta3 "github.com/akash-network/akash-api/go/node/deployment/v1beta3" + mv1beta3 "github.com/akash-network/akash-api/go/node/market/v1beta3" + + "github.com/akash-network/node/migrations/consensus" +) + +func init() { + consensus.RegisterMigration(dv1beta3.ModuleName, 2, newDeploymentMigration) + consensus.RegisterMigration(mv1beta3.ModuleName, 2, newMarketMigration) +} diff --git a/x/audit/keeper/keeper.go b/x/audit/keeper/keeper.go index 6ae6a5e4f8..15cf20908c 100644 --- a/x/audit/keeper/keeper.go +++ b/x/audit/keeper/keeper.go @@ -39,6 +39,11 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +// StoreKey returns store key +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // GetProviderByAuditor returns a provider with given auditor and owner id func (k Keeper) GetProviderByAuditor(ctx sdk.Context, id types.ProviderID) (types.Provider, bool) { store := ctx.KVStore(k.skey) diff --git a/x/audit/keeper/migrations.go b/x/audit/keeper/migrations.go deleted file mode 100644 index bfa5fe04fd..0000000000 --- a/x/audit/keeper/migrations.go +++ /dev/null @@ -1,22 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - - v015 "github.com/akash-network/node/x/audit/legacy/v015" -) - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper Keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k Keeper) Migrator { - return Migrator{keeper: k} -} - -// Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v015.MigrateStore(ctx, m.keeper.skey) -} diff --git a/x/audit/legacy/v015/store.go b/x/audit/legacy/v015/store.go deleted file mode 100644 index 9ce3e85691..0000000000 --- a/x/audit/legacy/v015/store.go +++ /dev/null @@ -1,18 +0,0 @@ -package v015 - -import ( - types "github.com/akash-network/akash-api/go/node/audit/v1beta2" - sdk "github.com/cosmos/cosmos-sdk/types" - v043 "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v043" -) - -// MigrateStore performs in-place store migrations from v0.14 to v0.15. The -// migration includes: -// -// - Change addresses to be length-prefixed -func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey) error { - store := ctx.KVStore(storeKey) - v043.MigratePrefixAddressAddress(store, types.PrefixProviderID()) - - return nil -} diff --git a/x/audit/module.go b/x/audit/module.go index 81eaf3c865..4f9e3b006f 100644 --- a/x/audit/module.go +++ b/x/audit/module.go @@ -25,6 +25,7 @@ import ( v1beta2types "github.com/akash-network/akash-api/go/node/audit/v1beta2" types "github.com/akash-network/akash-api/go/node/audit/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/audit/client/cli" "github.com/akash-network/node/x/audit/client/rest" "github.com/akash-network/node/x/audit/handler" @@ -163,10 +164,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { querier := keeper.Querier{Keeper: am.keeper} types.RegisterQueryServer(cfg.QueryServer(), querier) - m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(err) - } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // RegisterQueryService registers a GRPC query service to respond to the @@ -202,7 +204,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________ @@ -222,6 +224,7 @@ func NewAppModuleSimulation(k keeper.Keeper, pkeeper pkeeper.Keeper) AppModuleSi } // AppModuleSimulation functions + // GenerateGenesisState creates a randomized GenState of the staking module. func (AppModuleSimulation) GenerateGenesisState(simState *module.SimulationState) { // simulation.RandomizedGenState(simState) diff --git a/x/cert/keeper/keeper.go b/x/cert/keeper/keeper.go index 3856b7c20f..ed11117586 100644 --- a/x/cert/keeper/keeper.go +++ b/x/cert/keeper/keeper.go @@ -11,6 +11,7 @@ import ( type Keeper interface { Querier() types.QueryServer Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey CreateCertificate(sdk.Context, sdk.Address, []byte, []byte) error RevokeCertificate(sdk.Context, types.CertID) error GetCertificateByID(ctx sdk.Context, id types.CertID) (types.CertificateResponse, bool) @@ -42,6 +43,11 @@ func (k keeper) Codec() codec.BinaryCodec { return k.cdc } +// StoreKey returns store key +func (k keeper) StoreKey() sdk.StoreKey { + return k.skey +} + func (k keeper) CreateCertificate(ctx sdk.Context, owner sdk.Address, crt []byte, pubkey []byte) error { store := ctx.KVStore(k.skey) diff --git a/x/cert/keeper/migrations.go b/x/cert/keeper/migrations.go deleted file mode 100644 index c7a0ef2b79..0000000000 --- a/x/cert/keeper/migrations.go +++ /dev/null @@ -1,16 +0,0 @@ -package keeper - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper *keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k Keeper) Migrator { - return Migrator{keeper: k.(*keeper)} -} - -// // Migrate1to2 migrates from version 1 to 2. -// func (m Migrator) Migrate1to2(ctx sdk.Context) error { -// return v015.MigrateStore(ctx, m.keeper.skey) -// } diff --git a/x/cert/module.go b/x/cert/module.go index 0fded3ca9e..0b54426898 100644 --- a/x/cert/module.go +++ b/x/cert/module.go @@ -24,6 +24,7 @@ import ( "github.com/akash-network/akash-api/go/node/cert/v1beta2" types "github.com/akash-network/akash-api/go/node/cert/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/cert/client/cli" "github.com/akash-network/node/x/cert/handler" "github.com/akash-network/node/x/cert/keeper" @@ -150,10 +151,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), handler.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper.Querier()) - // m := keeper.NewMigrator(am.keeper) - // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - // panic(err) - // } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -182,7 +184,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________ diff --git a/x/deployment/keeper/keeper.go b/x/deployment/keeper/keeper.go index 3277b32ba1..ec1506cdb4 100644 --- a/x/deployment/keeper/keeper.go +++ b/x/deployment/keeper/keeper.go @@ -11,6 +11,7 @@ import ( ) type IKeeper interface { + StoreKey() sdk.StoreKey Codec() codec.BinaryCodec GetDeployment(ctx sdk.Context, id types.DeploymentID) (types.Deployment, bool) GetGroup(ctx sdk.Context, id types.GroupID) (types.Group, bool) @@ -63,6 +64,10 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // GetDeployment returns deployment details with provided DeploymentID func (k Keeper) GetDeployment(ctx sdk.Context, id types.DeploymentID) (types.Deployment, bool) { store := ctx.KVStore(k.skey) diff --git a/x/deployment/keeper/migrations.go b/x/deployment/keeper/migrations.go deleted file mode 100644 index bb634c8d23..0000000000 --- a/x/deployment/keeper/migrations.go +++ /dev/null @@ -1,16 +0,0 @@ -package keeper - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper Keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k IKeeper) Migrator { - return Migrator{keeper: k.(Keeper)} -} - -// // Migrate1to2 migrates from version 1 to 2. -// func (m Migrator) Migrate1to2(ctx sdk.Context) error { -// return v015.MigrateStore(ctx, m.keeper.skey, m.keeper.cdc) -// } diff --git a/x/deployment/module.go b/x/deployment/module.go index 37c94693dc..2972136dbb 100644 --- a/x/deployment/module.go +++ b/x/deployment/module.go @@ -19,6 +19,7 @@ import ( v1beta2types "github.com/akash-network/akash-api/go/node/deployment/v1beta2" types "github.com/akash-network/akash-api/go/node/deployment/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/deployment/client/cli" "github.com/akash-network/node/x/deployment/client/rest" "github.com/akash-network/node/x/deployment/handler" @@ -157,10 +158,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { querier := am.keeper.NewQuerier() types.RegisterQueryServer(cfg.QueryServer(), querier) - // m := keeper.NewMigrator(am.keeper) - // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - // panic(err) - // } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -189,7 +191,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // AppModuleSimulation implements an application simulation module for the deployment module. diff --git a/x/escrow/genesis.go b/x/escrow/genesis.go index 36b9f417c8..7c109fc844 100644 --- a/x/escrow/genesis.go +++ b/x/escrow/genesis.go @@ -1,6 +1,9 @@ package escrow import ( + "encoding/json" + + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/pkg/errors" abci "github.com/tendermint/tendermint/abci/types" @@ -12,7 +15,6 @@ import ( // ValidateGenesis does validation check of the Genesis and returns error in case of failure func ValidateGenesis(data *types.GenesisState) error { - amap := make(map[types.AccountID]types.Account, len(data.Accounts)) pmap := make(map[types.AccountID][]types.FractionalPayment, len(data.Payments)) @@ -92,3 +94,15 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { func DefaultGenesisState() *types.GenesisState { return &types.GenesisState{} } + +// GetGenesisStateFromAppState returns x/escrow GenesisState given raw application +// genesis state. +func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *types.GenesisState { + var genesisState types.GenesisState + + if appState[ModuleName] != nil { + cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState) + } + + return &genesisState +} diff --git a/x/escrow/keeper/keeper.go b/x/escrow/keeper/keeper.go index ee09caa761..ce90d42542 100644 --- a/x/escrow/keeper/keeper.go +++ b/x/escrow/keeper/keeper.go @@ -12,6 +12,8 @@ type AccountHook func(sdk.Context, types.Account) type PaymentHook func(sdk.Context, types.FractionalPayment) type Keeper interface { + Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey AccountCreate(ctx sdk.Context, id types.AccountID, owner, depositor sdk.AccAddress, deposit sdk.Coin) error AccountDeposit(ctx sdk.Context, id types.AccountID, depositor sdk.AccAddress, amount sdk.Coin) error AccountSettle(ctx sdk.Context, id types.AccountID) (bool, error) @@ -50,6 +52,15 @@ type keeper struct { } } +func (k *keeper) Codec() codec.BinaryCodec { + return k.cdc +} + +// StoreKey returns store key +func (k *keeper) StoreKey() sdk.StoreKey { + return k.skey +} + func (k *keeper) AccountCreate(ctx sdk.Context, id types.AccountID, owner, depositor sdk.AccAddress, deposit sdk.Coin) error { store := ctx.KVStore(k.skey) key := accountKey(id) diff --git a/x/escrow/keeper/migrations.go b/x/escrow/keeper/migrations.go deleted file mode 100644 index 52a8c61221..0000000000 --- a/x/escrow/keeper/migrations.go +++ /dev/null @@ -1,22 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - - v015 "github.com/akash-network/node/x/escrow/legacy/v015" -) - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper *keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k Keeper) Migrator { - return Migrator{keeper: k.(*keeper)} -} - -// Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v015.MigrateStore(ctx, m.keeper.skey, m.keeper.cdc) -} diff --git a/x/escrow/legacy/v015/store.go b/x/escrow/legacy/v015/store.go deleted file mode 100644 index bd842e1c2d..0000000000 --- a/x/escrow/legacy/v015/store.go +++ /dev/null @@ -1,62 +0,0 @@ -package v015 - -import ( - "github.com/akash-network/akash-api/go/node/escrow/v1beta1" - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - - types "github.com/akash-network/akash-api/go/node/escrow/v1beta2" - v015 "github.com/akash-network/node/util/legacy/v015" -) - -// MigrateStore performs in-place store migrations from v0.14 to v0.15. The -// migration includes: -// -// - Migrating Account proto from v1beta1 to v1beta2 -// - Migrating Payment proto from v1beta1 to v1beta2 -func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { - store := ctx.KVStore(storeKey) - v015.MigrateValue(store, cdc, types.AccountKeyPrefix(), migrateAccount) - v015.MigrateValue(store, cdc, types.PaymentKeyPrefix(), migratePayment) - - return nil -} - -func migrateAccount(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { - var oldObj v1beta1.Account - cdc.MustUnmarshal(oldValueBz, &oldObj) - return &types.Account{ - ID: types.AccountID{ - Scope: oldObj.ID.Scope, - XID: oldObj.ID.XID, - }, - Owner: oldObj.Owner, - State: types.Account_State(oldObj.State), - Balance: sdk.NewDecCoinFromCoin(oldObj.Balance), - Transferred: sdk.NewDecCoinFromCoin(oldObj.Transferred), - SettledAt: oldObj.SettledAt, - // Correctly initialize the new fields - // - Account.Depositor as Account.Owner - // - Account.Funds as a DecCoin of zero value - Depositor: oldObj.Owner, - Funds: sdk.NewDecCoin(oldObj.Balance.Denom, sdk.ZeroInt()), - } -} - -func migratePayment(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { - var oldObject v1beta1.Payment - cdc.MustUnmarshal(oldValueBz, &oldObject) - - return &types.FractionalPayment{ - AccountID: types.AccountID{ - Scope: oldObject.AccountID.Scope, - XID: oldObject.AccountID.XID, - }, - PaymentID: oldObject.PaymentID, - Owner: oldObject.Owner, - State: types.FractionalPayment_State(oldObject.State), - Rate: sdk.NewDecCoinFromCoin(oldObject.Rate), - Balance: sdk.NewDecCoinFromCoin(oldObject.Balance), - Withdrawn: oldObject.Withdrawn, - } -} diff --git a/x/escrow/module.go b/x/escrow/module.go index 262d908ce0..6bc470ea0f 100644 --- a/x/escrow/module.go +++ b/x/escrow/module.go @@ -21,6 +21,7 @@ import ( "github.com/gogo/protobuf/grpc" "github.com/pkg/errors" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/escrow/client/cli" "github.com/akash-network/node/x/escrow/client/rest" "github.com/akash-network/node/x/escrow/keeper" @@ -159,10 +160,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { querier := keeper.NewQuerier(am.keeper) types.RegisterQueryServer(cfg.QueryServer(), querier) - m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(err) - } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // RegisterQueryService registers a GRPC query service to respond to the @@ -198,7 +200,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________ diff --git a/x/gov/keeper/keeper.go b/x/gov/keeper/keeper.go index 7dc47d60a7..001f4a96c7 100644 --- a/x/gov/keeper/keeper.go +++ b/x/gov/keeper/keeper.go @@ -10,6 +10,7 @@ import ( type IKeeper interface { Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey SetDepositParams(ctx sdk.Context, params types.DepositParams) error GetDepositParams(ctx sdk.Context) (params types.DepositParams) } @@ -39,6 +40,10 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // SetDepositParams sets the deployment parameters to the paramspace. func (k Keeper) SetDepositParams(ctx sdk.Context, params types.DepositParams) error { k.pspace.Set(ctx, types.KeyDepositParams, ¶ms) diff --git a/x/gov/module.go b/x/gov/module.go index a237a950ca..2dd22c7910 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -19,6 +19,7 @@ import ( types "github.com/akash-network/akash-api/go/node/gov/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/gov/keeper" "github.com/akash-network/node/x/gov/simulation" ) @@ -126,14 +127,11 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { // RegisterServices registers the module's services func (am AppModule) RegisterServices(cfg module.Configurator) { - // types.RegisterMsgServer(cfg.MsgServer(), handler.NewMsgServerImpl(am.keeper, am.mkeeper)) - // querier := am.keeper.NewQuerier() - // types.RegisterQueryServer(cfg.QueryServer(), querier) - // - // m := keeper.NewMigrator(am.keeper) - // if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - // panic(err) - // } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -162,7 +160,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________ diff --git a/x/inflation/keeper/keeper.go b/x/inflation/keeper/keeper.go index 81c745f898..a3c98b44c0 100644 --- a/x/inflation/keeper/keeper.go +++ b/x/inflation/keeper/keeper.go @@ -10,6 +10,8 @@ import ( ) type IKeeper interface { + Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey GetParams(ctx sdk.Context) (params types.Params) SetParams(ctx sdk.Context, params types.Params) } @@ -35,6 +37,15 @@ func NewKeeper(cdc codec.BinaryCodec, skey sdk.StoreKey, pspace paramtypes.Subsp } } +// Codec returns keeper codec +func (k Keeper) Codec() codec.BinaryCodec { + return k.cdc +} + +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // GetParams returns the total set of deployment parameters. func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { k.pspace.GetParamSet(ctx, ¶ms) diff --git a/x/inflation/module.go b/x/inflation/module.go index dd05fd5630..8ae91a8eef 100644 --- a/x/inflation/module.go +++ b/x/inflation/module.go @@ -19,6 +19,7 @@ import ( types "github.com/akash-network/akash-api/go/node/inflation/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/inflation/keeper" "github.com/akash-network/node/x/inflation/simulation" ) @@ -118,7 +119,13 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { } // RegisterServices registers the module's services -func (am AppModule) RegisterServices(cfg module.Configurator) {} +func (am AppModule) RegisterServices(cfg module.Configurator) { + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) +} // BeginBlock performs no-op func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} @@ -146,7 +153,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 1 + return consensus.ModuleVersion(ModuleName) } // AppModuleSimulation implements an application simulation module for the deployment module. diff --git a/x/market/keeper/grpc_query.go b/x/market/keeper/grpc_query.go index cc2fca750f..cd457c04f9 100644 --- a/x/market/keeper/grpc_query.go +++ b/x/market/keeper/grpc_query.go @@ -3,8 +3,6 @@ package keeper import ( "context" - "github.com/akash-network/node/x/market/keeper/keys" - "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -14,6 +12,8 @@ import ( dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3" types "github.com/akash-network/akash-api/go/node/market/v1beta3" + + keys "github.com/akash-network/node/x/market/keeper/keys/v1beta3" ) // Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index 3c57a3f503..4c1fd2cc8c 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -1,21 +1,23 @@ package keeper import ( + "github.com/pkg/errors" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" - "github.com/pkg/errors" dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3" types "github.com/akash-network/akash-api/go/node/market/v1beta3" - "github.com/akash-network/node/x/market/keeper/keys" + keys "github.com/akash-network/node/x/market/keeper/keys/v1beta3" ) // TODO: use interface for all keepers, queriers type IKeeper interface { NewQuerier() Querier Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dtypes.GroupSpec) (types.Order, error) CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAddress, price sdk.DecCoin) (types.Bid, error) CreateLease(ctx sdk.Context, bid types.Bid) @@ -72,6 +74,11 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +// StoreKey returns store key +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // CreateOrder creates a new order with given group id and specifications. It returns created order func (k Keeper) CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dtypes.GroupSpec) (types.Order, error) { store := ctx.KVStore(k.skey) diff --git a/x/market/keeper/keys/v1beta1/key.go b/x/market/keeper/keys/v1beta1/key.go new file mode 100644 index 0000000000..e76fdbab41 --- /dev/null +++ b/x/market/keeper/keys/v1beta1/key.go @@ -0,0 +1,92 @@ +package v1beta1 + +import ( + "bytes" + "encoding/binary" + + dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta1" + types "github.com/akash-network/akash-api/go/node/market/v1beta1" +) + +var ( + orderPrefix = []byte{0x01, 0x00} + bidPrefix = []byte{0x02, 0x00} + leasePrefix = []byte{0x03, 0x00} // nolint: unused +) + +// nolint: unused +func orderKey(id types.OrderID) []byte { + buf := bytes.NewBuffer(orderPrefix) + buf.Write([]byte(id.Owner)) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + return buf.Bytes() +} + +// nolint: unused +func bidKey(id types.BidID) []byte { + buf := bytes.NewBuffer(bidPrefix) + buf.Write([]byte(id.Owner)) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + buf.Write([]byte(id.Provider)) + return buf.Bytes() +} + +// nolint: unused +func leaseKey(id types.LeaseID) []byte { + buf := bytes.NewBuffer(leasePrefix) + buf.Write([]byte(id.Owner)) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + buf.Write([]byte(id.Provider)) + return buf.Bytes() +} + +func OrdersForGroupPrefix(id dtypes.GroupID) []byte { + buf := bytes.NewBuffer(orderPrefix) + buf.Write([]byte(id.Owner)) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + return buf.Bytes() +} + +func BidsForOrderPrefix(id types.OrderID) []byte { + buf := bytes.NewBuffer(bidPrefix) + buf.Write([]byte(id.Owner)) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + return buf.Bytes() +} diff --git a/x/market/keeper/keys/v1beta2/key.go b/x/market/keeper/keys/v1beta2/key.go new file mode 100644 index 0000000000..991206c6e5 --- /dev/null +++ b/x/market/keeper/keys/v1beta2/key.go @@ -0,0 +1,164 @@ +package v1beta2 + +import ( + "bytes" + "encoding/binary" + + "github.com/cosmos/cosmos-sdk/types/address" + + dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta2" + types "github.com/akash-network/akash-api/go/node/market/v1beta2" + "github.com/akash-network/akash-api/go/sdkutil" +) + +func filterToPrefix(prefix []byte, owner string, dseq uint64, gseq, oseq uint32, provider string) ([]byte, error) { + buf := bytes.NewBuffer(prefix) + + if len(owner) == 0 { + return buf.Bytes(), nil + } + + if _, err := buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(owner))); err != nil { + return nil, err + } + + if dseq == 0 { + return buf.Bytes(), nil + } + if err := binary.Write(buf, binary.BigEndian, dseq); err != nil { + return nil, err + } + + if gseq == 0 { + return buf.Bytes(), nil + } + if err := binary.Write(buf, binary.BigEndian, gseq); err != nil { + return nil, err + } + + if oseq == 0 { + return buf.Bytes(), nil + } + if err := binary.Write(buf, binary.BigEndian, oseq); err != nil { + return nil, err + } + + if len(provider) == 0 { + return buf.Bytes(), nil + } + + if _, err := buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(provider))); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +func OrderPrefixFromFilter(f types.OrderFilters) ([]byte, error) { + return filterToPrefix(types.OrderPrefix(), f.Owner, f.DSeq, f.GSeq, f.OSeq, "") +} + +func LeasePrefixFromFilter(f types.LeaseFilters) ([]byte, bool, error) { + prefix, err := filterToPrefix(types.LeasePrefix(), f.Owner, f.DSeq, f.GSeq, f.OSeq, f.Provider) + return prefix, false, err +} + +func BidPrefixFromFilter(f types.BidFilters) ([]byte, error) { + return filterToPrefix(types.BidPrefix(), f.Owner, f.DSeq, f.GSeq, f.OSeq, f.Provider) +} + +func OrderKey(id types.OrderID) []byte { + buf := bytes.NewBuffer(types.OrderPrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + return buf.Bytes() +} + +func BidKey(id types.BidID) []byte { + buf := bytes.NewBuffer(types.BidPrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Provider))) + return buf.Bytes() +} + +func LeaseKey(id types.LeaseID) []byte { + buf := bytes.NewBuffer(types.LeasePrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Provider))) + return buf.Bytes() +} + +func secondaryLeaseKeyByProvider(id types.LeaseID) []byte { + buf := bytes.NewBuffer(types.SecondaryLeasePrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Provider))) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + return buf.Bytes() +} + +func SecondaryKeysForLease(id types.LeaseID) [][]byte { + return [][]byte{ + secondaryLeaseKeyByProvider(id), + } +} + +func OrdersForGroupPrefix(id dtypes.GroupID) []byte { + buf := bytes.NewBuffer(types.OrderPrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + return buf.Bytes() +} + +func BidsForOrderPrefix(id types.OrderID) []byte { + buf := bytes.NewBuffer(types.BidPrefix()) + buf.Write(address.MustLengthPrefix(sdkutil.MustAccAddressFromBech32(id.Owner))) + if err := binary.Write(buf, binary.BigEndian, id.DSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.GSeq); err != nil { + panic(err) + } + if err := binary.Write(buf, binary.BigEndian, id.OSeq); err != nil { + panic(err) + } + return buf.Bytes() +} diff --git a/x/market/keeper/keys/v1beta2/key_test.go b/x/market/keeper/keys/v1beta2/key_test.go new file mode 100644 index 0000000000..82636a734f --- /dev/null +++ b/x/market/keeper/keys/v1beta2/key_test.go @@ -0,0 +1,34 @@ +package v1beta2_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + types "github.com/akash-network/akash-api/go/node/market/v1beta2" + + _ "github.com/akash-network/node/testutil" + keys "github.com/akash-network/node/x/market/keeper/keys/v1beta2" +) + +func TestKeysAndSecondaryKeysFilter(t *testing.T) { + filter := types.LeaseFilters{ + Owner: "akash104fq56d9attl4m709h7mgx9lwqklnh05fhy5nu", + DSeq: 1, + GSeq: 2, + OSeq: 3, + Provider: "akash1vlaa09ytnl0hvu04wgs0d6zw5n6anjc3allk49", + State: types.LeaseClosed.String(), + } + + prefix, isSecondary, err := keys.LeasePrefixFromFilter(filter) + require.NoError(t, err) + require.False(t, isSecondary) + require.Equal(t, types.LeasePrefix(), prefix[0:2]) + + filter.Owner = "" + prefix, isSecondary, err = keys.LeasePrefixFromFilter(filter) + require.NoError(t, err) + require.False(t, isSecondary) + require.Equal(t, types.LeasePrefix(), prefix[0:2]) +} diff --git a/x/market/keeper/keys/key.go b/x/market/keeper/keys/v1beta3/key.go similarity index 99% rename from x/market/keeper/keys/key.go rename to x/market/keeper/keys/v1beta3/key.go index a060e8dafe..78ace686be 100644 --- a/x/market/keeper/keys/key.go +++ b/x/market/keeper/keys/v1beta3/key.go @@ -1,4 +1,4 @@ -package keys +package v1beta3 import ( "bytes" diff --git a/x/market/keeper/keys/key_test.go b/x/market/keeper/keys/v1beta3/key_test.go similarity index 90% rename from x/market/keeper/keys/key_test.go rename to x/market/keeper/keys/v1beta3/key_test.go index f79ef2ba08..ecac8320ec 100644 --- a/x/market/keeper/keys/key_test.go +++ b/x/market/keeper/keys/v1beta3/key_test.go @@ -1,18 +1,17 @@ -package keys_test +package v1beta3_test import ( "testing" + "github.com/stretchr/testify/require" + types "github.com/akash-network/akash-api/go/node/market/v1beta3" _ "github.com/akash-network/node/testutil" - "github.com/akash-network/node/x/market/keeper/keys" - - "github.com/stretchr/testify/require" + keys "github.com/akash-network/node/x/market/keeper/keys/v1beta3" ) func TestKeysAndSecondaryKeysFilter(t *testing.T) { - filter := types.LeaseFilters{ Owner: "akash104fq56d9attl4m709h7mgx9lwqklnh05fhy5nu", DSeq: 1, diff --git a/x/market/keeper/migrations.go b/x/market/keeper/migrations.go deleted file mode 100644 index ccfbfe272d..0000000000 --- a/x/market/keeper/migrations.go +++ /dev/null @@ -1,21 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper Keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k IKeeper) Migrator { - return Migrator{keeper: k.(Keeper)} -} - -// Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { - // return v015.MigrateStore(ctx, m.keeper.skey, m.keeper.cdc) - return nil -} diff --git a/x/market/legacy/v015/store.go b/x/market/legacy/v015/store.go deleted file mode 100644 index 7deada1893..0000000000 --- a/x/market/legacy/v015/store.go +++ /dev/null @@ -1,144 +0,0 @@ -package v015 - -// import ( -// "github.com/cosmos/cosmos-sdk/codec" -// "github.com/cosmos/cosmos-sdk/store/prefix" -// sdk "github.com/cosmos/cosmos-sdk/types" -// "github.com/cosmos/cosmos-sdk/types/address" -// -// v015 "github.com/akash-network/node/util/legacy/v015" -// dmigrate "github.com/akash-network/node/x/deployment/types/v1beta2/migrate" -// "github.com/akash-network/node/x/market/keeper/keys" -// "github.com/akash-network/akash-api/go/node/market/v1beta1" -// types "github.com/akash-network/akash-api/go/node/market/v1beta2" -// ) -// -// // MigrateStore performs in-place store migrations from v0.14 to v0.15. The -// // migration includes: -// // -// // - Change addresses to be length-prefixed -// // - Migrating Order proto from v1beta1 to v1beta2 -// // - Migrating Bid proto from v1beta1 to v1beta2 -// // - Migrating Lease proto from v1beta1 to v1beta2 -// func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { -// store := ctx.KVStore(storeKey) -// // Change addresses to be length-prefixed -// v015.MigratePrefixBech32AddrBytes(store, types.OrderPrefix()) -// migratePrefixBech32Uint64Uint32Uint32Bech32(store, types.BidPrefix()) -// migratePrefixBech32Uint64Uint32Uint32Bech32(store, types.LeasePrefix()) -// -// // Migrate protobuf -// v015.MigrateValue(store, cdc, types.OrderPrefix(), migrateOrder) -// v015.MigrateValue(store, cdc, types.BidPrefix(), migrateBid) -// v015.MigrateValue(store, cdc, types.LeasePrefix(), migrateLease) -// -// // add the mapping of secondary lease key -> lease key -// addSecondaryLeaseKeys(store, cdc) -// -// return nil -// } -// -// func addSecondaryLeaseKeys(baseStore sdk.KVStore, cdc codec.BinaryCodec) { -// store := prefix.NewStore(baseStore, types.LeasePrefix()) -// -// iter := sdk.KVStorePrefixIterator(store, types.LeasePrefix()) -// defer func() { -// _ = iter.Close() -// }() -// -// for ; iter.Valid(); iter.Next() { -// var lease types.Lease -// cdc.MustUnmarshal(iter.Value(), &lease) -// -// leaseKey := keys.LeaseKey(lease.GetLeaseID()) -// for _, secondaryKey := range keys.SecondaryKeysForLease(lease.GetLeaseID()) { -// store.Set(secondaryKey, leaseKey) -// } -// } -// } -// -// // migratePrefixBech32Uint64Uint32Uint32Bech32 is a helper function that migrates all keys of format: -// // prefix_bytes | address1_bech32_bytes | uint64 | uint32 | uint32 | address2_bech32_bytes -// // into format: -// // prefix_bytes | address1_len (1 byte) | address1_bytes | uint64 | uint32 | uint32 | address2_len (1 byte) | address2_bytes -// func migratePrefixBech32Uint64Uint32Uint32Bech32(store sdk.KVStore, prefixBz []byte) { -// oldStore := prefix.NewStore(store, prefixBz) -// -// oldStoreIter := oldStore.Iterator(nil, nil) -// defer oldStoreIter.Close() -// -// for ; oldStoreIter.Valid(); oldStoreIter.Next() { -// bech32Addr1 := string(oldStoreIter.Key()[:v015.V014Bech32AddrLen]) -// addr1, err := sdk.AccAddressFromBech32(bech32Addr1) -// if err != nil { -// panic(err) -// } -// -// midBz := oldStoreIter.Key()[v015.V014Bech32AddrLen : v015.V014Bech32AddrLen+16] -// -// bech32Addr2 := string(oldStoreIter.Key()[v015.V014Bech32AddrLen+16:]) -// addr2, err := sdk.AccAddressFromBech32(bech32Addr2) -// if err != nil { -// panic(err) -// } -// -// newStoreKey := append(append(append(prefixBz, address.MustLengthPrefix(addr1)...), midBz...), address.MustLengthPrefix(addr2)...) -// -// // Set new key on store. Values don't change. -// store.Set(newStoreKey, oldStoreIter.Value()) -// oldStore.Delete(oldStoreIter.Key()) -// } -// } -// -// func migrateLease(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { -// var oldObj v1beta1.Lease -// cdc.MustUnmarshal(oldValueBz, &oldObj) -// return &types.Lease{ -// LeaseID: types.LeaseID{ -// Owner: oldObj.LeaseID.Owner, -// DSeq: oldObj.LeaseID.DSeq, -// GSeq: oldObj.LeaseID.GSeq, -// OSeq: oldObj.LeaseID.OSeq, -// Provider: oldObj.LeaseID.Provider, -// }, -// State: types.Lease_State(oldObj.State), -// Price: sdk.NewDecCoinFromCoin(oldObj.Price), -// CreatedAt: oldObj.CreatedAt, -// ClosedOn: 0, // For leases created prior to this change never report the data -// } -// } -// -// func migrateBid(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { -// var oldObject v1beta1.Bid -// cdc.MustUnmarshal(oldValueBz, &oldObject) -// -// return &types.Bid{ -// BidID: types.BidID{ -// Owner: oldObject.BidID.Owner, -// DSeq: oldObject.BidID.DSeq, -// GSeq: oldObject.BidID.GSeq, -// OSeq: oldObject.BidID.OSeq, -// Provider: oldObject.BidID.Provider, -// }, -// State: types.Bid_State(oldObject.State), -// Price: sdk.NewDecCoinFromCoin(oldObject.Price), -// CreatedAt: oldObject.CreatedAt, -// } -// } -// -// func migrateOrder(oldValueBz []byte, cdc codec.BinaryCodec) codec.ProtoMarshaler { -// var oldObject v1beta1.Order -// cdc.MustUnmarshal(oldValueBz, &oldObject) -// -// return &types.Order{ -// OrderID: types.OrderID{ -// Owner: oldObject.OrderID.Owner, -// DSeq: oldObject.OrderID.DSeq, -// GSeq: oldObject.OrderID.GSeq, -// OSeq: oldObject.OrderID.OSeq, -// }, -// State: types.Order_State(oldObject.State), -// Spec: dmigrate.GroupSpecFromV1Beta1(oldObject.Spec), -// CreatedAt: oldObject.CreatedAt, -// } -// } diff --git a/x/market/module.go b/x/market/module.go index 16374db889..1a6c100e36 100644 --- a/x/market/module.go +++ b/x/market/module.go @@ -24,6 +24,7 @@ import ( v1beta2types "github.com/akash-network/akash-api/go/node/market/v1beta2" types "github.com/akash-network/akash-api/go/node/market/v1beta3" + "github.com/akash-network/node/migrations/consensus" akeeper "github.com/akash-network/node/x/audit/keeper" ekeeper "github.com/akash-network/node/x/escrow/keeper" "github.com/akash-network/node/x/market/client/cli" @@ -163,10 +164,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { querier := am.keepers.Market.NewQuerier() types.RegisterQueryServer(cfg.QueryServer(), querier) - m := keeper.NewMigrator(am.keepers.Market) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(err) - } + consensus.ModuleMigrations(ModuleName, am.keepers.Market, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -195,7 +197,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // AppModuleSimulation implements an application simulation module for the market module. diff --git a/x/provider/keeper/keeper.go b/x/provider/keeper/keeper.go index e42e900b32..e42c07d685 100644 --- a/x/provider/keeper/keeper.go +++ b/x/provider/keeper/keeper.go @@ -9,6 +9,7 @@ import ( type IKeeper interface { Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey Get(ctx sdk.Context, id sdk.Address) (types.Provider, bool) Create(ctx sdk.Context, provider types.Provider) error WithProviders(ctx sdk.Context, fn func(types.Provider) bool) @@ -40,6 +41,11 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +// StoreKey returns store key +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + // Get returns a provider with given provider id func (k Keeper) Get(ctx sdk.Context, id sdk.Address) (types.Provider, bool) { store := ctx.KVStore(k.skey) diff --git a/x/provider/keeper/migrations.go b/x/provider/keeper/migrations.go deleted file mode 100644 index 7e59eaa3f6..0000000000 --- a/x/provider/keeper/migrations.go +++ /dev/null @@ -1,22 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - - v015 "github.com/akash-network/node/x/provider/legacy/v015" -) - -// Migrator is a struct for handling in-place store migrations. -type Migrator struct { - keeper Keeper -} - -// NewMigrator returns a new Migrator. -func NewMigrator(k IKeeper) Migrator { - return Migrator{keeper: k.(Keeper)} -} - -// Migrate1to2 migrates from version 1 to 2. -func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v015.MigrateStore(ctx, m.keeper.skey) -} diff --git a/x/provider/legacy/v015/store.go b/x/provider/legacy/v015/store.go deleted file mode 100644 index 9ddc380f12..0000000000 --- a/x/provider/legacy/v015/store.go +++ /dev/null @@ -1,37 +0,0 @@ -package v015 - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" -) - -// MigrateStore performs in-place store migrations from v0.14 to v0.15. The -// migration includes: -// -// - Change addresses to be length-prefixed -func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey) error { - store := ctx.KVStore(storeKey) - migrateProviderKeys(store) - - return nil -} - -// migrateProviderKeys migrate the provider keys to cater for variable-length -// addresses. -func migrateProviderKeys(store sdk.KVStore) { - // old key is of format: - // ownerAddrBytes (20 bytes) - // new key is of format - // ownerAddrLen (1 byte) || ownerAddrBytes - - oldStoreIter := store.Iterator(nil, nil) - defer oldStoreIter.Close() - - for ; oldStoreIter.Valid(); oldStoreIter.Next() { - newStoreKey := address.MustLengthPrefix(oldStoreIter.Key()) - - // Set new key on store. Values don't change. - store.Set(newStoreKey, oldStoreIter.Value()) - store.Delete(oldStoreIter.Key()) - } -} diff --git a/x/provider/module.go b/x/provider/module.go index f3dbef6f05..29897afbc6 100644 --- a/x/provider/module.go +++ b/x/provider/module.go @@ -25,6 +25,7 @@ import ( v1beta2types "github.com/akash-network/akash-api/go/node/provider/v1beta2" types "github.com/akash-network/akash-api/go/node/provider/v1beta3" + "github.com/akash-network/node/migrations/consensus" mkeeper "github.com/akash-network/node/x/market/keeper" "github.com/akash-network/node/x/provider/client/cli" "github.com/akash-network/node/x/provider/client/rest" @@ -153,10 +154,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { querier := am.keeper.NewQuerier() types.RegisterQueryServer(cfg.QueryServer(), querier) - m := keeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { - panic(err) - } + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -185,7 +187,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________ diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index cdaee3adf9..5b2a5677f3 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -10,6 +10,7 @@ import ( type IKeeper interface { Codec() codec.BinaryCodec + StoreKey() sdk.StoreKey SetParams(ctx sdk.Context, params types.Params) error GetParams(ctx sdk.Context) (params types.Params) MinCommissionRate(ctx sdk.Context) sdk.Dec @@ -40,6 +41,10 @@ func (k Keeper) Codec() codec.BinaryCodec { return k.cdc } +func (k Keeper) StoreKey() sdk.StoreKey { + return k.skey +} + func (k Keeper) MinCommissionRate(ctx sdk.Context) sdk.Dec { res := sdk.NewDec(0) k.pspace.Get(ctx, types.KeyMinCommissionRate, &res) diff --git a/x/staking/module.go b/x/staking/module.go index c54f0fcf24..2d58c18b9b 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -19,6 +19,7 @@ import ( types "github.com/akash-network/akash-api/go/node/staking/v1beta3" + "github.com/akash-network/node/migrations/consensus" "github.com/akash-network/node/x/staking/keeper" "github.com/akash-network/node/x/staking/simulation" ) @@ -124,7 +125,12 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier { } // RegisterServices registers the module's services -func (am AppModule) RegisterServices(_ module.Configurator) { +func (am AppModule) RegisterServices(cfg module.Configurator) { + consensus.ModuleMigrations(ModuleName, am.keeper, func(name string, forVersion uint64, handler module.MigrationHandler) { + if err := cfg.RegisterMigration(name, forVersion, handler); err != nil { + panic(err) + } + }) } // BeginBlock performs no-op @@ -153,7 +159,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements module.AppModule#ConsensusVersion func (am AppModule) ConsensusVersion() uint64 { - return 2 + return consensus.ModuleVersion(ModuleName) } // ____________________________________________________________________________