Skip to content

Commit

Permalink
feat: implement migration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed Oct 31, 2024
1 parent 4e1fcae commit 3a97625
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
10 changes: 5 additions & 5 deletions local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ if ! [ -f .halo/data/priv_validator_state.json ]; then
halod genesis add-genesis-account validator 1000000ustake --home .halo --keyring-backend test
OWNER=$(halod keys add owner --home .halo --keyring-backend test --output json | jq .address)
halod genesis add-genesis-account owner 10000000uusdc --home .halo --keyring-backend test
AGGREGATOR_OWNER=$(halod keys add aggregator-owner --home .halo --keyring-backend test --output json | jq .address)
halod genesis add-genesis-account aggregator-owner 10000000uusdc --home .halo --keyring-backend test
halod keys add aggregator-pending-owner --home .halo --keyring-backend test &> /dev/null
halod genesis add-genesis-account aggregator-pending-owner 10000000uusdc --home .halo --keyring-backend test
REPORTER=$(halod keys add reporter --home .halo --keyring-backend test --output json | jq .address)
halod genesis add-genesis-account reporter 10000000uusdc --home .halo --keyring-backend test
halod keys add pending-reporter --home .halo --keyring-backend test &> /dev/null
halod genesis add-genesis-account pending-reporter 10000000uusdc --home .halo --keyring-backend test
ENTITLEMENTS_OWNER=$(halod keys add entitlements-owner --home .halo --keyring-backend test --output json | jq .address)
halod genesis add-genesis-account entitlements-owner 10000000uusdc --home .halo --keyring-backend test
halod keys add entitlements-pending-owner --home .halo --keyring-backend test &> /dev/null
halod genesis add-genesis-account entitlements-pending-owner 10000000uusdc --home .halo --keyring-backend test

TEMP=.halo/genesis.json
touch $TEMP && jq '.app_state.staking.params.bond_denom = "ustake"' .halo/config/genesis.json > $TEMP && mv $TEMP .halo/config/genesis.json
touch $TEMP && jq '.app_state.halo.aggregator_state.owner = '$AGGREGATOR_OWNER'' .halo/config/genesis.json > $TEMP && mv $TEMP .halo/config/genesis.json
touch $TEMP && jq '.app_state.halo.aggregator_state.reporter = '$REPORTER'' .halo/config/genesis.json > $TEMP && mv $TEMP .halo/config/genesis.json
touch $TEMP && jq '.app_state.halo.entitlements_state.owner = '$ENTITLEMENTS_OWNER'' .halo/config/genesis.json > $TEMP && mv $TEMP .halo/config/genesis.json
touch $TEMP && jq '.app_state.halo.owner = '$OWNER'' .halo/config/genesis.json > $TEMP && mv $TEMP .halo/config/genesis.json

Expand Down
65 changes: 65 additions & 0 deletions migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 NASD Inc.
//
// Use of this source code is governed by a BSL-style
// license that can be found in the LICENSE file or at
// https://mariadb.com/bsl11.

package halo

import (
"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/noble-assets/halo/v2/keeper"
"github.com/noble-assets/halo/v2/types/aggregator"
aggregatorv1 "github.com/noble-assets/halo/v2/types/aggregator/v1"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper *keeper.Keeper

v1Rounds collections.Map[uint64, aggregatorv1.RoundData]
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper *keeper.Keeper, service store.KVStoreService, cdc codec.Codec) Migrator {
builder := collections.NewSchemaBuilder(service)

migrator := Migrator{
keeper: keeper,

v1Rounds: collections.NewMap(builder, aggregator.RoundPrefix, "aggregator_rounds", collections.Uint64Key, codec.CollValue[aggregatorv1.RoundData](cdc)),
}

if _, err := builder.Build(); err != nil {
panic(err)
}

return migrator
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
rounds := make(map[uint64]aggregator.RoundData)
err := m.v1Rounds.Walk(ctx, nil, func(id uint64, round aggregatorv1.RoundData) (stop bool, err error) {
rounds[id] = aggregator.RoundData{
Answer: round.Answer,
UpdatedAt: uint32(round.UpdatedAt),
}

return false, nil
})
if err != nil {
return err
}

for id, round := range rounds {
if err = m.keeper.Rounds.Set(ctx, id, round); err != nil {
return err
}
}

return nil
}
19 changes: 14 additions & 5 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
"github.com/spf13/cobra"
)

// ConsensusVersion defines the current x/halo module consensus version.
const ConsensusVersion = 1
// ConsensusVersion defines the current Halo module consensus version.
const ConsensusVersion = 2

var (
_ module.AppModuleBasic = AppModule{}
Expand Down Expand Up @@ -97,13 +97,17 @@ func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncoding
type AppModule struct {
AppModuleBasic

keeper *keeper.Keeper
keeper *keeper.Keeper
storeService store.KVStoreService
cdc codec.Codec
}

func NewAppModule(keeper *keeper.Keeper, addressCodec address.Codec) AppModule {
func NewAppModule(keeper *keeper.Keeper, addressCodec address.Codec, storeService store.KVStoreService, cdc codec.Codec) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(addressCodec),
keeper: keeper,
storeService: storeService,
cdc: cdc,
}
}

Expand Down Expand Up @@ -134,6 +138,11 @@ func (m AppModule) RegisterServices(cfg module.Configurator) {

entitlements.RegisterMsgServer(cfg.MsgServer(), keeper.NewEntitlementsMsgServer(m.keeper))
entitlements.RegisterQueryServer(cfg.QueryServer(), keeper.NewEntitlementsQueryServer(m.keeper))

migrator := NewMigrator(m.keeper, m.storeService, m.cdc)
if err := cfg.RegisterMigration(types.ModuleName, 1, migrator.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate Halo from version 1 to 2: %v", err))
}
}

//
Expand Down Expand Up @@ -190,7 +199,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.BankKeeper,
in.InterfaceRegistry,
)
m := NewAppModule(k, in.AddressCodec)
m := NewAppModule(k, in.AddressCodec, in.StoreService, in.Cdc)

return ModuleOutputs{Keeper: k, Module: m, Restrictions: k.SendRestrictionFn}
}

0 comments on commit 3a97625

Please sign in to comment.