Skip to content

Commit

Permalink
refactor(migrations): group migrations by upgrade name (#1806)
Browse files Browse the repository at this point in the history
place all migrations within migrations dir

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Apr 28, 2023
1 parent e56322c commit d5ad38a
Show file tree
Hide file tree
Showing 55 changed files with 1,259 additions and 523 deletions.
4 changes: 3 additions & 1 deletion app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
56 changes: 49 additions & 7 deletions app/upgrades/v0.24.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ 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"
"github.com/cosmos/cosmos-sdk/x/feegrant"
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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
91 changes: 91 additions & 0 deletions migrations/consensus/consensus.go
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())
}
}
27 changes: 27 additions & 0 deletions migrations/consensus/helpers.go
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
}
9 changes: 9 additions & 0 deletions migrations/migrations.go
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"
)
34 changes: 34 additions & 0 deletions migrations/v0.15.0/audit.go
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
}
34 changes: 34 additions & 0 deletions migrations/v0.15.0/cert.go
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
}
48 changes: 48 additions & 0 deletions migrations/v0.15.0/deployment.go
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
}
Loading

0 comments on commit d5ad38a

Please sign in to comment.