|
1 | 1 | package cmd |
2 | 2 |
|
3 | | -// |
4 | | -// import ( |
5 | | -// "crypto/sha256" |
6 | | -// "encoding/json" |
7 | | -// "fmt" |
8 | | -// "github.com/MinterTeam/minter-go-node/cmd/utils" |
9 | | -// "github.com/MinterTeam/minter-go-node/coreV2/state" |
10 | | -// "github.com/spf13/cobra" |
11 | | -// "github.com/tendermint/go-amino" |
12 | | -// tmproto "github.com/tendermint/tendermint/proto/tendermint/types" |
13 | | -// "github.com/tendermint/tendermint/types" |
14 | | -// "io" |
15 | | -// "log" |
16 | | -// "os" |
17 | | -// "time" |
18 | | -// ) |
19 | | -// |
20 | | -// var ( |
21 | | -// ExportCommand = &cobra.Command{ |
22 | | -// Use: "export", |
23 | | -// Short: "Minter export command", |
24 | | -// RunE: export, |
25 | | -// } |
26 | | -// ) |
27 | | -// |
28 | | -// const ( |
29 | | -// genesisPath = "genesis.json" |
30 | | -// |
31 | | -// blockMaxBytes int64 = 10000000 |
32 | | -// blockMaxGas int64 = 100000 |
33 | | -// blockTimeIotaMs int64 = 1000 |
34 | | -// |
35 | | -// evidenceMaxAgeNumBlocks = 1000 |
36 | | -// evidenceMaxAgeDuration = 24 * time.Hour |
37 | | -// ) |
38 | | -// |
39 | | -// func export(cmd *cobra.Command, args []string) error { |
40 | | -// height, err := cmd.Flags().GetUint64("height") |
41 | | -// if err != nil { |
42 | | -// log.Panicf("Cannot parse height: %s", err) |
43 | | -// } |
44 | | -// // |
45 | | -// // startHeight, err := cmd.Flags().GetUint64("start-height") |
46 | | -// // if err != nil { |
47 | | -// // log.Panicf("Cannot parse start-height: %s", err) |
48 | | -// // } |
49 | | -// |
50 | | -// chainID, err := cmd.Flags().GetString("chain-id") |
51 | | -// if err != nil { |
52 | | -// log.Panicf("Cannot parse chain id: %s", err) |
53 | | -// } |
54 | | -// |
55 | | -// genesisTime, err := cmd.Flags().GetDuration("genesis-time") |
56 | | -// if err != nil { |
57 | | -// log.Panicf("Cannot parse genesis time: %s", err) |
58 | | -// } |
59 | | -// |
60 | | -// indent, err := cmd.Flags().GetBool("indent") |
61 | | -// if err != nil { |
62 | | -// log.Panicf("Cannot parse indent: %s", err) |
63 | | -// } |
64 | | -// |
65 | | -// fmt.Println("Start exporting...") |
66 | | -// |
67 | | -// ldb, err := utils.NewStorage("", "").InitStateLevelDB("state", nil) |
68 | | -// if err != nil { |
69 | | -// log.Panicf("Cannot load db: %s", err) |
70 | | -// } |
71 | | -// |
72 | | -// currentState, err := state.NewCheckStateAtHeight(height, ldb) |
73 | | -// if err != nil { |
74 | | -// log.Panicf("Cannot new state at given height: %s", err) |
75 | | -// } |
76 | | -// |
77 | | -// exportTimeStart, newState := time.Now(), currentState.Export() |
78 | | -// fmt.Printf("State has been exported. Took %s", time.Since(exportTimeStart)) |
79 | | -// |
80 | | -// initialHeight := height |
81 | | -// |
82 | | -// var jsonBytes []byte |
83 | | -// if indent { |
84 | | -// jsonBytes, err = amino.NewCodec().MarshalJSONIndent(newState, "", " ") |
85 | | -// } else { |
86 | | -// jsonBytes, err = amino.NewCodec().MarshalJSON(newState) |
87 | | -// } |
88 | | -// if err != nil { |
89 | | -// log.Panicf("Cannot marshal state to json: %s", err) |
90 | | -// } |
91 | | -// |
92 | | -// appHash := [32]byte{} |
93 | | -// |
94 | | -// // compose genesis |
95 | | -// genesis := types.GenesisDoc{ |
96 | | -// GenesisTime: time.Unix(0, 0).Add(genesisTime), |
97 | | -// InitialHeight: int64(initialHeight), |
98 | | -// ChainID: chainID, |
99 | | -// ConsensusParams: &tmproto.ConsensusParams{ |
100 | | -// Block: tmproto.BlockParams{ |
101 | | -// MaxBytes: blockMaxBytes, |
102 | | -// MaxGas: blockMaxGas, |
103 | | -// TimeIotaMs: blockTimeIotaMs, |
104 | | -// }, |
105 | | -// Evidence: tmproto.EvidenceParams{ |
106 | | -// MaxAgeNumBlocks: evidenceMaxAgeNumBlocks, |
107 | | -// MaxAgeDuration: evidenceMaxAgeDuration, |
108 | | -// }, |
109 | | -// Validator: tmproto.ValidatorParams{ |
110 | | -// PubKeyTypes: []string{ |
111 | | -// types.ABCIPubKeyTypeEd25519, |
112 | | -// }, |
113 | | -// }, |
114 | | -// }, |
115 | | -// AppHash: appHash[:], |
116 | | -// AppState: json.RawMessage(jsonBytes), |
117 | | -// } |
118 | | -// |
119 | | -// err = genesis.ValidateAndComplete() |
120 | | -// if err != nil { |
121 | | -// log.Panicf("Failed to validate: %s", err) |
122 | | -// } |
123 | | -// |
124 | | -// if err := genesis.SaveAs(genesisPath); err != nil { |
125 | | -// log.Panicf("Failed to save genesis file: %s", err) |
126 | | -// } |
127 | | -// |
128 | | -// hash := getFileSha256Hash(genesisPath) |
129 | | -// fmt.Printf("\nOK\n%x\n", hash) |
130 | | -// |
131 | | -// return nil |
132 | | -// } |
133 | | -// |
134 | | -// func getFileSha256Hash(file string) []byte { |
135 | | -// f, err := os.Open(file) |
136 | | -// if err != nil { |
137 | | -// log.Fatal(err) |
138 | | -// } |
139 | | -// |
140 | | -// defer f.Close() |
141 | | -// |
142 | | -// h := sha256.New() |
143 | | -// if _, err := io.Copy(h, f); err != nil { |
144 | | -// log.Fatal(err) |
145 | | -// } |
146 | | -// |
147 | | -// return h.Sum(nil) |
148 | | -// } |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/json" |
| 6 | + "github.com/MinterTeam/minter-go-node/cmd/utils" |
| 7 | + "github.com/MinterTeam/minter-go-node/coreV2/appdb" |
| 8 | + "github.com/MinterTeam/minter-go-node/coreV2/state" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/tendermint/go-amino" |
| 11 | + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" |
| 12 | + "github.com/tendermint/tendermint/types" |
| 13 | + "io" |
| 14 | + "log" |
| 15 | + "os" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + ExportCommand = &cobra.Command{ |
| 21 | + Use: "export", |
| 22 | + Short: "Minter export command", |
| 23 | + RunE: export, |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + genesisPath = "genesis.json" |
| 29 | + |
| 30 | + blockMaxBytes int64 = 10000000 |
| 31 | + blockMaxGas int64 = 100000 |
| 32 | + blockTimeIotaMs int64 = 1000 |
| 33 | + |
| 34 | + evidenceMaxAgeNumBlocks = 1000 |
| 35 | + evidenceMaxAgeDuration = 24 * time.Hour |
| 36 | +) |
| 37 | + |
| 38 | +func export(cmd *cobra.Command, args []string) error { |
| 39 | + height, err := cmd.Flags().GetUint64("height") |
| 40 | + if err != nil { |
| 41 | + log.Panicf("Cannot parse height: %s", err) |
| 42 | + } |
| 43 | + |
| 44 | + chainID, err := cmd.Flags().GetString("chain-id") |
| 45 | + if err != nil { |
| 46 | + log.Panicf("Cannot parse chain id: %s", err) |
| 47 | + } |
| 48 | + |
| 49 | + genesisTime, err := cmd.Flags().GetDuration("genesis-time") |
| 50 | + if err != nil { |
| 51 | + log.Panicf("Cannot parse genesis time: %s", err) |
| 52 | + } |
| 53 | + |
| 54 | + indent, err := cmd.Flags().GetBool("indent") |
| 55 | + if err != nil { |
| 56 | + log.Panicf("Cannot parse indent: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + log.Println("Start exporting...") |
| 60 | + |
| 61 | + homeDir, err := cmd.Flags().GetString("home-dir") |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + storages := utils.NewStorage(homeDir, "") |
| 66 | + |
| 67 | + ldb, err := storages.InitStateLevelDB("data/state", nil) |
| 68 | + if err != nil { |
| 69 | + log.Panicf("Cannot load db: %s", err) |
| 70 | + } |
| 71 | + |
| 72 | + currentState, err := state.NewCheckStateAtHeight(height, ldb) |
| 73 | + if err != nil { |
| 74 | + log.Panicf("Cannot new state at given height: %s, last available height %d", err, appdb.NewAppDB(storages.GetMinterHome(), cfg).GetLastHeight()) |
| 75 | + } |
| 76 | + |
| 77 | + exportTimeStart := time.Now() |
| 78 | + appState := currentState.Export() |
| 79 | + log.Printf("State has been exported. Took %s\n", time.Since(exportTimeStart)) |
| 80 | + |
| 81 | + if err := appState.Verify(); err != nil { |
| 82 | + log.Fatalf("Failed to validate: %s\n", err) |
| 83 | + } |
| 84 | + log.Printf("Verify state OK\n") |
| 85 | + |
| 86 | + var jsonBytes []byte |
| 87 | + if indent { |
| 88 | + jsonBytes, err = amino.NewCodec().MarshalJSONIndent(appState, "", " ") |
| 89 | + } else { |
| 90 | + jsonBytes, err = amino.NewCodec().MarshalJSON(appState) |
| 91 | + } |
| 92 | + if err != nil { |
| 93 | + log.Panicf("Cannot marshal state to json: %s", err) |
| 94 | + } |
| 95 | + log.Printf("Marshal OK\n") |
| 96 | + |
| 97 | + // compose genesis |
| 98 | + genesis := types.GenesisDoc{ |
| 99 | + GenesisTime: time.Unix(0, 0).Add(genesisTime), |
| 100 | + InitialHeight: int64(height), |
| 101 | + ChainID: chainID, |
| 102 | + ConsensusParams: &tmproto.ConsensusParams{ |
| 103 | + Block: tmproto.BlockParams{ |
| 104 | + MaxBytes: blockMaxBytes, |
| 105 | + MaxGas: blockMaxGas, |
| 106 | + TimeIotaMs: blockTimeIotaMs, |
| 107 | + }, |
| 108 | + Evidence: tmproto.EvidenceParams{ |
| 109 | + MaxAgeNumBlocks: evidenceMaxAgeNumBlocks, |
| 110 | + MaxAgeDuration: evidenceMaxAgeDuration, |
| 111 | + }, |
| 112 | + Validator: tmproto.ValidatorParams{ |
| 113 | + PubKeyTypes: []string{ |
| 114 | + types.ABCIPubKeyTypeEd25519, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + AppHash: nil, |
| 119 | + AppState: json.RawMessage(jsonBytes), |
| 120 | + } |
| 121 | + |
| 122 | + err = genesis.ValidateAndComplete() |
| 123 | + if err != nil { |
| 124 | + log.Panicf("Failed to validate: %s", err) |
| 125 | + } |
| 126 | + log.Printf("Validate genesis OK\n") |
| 127 | + |
| 128 | + if err := genesis.SaveAs(genesisPath); err != nil { |
| 129 | + log.Panicf("Failed to save genesis file: %s", err) |
| 130 | + } |
| 131 | + |
| 132 | + hash := getFileSha256Hash(genesisPath) |
| 133 | + log.Printf("Finish with sha256 hash: \n%x\n", hash) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func getFileSha256Hash(file string) []byte { |
| 139 | + f, err := os.Open(file) |
| 140 | + if err != nil { |
| 141 | + log.Fatal(err) |
| 142 | + } |
| 143 | + |
| 144 | + defer f.Close() |
| 145 | + |
| 146 | + h := sha256.New() |
| 147 | + if _, err := io.Copy(h, f); err != nil { |
| 148 | + log.Fatal(err) |
| 149 | + } |
| 150 | + |
| 151 | + return h.Sum(nil) |
| 152 | +} |
0 commit comments