forked from benjaminbollen/mintnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
302 lines (255 loc) · 6.95 KB
/
init.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"encoding/hex"
"fmt"
"path"
"time"
"github.com/codegangsta/cli"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
tmtypes "github.com/tendermint/tendermint/types"
)
//--------------------------------------------------------------------------------
func cmdInit(c *cli.Context) {
cli.ShowAppHelp(c)
}
// initialize a new validator set
func cmdValidatorsInit(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
cli.ShowAppHelp(c)
return
}
base := args[0]
N := c.Int("N")
vals := make([]*Validator, N)
// Initialize priv_validator.json's
for i := 0; i < N; i++ {
err := initValDirectory(base, i)
if err != nil {
Exit(err.Error())
}
// Read priv_validator.json to populate vals
name := fmt.Sprintf("val%d", i)
privValFile := path.Join(base, name, "priv_validator.json")
privVal := tmtypes.LoadPrivValidator(privValFile)
vals[i] = &Validator{
ID: name,
PubKey: privVal.PubKey,
}
}
valSet := ValidatorSet{
ID: path.Base(base),
Validators: vals,
}
// write the validator set file
b := wire.JSONBytesPretty(valSet)
err := WriteFile(path.Join(base, "validator_set.json"), b, 0444)
if err != nil {
Exit(err.Error())
}
fmt.Println(Fmt("Successfully initialized %v validators", N))
}
// Initialize directories for each node
func cmdChainInit(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
cli.ShowAppHelp(c)
return
}
base := args[0]
machines := ParseMachines(c.GlobalString("machines"))
app := c.String("app")
var appHash []byte
appHashString := c.String("app-hash")
if appHashString != "" {
if len(appHashString) >= 2 && appHashString[:2] == "0x" {
var err error
appHash, err = hex.DecodeString(appHashString[2:])
if err != nil {
Exit(err.Error())
}
} else {
appHash = []byte(appHashString)
}
}
err := initDataDirectory(base)
if err != nil {
Exit(err.Error())
}
err = initAppDirectory(base, app)
if err != nil {
Exit(err.Error())
}
err = initCoreDirectory(base)
if err != nil {
Exit(err.Error())
}
genVals := make([]tmtypes.GenesisValidator, len(machines))
//var valSetID string
valSetDir := c.String("validator-set")
if valSetDir != "" {
// validator-set name is the last element of the path
//valSetID = path.Base(valSetDir)
var valSet ValidatorSet
err := ReadJSONFile(&valSet, path.Join(valSetDir, "validator_set.json"))
if err != nil {
Exit(err.Error())
}
vals := valSet.Validators
if len(machines) != len(vals) {
Exit(fmt.Sprintf("Validator set size must match number of machines. Got %d validators, %d machines", len(vals), len(machines)))
}
for i, val := range vals {
// build the directory
mach := machines[i]
err := initMachCoreDirectory(base, mach)
if err != nil {
Exit(err.Error())
}
// overwrite the priv validator
privValFile := path.Join(valSetDir, val.ID, "priv_validator.json")
privVal := tmtypes.LoadPrivValidator(privValFile)
privVal.SetFile(path.Join(base, mach, "core", "priv_validator.json"))
privVal.Save()
}
// copy the vals into genVals
for i, val := range vals {
genVals[i] = tmtypes.GenesisValidator{
Name: val.ID,
PubKey: val.PubKey,
Amount: 1, // TODO
}
}
} else {
//valSetID = ValSetAnon
// Initialize core dir and priv_validator.json's
for i, mach := range machines {
err := initMachCoreDirectory(base, mach)
if err != nil {
Exit(err.Error())
}
// Read priv_validator.json to populate vals
privValFile := path.Join(base, mach, "core", "priv_validator.json")
privVal := tmtypes.LoadPrivValidator(privValFile)
genVals[i] = tmtypes.GenesisValidator{
PubKey: privVal.PubKey,
Amount: 1,
Name: mach,
}
}
}
// Generate genesis doc from generated validators
genDoc := &tmtypes.GenesisDoc{
GenesisTime: time.Now(),
ChainID: "chain-" + RandStr(6),
Validators: genVals,
AppHash: appHash,
}
// Write genesis file.
for _, mach := range machines {
genDoc.SaveAs(path.Join(base, mach, "core", "genesis.json"))
}
fmt.Println(Fmt("Successfully initialized %v node directories", len(machines)))
}
// Initialize per-machine core directory
func initMachCoreDirectory(base, mach string) error {
dir := path.Join(base, mach, "core")
err := EnsureDir(dir, 0777)
if err != nil {
return err
}
// Create priv_validator.json file if not present
ensurePrivValidator(path.Join(dir, "priv_validator.json"))
return nil
}
func initValDirectory(base string, i int) error {
name := fmt.Sprintf("val%d", i)
dir := path.Join(base, name)
err := EnsureDir(dir, 0777)
if err != nil {
return err
}
// Create priv_validator.json file if not present
ensurePrivValidator(path.Join(dir, "priv_validator.json"))
return nil
}
func ensurePrivValidator(file string) {
if FileExists(file) {
return
}
privValidator := tmtypes.GenPrivValidator()
privValidator.SetFile(file)
privValidator.Save()
}
// Initialize common data directory
func initDataDirectory(base string) error {
dir := path.Join(base, "data")
err := EnsureDir(dir, 0777)
if err != nil {
return err
}
// Write a silly sample bash script.
scriptBytes := []byte(`#! /bin/bash
# This is a sample bash script for MerkleEyes.
# NOTE: mintnet expects data.sock to be created
go get github.com/tendermint/merkleeyes/cmd/merkleeyes
merkleeyes server --address="unix:///data/tendermint/data/data.sock"`)
err = WriteFile(path.Join(dir, "init.sh"), scriptBytes, 0777)
return err
}
// Initialize common app directory
func initAppDirectory(base, app string) error {
dir := path.Join(base, "app")
err := EnsureDir(dir, 0777)
if err != nil {
return err
}
var scriptBytes []byte
if app == "" {
// Write a silly sample bash script.
scriptBytes = []byte(`#! /bin/bash
# This is a sample bash script for a TMSP application
cd app/
git clone https://github.com/tendermint/nomnomcoin.git
cd nomnomcoin
npm install .
node app.js --addr="unix:///data/tendermint/app/app.sock" --eyes="unix:///data/tendermint/data/data.sock"`)
} else {
var err error
scriptBytes, err = ReadFile(app)
if err != nil {
return err
}
}
err = WriteFile(path.Join(dir, "init.sh"), scriptBytes, 0777)
return err
}
// Initialize common core directory
func initCoreDirectory(base string) error {
dir := path.Join(base, "core")
err := EnsureDir(dir, 0777)
if err != nil {
return err
}
// Write a silly sample bash script.
scriptBytes := []byte(`#! /bin/bash
# This is a sample bash script for tendermint core
# Edit this script before "mintnet start" to change
# the core blockchain engine.
TMREPO="github.com/tendermint/tendermint"
BRANCH="master"
go get -d $TMREPO/cmd/tendermint
### DEPENDENCIES (example)
# cd $GOPATH/src/github.com/tendermint/tmsp
# git fetch origin $BRANCH
# git checkout $BRANCH
### DEPENDENCIES END
cd $GOPATH/src/$TMREPO
git fetch origin $BRANCH
git checkout $BRANCH
make install
tendermint node --seeds="$TMSEEDS" --moniker="$TMNAME" --proxy_app="$PROXYAPP"`)
err = WriteFile(path.Join(dir, "init.sh"), scriptBytes, 0777)
return err
}