Skip to content

Commit 86f9e83

Browse files
authored
cmd/geth: tidied up the source (bnb-chain#3385)
cmd/geth: tidied up the source
1 parent a90a170 commit 86f9e83

3 files changed

Lines changed: 174 additions & 133 deletions

File tree

‎cmd/geth/chaincmd.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,26 @@ import (
3232
"github.com/ethereum/go-ethereum/core/state"
3333
"github.com/ethereum/go-ethereum/core/types"
3434
"github.com/ethereum/go-ethereum/ethdb"
35+
"github.com/ethereum/go-ethereum/logger"
3536
"github.com/ethereum/go-ethereum/logger/glog"
3637
"github.com/ethereum/go-ethereum/trie"
3738
"github.com/syndtr/goleveldb/leveldb/util"
3839
"gopkg.in/urfave/cli.v1"
3940
)
4041

4142
var (
43+
initCommand = cli.Command{
44+
Action: initGenesis,
45+
Name: "init",
46+
Usage: "Bootstrap and initialize a new genesis block",
47+
ArgsUsage: "<genesisPath>",
48+
Category: "BLOCKCHAIN COMMANDS",
49+
Description: `
50+
The init command initializes a new genesis block and definition for the network.
51+
This is a destructive action and changes the network in which you will be
52+
participating.
53+
`,
54+
}
4255
importCommand = cli.Command{
4356
Action: importChain,
4457
Name: "import",
@@ -95,6 +108,30 @@ Use "ethereum dump 0" to dump the genesis block.
95108
}
96109
)
97110

111+
// initGenesis will initialise the given JSON format genesis file and writes it as
112+
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
113+
func initGenesis(ctx *cli.Context) error {
114+
genesisPath := ctx.Args().First()
115+
if len(genesisPath) == 0 {
116+
utils.Fatalf("must supply path to genesis JSON file")
117+
}
118+
119+
stack := makeFullNode(ctx)
120+
chaindb := utils.MakeChainDatabase(ctx, stack)
121+
122+
genesisFile, err := os.Open(genesisPath)
123+
if err != nil {
124+
utils.Fatalf("failed to read genesis file: %v", err)
125+
}
126+
127+
block, err := core.WriteGenesisBlock(chaindb, genesisFile)
128+
if err != nil {
129+
utils.Fatalf("failed to write genesis block: %v", err)
130+
}
131+
glog.V(logger.Info).Infof("successfully wrote genesis block and/or chain rule set: %x", block.Hash())
132+
return nil
133+
}
134+
98135
func importChain(ctx *cli.Context) error {
99136
if len(ctx.Args()) != 1 {
100137
utils.Fatalf("This command requires an argument.")

‎cmd/geth/main.go‎

Lines changed: 9 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@ package main
2020
import (
2121
"encoding/hex"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
25-
"path/filepath"
2624
"runtime"
27-
"strconv"
2825
"strings"
2926
"time"
3027

31-
"github.com/ethereum/ethash"
3228
"github.com/ethereum/go-ethereum/cmd/utils"
3329
"github.com/ethereum/go-ethereum/common"
3430
"github.com/ethereum/go-ethereum/console"
3531
"github.com/ethereum/go-ethereum/contracts/release"
36-
"github.com/ethereum/go-ethereum/core"
3732
"github.com/ethereum/go-ethereum/eth"
3833
"github.com/ethereum/go-ethereum/internal/debug"
3934
"github.com/ethereum/go-ethereum/logger"
@@ -64,59 +59,26 @@ func init() {
6459
app.HideVersion = true // we have a command to print the version
6560
app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
6661
app.Commands = []cli.Command{
62+
// See chaincmd.go:
63+
initCommand,
6764
importCommand,
6865
exportCommand,
6966
upgradedbCommand,
7067
removedbCommand,
7168
dumpCommand,
69+
// See monitorcmd.go:
7270
monitorCommand,
71+
// See accountcmd.go:
7372
accountCommand,
7473
walletCommand,
74+
// See consolecmd.go:
7575
consoleCommand,
7676
attachCommand,
7777
javascriptCommand,
78-
{
79-
Action: makedag,
80-
Name: "makedag",
81-
Usage: "Generate ethash DAG (for testing)",
82-
ArgsUsage: "<blockNum> <outputDir>",
83-
Category: "MISCELLANEOUS COMMANDS",
84-
Description: `
85-
The makedag command generates an ethash DAG in /tmp/dag.
86-
87-
This command exists to support the system testing project.
88-
Regular users do not need to execute it.
89-
`,
90-
},
91-
{
92-
Action: version,
93-
Name: "version",
94-
Usage: "Print version numbers",
95-
ArgsUsage: " ",
96-
Category: "MISCELLANEOUS COMMANDS",
97-
Description: `
98-
The output of this command is supposed to be machine-readable.
99-
`,
100-
},
101-
{
102-
Action: initGenesis,
103-
Name: "init",
104-
Usage: "Bootstrap and initialize a new genesis block",
105-
ArgsUsage: "<genesisPath>",
106-
Category: "BLOCKCHAIN COMMANDS",
107-
Description: `
108-
The init command initializes a new genesis block and definition for the network.
109-
This is a destructive action and changes the network in which you will be
110-
participating.
111-
`,
112-
},
113-
{
114-
Action: license,
115-
Name: "license",
116-
Usage: "Display license information",
117-
ArgsUsage: " ",
118-
Category: "MISCELLANEOUS COMMANDS",
119-
},
78+
// See misccmd.go:
79+
makedagCommand,
80+
versionCommand,
81+
licenseCommand,
12082
}
12183

12284
app.Flags = []cli.Flag{
@@ -230,30 +192,6 @@ func geth(ctx *cli.Context) error {
230192
return nil
231193
}
232194

233-
// initGenesis will initialise the given JSON format genesis file and writes it as
234-
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
235-
func initGenesis(ctx *cli.Context) error {
236-
genesisPath := ctx.Args().First()
237-
if len(genesisPath) == 0 {
238-
utils.Fatalf("must supply path to genesis JSON file")
239-
}
240-
241-
stack := makeFullNode(ctx)
242-
chaindb := utils.MakeChainDatabase(ctx, stack)
243-
244-
genesisFile, err := os.Open(genesisPath)
245-
if err != nil {
246-
utils.Fatalf("failed to read genesis file: %v", err)
247-
}
248-
249-
block, err := core.WriteGenesisBlock(chaindb, genesisFile)
250-
if err != nil {
251-
utils.Fatalf("failed to write genesis block: %v", err)
252-
}
253-
glog.V(logger.Info).Infof("successfully wrote genesis block and/or chain rule set: %x", block.Hash())
254-
return nil
255-
}
256-
257195
func makeFullNode(ctx *cli.Context) *node.Node {
258196
// Create the default extradata and construct the base node
259197
var clientInfo = struct {
@@ -328,65 +266,3 @@ func startNode(ctx *cli.Context, stack *node.Node) {
328266
}
329267
}
330268
}
331-
332-
func makedag(ctx *cli.Context) error {
333-
args := ctx.Args()
334-
wrongArgs := func() {
335-
utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
336-
}
337-
switch {
338-
case len(args) == 2:
339-
blockNum, err := strconv.ParseUint(args[0], 0, 64)
340-
dir := args[1]
341-
if err != nil {
342-
wrongArgs()
343-
} else {
344-
dir = filepath.Clean(dir)
345-
// seems to require a trailing slash
346-
if !strings.HasSuffix(dir, "/") {
347-
dir = dir + "/"
348-
}
349-
_, err = ioutil.ReadDir(dir)
350-
if err != nil {
351-
utils.Fatalf("Can't find dir")
352-
}
353-
fmt.Println("making DAG, this could take awhile...")
354-
ethash.MakeDAG(blockNum, dir)
355-
}
356-
default:
357-
wrongArgs()
358-
}
359-
return nil
360-
}
361-
362-
func version(ctx *cli.Context) error {
363-
fmt.Println(strings.Title(clientIdentifier))
364-
fmt.Println("Version:", params.Version)
365-
if gitCommit != "" {
366-
fmt.Println("Git Commit:", gitCommit)
367-
}
368-
fmt.Println("Protocol Versions:", eth.ProtocolVersions)
369-
fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
370-
fmt.Println("Go Version:", runtime.Version())
371-
fmt.Println("OS:", runtime.GOOS)
372-
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
373-
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
374-
return nil
375-
}
376-
377-
func license(_ *cli.Context) error {
378-
fmt.Println(`Geth is free software: you can redistribute it and/or modify
379-
it under the terms of the GNU General Public License as published by
380-
the Free Software Foundation, either version 3 of the License, or
381-
(at your option) any later version.
382-
383-
Geth is distributed in the hope that it will be useful,
384-
but WITHOUT ANY WARRANTY; without even the implied warranty of
385-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
386-
GNU General Public License for more details.
387-
388-
You should have received a copy of the GNU General Public License
389-
along with geth. If not, see <http://www.gnu.org/licenses/>.
390-
`)
391-
return nil
392-
}

‎cmd/geth/misccmd.go‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"path/filepath"
24+
"runtime"
25+
"strconv"
26+
"strings"
27+
28+
"github.com/ethereum/ethash"
29+
"github.com/ethereum/go-ethereum/cmd/utils"
30+
"github.com/ethereum/go-ethereum/eth"
31+
"github.com/ethereum/go-ethereum/params"
32+
"gopkg.in/urfave/cli.v1"
33+
)
34+
35+
var (
36+
makedagCommand = cli.Command{
37+
Action: makedag,
38+
Name: "makedag",
39+
Usage: "Generate ethash DAG (for testing)",
40+
ArgsUsage: "<blockNum> <outputDir>",
41+
Category: "MISCELLANEOUS COMMANDS",
42+
Description: `
43+
The makedag command generates an ethash DAG in /tmp/dag.
44+
45+
This command exists to support the system testing project.
46+
Regular users do not need to execute it.
47+
`,
48+
}
49+
versionCommand = cli.Command{
50+
Action: version,
51+
Name: "version",
52+
Usage: "Print version numbers",
53+
ArgsUsage: " ",
54+
Category: "MISCELLANEOUS COMMANDS",
55+
Description: `
56+
The output of this command is supposed to be machine-readable.
57+
`,
58+
}
59+
licenseCommand = cli.Command{
60+
Action: license,
61+
Name: "license",
62+
Usage: "Display license information",
63+
ArgsUsage: " ",
64+
Category: "MISCELLANEOUS COMMANDS",
65+
}
66+
)
67+
68+
func makedag(ctx *cli.Context) error {
69+
args := ctx.Args()
70+
wrongArgs := func() {
71+
utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
72+
}
73+
switch {
74+
case len(args) == 2:
75+
blockNum, err := strconv.ParseUint(args[0], 0, 64)
76+
dir := args[1]
77+
if err != nil {
78+
wrongArgs()
79+
} else {
80+
dir = filepath.Clean(dir)
81+
// seems to require a trailing slash
82+
if !strings.HasSuffix(dir, "/") {
83+
dir = dir + "/"
84+
}
85+
_, err = ioutil.ReadDir(dir)
86+
if err != nil {
87+
utils.Fatalf("Can't find dir")
88+
}
89+
fmt.Println("making DAG, this could take awhile...")
90+
ethash.MakeDAG(blockNum, dir)
91+
}
92+
default:
93+
wrongArgs()
94+
}
95+
return nil
96+
}
97+
98+
func version(ctx *cli.Context) error {
99+
fmt.Println(strings.Title(clientIdentifier))
100+
fmt.Println("Version:", params.Version)
101+
if gitCommit != "" {
102+
fmt.Println("Git Commit:", gitCommit)
103+
}
104+
fmt.Println("Protocol Versions:", eth.ProtocolVersions)
105+
fmt.Println("Network Id:", ctx.GlobalInt(utils.NetworkIdFlag.Name))
106+
fmt.Println("Go Version:", runtime.Version())
107+
fmt.Println("OS:", runtime.GOOS)
108+
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
109+
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
110+
return nil
111+
}
112+
113+
func license(_ *cli.Context) error {
114+
fmt.Println(`Geth is free software: you can redistribute it and/or modify
115+
it under the terms of the GNU General Public License as published by
116+
the Free Software Foundation, either version 3 of the License, or
117+
(at your option) any later version.
118+
119+
Geth is distributed in the hope that it will be useful,
120+
but WITHOUT ANY WARRANTY; without even the implied warranty of
121+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
122+
GNU General Public License for more details.
123+
124+
You should have received a copy of the GNU General Public License
125+
along with geth. If not, see <http://www.gnu.org/licenses/>.
126+
`)
127+
return nil
128+
}

0 commit comments

Comments
 (0)