Skip to content

Commit

Permalink
CNS-434: add (and fix) migration to the module
Browse files Browse the repository at this point in the history
  • Loading branch information
orenl-lava committed Jun 20, 2023
1 parent 4546749 commit f673aca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ var Upgrades = []upgrades.Upgrade{
upgrades.Upgrade_0_13_0,
upgrades.Upgrade_0_13_1,
upgrades.Upgrade_0_14_0,
upgrades.Upgrade_0_14_1,
}

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down
6 changes: 6 additions & 0 deletions app/upgrades/empty_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,9 @@ var Upgrade_0_14_0 = Upgrade{
CreateUpgradeHandler: defaultUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{},
}

var Upgrade_0_14_1 = Upgrade{
UpgradeName: "v0.14.1",
CreateUpgradeHandler: defaultUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{},
}
29 changes: 23 additions & 6 deletions x/subscription/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -21,6 +22,8 @@ func NewMigrator(keeper Keeper) Migrator {
// Migrate2to3 implements store migration from v2 to v3:
// - Convert subscription store to fixation store and use timers
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate: subscriptions")

keeper := m.keeper

store := prefix.NewStore(
Expand Down Expand Up @@ -56,16 +59,30 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return fmt.Errorf("%w: subscriptions %s", err, sub_V3.Consumer)
}

// if the subscription is alive, then set the timer for the monthly expiry.
// otherwise, delete the entry from V3 to induce stale-period state (use the
// block from last expiry as the block for deletion).
// if the subscription has expired, then delete the entry from V3 store to induce
// stale-period state (use the block of last expiry as the block for deletion).
// otherwise, the subscription is alive, but the current month may have expired
// between since the upgrade proposal took effect (and until now); if indeed so,
// then invoke advanceMonth() since the current block is the (month) expiry block.
// otherwise, set the timer for the monthly expiry as already was set in V2.
if sub_V3.DurationLeft > 0 {
expiry := sub_V2.MonthExpiryTime
if expiry >= uint64(ctx.BlockTime().UTC().Unix()) {
return fmt.Errorf("expiry time passed for subscription %s", sub_V3.Consumer)
if expiry <= uint64(ctx.BlockTime().UTC().Unix()) {
utils.LavaFormatDebug(" subscription live, month expired",
utils.Attribute{Key: "expiry", Value: time.Unix(int64(expiry), 0)},
utils.Attribute{Key: "blockTime", Value: ctx.BlockTime().UTC()},
)
keeper.advanceMonth(ctx, []byte(sub_V3.Consumer))
} else {
utils.LavaFormatDebug(" subscription live, future expiry",
utils.Attribute{Key: "expiry", Value: time.Unix(int64(expiry), 0)},
utils.Attribute{Key: "blockTime", Value: ctx.BlockTime().UTC()},
)
keeper.subsTS.AddTimerByBlockTime(ctx, expiry, []byte(sub_V3.Consumer), []byte{})
}
keeper.subsTS.AddTimerByBlockTime(ctx, expiry, []byte(sub_V3.Consumer), []byte{})
} else {
utils.LavaFormatDebug(" subscription deleted",
utils.Attribute{Key: "block", Value: sub_V2.PrevExpiryBlock})
keeper.subsFS.DelEntry(ctx, sub_V3.Consumer, sub_V2.PrevExpiryBlock)
}

Expand Down
9 changes: 8 additions & 1 deletion x/subscription/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(am.keeper)

// register v2 -> v3 migration
if err := cfg.RegisterMigration(types.ModuleName, 2, migrator.Migrate2to3); err != nil {
panic(fmt.Errorf("%s: failed to register migration to v3: %w", types.ModuleName, err))
}
}

// RegisterInvariants registers the capability module's invariants.
Expand All @@ -164,7 +171,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down

0 comments on commit f673aca

Please sign in to comment.