mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 15:25:18 +00:00
kava-3 to kava-4 migration (#676)
* feat: kava-3 to kava-4 migration function * update migrate command
This commit is contained in:
parent
d15e3a43dd
commit
04946493ae
@ -85,7 +85,7 @@ func (tApp TestApp) GetPriceFeedKeeper() pricefeed.Keeper { return tApp.pricefee
|
|||||||
func (tApp TestApp) GetBep3Keeper() bep3.Keeper { return tApp.bep3Keeper }
|
func (tApp TestApp) GetBep3Keeper() bep3.Keeper { return tApp.bep3Keeper }
|
||||||
func (tApp TestApp) GetKavadistKeeper() kavadist.Keeper { return tApp.kavadistKeeper }
|
func (tApp TestApp) GetKavadistKeeper() kavadist.Keeper { return tApp.kavadistKeeper }
|
||||||
func (tApp TestApp) GetIncentiveKeeper() incentive.Keeper { return tApp.incentiveKeeper }
|
func (tApp TestApp) GetIncentiveKeeper() incentive.Keeper { return tApp.incentiveKeeper }
|
||||||
func (tApp TestApp) GetHarvestKeeper() harvest.Keeper { return tApp.harvestKeeper }
|
func (tApp TestApp) GetHarvestKeeper() harvest.Keeper { return tApp.harvestKeeper }
|
||||||
func (tApp TestApp) GetCommitteeKeeper() committee.Keeper { return tApp.committeeKeeper }
|
func (tApp TestApp) GetCommitteeKeeper() committee.Keeper { return tApp.committeeKeeper }
|
||||||
func (tApp TestApp) GetIssuanceKeeper() issuance.Keeper { return tApp.issuanceKeeper }
|
func (tApp TestApp) GetIssuanceKeeper() issuance.Keeper { return tApp.issuanceKeeper }
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func (tApp TestApp) InitializeFromGenesisStatesWithTime(genTime time.Time, genes
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
tApp.Commit()
|
tApp.Commit()
|
||||||
tApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: tApp.LastBlockHeight() + 1}})
|
tApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: tApp.LastBlockHeight() + 1, Time: genTime}})
|
||||||
return tApp
|
return tApp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/version"
|
"github.com/cosmos/cosmos-sdk/version"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/migrate/v0_8"
|
"github.com/kava-labs/kava/migrate/v0_11"
|
||||||
v032tendermint "github.com/kava-labs/kava/migrate/v0_8/tendermint/v0_32"
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -24,8 +24,8 @@ const (
|
|||||||
func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
|
func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "migrate [genesis-file]",
|
Use: "migrate [genesis-file]",
|
||||||
Short: "Migrate genesis from kava v0.3 to v0.8",
|
Short: "Migrate genesis file from kava v0.10 to v0.11",
|
||||||
Long: "Migrate the source genesis into the current version, sorts it, and print to STDOUT.",
|
Long: "Migrate the source genesis into the current version, sorts it, and print to STDOUT. If not provided, chain-id is set to kava-4 and genesis time is set to 2020-10-15T:14:00:00Z",
|
||||||
Example: fmt.Sprintf(`%s migrate /path/to/genesis.json --chain-id=new-chain-id --genesis-time=1998-01-01T00:00:00Z`, version.ServerName),
|
Example: fmt.Sprintf(`%s migrate /path/to/genesis.json --chain-id=new-chain-id --genesis-time=1998-01-01T00:00:00Z`, version.ServerName),
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -33,14 +33,14 @@ func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
|
|||||||
// 1) Unmarshal existing genesis.json
|
// 1) Unmarshal existing genesis.json
|
||||||
|
|
||||||
importGenesis := args[0]
|
importGenesis := args[0]
|
||||||
genDoc, err := v032tendermint.GenesisDocFromFile(importGenesis)
|
genDoc, err := tmtypes.GenesisDocFromFile(importGenesis)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read genesis document from file %s: %w", importGenesis, err)
|
return fmt.Errorf("failed to read genesis document from file %s: %w", importGenesis, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) Migrate state from kava v0.3 to v0.8
|
// 2) Migrate state from kava v0.3 to v0.8
|
||||||
|
|
||||||
newGenDoc := v0_8.Migrate(*genDoc)
|
newGenDoc := v0_11.Migrate(*genDoc)
|
||||||
|
|
||||||
// 3) Create and output a new genesis file
|
// 3) Create and output a new genesis file
|
||||||
|
|
||||||
|
@ -4,14 +4,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
v39_1auth "github.com/cosmos/cosmos-sdk/x/auth"
|
v39_1auth "github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
v39_1authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
v39_1authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
||||||
v39_1vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
v39_1vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
||||||
|
v39_genutil "github.com/cosmos/cosmos-sdk/x/genutil"
|
||||||
v39_1gov "github.com/cosmos/cosmos-sdk/x/gov"
|
v39_1gov "github.com/cosmos/cosmos-sdk/x/gov"
|
||||||
v39_1supply "github.com/cosmos/cosmos-sdk/x/supply"
|
v39_1supply "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"
|
||||||
v38_5auth "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/auth"
|
v38_5auth "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/auth"
|
||||||
v38_5supply "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/supply"
|
v38_5supply "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/supply"
|
||||||
v0_11bep3 "github.com/kava-labs/kava/x/bep3"
|
v0_11bep3 "github.com/kava-labs/kava/x/bep3"
|
||||||
@ -23,6 +29,7 @@ import (
|
|||||||
v0_11harvest "github.com/kava-labs/kava/x/harvest"
|
v0_11harvest "github.com/kava-labs/kava/x/harvest"
|
||||||
v0_11incentive "github.com/kava-labs/kava/x/incentive"
|
v0_11incentive "github.com/kava-labs/kava/x/incentive"
|
||||||
v0_9incentive "github.com/kava-labs/kava/x/incentive/legacy/v0_9"
|
v0_9incentive "github.com/kava-labs/kava/x/incentive/legacy/v0_9"
|
||||||
|
v0_11issuance "github.com/kava-labs/kava/x/issuance"
|
||||||
v0_11pricefeed "github.com/kava-labs/kava/x/pricefeed"
|
v0_11pricefeed "github.com/kava-labs/kava/x/pricefeed"
|
||||||
v0_9pricefeed "github.com/kava-labs/kava/x/pricefeed/legacy/v0_9"
|
v0_9pricefeed "github.com/kava-labs/kava/x/pricefeed/legacy/v0_9"
|
||||||
v0_11validator_vesting "github.com/kava-labs/kava/x/validator-vesting"
|
v0_11validator_vesting "github.com/kava-labs/kava/x/validator-vesting"
|
||||||
@ -32,6 +39,101 @@ import (
|
|||||||
var deputyBnbBalance sdk.Coin
|
var deputyBnbBalance sdk.Coin
|
||||||
var hardBalance sdk.Coin
|
var hardBalance sdk.Coin
|
||||||
|
|
||||||
|
// Migrate translates a genesis file from kava v0.9 (or v0.10) format to kava v0.11.x format.
|
||||||
|
func Migrate(genDoc tmtypes.GenesisDoc) tmtypes.GenesisDoc {
|
||||||
|
// migrate app state
|
||||||
|
var appStateMap v39_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_11Codec := app.MakeCodec()
|
||||||
|
marshaledNewAppState, err := v0_11Codec.MarshalJSON(newAppState)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
genDoc.AppState = marshaledNewAppState
|
||||||
|
genDoc.GenesisTime = time.Date(2020, 10, 15, 14, 0, 0, 0, time.UTC)
|
||||||
|
genDoc.ChainID = "kava-4"
|
||||||
|
return genDoc
|
||||||
|
}
|
||||||
|
|
||||||
|
// MigrateAppState migrates application state from v0.9 (or v0.10) format to a kava v0.11.x format
|
||||||
|
func MigrateAppState(v0_9AppState v39_genutil.AppMap) v39_genutil.AppMap {
|
||||||
|
v0_11AppState := v0_9AppState
|
||||||
|
v0_11Codec := app.MakeCodec()
|
||||||
|
if v0_9AppState[v38_5auth.ModuleName] != nil {
|
||||||
|
v0_9cdc := codec.New()
|
||||||
|
codec.RegisterCrypto(v0_9cdc)
|
||||||
|
v38_5auth.RegisterCodec(v0_9cdc)
|
||||||
|
v38_5auth.RegisterCodecVesting(v0_9cdc)
|
||||||
|
v38_5supply.RegisterCodec(v0_9cdc)
|
||||||
|
v0_9validator_vesting.RegisterCodec(v0_9cdc)
|
||||||
|
var authGenState v38_5auth.GenesisState
|
||||||
|
v0_9cdc.MustUnmarshalJSON(v0_9AppState[v38_5auth.ModuleName], &authGenState)
|
||||||
|
delete(v0_9AppState, v38_5auth.ModuleName)
|
||||||
|
newAuthGS := MigrateAuth(authGenState)
|
||||||
|
v0_11AppState[v39_1auth.ModuleName] = v0_11Codec.MustMarshalJSON(newAuthGS)
|
||||||
|
}
|
||||||
|
if v0_9AppState[v39_1supply.ModuleName] != nil {
|
||||||
|
var supplyGenstate v39_1supply.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v39_1supply.ModuleName], &supplyGenstate)
|
||||||
|
delete(v0_9AppState, v39_1supply.ModuleName)
|
||||||
|
v0_11AppState[v39_1supply.ModuleName] = v0_11Codec.MustMarshalJSON(
|
||||||
|
MigrateSupply(
|
||||||
|
supplyGenstate, deputyBnbBalance, sdk.NewCoin("hard", sdk.NewInt(200000000000000))))
|
||||||
|
|
||||||
|
}
|
||||||
|
if v0_9AppState[v39_1gov.ModuleName] != nil {
|
||||||
|
var govGenstate v39_1gov.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v39_1gov.ModuleName], &govGenstate)
|
||||||
|
delete(v0_9AppState, v39_1gov.ModuleName)
|
||||||
|
v0_11AppState[v39_1gov.ModuleName] = v0_11Codec.MustMarshalJSON(
|
||||||
|
MigrateGov(govGenstate))
|
||||||
|
|
||||||
|
}
|
||||||
|
if v0_9AppState[v0_9bep3.ModuleName] != nil {
|
||||||
|
var bep3GenState v0_9bep3.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v0_9bep3.ModuleName], &bep3GenState)
|
||||||
|
delete(v0_9AppState, v0_9bep3.ModuleName)
|
||||||
|
v0_11AppState[v0_9bep3.ModuleName] = v0_11Codec.MustMarshalJSON(MigrateBep3(bep3GenState))
|
||||||
|
}
|
||||||
|
if v0_9AppState[v0_9cdp.ModuleName] != nil {
|
||||||
|
var cdpGenState v0_9cdp.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v0_9cdp.ModuleName], &cdpGenState)
|
||||||
|
delete(v0_9AppState, v0_9cdp.ModuleName)
|
||||||
|
v0_11AppState[v0_9cdp.ModuleName] = v0_11Codec.MustMarshalJSON(MigrateCDP(cdpGenState))
|
||||||
|
}
|
||||||
|
if v0_9AppState[v0_9committee.ModuleName] != nil {
|
||||||
|
var committeeGenState v0_9committee.GenesisState
|
||||||
|
cdc := codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
v0_9committee.RegisterCodec(cdc)
|
||||||
|
cdc.MustUnmarshalJSON(v0_9AppState[v0_9committee.ModuleName], &committeeGenState)
|
||||||
|
delete(v0_9AppState, v0_9committee.ModuleName)
|
||||||
|
v0_11AppState[v0_9committee.ModuleName] = v0_11Codec.MustMarshalJSON(MigrateCommittee(committeeGenState))
|
||||||
|
}
|
||||||
|
if v0_9AppState[v0_9incentive.ModuleName] != nil {
|
||||||
|
var incentiveGenState v0_9incentive.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v0_9incentive.ModuleName], &incentiveGenState)
|
||||||
|
delete(v0_9AppState, v0_9incentive.ModuleName)
|
||||||
|
v0_11AppState[v0_9incentive.ModuleName] = v0_11Codec.MustMarshalJSON(MigrateIncentive(incentiveGenState))
|
||||||
|
}
|
||||||
|
if v0_9AppState[v0_9pricefeed.ModuleName] != nil {
|
||||||
|
var pricefeedGenState v0_9pricefeed.GenesisState
|
||||||
|
v0_11Codec.MustUnmarshalJSON(v0_9AppState[v0_9pricefeed.ModuleName], &pricefeedGenState)
|
||||||
|
delete(v0_9AppState, v0_9pricefeed.ModuleName)
|
||||||
|
v0_11AppState[v0_9pricefeed.ModuleName] = v0_11Codec.MustMarshalJSON(MigratePricefeed(pricefeedGenState))
|
||||||
|
}
|
||||||
|
v0_11AppState[v0_11harvest.ModuleName] = v0_11Codec.MustMarshalJSON(MigrateHarvest())
|
||||||
|
v0_11AppState[v0_11issuance.ModuleName] = v0_11Codec.MustMarshalJSON(v0_11issuance.DefaultGenesisState())
|
||||||
|
return v0_11AppState
|
||||||
|
}
|
||||||
|
|
||||||
// MigrateBep3 migrates from a v0.9 (or v0.10) bep3 genesis state to a v0.11 bep3 genesis state
|
// MigrateBep3 migrates from a v0.9 (or v0.10) bep3 genesis state to a v0.11 bep3 genesis state
|
||||||
func MigrateBep3(oldGenState v0_9bep3.GenesisState) v0_11bep3.GenesisState {
|
func MigrateBep3(oldGenState v0_9bep3.GenesisState) v0_11bep3.GenesisState {
|
||||||
var assetParams v0_11bep3.AssetParams
|
var assetParams v0_11bep3.AssetParams
|
||||||
|
@ -10,11 +10,13 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
v39_1auth "github.com/cosmos/cosmos-sdk/x/auth"
|
v39_1auth "github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
v39_1auth_vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
v39_1auth_vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||||
v39_1supply "github.com/cosmos/cosmos-sdk/x/supply"
|
v39_1supply "github.com/cosmos/cosmos-sdk/x/supply"
|
||||||
|
|
||||||
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/app"
|
"github.com/kava-labs/kava/app"
|
||||||
v38_5auth "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/auth"
|
v38_5auth "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/auth"
|
||||||
v38_5supply "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/supply"
|
v38_5supply "github.com/kava-labs/kava/migrate/v0_11/legacy/cosmos-sdk/v0.38.5/supply"
|
||||||
@ -165,3 +167,25 @@ func TestMigratePricefeed(t *testing.T) {
|
|||||||
err = newGenState.Validate()
|
err = newGenState.Validate()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMigrateFull(t *testing.T) {
|
||||||
|
oldGenDoc, err := tmtypes.GenesisDocFromFile(filepath.Join("testdata", "kava-3-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_11/testdata/kava-3-export.json
vendored
Normal file
1
migrate/v0_11/testdata/kava-3-export.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -6,6 +6,10 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ModuleName = "cdp"
|
||||||
|
)
|
||||||
|
|
||||||
// CDP is the state of a single collateralized debt position.
|
// CDP is the state of a single collateralized debt position.
|
||||||
type CDP struct {
|
type CDP struct {
|
||||||
ID uint64 `json:"id" yaml:"id"` // unique id for cdp
|
ID uint64 `json:"id" yaml:"id"` // unique id for cdp
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
MaxCommitteeDescriptionLength int = 512
|
MaxCommitteeDescriptionLength int = 512
|
||||||
|
ModuleName = "committee"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Permission is anything with a method that validates whether a proposal is allowed by it or not.
|
// Permission is anything with a method that validates whether a proposal is allowed by it or not.
|
||||||
|
@ -8,6 +8,10 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ModuleName = "incentive"
|
||||||
|
)
|
||||||
|
|
||||||
// GenesisClaimPeriodID stores the next claim id and its corresponding denom
|
// GenesisClaimPeriodID stores the next claim id and its corresponding denom
|
||||||
type GenesisClaimPeriodID struct {
|
type GenesisClaimPeriodID struct {
|
||||||
Denom string `json:"denom" yaml:"denom"`
|
Denom string `json:"denom" yaml:"denom"`
|
||||||
|
@ -9,6 +9,10 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ModuleName = "pricefeed"
|
||||||
|
)
|
||||||
|
|
||||||
// GenesisState - pricefeed state that must be provided at genesis
|
// GenesisState - pricefeed state that must be provided at genesis
|
||||||
type GenesisState struct {
|
type GenesisState struct {
|
||||||
Params Params `json:"params" yaml:"params"`
|
Params Params `json:"params" yaml:"params"`
|
||||||
|
Loading…
Reference in New Issue
Block a user