forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CNS-45: added migrator for Spec (added blockLatestUpdate)
- Loading branch information
Showing
5 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package v020 | ||
|
||
import ( | ||
store "github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/lavanet/lava/app/upgrades" | ||
) | ||
|
||
const UpgradeName = "v0.2.0" | ||
|
||
var Upgrade = upgrades.Upgrade{ | ||
UpgradeName: UpgradeName, // upgrade name defined few lines above | ||
CreateUpgradeHandler: CreateUpgradeHandler, // create CreateUpgradeHandler in upgrades.go below | ||
StoreUpgrades: store.StoreUpgrades{}, // StoreUpgrades has 3 fields: Added/Renamed/Deleted any module that fits these description should be added in the way below | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build ignore | ||
|
||
package v020_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ignite-hq/cli/ignite/pkg/cosmoscmd" | ||
keepertest "github.com/lavanet/lava/testutil/keeper" | ||
v020 "github.com/lavanet/lava/x/spec/migrations/v0.2.0" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type UpgradeTestSuite struct { | ||
suite.Suite | ||
|
||
ctx sdk.Context | ||
app cosmoscmd.App | ||
} | ||
|
||
func (suite *UpgradeTestSuite) SetupTestApp() { | ||
suite.app, suite.ctx = app.TestSetup() | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(UpgradeTestSuite)) | ||
} | ||
|
||
func (suite *UpgradeTestSuite) TestBody() { | ||
suite.SetupTestApp() // setup test app | ||
suite.T().Log("test") | ||
} | ||
|
||
func (suite *UpgradeTestSuite) TestAddingBlockLastUpdated() { | ||
_, keepers, ctx := keepertest.InitAllKeepers(suite.T()) | ||
sdkctx := sdk.UnwrapSDKContext(ctx) | ||
suite.T().Log("TestAddingBlockLastUpdated") | ||
|
||
migrator := v020.NewMigrator(keepers.Spec) | ||
migrator.MigrateToV020(sdkctx) | ||
|
||
allSpec := keepers.Spec.GetAllSpec(sdk.UnwrapSDKContext(ctx)) | ||
for _, spec := range allSpec { | ||
suite.Require().Equal(0, spec.BlockLastUpdated) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package v020 | ||
|
||
import ( | ||
"log" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
"github.com/lavanet/lava/app/keepers" | ||
"github.com/lavanet/lava/app/upgrades" | ||
v020 "github.com/lavanet/lava/x/spec/migrations/v0.2.0" | ||
) | ||
|
||
func CreateUpgradeHandler( | ||
mm *module.Manager, | ||
configurator module.Configurator, | ||
bpm upgrades.BaseAppParamManager, | ||
keepers *keepers.LavaKeepers, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
log.Println("########################") | ||
log.Println("# STARTING UPGRADE #") | ||
log.Println("########################") | ||
|
||
//delete all existing conflicts | ||
migrator := v020.NewMigrator(keepers.SpecKeeper) | ||
migrator.MigrateToV020(ctx) | ||
|
||
return mm.RunMigrations(ctx, configurator, vm) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package v020 // migrations.go | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/x/spec/keeper" | ||
) | ||
|
||
type Migrator struct { | ||
keeper keeper.Keeper | ||
} | ||
|
||
func NewMigrator(keeper keeper.Keeper) Migrator { | ||
return Migrator{keeper: keeper} | ||
} | ||
|
||
func (m Migrator) MigrateToV020(ctx sdk.Context) error { | ||
return initBlockLastUpdated(ctx, m.keeper) | ||
} | ||
|
||
func initBlockLastUpdated(ctx sdk.Context, k keeper.Keeper) error { | ||
specs := k.GetAllSpec(ctx) | ||
for _, spec := range specs { | ||
spec.BlockLastUpdated = uint64(0) | ||
k.SetSpec(ctx, spec) | ||
} | ||
|
||
return nil | ||
} |