2019-07-03 09:21:34 -07:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-08-15 01:52:33 -07:00
|
|
|
"encoding/json"
|
2019-07-03 09:21:34 -07:00
|
|
|
"fmt"
|
2019-09-26 09:07:15 -07:00
|
|
|
"sort"
|
2019-07-03 09:21:34 -07:00
|
|
|
"time"
|
|
|
|
|
2019-08-30 06:59:41 -07:00
|
|
|
"github.com/pkg/errors"
|
2019-07-03 09:21:34 -07:00
|
|
|
"github.com/spf13/cobra"
|
2020-10-05 14:28:14 -07:00
|
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
2019-07-03 09:21:34 -07:00
|
|
|
|
2020-10-05 14:28:14 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-08-17 14:32:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-07-23 13:08:46 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-07-03 09:21:34 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2021-07-05 07:55:51 -07:00
|
|
|
v040 "github.com/cosmos/cosmos-sdk/x/genutil/migrations/v040"
|
|
|
|
v043 "github.com/cosmos/cosmos-sdk/x/genutil/migrations/v043"
|
2020-06-13 00:41:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil/types"
|
2019-07-03 09:21:34 -07:00
|
|
|
)
|
|
|
|
|
2020-08-17 14:32:16 -07:00
|
|
|
const flagGenesisTime = "genesis-time"
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
// Allow applications to extend and modify the migration process.
|
|
|
|
//
|
|
|
|
// Ref: https://github.com/cosmos/cosmos-sdk/issues/5041
|
2020-06-13 00:41:45 -07:00
|
|
|
var migrationMap = types.MigrationMap{
|
2021-03-22 04:30:26 -07:00
|
|
|
"v0.42": v040.Migrate, // NOTE: v0.40, v0.41 and v0.42 are genesis compatible.
|
|
|
|
"v0.43": v043.Migrate,
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
// GetMigrationCallback returns a MigrationCallback for a given version.
|
2020-06-13 00:41:45 -07:00
|
|
|
func GetMigrationCallback(version string) types.MigrationCallback {
|
2019-09-26 09:07:15 -07:00
|
|
|
return migrationMap[version]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMigrationVersions get all migration version in a sorted slice.
|
|
|
|
func GetMigrationVersions() []string {
|
|
|
|
versions := make([]string, len(migrationMap))
|
2019-07-03 09:21:34 -07:00
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
var i int
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
for version := range migrationMap {
|
|
|
|
versions[i] = version
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(versions)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
return versions
|
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateGenesisCmd returns a command to execute genesis state migration.
|
2020-07-07 08:40:46 -07:00
|
|
|
func MigrateGenesisCmd() *cobra.Command {
|
2019-07-03 09:21:34 -07:00
|
|
|
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
|
2020-07-07 08:40:46 -07:00
|
|
|
`, version.AppName),
|
2019-07-03 09:21:34 -07:00
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-10-05 14:28:14 -07:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
var err error
|
|
|
|
|
2019-07-03 09:21:34 -07:00
|
|
|
target := args[0]
|
|
|
|
importGenesis := args[1]
|
|
|
|
|
2020-12-14 14:51:23 -08:00
|
|
|
genDoc, err := validateGenDoc(importGenesis)
|
2019-07-03 09:21:34 -07:00
|
|
|
if err != nil {
|
2020-12-14 14:51:23 -08:00
|
|
|
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)
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
2020-06-13 00:41:45 -07:00
|
|
|
var initialState types.AppMap
|
2020-08-26 02:39:38 -07:00
|
|
|
if err := json.Unmarshal(genDoc.AppState, &initialState); err != nil {
|
2019-09-26 09:07:15 -07:00
|
|
|
return errors.Wrap(err, "failed to JSON unmarshal initial genesis state")
|
|
|
|
}
|
2019-07-03 09:21:34 -07:00
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
migrationFunc := GetMigrationCallback(target)
|
|
|
|
if migrationFunc == nil {
|
|
|
|
return fmt.Errorf("unknown migration function for version: %s", target)
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
// TODO: handler error from migrationFunc call
|
2020-10-05 14:28:14 -07:00
|
|
|
newGenState := migrationFunc(initialState, clientCtx)
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
genDoc.AppState, err = json.Marshal(newGenState)
|
2019-09-26 09:07:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to JSON marshal migrated genesis state")
|
|
|
|
}
|
2019-07-03 09:21:34 -07:00
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
genesisTime, _ := cmd.Flags().GetString(flagGenesisTime)
|
2019-07-03 09:21:34 -07:00
|
|
|
if genesisTime != "" {
|
|
|
|
var t time.Time
|
|
|
|
|
|
|
|
err := t.UnmarshalText([]byte(genesisTime))
|
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to unmarshal genesis time")
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
genDoc.GenesisTime = t
|
|
|
|
}
|
|
|
|
|
2020-08-17 14:32:16 -07:00
|
|
|
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
|
2019-09-17 09:13:26 -07:00
|
|
|
if chainID != "" {
|
|
|
|
genDoc.ChainID = chainID
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
2020-10-05 14:28:14 -07:00
|
|
|
bz, err := tmjson.Marshal(genDoc)
|
2019-07-03 09:21:34 -07:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to marshal genesis doc")
|
2019-07-03 09:21:34 -07:00
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
sortedBz, err := sdk.SortJSON(bz)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to sort JSON genesis doc")
|
|
|
|
}
|
|
|
|
|
2021-03-22 04:30:26 -07:00
|
|
|
cmd.Println(string(sortedBz))
|
2019-07-03 09:21:34 -07:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag")
|
2020-08-17 14:32:16 -07:00
|
|
|
cmd.Flags().String(flags.FlagChainID, "", "override chain_id with this flag")
|
2019-07-03 09:21:34 -07:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|