-
Notifications
You must be signed in to change notification settings - Fork 20
/
migrate.go
61 lines (49 loc) · 1.77 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
package migrate
// this file is adapted version of github.com/cosmos/cosmos-sdk/x/genutil/client/cli/migrate.go
import (
"fmt"
"github.com/spf13/cobra"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"
v150 "github.com/Decentr-net/decentr/migrate/v150"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)
// MigrateGenesisCmd returns a command to execute genesis state migration.
func MigrateGenesisCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate [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 v1.5.0 /path/to/genesis.json --chain-id=mainnet-2 --genesis-time=2021-11-22T17:00:00Z
`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
importGenesis := args[0]
oldGenDoc, err := tmtypes.GenesisDocFromFile(importGenesis)
if err != nil {
return fmt.Errorf("failed to read genesis document from file %s: %w", importGenesis, err)
}
newGenDoc, err := v150.Migrate(oldGenDoc, clientCtx)
if err != nil {
return fmt.Errorf("failed to run migration: %w", err)
}
if err := newGenDoc.ValidateAndComplete(); err != nil {
return fmt.Errorf("failed to validate: %w", err)
}
bz, err := tmjson.Marshal(newGenDoc)
if err != nil {
return fmt.Errorf("failed to marshal genesis doc: %w", err)
}
sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return fmt.Errorf("failed to sort JSON genesis doc: %w", err)
}
fmt.Println(string(sortedBz))
return nil
},
}
}