/
migrate.go
153 lines (117 loc) Β· 4.92 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
150
151
152
153
package legacy
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/authz"
"io/ioutil"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"
ibcxfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
ibccoretypes "github.com/cosmos/ibc-go/v3/modules/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
captypes "github.com/cosmos/cosmos-sdk/x/capability/types"
evtypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
legacy120 "github.com/enigmampc/SecretNetwork/app/legacy/v120"
)
const (
flagGenesisTime = "genesis-time"
flagInitialHeight = "initial-height"
)
// MigrateGenesisCmd returns a command to execute genesis state migration.
func MigrateGenesisCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate [genesis-file]",
Short: "Migrate genesis to a specified target version",
Long: `Migrate the source genesis into the target version and print to STDOUT.
Example:
$ secretd migrate /path/to/genesis.json --chain-id=secret-4 --genesis-time=2019-04-22T17:00:00Z --initial-height=5000
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
var err error
importGenesis := args[0]
jsonBlob, err := ioutil.ReadFile(importGenesis)
if err != nil {
return errors.Wrap(err, "failed to read provided genesis file")
}
genDoc, err := tmtypes.GenesisDocFromJSON(jsonBlob)
if err != nil {
return errors.Wrapf(err, "failed to read genesis document from file %s", importGenesis)
}
// increase block consensus params
genDoc.ConsensusParams.Block.MaxBytes = int64(10_000_000)
genDoc.ConsensusParams.Block.MaxGas = int64(10_000_000)
// decrease evidence max bytes
genDoc.ConsensusParams.Evidence.MaxBytes = int64(50000)
var initialState types.AppMap
if err := json.Unmarshal(genDoc.AppState, &initialState); err != nil {
return errors.Wrap(err, "failed to JSON unmarshal initial genesis state")
}
// Migrate Terra specific state
newGenState := legacy120.Migrate(initialState, clientCtx)
var bankGenesis banktypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(newGenState[banktypes.ModuleName], &bankGenesis)
var stakingGenesis staking.GenesisState
clientCtx.Codec.MustUnmarshalJSON(newGenState[staking.ModuleName], &stakingGenesis)
ibcTransferGenesis := ibcxfertypes.DefaultGenesisState()
ibcCoreGenesis := ibccoretypes.DefaultGenesisState()
capGenesis := captypes.DefaultGenesis()
evGenesis := evtypes.DefaultGenesisState()
authzGenesis := authz.DefaultGenesisState()
ibcTransferGenesis.Params.ReceiveEnabled = false
ibcTransferGenesis.Params.SendEnabled = false
ibcCoreGenesis.ClientGenesis.Params.AllowedClients = []string{exported.Tendermint}
stakingGenesis.Params.HistoricalEntries = 10000
newGenState[ibcxfertypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(ibcTransferGenesis)
newGenState[host.ModuleName] = clientCtx.Codec.MustMarshalJSON(ibcCoreGenesis)
newGenState[captypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(capGenesis)
newGenState[evtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(evGenesis)
newGenState[staking.ModuleName] = clientCtx.Codec.MustMarshalJSON(&stakingGenesis)
newGenState[authz.ModuleName] = clientCtx.Codec.MustMarshalJSON(authzGenesis)
genDoc.AppState, err = json.Marshal(newGenState)
if err != nil {
return errors.Wrap(err, "failed to JSON marshal migrated genesis state")
}
genesisTime, _ := cmd.Flags().GetString(flagGenesisTime)
if genesisTime != "" {
var t time.Time
err := t.UnmarshalText([]byte(genesisTime))
if err != nil {
return errors.Wrap(err, "failed to unmarshal genesis time")
}
genDoc.GenesisTime = t
}
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID != "" {
genDoc.ChainID = chainID
}
initialHeight, _ := cmd.Flags().GetInt(flagInitialHeight)
genDoc.InitialHeight = int64(initialHeight)
bz, err := tmjson.Marshal(genDoc)
if err != nil {
return errors.Wrap(err, "failed to marshal genesis doc")
}
sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return errors.Wrap(err, "failed to sort JSON genesis doc")
}
fmt.Println(string(sortedBz))
return nil
},
}
cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag")
cmd.Flags().Int(flagInitialHeight, 0, "Set the starting height for the chain")
cmd.Flags().String(flags.FlagChainID, "", "override chain_id with this flag")
return cmd
}