-
Notifications
You must be signed in to change notification settings - Fork 45
/
migrate.go
149 lines (121 loc) · 4.12 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cli
import (
"fmt"
"sort"
"strconv"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
extypes "github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/desmos-labs/desmos/x/genutil/internal/types"
v020 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.2.0"
v030 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.3.0"
v040 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.4.0"
v050 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.5.0"
"github.com/spf13/cobra"
tm "github.com/tendermint/tendermint/types"
)
// migrationMap contains the list of migrations that should be performed when migrating
// a version of the chain to the next one. It contains an array as we need to support Cosmos SDK migrations
// too if needed.
var migrationMap = map[string]types.MigrationCallback{
"v0.2.0": v020.Migrate,
"v0.3.0": v030.Migrate,
"v0.4.0": v040.Migrate,
"v0.5.0": v050.Migrate,
}
const (
flagGenesisTime = "genesis-time"
flagChainID = "chain-id"
flagBlockInterval = "block-interval"
)
func MigrationsListCmd() *cobra.Command {
return &cobra.Command{
Use: "migrations-list",
Short: "Lists all the available migrations",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var migrations []string
for migration := range migrationMap {
migrations = append(migrations, migration)
}
sort.Strings(migrations)
for _, m := range migrations {
fmt.Println(m)
}
return nil
},
}
}
func MigrateGenesisCmd(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.
Please note that migrations should be only be done sequentially. As a reference, suppose we have the following versions:
- v0.1.0
- v0.2.0
- v0.3.0
If you want to migrate from version v0.1.0 to v0.3.0, you need to execute two migrations:
1. From v0.1.0 to v0.2.0
$ %s migrate v0.2.0 ...
2. From v0.2.0 to v0.3.0
$ %s migrate v0.3.0 ...
To see get a full list of available migrations, use the migrations-list command.
Example:
$ %s migrate v0.2.0 /path/to/genesis.json --chain-id=morpheus-XXXXX --genesis-time=2019-11-31T18:00:00Z
`, version.ServerName, version.ServerName, version.ServerName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
target := args[0]
importGenesis := args[1]
genDoc, err := tm.GenesisDocFromFile(importGenesis)
if err != nil {
return err
}
var initialState extypes.AppMap
cdc.MustUnmarshalJSON(genDoc.AppState, &initialState)
migration := migrationMap[target]
if migration == nil {
return fmt.Errorf("unknown migration function version: %s", target)
}
newGenState := initialState
// v0.2.0 migration needs to have the previous version's genesis time and the
// block interval to convert the block height dates into timestamps
if target == "v0.2.0" {
blockInterval, err := strconv.Atoi(cmd.Flag(flagBlockInterval).Value.String())
if err != nil {
panic(err)
}
newGenState = migration(newGenState, genDoc.GenesisTime, blockInterval)
} else {
newGenState = migration(newGenState)
}
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")
cmd.Flags().Int(flagBlockInterval, 0, "Block interval of seconds to consider while computing timestamps dates")
return cmd
}