## Description Closes: #10869 depends on: - [x] #11029 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
142 lines
4.0 KiB
Go
142 lines
4.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
v040 "github.com/cosmos/cosmos-sdk/x/genutil/migrations/v040"
|
|
v043 "github.com/cosmos/cosmos-sdk/x/genutil/migrations/v043"
|
|
v046 "github.com/cosmos/cosmos-sdk/x/genutil/migrations/v046"
|
|
"github.com/cosmos/cosmos-sdk/x/genutil/types"
|
|
)
|
|
|
|
const flagGenesisTime = "genesis-time"
|
|
|
|
// Allow applications to extend and modify the migration process.
|
|
//
|
|
// Ref: https://github.com/cosmos/cosmos-sdk/issues/5041
|
|
var migrationMap = types.MigrationMap{
|
|
"v0.42": v040.Migrate, // NOTE: v0.40, v0.41 and v0.42 are genesis compatible.
|
|
"v0.43": v043.Migrate, // NOTE: v0.43, v0.44 and v0.45 are genesis compatible.
|
|
"v0.46": v046.Migrate,
|
|
}
|
|
|
|
// GetMigrationCallback returns a MigrationCallback for a given version.
|
|
func GetMigrationCallback(version string) types.MigrationCallback {
|
|
return migrationMap[version]
|
|
}
|
|
|
|
// GetMigrationVersions get all migration version in a sorted slice.
|
|
func GetMigrationVersions() []string {
|
|
versions := make([]string, len(migrationMap))
|
|
|
|
var i int
|
|
|
|
for version := range migrationMap {
|
|
versions[i] = version
|
|
i++
|
|
}
|
|
|
|
sort.Strings(versions)
|
|
|
|
return versions
|
|
}
|
|
|
|
// MigrateGenesisCmd returns a command to execute genesis state migration.
|
|
func MigrateGenesisCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "migrate [target-version] [genesis-file]",
|
|
Short: "Migrate genesis to a specified target version",
|
|
Long: fmt.Sprintf(`Migrate the source genesis into the target version and print to STDOUT.
|
|
|
|
Example:
|
|
$ %s migrate v0.36 /path/to/genesis.json --chain-id=cosmoshub-3 --genesis-time=2019-04-22T17:00:00Z
|
|
`, version.AppName),
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
var err error
|
|
|
|
target := args[0]
|
|
importGenesis := args[1]
|
|
|
|
genDoc, err := validateGenDoc(importGenesis)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Since some default values are valid values, we just print to
|
|
// make sure the user didn't forget to update these values.
|
|
if genDoc.ConsensusParams.Evidence.MaxBytes == 0 {
|
|
fmt.Printf("Warning: consensus_params.evidence.max_bytes is set to 0. If this is"+
|
|
" deliberate, feel free to ignore this warning. If not, please have a look at the chain"+
|
|
" upgrade guide at %s.\n", chainUpgradeGuide)
|
|
}
|
|
|
|
var initialState types.AppMap
|
|
if err := json.Unmarshal(genDoc.AppState, &initialState); err != nil {
|
|
return errors.Wrap(err, "failed to JSON unmarshal initial genesis state")
|
|
}
|
|
|
|
migrationFunc := GetMigrationCallback(target)
|
|
if migrationFunc == nil {
|
|
return fmt.Errorf("unknown migration function for version: %s", target)
|
|
}
|
|
|
|
// TODO: handler error from migrationFunc call
|
|
newGenState := migrationFunc(initialState, clientCtx)
|
|
|
|
genDoc.AppState, err = json.Marshal(newGenState)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to JSON marshal migrated genesis state")
|
|
}
|
|
|
|
genesisTime, _ := cmd.Flags().GetString(flagGenesisTime)
|
|
if genesisTime != "" {
|
|
var t time.Time
|
|
|
|
err := t.UnmarshalText([]byte(genesisTime))
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to unmarshal genesis time")
|
|
}
|
|
|
|
genDoc.GenesisTime = t
|
|
}
|
|
|
|
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
|
|
if chainID != "" {
|
|
genDoc.ChainID = chainID
|
|
}
|
|
|
|
bz, err := tmjson.Marshal(genDoc)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal genesis doc")
|
|
}
|
|
|
|
sortedBz, err := sdk.SortJSON(bz)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to sort JSON genesis doc")
|
|
}
|
|
|
|
cmd.Println(string(sortedBz))
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag")
|
|
cmd.Flags().String(flags.FlagChainID, "", "override chain_id with this flag")
|
|
|
|
return cmd
|
|
}
|