mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 15:25:18 +00:00
[R4R] kava 5 migration (#848)
* wip: kava-5 migration * feat: kava 5 migration * fix: don't repeat genesis time
This commit is contained in:
parent
3bf40b97f9
commit
667ad87825
@ -5,11 +5,17 @@ import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/supply"
|
||||
|
||||
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/kava-labs/kava/app"
|
||||
"github.com/kava-labs/kava/x/bep3"
|
||||
v0_13cdp "github.com/kava-labs/kava/x/cdp"
|
||||
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
|
||||
@ -31,6 +37,87 @@ var (
|
||||
ClaimEndTime = time.Date(2026, 2, 25, 14, 0, 0, 0, time.UTC)
|
||||
)
|
||||
|
||||
// Migrate translates a genesis file from kava v0.11 (or v0.12) format to kava v0.13.x format.
|
||||
func Migrate(genDoc tmtypes.GenesisDoc) tmtypes.GenesisDoc {
|
||||
// migrate app state
|
||||
var appStateMap genutil.AppMap
|
||||
cdc := codec.New()
|
||||
cryptoAmino.RegisterAmino(cdc)
|
||||
tmtypes.RegisterEvidences(cdc)
|
||||
|
||||
if err := cdc.UnmarshalJSON(genDoc.AppState, &appStateMap); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
newAppState := MigrateAppState(appStateMap)
|
||||
v0_13Codec := app.MakeCodec()
|
||||
marshaledNewAppState, err := v0_13Codec.MarshalJSON(newAppState)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
genDoc.AppState = marshaledNewAppState
|
||||
genDoc.GenesisTime = GenesisTime
|
||||
genDoc.ChainID = "kava-5"
|
||||
genDoc.ConsensusParams.Block.MaxGas = 20000000
|
||||
return genDoc
|
||||
}
|
||||
|
||||
// MigrateAppState migrates application state from v0.11 (or v0.12) format to a kava v0.13.x format
|
||||
func MigrateAppState(v0_11AppState genutil.AppMap) genutil.AppMap {
|
||||
v0_13AppState := v0_11AppState
|
||||
cdc := app.MakeCodec()
|
||||
var hardGenState v0_11hard.GenesisState
|
||||
if v0_11AppState[v0_13hard.ModuleName] == nil {
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[v0_11hard.ModuleName], &hardGenState)
|
||||
delete(v0_11AppState, v0_11hard.ModuleName)
|
||||
v0_13AppState[v0_13hard.ModuleName] = cdc.MustMarshalJSON(
|
||||
Hard(hardGenState))
|
||||
}
|
||||
delete(v0_13AppState, v0_11hard.ModuleName)
|
||||
if v0_11AppState[v0_11cdp.ModuleName] != nil {
|
||||
var cdpGenState v0_11cdp.GenesisState
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[v0_11cdp.ModuleName], &cdpGenState)
|
||||
delete(v0_11AppState, v0_11cdp.ModuleName)
|
||||
v0_13AppState[v0_13cdp.ModuleName] = cdc.MustMarshalJSON(
|
||||
CDP(cdpGenState))
|
||||
}
|
||||
if v0_11AppState[v0_11incentive.ModuleName] != nil {
|
||||
var incentiveGenState v0_11incentive.GenesisState
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[v0_13incentive.ModuleName], &incentiveGenState)
|
||||
delete(v0_11AppState, v0_11incentive.ModuleName)
|
||||
v0_13AppState[v0_13incentive.ModuleName] = cdc.MustMarshalJSON(Incentive(hardGenState, incentiveGenState))
|
||||
}
|
||||
if v0_11AppState[v0_11pricefeed.ModuleName] != nil {
|
||||
var pricefeedGS v0_11pricefeed.GenesisState
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[v0_13pricefeed.ModuleName], &pricefeedGS)
|
||||
delete(v0_11AppState, v0_11pricefeed.ModuleName)
|
||||
v0_13AppState[v0_13pricefeed.ModuleName] = cdc.MustMarshalJSON(Pricefeed(pricefeedGS))
|
||||
}
|
||||
if v0_11AppState[bep3.ModuleName] != nil {
|
||||
var bep3GS bep3.GenesisState
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[bep3.ModuleName], &bep3GS)
|
||||
delete(v0_11AppState, bep3.ModuleName)
|
||||
v0_13AppState[bep3.ModuleName] = cdc.MustMarshalJSON(Bep3(bep3GS))
|
||||
}
|
||||
if v0_11AppState[v0_11committee.ModuleName] != nil {
|
||||
var committeeGS v0_11committee.GenesisState
|
||||
cdc := codec.New()
|
||||
sdk.RegisterCodec(cdc)
|
||||
v0_11committee.RegisterCodec(cdc)
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[v0_11committee.ModuleName], &committeeGS)
|
||||
delete(v0_11AppState, v0_11committee.ModuleName)
|
||||
cdc = app.MakeCodec()
|
||||
v0_13AppState[v0_13committee.ModuleName] = cdc.MustMarshalJSON(Committee(committeeGS))
|
||||
}
|
||||
if v0_11AppState[auth.ModuleName] != nil {
|
||||
var authGS auth.GenesisState
|
||||
cdc.MustUnmarshalJSON(v0_11AppState[auth.ModuleName], &authGS)
|
||||
delete(v0_11AppState, auth.ModuleName)
|
||||
v0_13AppState[auth.ModuleName] = cdc.MustMarshalJSON(Auth(authGS))
|
||||
}
|
||||
return v0_13AppState
|
||||
|
||||
}
|
||||
|
||||
// CDP migrates from a v0.11 cdp genesis state to a v0.13 cdp genesis state
|
||||
func CDP(oldGenState v0_11cdp.GenesisState) v0_13cdp.GenesisState {
|
||||
var newCDPs v0_13cdp.CDPs
|
||||
|
@ -11,9 +11,12 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/supply"
|
||||
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
||||
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/kava-labs/kava/app"
|
||||
"github.com/kava-labs/kava/x/bep3"
|
||||
v0_11cdp "github.com/kava-labs/kava/x/cdp/legacy/v0_11"
|
||||
@ -223,3 +226,25 @@ func TestBep3(t *testing.T) {
|
||||
require.Equal(t, uint64(24686), newGenState.Params.AssetParams[0].MinBlockLock)
|
||||
require.Equal(t, uint64(86400), newGenState.Params.AssetParams[0].MaxBlockLock)
|
||||
}
|
||||
|
||||
func TestMigrateFull(t *testing.T) {
|
||||
oldGenDoc, err := tmtypes.GenesisDocFromFile(filepath.Join("testdata", "kava-4-export.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.InitializeFromGenesisStatesWithTime(newGenDoc.GenesisTime, app.GenesisState(newAppState))
|
||||
})
|
||||
}
|
||||
|
1
migrate/v0_13/testdata/kava-4-export.json
vendored
Normal file
1
migrate/v0_13/testdata/kava-4-export.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -21,6 +21,7 @@ import (
|
||||
|
||||
const (
|
||||
MaxCommitteeDescriptionLength int = 512
|
||||
ModuleName = "committee"
|
||||
)
|
||||
|
||||
// Permission is anything with a method that validates whether a proposal is allowed by it or not.
|
||||
|
@ -11,9 +11,10 @@ import (
|
||||
|
||||
// Valid reward multipliers
|
||||
const (
|
||||
Small MultiplierName = "small"
|
||||
Medium MultiplierName = "medium"
|
||||
Large MultiplierName = "large"
|
||||
Small MultiplierName = "small"
|
||||
Medium MultiplierName = "medium"
|
||||
Large MultiplierName = "large"
|
||||
ModuleName = "incentive"
|
||||
)
|
||||
|
||||
// GenesisClaimPeriodID stores the next claim id and its corresponding collateral type
|
||||
|
Loading…
Reference in New Issue
Block a user