Skip to content

Commit

Permalink
Add supply migration for kava-8 (Kava-Labs#1008)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
karzak authored Aug 24, 2021
1 parent a9c77f0 commit dd03ba5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions migrate/v0_15/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down
23 changes: 23 additions & 0 deletions migrate/v0_15/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd03ba5

Please sign in to comment.