-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(migrations): group migrations by upgrade name (#1806)
place all migrations within migrations dir Signed-off-by: Artur Troian <[email protected]>
- Loading branch information
Showing
55 changed files
with
1,259 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...s/akash_v0.15.0_cosmos_v0.44.x/upgrade.go → app/upgrades/v0.15.0/upgrade.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.