From dd03ba5671765e7b475293851d6665181e4bdbaa Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Tue, 24 Aug 2021 16:33:10 -0500 Subject: [PATCH] Add supply migration for kava-8 (#1008) * fix: update supply migration to add swp token supply * add full migration test with app initialization * fix: use correct chain-id in test app initialization * address review comments --- migrate/v0_15/migrate.go | 15 +++++++++++++++ migrate/v0_15/migrate_test.go | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/migrate/v0_15/migrate.go b/migrate/v0_15/migrate.go index 719ad1b9..24693360 100644 --- a/migrate/v0_15/migrate.go +++ b/migrate/v0_15/migrate.go @@ -31,6 +31,7 @@ var ( GenesisTime = time.Date(2021, 8, 30, 15, 0, 0, 0, time.UTC) ChainID = "kava-8" SwpDelegatorRewardsPerSecond = sdk.NewCoin("swp", sdk.NewInt(198186)) + SwpTotalSupply = sdk.NewCoin("swp", sdk.NewInt(250000000e6)) ) // Migrate translates a genesis file from kava v0.14 format to kava v0.15 format @@ -74,6 +75,14 @@ func MigrateAppState(v0_14AppState genutil.AppMap) { v0_14AppState[auth.ModuleName] = v0_15Codec.MustMarshalJSON(Auth(v0_15Codec, authGenState, GenesisTime)) } + // Migrate supply app state + if v0_14AppState[supply.ModuleName] != nil { + var supplyGenState supply.GenesisState + v0_14Codec.MustUnmarshalJSON(v0_14AppState[supply.ModuleName], &supplyGenState) + delete(v0_14AppState, supply.ModuleName) + v0_14AppState[supply.ModuleName] = v0_15Codec.MustMarshalJSON(Supply(supplyGenState, SwpTotalSupply)) + } + // Migrate incentive app state if v0_14AppState[v0_14incentive.ModuleName] != nil { var incentiveGenState v0_14incentive.GenesisState @@ -258,6 +267,12 @@ func DistributeSwpTokens(genesisState auth.GenesisState) auth.GenesisState { return auth.NewGenesisState(genesisState.Params, accounts) } +// Supply adds SWP token to total supply +func Supply(supplyGenesisState supply.GenesisState, swpBalance sdk.Coin) supply.GenesisState { + supplyGenesisState.Supply = supplyGenesisState.Supply.Add(swpBalance) + return supplyGenesisState +} + // Committee migrates from a v0.14 committee genesis state to a v0.15 committee genesis state func Committee(genesisState v0_14committee.GenesisState) v0_15committee.GenesisState { diff --git a/migrate/v0_15/migrate_test.go b/migrate/v0_15/migrate_test.go index ea7c92ee..f328243d 100644 --- a/migrate/v0_15/migrate_test.go +++ b/migrate/v0_15/migrate_test.go @@ -36,6 +36,29 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestMigrateFull(t *testing.T) { + t.Skip() // skip to avoid having to commit a large genesis file to the repo + oldGenDoc, err := tmtypes.GenesisDocFromFile(filepath.Join("testdata", "genesis.json")) + require.NoError(t, err) + + // 2) migrate + newGenDoc := Migrate(*oldGenDoc) + tApp := app.NewTestApp() + cdc := app.MakeCodec() + var newAppState genutil.AppMap + require.NoError(t, + cdc.UnmarshalJSON(newGenDoc.AppState, &newAppState), + ) + err = app.ModuleBasics.ValidateGenesis(newAppState) + if err != nil { + require.NoError(t, err) + } + require.NotPanics(t, func() { + // this runs both InitGenesis for all modules (which panic on errors) and runs all invariants + tApp.InitializeFromGenesisStatesWithTimeAndChainID(newGenDoc.GenesisTime, newGenDoc.ChainID, app.GenesisState(newAppState)) + }) +} + func TestCommittee(t *testing.T) { bz, err := ioutil.ReadFile(filepath.Join("testdata", "kava-7-committee-state.json")) require.NoError(t, err)