mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-24 06:25:47 +00:00
chore: add migration for new store mechanics
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
60bf019dd8
commit
f8933a99d8
18
x/asset/keeper/migrations.go
Normal file
18
x/asset/keeper/migrations.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package keeper
|
||||||
|
|
||||||
|
import (
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
v2 "github.com/planetmint/planetmint-go/x/asset/migrations/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Migrator struct {
|
||||||
|
keeper Keeper
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMigrator(keeper Keeper) Migrator {
|
||||||
|
return Migrator{keeper: keeper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
||||||
|
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
|
||||||
|
}
|
||||||
39
x/asset/migrations/v2/store.go
Normal file
39
x/asset/migrations/v2/store.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package v2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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/util"
|
||||||
|
"github.com/planetmint/planetmint-go/x/asset/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.AssetKey))
|
||||||
|
|
||||||
|
mapping := make(map[string][][]byte)
|
||||||
|
|
||||||
|
// read all cids
|
||||||
|
iterator := store.Iterator(nil, nil)
|
||||||
|
defer iterator.Close()
|
||||||
|
for ; iterator.Valid(); iterator.Next() {
|
||||||
|
addressBytes := iterator.Value()
|
||||||
|
cidBytes := iterator.Key()
|
||||||
|
|
||||||
|
// map all cids by address
|
||||||
|
mapping[string(addressBytes)] = append(mapping[string(addressBytes)], cidBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// store all cids with new key
|
||||||
|
for address, cids := range mapping {
|
||||||
|
assetByAddressStore := prefix.NewStore(ctx.KVStore(storeKey), types.AddressPrefix(address))
|
||||||
|
for i, cid := range cids {
|
||||||
|
assetByAddressStore.Set(util.SerializeUint64(uint64(i)), cid)
|
||||||
|
}
|
||||||
|
addressAssetCountStore := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.AssetKey))
|
||||||
|
addressAssetCountStore.Set(types.AddressCountKey(address), util.SerializeUint64(uint64(len(cids))))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -116,6 +116,11 @@ func NewAppModule(
|
|||||||
func (am AppModule) RegisterServices(cfg module.Configurator) {
|
func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||||
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
||||||
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
|
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
|
||||||
|
|
||||||
|
m := keeper.NewMigrator(am.keeper)
|
||||||
|
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
|
||||||
|
panic(fmt.Errorf("failed to register migration of %s to v2: %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)
|
||||||
@ -141,7 +146,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
|
// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
|
||||||
func (AppModule) ConsensusVersion() uint64 { return 1 }
|
func (AppModule) ConsensusVersion() uint64 { return 2 }
|
||||||
|
|
||||||
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
|
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
|
||||||
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {
|
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user