refactor: add MigrateHandler to allow reuse migrate genesis related function (#17296)
This commit is contained in:
parent
30f0be9cb7
commit
7a778f5c90
@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
* (all) [#16537](https://github.com/cosmos/cosmos-sdk/pull/16537) Properly propagated `fmt.Errorf` errors and using `errors.New` where appropriate.
|
||||
* (version) [#17096](https://github.com/cosmos/cosmos-sdk/pull/17096) Improve `getSDKVersion()` to handle module replacements
|
||||
* (x/staking) [#17164](https://github.com/cosmos/cosmos-sdk/pull/17164) Add `BondedTokensAndPubKeyByConsAddr` to the keeper to enable vote extension verification.
|
||||
* (x/genutil) [#17296](https://github.com/cosmos/cosmos-sdk/pull/17296) Add `MigrateHandler` to allow reuse migrate genesis related function.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
|
||||
@ -39,82 +39,7 @@ func MigrateGenesisCmd(migrations types.MigrationMap) *cobra.Command {
|
||||
Example: fmt.Sprintf("%s migrate v0.47 /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)
|
||||
|
||||
target := args[0]
|
||||
migrationFunc, ok := migrations[target]
|
||||
if !ok || migrationFunc == nil {
|
||||
versions := maps.Keys(migrations)
|
||||
sort.Strings(versions)
|
||||
return fmt.Errorf("unknown migration function for version: %s (supported versions %s)", target, strings.Join(versions, ", "))
|
||||
}
|
||||
|
||||
importGenesis := args[1]
|
||||
appGenesis, err := types.AppGenesisFromFile(importGenesis)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := appGenesis.ValidateAndComplete(); err != nil {
|
||||
return fmt.Errorf("make sure that you have correctly migrated all CometBFT consensus params. Refer the UPGRADING.md (%s): %w", chainUpgradeGuide, err)
|
||||
}
|
||||
|
||||
// Since some default values are valid values, we just print to
|
||||
// make sure the user didn't forget to update these values.
|
||||
if appGenesis.Consensus.Params.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(appGenesis.AppState, &initialState); err != nil {
|
||||
return fmt.Errorf("failed to JSON unmarshal initial genesis state: %w", err)
|
||||
}
|
||||
|
||||
newGenState, err := migrationFunc(initialState, clientCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to migrate genesis state: %w", err)
|
||||
}
|
||||
|
||||
appGenesis.AppState, err = json.Marshal(newGenState)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to JSON marshal migrated genesis state: %w", err)
|
||||
}
|
||||
|
||||
genesisTime, _ := cmd.Flags().GetString(flagGenesisTime)
|
||||
if genesisTime != "" {
|
||||
var t time.Time
|
||||
|
||||
err := t.UnmarshalText([]byte(genesisTime))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal genesis time: %w", err)
|
||||
}
|
||||
|
||||
appGenesis.GenesisTime = t
|
||||
}
|
||||
|
||||
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
|
||||
if chainID != "" {
|
||||
appGenesis.ChainID = chainID
|
||||
}
|
||||
|
||||
bz, err := json.Marshal(appGenesis)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal app genesis: %w", err)
|
||||
}
|
||||
|
||||
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
||||
if outputDocument == "" {
|
||||
cmd.Println(string(bz))
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = appGenesis.SaveAs(outputDocument); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return MigrateHandler(cmd, args, migrations)
|
||||
},
|
||||
}
|
||||
|
||||
@ -124,3 +49,84 @@ func MigrateGenesisCmd(migrations types.MigrationMap) *cobra.Command {
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// MigrateHandler handles the migration command with a migration map as input,
|
||||
// returning an error upon failure.
|
||||
func MigrateHandler(cmd *cobra.Command, args []string, migrations types.MigrationMap) error {
|
||||
clientCtx := client.GetClientContextFromCmd(cmd)
|
||||
|
||||
target := args[0]
|
||||
migrationFunc, ok := migrations[target]
|
||||
if !ok || migrationFunc == nil {
|
||||
versions := maps.Keys(migrations)
|
||||
sort.Strings(versions)
|
||||
return fmt.Errorf("unknown migration function for version: %s (supported versions %s)", target, strings.Join(versions, ", "))
|
||||
}
|
||||
|
||||
importGenesis := args[1]
|
||||
appGenesis, err := types.AppGenesisFromFile(importGenesis)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := appGenesis.ValidateAndComplete(); err != nil {
|
||||
return fmt.Errorf("make sure that you have correctly migrated all CometBFT consensus params. Refer the UPGRADING.md (%s): %w", chainUpgradeGuide, err)
|
||||
}
|
||||
|
||||
// Since some default values are valid values, we just print to
|
||||
// make sure the user didn't forget to update these values.
|
||||
if appGenesis.Consensus.Params.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(appGenesis.AppState, &initialState); err != nil {
|
||||
return fmt.Errorf("failed to JSON unmarshal initial genesis state: %w", err)
|
||||
}
|
||||
|
||||
newGenState, err := migrationFunc(initialState, clientCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to migrate genesis state: %w", err)
|
||||
}
|
||||
|
||||
appGenesis.AppState, err = json.Marshal(newGenState)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to JSON marshal migrated genesis state: %w", err)
|
||||
}
|
||||
|
||||
genesisTime, _ := cmd.Flags().GetString(flagGenesisTime)
|
||||
if genesisTime != "" {
|
||||
var t time.Time
|
||||
|
||||
err := t.UnmarshalText([]byte(genesisTime))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal genesis time: %w", err)
|
||||
}
|
||||
|
||||
appGenesis.GenesisTime = t
|
||||
}
|
||||
|
||||
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
|
||||
if chainID != "" {
|
||||
appGenesis.ChainID = chainID
|
||||
}
|
||||
|
||||
bz, err := json.Marshal(appGenesis)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal app genesis: %w", err)
|
||||
}
|
||||
|
||||
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
||||
if outputDocument == "" {
|
||||
cmd.Println(string(bz))
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = appGenesis.SaveAs(outputDocument); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user