mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-26 15:35:46 +00:00
feat: add migration for activated ta counter
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
66f4dec01b
commit
76ec1fbb7d
@ -1002,4 +1002,7 @@ func (app *App) setupUpgradeHandlers() {
|
|||||||
app.UpgradeKeeper.SetUpgradeHandler("v0.10.5", func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
app.UpgradeKeeper.SetUpgradeHandler("v0.10.5", func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
||||||
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
|
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
|
||||||
})
|
})
|
||||||
|
app.UpgradeKeeper.SetUpgradeHandler("v0.10.6", func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
||||||
|
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package keeper
|
|||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
v2 "github.com/planetmint/planetmint-go/x/machine/migrations/v2"
|
v2 "github.com/planetmint/planetmint-go/x/machine/migrations/v2"
|
||||||
|
v3 "github.com/planetmint/planetmint-go/x/machine/migrations/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Migrator is a struct for handling in-place store migrations.
|
// Migrator is a struct for handling in-place store migrations.
|
||||||
@ -19,3 +20,7 @@ func NewMigrator(keeper Keeper) Migrator {
|
|||||||
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
||||||
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
|
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
|
||||||
|
return v3.MigrateStore(ctx, m.keeper.taStoreKey, m.keeper.cdc)
|
||||||
|
}
|
||||||
|
|||||||
@ -51,15 +51,15 @@ func (k Keeper) GetTrustAnchor(ctx sdk.Context, pubKey string) (val types.TrustA
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) setActivatedTACount(ctx sdk.Context, counter uint64) {
|
func (k Keeper) setActivatedTACount(ctx sdk.Context, counter uint64) {
|
||||||
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TAIndexKey))
|
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ActivatedTACounterPrefix))
|
||||||
bz := make([]byte, 8)
|
bz := make([]byte, 8)
|
||||||
binary.BigEndian.PutUint64(bz, counter)
|
binary.BigEndian.PutUint64(bz, counter)
|
||||||
taCounterStore.Set([]byte(types.ActivatedTACounterKey), bz)
|
taCounterStore.Set([]byte{1}, bz)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) GetActivatedTACount(ctx sdk.Context) (counter uint64) {
|
func (k Keeper) GetActivatedTACount(ctx sdk.Context) (counter uint64) {
|
||||||
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TAIndexKey))
|
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ActivatedTACounterPrefix))
|
||||||
bz := taCounterStore.Get([]byte(types.ActivatedTACounterKey))
|
bz := taCounterStore.Get([]byte{1})
|
||||||
if bz == nil {
|
if bz == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
29
x/machine/migrations/v3/migrate.go
Normal file
29
x/machine/migrations/v3/migrate.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package v3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||||
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/planetmint/planetmint-go/x/machine/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.ActivatedTACounterPrefix))
|
||||||
|
|
||||||
|
count := uint64(0)
|
||||||
|
|
||||||
|
iterator := store.Iterator(nil, nil)
|
||||||
|
defer iterator.Close()
|
||||||
|
for ; iterator.Valid(); iterator.Next() {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
bz := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bz, count)
|
||||||
|
store.Set([]byte{1}, bz)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -121,6 +121,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
|||||||
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate1to2); err != nil {
|
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate1to2); err != nil {
|
||||||
panic(fmt.Errorf("failed to register migration of %s to v2: %w", types.ModuleName, err))
|
panic(fmt.Errorf("failed to register migration of %s to v2: %w", types.ModuleName, err))
|
||||||
}
|
}
|
||||||
|
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate2to3); err != nil {
|
||||||
|
panic(fmt.Errorf("failed to register migration of %s to v3: %w", types.ModuleName, err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
|
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
|
||||||
|
|||||||
@ -29,7 +29,7 @@ const (
|
|||||||
|
|
||||||
ParamsKey = "Machine/Params"
|
ParamsKey = "Machine/Params"
|
||||||
|
|
||||||
ActivatedTACounterKey = "ActivatedTACounter"
|
ActivatedTACounterPrefix = "ActivatedTACounter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func KeyPrefix(p string) []byte {
|
func KeyPrefix(p string) []byte {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user