Skip to content

Commit 44bc2e8

Browse files
authored
Merge pull request bnb-chain#2914 from fjl/node-coinhabit
cmd/utils, node: make datadir reusable for bzzd
2 parents 4e8cec0 + b42a5b1 commit 44bc2e8

19 files changed

Lines changed: 470 additions & 239 deletions

File tree

‎cmd/geth/chaincmd.go‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func importChain(ctx *cli.Context) error {
7979
if ctx.GlobalBool(utils.TestNetFlag.Name) {
8080
state.StartingNonce = 1048576 // (2**20)
8181
}
82-
chain, chainDb := utils.MakeChain(ctx)
82+
stack := makeFullNode(ctx)
83+
chain, chainDb := utils.MakeChain(ctx, stack)
8384
start := time.Now()
8485
err := utils.ImportChain(chain, ctx.Args().First())
8586
chainDb.Close()
@@ -94,7 +95,8 @@ func exportChain(ctx *cli.Context) error {
9495
if len(ctx.Args()) < 1 {
9596
utils.Fatalf("This command requires an argument.")
9697
}
97-
chain, _ := utils.MakeChain(ctx)
98+
stack := makeFullNode(ctx)
99+
chain, _ := utils.MakeChain(ctx, stack)
98100
start := time.Now()
99101

100102
var err error
@@ -122,28 +124,34 @@ func exportChain(ctx *cli.Context) error {
122124
}
123125

124126
func removeDB(ctx *cli.Context) error {
125-
confirm, err := console.Stdin.PromptConfirm("Remove local database?")
126-
if err != nil {
127-
utils.Fatalf("%v", err)
127+
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
128+
dbdir := stack.ResolvePath("chaindata")
129+
if !common.FileExist(dbdir) {
130+
fmt.Println(dbdir, "does not exist")
131+
return nil
128132
}
129133

130-
if confirm {
131-
fmt.Println("Removing chaindata...")
134+
fmt.Println(dbdir)
135+
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
136+
switch {
137+
case err != nil:
138+
utils.Fatalf("%v", err)
139+
case !confirm:
140+
fmt.Println("Operation aborted")
141+
default:
142+
fmt.Println("Removing...")
132143
start := time.Now()
133-
134-
os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
135-
144+
os.RemoveAll(dbdir)
136145
fmt.Printf("Removed in %v\n", time.Since(start))
137-
} else {
138-
fmt.Println("Operation aborted")
139146
}
140147
return nil
141148
}
142149

143150
func upgradeDB(ctx *cli.Context) error {
144151
glog.Infoln("Upgrading blockchain database")
145152

146-
chain, chainDb := utils.MakeChain(ctx)
153+
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
154+
chain, chainDb := utils.MakeChain(ctx, stack)
147155
bcVersion := core.GetBlockChainVersion(chainDb)
148156
if bcVersion == 0 {
149157
bcVersion = core.BlockChainVersion
@@ -156,10 +164,12 @@ func upgradeDB(ctx *cli.Context) error {
156164
utils.Fatalf("Unable to export chain for reimport %s", err)
157165
}
158166
chainDb.Close()
159-
os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
167+
if dir := dbDirectory(chainDb); dir != "" {
168+
os.RemoveAll(dir)
169+
}
160170

161171
// Import the chain file.
162-
chain, chainDb = utils.MakeChain(ctx)
172+
chain, chainDb = utils.MakeChain(ctx, stack)
163173
core.WriteBlockChainVersion(chainDb, core.BlockChainVersion)
164174
err := utils.ImportChain(chain, exportFile)
165175
chainDb.Close()
@@ -172,8 +182,17 @@ func upgradeDB(ctx *cli.Context) error {
172182
return nil
173183
}
174184

185+
func dbDirectory(db ethdb.Database) string {
186+
ldb, ok := db.(*ethdb.LDBDatabase)
187+
if !ok {
188+
return ""
189+
}
190+
return ldb.Path()
191+
}
192+
175193
func dump(ctx *cli.Context) error {
176-
chain, chainDb := utils.MakeChain(ctx)
194+
stack := makeFullNode(ctx)
195+
chain, chainDb := utils.MakeChain(ctx, stack)
177196
for _, arg := range ctx.Args() {
178197
var block *types.Block
179198
if hashish(arg) {

‎cmd/geth/consolecmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func remoteConsole(ctx *cli.Context) error {
107107
utils.Fatalf("Unable to attach to remote geth: %v", err)
108108
}
109109
config := console.Config{
110-
DataDir: utils.MustMakeDataDir(ctx),
110+
DataDir: utils.MakeDataDir(ctx),
111111
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
112112
Client: client,
113113
Preload: utils.MakeConsolePreloads(ctx),
@@ -135,7 +135,7 @@ func remoteConsole(ctx *cli.Context) error {
135135
// for "geth attach" and "geth monitor" with no argument.
136136
func dialRPC(endpoint string) (*rpc.Client, error) {
137137
if endpoint == "" {
138-
endpoint = node.DefaultIPCEndpoint()
138+
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
139139
} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
140140
// Backwards compatibility with geth < 1.5 which required
141141
// these prefixes.

‎cmd/geth/dao_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ func testDAOForkBlockNewChain(t *testing.T, testnet bool, genesis string, votes
195195
geth.cmd.Wait()
196196
}
197197
// Retrieve the DAO config flag from the database
198-
path := filepath.Join(datadir, "chaindata")
198+
path := filepath.Join(datadir, "geth", "chaindata")
199199
if testnet && genesis == "" {
200-
path = filepath.Join(datadir, "testnet", "chaindata")
200+
path = filepath.Join(datadir, "testnet", "geth", "chaindata")
201201
}
202202
db, err := ethdb.NewLDBDatabase(path, 0, 0)
203203
if err != nil {

‎cmd/geth/main.go‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"github.com/ethereum/go-ethereum/core"
3737
"github.com/ethereum/go-ethereum/core/state"
3838
"github.com/ethereum/go-ethereum/eth"
39-
"github.com/ethereum/go-ethereum/ethdb"
4039
"github.com/ethereum/go-ethereum/internal/debug"
4140
"github.com/ethereum/go-ethereum/logger"
4241
"github.com/ethereum/go-ethereum/logger/glog"
@@ -46,7 +45,7 @@ import (
4645
)
4746

4847
const (
49-
clientIdentifier = "Geth" // Client identifier to advertise over the network
48+
clientIdentifier = "geth" // Client identifier to advertise over the network
5049
)
5150

5251
var (
@@ -245,17 +244,15 @@ func initGenesis(ctx *cli.Context) error {
245244
state.StartingNonce = 1048576 // (2**20)
246245
}
247246

248-
chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), "chaindata"), 0, 0)
249-
if err != nil {
250-
utils.Fatalf("could not open database: %v", err)
251-
}
247+
stack := makeFullNode(ctx)
248+
chaindb := utils.MakeChainDatabase(ctx, stack)
252249

253250
genesisFile, err := os.Open(genesisPath)
254251
if err != nil {
255252
utils.Fatalf("failed to read genesis file: %v", err)
256253
}
257254

258-
block, err := core.WriteGenesisBlock(chainDb, genesisFile)
255+
block, err := core.WriteGenesisBlock(chaindb, genesisFile)
259256
if err != nil {
260257
utils.Fatalf("failed to write genesis block: %v", err)
261258
}
@@ -296,9 +293,6 @@ func makeFullNode(ctx *cli.Context) *node.Node {
296293
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
297294
// miner.
298295
func startNode(ctx *cli.Context, stack *node.Node) {
299-
// Report geth version
300-
glog.V(logger.Info).Infof("instance: Geth/%s/%s/%s\n", utils.Version, runtime.Version(), runtime.GOOS)
301-
302296
// Start up the node itself
303297
utils.StartNode(stack)
304298

@@ -379,7 +373,7 @@ func gpubench(ctx *cli.Context) error {
379373
}
380374

381375
func version(c *cli.Context) error {
382-
fmt.Println(clientIdentifier)
376+
fmt.Println(strings.Title(clientIdentifier))
383377
fmt.Println("Version:", utils.Version)
384378
if gitCommit != "" {
385379
fmt.Println("Git Commit:", gitCommit)

‎cmd/geth/monitorcmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
var (
3636
monitorCommandAttachFlag = cli.StringFlag{
3737
Name: "attach",
38-
Value: node.DefaultIPCEndpoint(),
38+
Value: node.DefaultIPCEndpoint(clientIdentifier),
3939
Usage: "API endpoint to attach to",
4040
}
4141
monitorCommandRowsFlag = cli.IntFlag{

‎cmd/gethrpctest/main.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"os"
2424
"os/signal"
2525

26-
"github.com/ethereum/go-ethereum/common"
2726
"github.com/ethereum/go-ethereum/core"
2827
"github.com/ethereum/go-ethereum/crypto"
2928
"github.com/ethereum/go-ethereum/eth"
@@ -88,12 +87,12 @@ func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
8887
// Create a networkless protocol stack
8988
stack, err := node.New(&node.Config{
9089
UseLightweightKDF: true,
91-
IPCPath: node.DefaultIPCEndpoint(),
92-
HTTPHost: common.DefaultHTTPHost,
93-
HTTPPort: common.DefaultHTTPPort,
90+
IPCPath: node.DefaultIPCEndpoint(""),
91+
HTTPHost: node.DefaultHTTPHost,
92+
HTTPPort: node.DefaultHTTPPort,
9493
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
95-
WSHost: common.DefaultWSHost,
96-
WSPort: common.DefaultWSPort,
94+
WSHost: node.DefaultWSHost,
95+
WSPort: node.DefaultWSPort,
9796
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
9897
NoDiscovery: true,
9998
})

‎cmd/utils/customflags.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,19 @@ func (self *DirectoryFlag) Set(value string) {
137137
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
138138
func expandPath(p string) string {
139139
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
140-
if user, err := user.Current(); err == nil {
141-
p = user.HomeDir + p[1:]
140+
if home := homeDir(); home != "" {
141+
p = home + p[1:]
142142
}
143143
}
144144
return path.Clean(os.ExpandEnv(p))
145145
}
146+
147+
func homeDir() string {
148+
if home := os.Getenv("HOME"); home != "" {
149+
return home
150+
}
151+
if usr, err := user.Current(); err == nil {
152+
return usr.HomeDir
153+
}
154+
return ""
155+
}

0 commit comments

Comments
 (0)