forked from ethereumproject/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
479 lines (433 loc) · 13.9 KB
/
cmd.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"os"
"os/signal"
"runtime"
"strings"
"github.com/ethereumproject/go-ethereum/core"
"github.com/ethereumproject/go-ethereum/core/types"
"github.com/ethereumproject/go-ethereum/eth"
"github.com/ethereumproject/go-ethereum/logger"
"github.com/ethereumproject/go-ethereum/logger/glog"
"github.com/ethereumproject/go-ethereum/node"
"github.com/ethereumproject/go-ethereum/rlp"
"time"
"syscall"
)
const (
importBatchSize = 2500
)
var sigc = make(chan os.Signal, 1)
// Fatalf formats a message to standard error and exits the program.
// The message is also printed to standard output if standard error
// is redirected to a different file.
func Fatalf(format string, args ...interface{}) {
w := io.MultiWriter(os.Stdout, os.Stderr)
if runtime.GOOS == "windows" {
// The SameFile check below doesn't work on Windows.
// stdout is unlikely to get redirected though, so just print there.
w = os.Stdout
} else {
outf, _ := os.Stdout.Stat()
errf, _ := os.Stderr.Stat()
if outf != nil && errf != nil && os.SameFile(outf, errf) {
w = os.Stderr
}
}
fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
logger.Flush()
os.Exit(1)
}
func StartNode(stack *node.Node) {
if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err)
}
go func() {
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigc)
sig := <-sigc
glog.Infof("Got %v, shutting down...", sig)
fails := make(chan error, 1)
go func (fs chan error) {
for {
select {
case e := <-fs:
if e != nil {
glog.V(logger.Error).Infof("node stop failure: %v", e)
}
}
}
}(fails)
go func(stack *node.Node) {
defer close(fails)
fails <- stack.Stop()
}(stack)
// WTF?
for i := 10; i > 0; i-- {
<-sigc
if i > 1 {
glog.Infof("Already shutting down, interrupt %d more times for panic.", i-1)
}
}
glog.Fatal("boom")
}()
}
func ImportChain(chain *core.BlockChain, fn string) error {
// Watch for Ctrl-C while the import is running.
// If a signal is received, the import will stop at the next batch.
interrupt := make(chan os.Signal, 1)
stop := make(chan struct{})
signal.Notify(interrupt, os.Interrupt)
defer signal.Stop(interrupt)
defer close(interrupt)
go func() {
if _, ok := <-interrupt; ok {
glog.Info("caught interrupt during import, will stop at next batch")
}
close(stop)
}()
checkInterrupt := func() bool {
select {
case <-stop:
return true
default:
return false
}
}
glog.Infoln("Importing blockchain ", fn)
fh, err := os.Open(fn)
if err != nil {
return err
}
defer fh.Close()
stream := rlp.NewStream(fh, 0)
// Run actual the import.
blocks := make(types.Blocks, importBatchSize)
n := 0
for batch := 0; ; batch++ {
// Load a batch of RLP blocks.
if checkInterrupt() {
return fmt.Errorf("interrupted")
}
i := 0
for ; i < importBatchSize; i++ {
var b types.Block
if err := stream.Decode(&b); err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("at block %d: %v", n, err)
}
// don't import first block
if b.NumberU64() == 0 {
i--
continue
}
blocks[i] = &b
n++
}
if i == 0 {
break
}
// Import the batch.
if checkInterrupt() {
return fmt.Errorf("interrupted")
}
if hasAllBlocks(chain, blocks[:i]) {
glog.Infof("skipping batch %d, all blocks present [%x / %x]",
batch, blocks[0].Hash().Bytes()[:4], blocks[i-1].Hash().Bytes()[:4])
continue
}
if _, err := chain.InsertChain(blocks[:i]); err != nil {
return fmt.Errorf("invalid block %d: %v", n, err)
}
}
return nil
}
func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
for _, b := range bs {
if !chain.HasBlock(b.Hash()) {
return false
}
}
return true
}
func ExportChain(blockchain *core.BlockChain, fn string) error {
glog.Infoln("Exporting blockchain to ", fn)
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
}
defer fh.Close()
if err := blockchain.Export(fh); err != nil {
return err
}
glog.Infoln("Exported blockchain to ", fn)
return nil
}
func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, last uint64) error {
glog.Infoln("Exporting blockchain to ", fn)
// TODO verify mode perms
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer fh.Close()
if err := blockchain.ExportN(fh, first, last); err != nil {
return err
}
glog.Infoln("Exported blockchain to ", fn)
return nil
}
func withLineBreak(s string) string {
return s + "\n"
}
func colorGreen(s interface{}) string {
return fmt.Sprintf("\x1b[32m%v\x1b[39m", s)
}
func colorBlue(s interface{}) string {
return fmt.Sprintf("\x1b[36m%v\x1b[39m", s)
}
func formatStatusKeyValue(prefix string, ss ...interface{}) (s string) {
s = ""
// Single arg; category? ie Forks?
if len(ss) == 1 {
s += colorBlue(ss[0])
}
if len(ss) == 2 {
if ss[0] == "" {
s += fmt.Sprintf("%v", colorGreen(ss[1]))
} else {
s += fmt.Sprintf("%v: %v", ss[0], colorGreen(ss[1]))
}
}
if len(ss) > 2 {
s += fmt.Sprintf("%v:", ss[0])
for i := 2; i < len(ss); i++ {
s += withLineBreak(fmt.Sprintf(" %v", colorGreen(ss[i])))
}
}
return withLineBreak(prefix + s)
}
type printable struct {
indent int
key string
val interface{}
}
var indent string = " "
// For use by 'status' command.
// These are one of doing it, with each key/val per line and some appropriate indentations to signal grouping/parentage.
// ... But there might be a more elegant way using columns and stuff. VeryPretty?
func formatSufficientChainConfigPretty(config *core.SufficientChainConfig) (s []string) {
ss := []printable{}
// Chain identifiers.
ss = append(ss, printable{0, "Chain identity", config.Identity})
ss = append(ss, printable{0, "Chain name", config.Name})
// Genesis.
ss = append(ss, printable{0, "Genesis", nil})
ss = append(ss, printable{1, "Nonce", config.Genesis.Nonce})
ss = append(ss, printable{1, "Coinbase", config.Genesis.Coinbase})
ss = append(ss, printable{1, "Extra data", config.Genesis.ExtraData})
ss = append(ss, printable{1, "Gas limit", config.Genesis.GasLimit})
ss = append(ss, printable{1, "Difficulty", config.Genesis.Difficulty})
ss = append(ss, printable{1, "Time", config.Genesis.Timestamp})
lenAlloc := len(config.Genesis.Alloc)
ss = append(ss, printable{1, "Number of allocations", lenAlloc})
// Chain configuration.
ss = append(ss, printable{0, "Chain Configuration", nil})
ss = append(ss, printable{1, "Forks", nil})
for _, v := range config.ChainConfig.Forks {
ss = append(ss, printable{2, "Name", v.Name})
ss = append(ss, printable{2, "Block", v.Block})
if !v.RequiredHash.IsEmpty() {
ss = append(ss, printable{2, "Required hash", v.RequiredHash.Hex()})
}
for _, fv := range v.Features {
ss = append(ss, printable{3, fv.ID, nil})
for k, ffv := range fv.Options {
ss = append(ss, printable{4, k, ffv})
}
}
}
ss = append(ss, printable{1, "Bad hashes", nil})
for _, v := range config.ChainConfig.BadHashes {
ss = append(ss, printable{2, "Block", v.Block})
ss = append(ss, printable{2, "Hash", v.Hash.Hex()})
}
for _, v := range ss {
if v.val != nil {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key, v.val))
} else {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key))
}
}
return s
}
func formatEthConfigPretty(ethConfig *eth.Config) (s []string) {
ss := []printable{}
// NetworkID
ss = append(ss, printable{0, "Network", ethConfig.NetworkId})
// FastSync?
ss = append(ss, printable{0, "Fast sync", ethConfig.FastSync})
// BlockChainVersion
ss = append(ss, printable{0, "Blockchain version", ethConfig.BlockChainVersion})
// DatabaseCache
ss = append(ss, printable{0, "Database cache (MB)", ethConfig.DatabaseCache})
// DatabaseHandles
ss = append(ss, printable{0, "Database file handles", ethConfig.DatabaseHandles})
// NatSpec?
ss = append(ss, printable{0, "NAT spec", ethConfig.NatSpec})
// AutoDAG?
ss = append(ss, printable{0, "Auto DAG", ethConfig.AutoDAG})
// PowTest?
ss = append(ss, printable{0, "Pow test", ethConfig.PowTest})
// PowShared?
ss = append(ss, printable{0, "Pow shared", ethConfig.PowShared})
// SolcPath
ss = append(ss, printable{0, "Solc path", ethConfig.SolcPath})
// Account Manager
lenAccts := len(ethConfig.AccountManager.Accounts())
ss = append(ss, printable{0, "Account Manager", nil})
//Number of accounts
ss = append(ss, printable{1, "Number of accounts", lenAccts})
// keystore not exported
// Keystore
// Dir
// ScryptN
// ScryptP
// Etherbase (if set)
ss = append(ss, printable{0, "Etherbase", ethConfig.Etherbase.Hex()})
// GasPrice
ss = append(ss, printable{0, "Gas price", ethConfig.GasPrice})
ss = append(ss, printable{0, "GPO min gas price", ethConfig.GpoMinGasPrice})
ss = append(ss, printable{0, "GPO max gas price", ethConfig.GpoMaxGasPrice})
// MinerThreads
ss = append(ss, printable{0, "Miner threads", ethConfig.MinerThreads})
for _, v := range ss {
if v.val != nil {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key, v.val))
} else {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key))
}
}
return s
}
func formatStackConfigPretty(stackConfig *node.Config) (s []string) {
ss := []printable{}
// Name
ss = append(ss, printable{0, "Name", stackConfig.Name})
// Datadir
ss = append(ss, printable{0, "Node data dir", stackConfig.DataDir})
// IPCPath
ss = append(ss, printable{0, "IPC path", stackConfig.IPCPath})
// PrivateKey?
if stackConfig.PrivateKey != nil {
ss = append(ss, printable{1, "Private key", nil})
ss = append(ss, printable{2, "Private key", nil})
ss = append(ss, printable{2, "X", stackConfig.PrivateKey.PublicKey.X})
ss = append(ss, printable{2, "Y", stackConfig.PrivateKey.PublicKey.Y})
}
// Discovery?
ss = append(ss, printable{0, "Discovery", !stackConfig.NoDiscovery})
// BoostrapNodes
ss = append(ss, printable{0, "Bootstrap nodes", nil})
for _, n := range stackConfig.BootstrapNodes {
ss = append(ss, printable{1, "", n.String()})
}
// ListenAddrg
sla := stackConfig.ListenAddr
if sla == ":0" {
sla += " (OS will pick)"
}
ss = append(ss, printable{0, "Listen address", sla})
// NAT
ss = append(ss, printable{0, "NAT", stackConfig.NAT.String()})
// MaxPeers
ss = append(ss, printable{0, "Max peers", stackConfig.MaxPeers})
// MaxPendingPeers
ss = append(ss, printable{0, "Max pending peers", stackConfig.MaxPendingPeers})
// HTTP
ss = append(ss, printable{0, "HTTP", nil})
// HTTPHost
ss = append(ss, printable{1, "host", stackConfig.HTTPHost})
// HTTPPort
ss = append(ss, printable{1, "port", stackConfig.HTTPPort})
// HTTPCors
ss = append(ss, printable{1, "CORS", stackConfig.HTTPCors})
// HTTPModules[]
ss = append(ss, printable{1, "modules", stackConfig.HTTPModules})
// Endpoint()
ss = append(ss, printable{1, "endpoint", stackConfig.HTTPEndpoint()})
// WS
ss = append(ss, printable{0, "WS", nil})
// WSHost
ss = append(ss, printable{1, "host", stackConfig.WSHost})
// WSPort
ss = append(ss, printable{1, "port", stackConfig.WSPort})
// WSOrigins
ss = append(ss, printable{1, "origins", stackConfig.WSOrigins})
// WSModules[]
ss = append(ss, printable{1, "modules", stackConfig.WSModules})
// Endpoint()
ss = append(ss, printable{1, "endpoint", stackConfig.WSEndpoint()})
for _, v := range ss {
if v.val != nil {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key, v.val))
} else {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key))
}
}
return s
}
func formatBlockPretty(b *types.Block) (ss []printable) {
bh := b.Header()
ss = append(ss, printable{1, "Number", bh.Number})
ss = append(ss, printable{1, "Hash (hex)", bh.Hash().Hex()})
ss = append(ss, printable{1, "Parent hash (hex)", bh.ParentHash.Hex()})
ss = append(ss, printable{1, "Nonce (uint64)", bh.Nonce.Uint64()})
ss = append(ss, printable{1, "Time", fmt.Sprintf("%v (%v)", bh.Time, time.Unix(int64(bh.Time.Uint64()), 0))})
ss = append(ss, printable{1, "Difficulty", bh.Difficulty})
ss = append(ss, printable{1, "Coinbase", bh.Coinbase.Hex()})
ss = append(ss, printable{1, "Tx hash (hex)", bh.TxHash.Hex()})
ss = append(ss, printable{1, "Bloom (string)", string(bh.Bloom.Bytes())})
ss = append(ss, printable{1, "Gas limit", bh.GasLimit})
ss = append(ss, printable{1, "Gas used", bh.GasUsed})
ss = append(ss, printable{1, "Extra data (bytes)", bh.Extra})
return ss
}
func formatChainDataPretty(datadir string, chain *core.BlockChain) (s []string) {
ss := []printable{}
ss = append(ss, printable{0, "Chain data dir", datadir})
ss = append(ss, printable{0, "Genesis", nil})
ss = append(ss, formatBlockPretty(chain.Genesis())...)
currentBlock := chain.CurrentBlock()
ss = append(ss, printable{0, "Current block", nil})
ss = append(ss, formatBlockPretty(currentBlock)...)
currentFastBlock := chain.CurrentFastBlock()
ss = append(ss, printable{0, "Current fast block", nil})
ss = append(ss, formatBlockPretty(currentFastBlock)...)
for _, v := range ss {
if v.val != nil {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key, v.val))
} else {
s = append(s, formatStatusKeyValue(strings.Repeat(indent, v.indent), v.key))
}
}
return s
}