-
Notifications
You must be signed in to change notification settings - Fork 182
/
migrate.go
88 lines (69 loc) · 2.24 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cli
import (
"fmt"
"time"
"github.com/okex/exchain/libs/tendermint/types"
"github.com/spf13/cobra"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
"github.com/okex/exchain/libs/cosmos-sdk/server"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/version"
extypes "github.com/okex/exchain/libs/cosmos-sdk/x/genutil"
v018 "github.com/okex/exchain/x/genutil/client/legacy/v0_18"
)
var migrationMap = extypes.MigrationMap{
"v0.18": v018.Migrate,
}
const (
flagGenesisTime = "genesis-time"
flagChainId = "chain-id"
)
// MigrateGenesisCmd returns the cobra command to migrate
func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *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.11 /path/to/genesis.json --chain-id=exchain --genesis-time=2019-04-22T17:00:00Z
`, version.ServerName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
target := args[0]
importGenesis := args[1]
genDoc, err := types.GenesisDocFromFile(importGenesis)
if err != nil {
return err
}
var initialState extypes.AppMap
cdc.MustUnmarshalJSON(genDoc.AppState, &initialState)
if migrationMap[target] == nil {
return fmt.Errorf("unknown migration function version: %s", target)
}
newGenState := migrationMap[target](initialState)
genDoc.AppState = cdc.MustMarshalJSON(newGenState)
genesisTime := cmd.Flag(flagGenesisTime).Value.String()
if genesisTime != "" {
var t time.Time
err := t.UnmarshalText([]byte(genesisTime))
if err != nil {
return err
}
genDoc.GenesisTime = t
}
chainId := cmd.Flag(flagChainId).Value.String()
if chainId != "" {
genDoc.ChainID = chainId
}
out, err := cdc.MarshalJSONIndent(genDoc, "", " ")
if err != nil {
return err
}
fmt.Println(string(sdk.MustSortJSON(out)))
return nil
},
}
cmd.Flags().String(flagGenesisTime, "", "Override genesis_time with this flag")
cmd.Flags().String(flagChainId, "", "Override chain_id with this flag")
return cmd
}