-
Notifications
You must be signed in to change notification settings - Fork 203
/
chaincmd.go
126 lines (109 loc) · 2.96 KB
/
chaincmd.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
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library 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.
//
// the go-nebulas library 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 the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"strconv"
"bytes"
"encoding/json"
"github.com/nebulasio/go-nebulas/core"
"github.com/urfave/cli"
)
var (
initCommand = cli.Command{
Action: MergeFlags(initGenesis),
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Category: "BLOCKCHAIN COMMANDS",
Description: `
The init command initializes a new genesis block and definition for the network.`,
}
genesisCommand = cli.Command{
Name: "genesis",
Usage: "the genesis block command",
Category: "BLOCKCHAIN COMMANDS",
Description: `
The genesis command for genesis dump or other commands.`,
Subcommands: []cli.Command{
{
Name: "dump",
Usage: "dump the genesis",
Action: MergeFlags(dumpGenesis),
Description: `
genesis dump
Dump the genesis config info.`,
},
},
}
blockDumpCommand = cli.Command{
Action: MergeFlags(dumpblock),
Name: "dump",
Usage: "Dump the number of newest block before tail block from storage",
ArgsUsage: "<blocknumber>",
Category: "BLOCKCHAIN COMMANDS",
Description: `
Use "./neb dump 10" to dump 10 blocks before tail block.`,
}
)
func initGenesis(ctx *cli.Context) error {
filePath := ctx.Args().First()
genesis, err := core.LoadGenesisConf(filePath)
if err != nil {
FatalF("load genesis conf faild: %v", err)
}
neb, err := makeNeb(ctx)
if err != nil {
return err
}
neb.SetGenesis(genesis)
neb.Setup()
return nil
}
func dumpGenesis(ctx *cli.Context) error {
neb, err := makeNeb(ctx)
if err != nil {
FatalF("dump genesis conf faild: %v", err)
}
neb.Setup()
genesis, err := core.DumpGenesis(neb.BlockChain())
if err != nil {
FatalF("dump genesis conf faild: %v", err)
}
genesisJSON, err := json.Marshal(genesis)
var buf bytes.Buffer
err = json.Indent(&buf, genesisJSON, "", " ")
if err != nil {
FatalF("dump genesis conf faild: %v", err)
}
fmt.Println(buf.String())
return nil
}
func dumpblock(ctx *cli.Context) error {
neb, err := makeNeb(ctx)
if err != nil {
return err
}
neb.Setup()
count, err := strconv.Atoi(ctx.Args().First())
if err != nil {
return err
}
fmt.Printf("blockchain dump: %s\n", neb.BlockChain().Dump(count))
return nil
}