aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-09-29 20:24:37 +0800
committerGitHub <noreply@github.com>2016-09-29 20:24:37 +0800
commit44bc2e80ddf82ea2a37c25e7beb0ca06b46b7308 (patch)
treeaaab07e78f7b9341f0fcfbd9348fe96273bd6a0d /cmd
parent4e8cec05abb567457e80185cb9fcf7ec2cc90596 (diff)
parentb42a5b118f1aa7ac1235547c8594146978941401 (diff)
downloaddexon-44bc2e80ddf82ea2a37c25e7beb0ca06b46b7308.tar.gz
dexon-44bc2e80ddf82ea2a37c25e7beb0ca06b46b7308.tar.zst
dexon-44bc2e80ddf82ea2a37c25e7beb0ca06b46b7308.zip
Merge pull request #2914 from fjl/node-coinhabit
cmd/utils, node: make datadir reusable for bzzd
Diffstat (limited to 'cmd')
-rw-r--r--cmd/geth/chaincmd.go51
-rw-r--r--cmd/geth/consolecmd.go4
-rw-r--r--cmd/geth/dao_test.go4
-rw-r--r--cmd/geth/main.go16
-rw-r--r--cmd/geth/monitorcmd.go2
-rw-r--r--cmd/gethrpctest/main.go11
-rw-r--r--cmd/utils/customflags.go14
-rw-r--r--cmd/utils/flags.go66
8 files changed, 96 insertions, 72 deletions
diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go
index 54984d6e0..553e5367c 100644
--- a/cmd/geth/chaincmd.go
+++ b/cmd/geth/chaincmd.go
@@ -79,7 +79,8 @@ func importChain(ctx *cli.Context) error {
if ctx.GlobalBool(utils.TestNetFlag.Name) {
state.StartingNonce = 1048576 // (2**20)
}
- chain, chainDb := utils.MakeChain(ctx)
+ stack := makeFullNode(ctx)
+ chain, chainDb := utils.MakeChain(ctx, stack)
start := time.Now()
err := utils.ImportChain(chain, ctx.Args().First())
chainDb.Close()
@@ -94,7 +95,8 @@ func exportChain(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
- chain, _ := utils.MakeChain(ctx)
+ stack := makeFullNode(ctx)
+ chain, _ := utils.MakeChain(ctx, stack)
start := time.Now()
var err error
@@ -122,20 +124,25 @@ func exportChain(ctx *cli.Context) error {
}
func removeDB(ctx *cli.Context) error {
- confirm, err := console.Stdin.PromptConfirm("Remove local database?")
- if err != nil {
- utils.Fatalf("%v", err)
+ stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
+ dbdir := stack.ResolvePath("chaindata")
+ if !common.FileExist(dbdir) {
+ fmt.Println(dbdir, "does not exist")
+ return nil
}
- if confirm {
- fmt.Println("Removing chaindata...")
+ fmt.Println(dbdir)
+ confirm, err := console.Stdin.PromptConfirm("Remove this database?")
+ switch {
+ case err != nil:
+ utils.Fatalf("%v", err)
+ case !confirm:
+ fmt.Println("Operation aborted")
+ default:
+ fmt.Println("Removing...")
start := time.Now()
-
- os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
-
+ os.RemoveAll(dbdir)
fmt.Printf("Removed in %v\n", time.Since(start))
- } else {
- fmt.Println("Operation aborted")
}
return nil
}
@@ -143,7 +150,8 @@ func removeDB(ctx *cli.Context) error {
func upgradeDB(ctx *cli.Context) error {
glog.Infoln("Upgrading blockchain database")
- chain, chainDb := utils.MakeChain(ctx)
+ stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
+ chain, chainDb := utils.MakeChain(ctx, stack)
bcVersion := core.GetBlockChainVersion(chainDb)
if bcVersion == 0 {
bcVersion = core.BlockChainVersion
@@ -156,10 +164,12 @@ func upgradeDB(ctx *cli.Context) error {
utils.Fatalf("Unable to export chain for reimport %s", err)
}
chainDb.Close()
- os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata"))
+ if dir := dbDirectory(chainDb); dir != "" {
+ os.RemoveAll(dir)
+ }
// Import the chain file.
- chain, chainDb = utils.MakeChain(ctx)
+ chain, chainDb = utils.MakeChain(ctx, stack)
core.WriteBlockChainVersion(chainDb, core.BlockChainVersion)
err := utils.ImportChain(chain, exportFile)
chainDb.Close()
@@ -172,8 +182,17 @@ func upgradeDB(ctx *cli.Context) error {
return nil
}
+func dbDirectory(db ethdb.Database) string {
+ ldb, ok := db.(*ethdb.LDBDatabase)
+ if !ok {
+ return ""
+ }
+ return ldb.Path()
+}
+
func dump(ctx *cli.Context) error {
- chain, chainDb := utils.MakeChain(ctx)
+ stack := makeFullNode(ctx)
+ chain, chainDb := utils.MakeChain(ctx, stack)
for _, arg := range ctx.Args() {
var block *types.Block
if hashish(arg) {
diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go
index 92d6f7f86..066247303 100644
--- a/cmd/geth/consolecmd.go
+++ b/cmd/geth/consolecmd.go
@@ -107,7 +107,7 @@ func remoteConsole(ctx *cli.Context) error {
utils.Fatalf("Unable to attach to remote geth: %v", err)
}
config := console.Config{
- DataDir: utils.MustMakeDataDir(ctx),
+ DataDir: utils.MakeDataDir(ctx),
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
Client: client,
Preload: utils.MakeConsolePreloads(ctx),
@@ -135,7 +135,7 @@ func remoteConsole(ctx *cli.Context) error {
// for "geth attach" and "geth monitor" with no argument.
func dialRPC(endpoint string) (*rpc.Client, error) {
if endpoint == "" {
- endpoint = node.DefaultIPCEndpoint()
+ endpoint = node.DefaultIPCEndpoint(clientIdentifier)
} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
// Backwards compatibility with geth < 1.5 which required
// these prefixes.
diff --git a/cmd/geth/dao_test.go b/cmd/geth/dao_test.go
index 7058fb385..59730b17f 100644
--- a/cmd/geth/dao_test.go
+++ b/cmd/geth/dao_test.go
@@ -195,9 +195,9 @@ func testDAOForkBlockNewChain(t *testing.T, testnet bool, genesis string, votes
geth.cmd.Wait()
}
// Retrieve the DAO config flag from the database
- path := filepath.Join(datadir, "chaindata")
+ path := filepath.Join(datadir, "geth", "chaindata")
if testnet && genesis == "" {
- path = filepath.Join(datadir, "testnet", "chaindata")
+ path = filepath.Join(datadir, "testnet", "geth", "chaindata")
}
db, err := ethdb.NewLDBDatabase(path, 0, 0)
if err != nil {
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index a7b332d0f..65311ca41 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -36,7 +36,6 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/eth"
- "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
@@ -46,7 +45,7 @@ import (
)
const (
- clientIdentifier = "Geth" // Client identifier to advertise over the network
+ clientIdentifier = "geth" // Client identifier to advertise over the network
)
var (
@@ -245,17 +244,15 @@ func initGenesis(ctx *cli.Context) error {
state.StartingNonce = 1048576 // (2**20)
}
- chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), "chaindata"), 0, 0)
- if err != nil {
- utils.Fatalf("could not open database: %v", err)
- }
+ stack := makeFullNode(ctx)
+ chaindb := utils.MakeChainDatabase(ctx, stack)
genesisFile, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("failed to read genesis file: %v", err)
}
- block, err := core.WriteGenesisBlock(chainDb, genesisFile)
+ block, err := core.WriteGenesisBlock(chaindb, genesisFile)
if err != nil {
utils.Fatalf("failed to write genesis block: %v", err)
}
@@ -296,9 +293,6 @@ func makeFullNode(ctx *cli.Context) *node.Node {
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
// miner.
func startNode(ctx *cli.Context, stack *node.Node) {
- // Report geth version
- glog.V(logger.Info).Infof("instance: Geth/%s/%s/%s\n", utils.Version, runtime.Version(), runtime.GOOS)
-
// Start up the node itself
utils.StartNode(stack)
@@ -379,7 +373,7 @@ func gpubench(ctx *cli.Context) error {
}
func version(c *cli.Context) error {
- fmt.Println(clientIdentifier)
+ fmt.Println(strings.Title(clientIdentifier))
fmt.Println("Version:", utils.Version)
if gitCommit != "" {
fmt.Println("Git Commit:", gitCommit)
diff --git a/cmd/geth/monitorcmd.go b/cmd/geth/monitorcmd.go
index d1490dce2..b74315dab 100644
--- a/cmd/geth/monitorcmd.go
+++ b/cmd/geth/monitorcmd.go
@@ -35,7 +35,7 @@ import (
var (
monitorCommandAttachFlag = cli.StringFlag{
Name: "attach",
- Value: node.DefaultIPCEndpoint(),
+ Value: node.DefaultIPCEndpoint(clientIdentifier),
Usage: "API endpoint to attach to",
}
monitorCommandRowsFlag = cli.IntFlag{
diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go
index d267dbf58..d0d6e1618 100644
--- a/cmd/gethrpctest/main.go
+++ b/cmd/gethrpctest/main.go
@@ -23,7 +23,6 @@ import (
"os"
"os/signal"
- "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
@@ -88,12 +87,12 @@ func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
// Create a networkless protocol stack
stack, err := node.New(&node.Config{
UseLightweightKDF: true,
- IPCPath: node.DefaultIPCEndpoint(),
- HTTPHost: common.DefaultHTTPHost,
- HTTPPort: common.DefaultHTTPPort,
+ IPCPath: node.DefaultIPCEndpoint(""),
+ HTTPHost: node.DefaultHTTPHost,
+ HTTPPort: node.DefaultHTTPPort,
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
- WSHost: common.DefaultWSHost,
- WSPort: common.DefaultWSPort,
+ WSHost: node.DefaultWSHost,
+ WSPort: node.DefaultWSPort,
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
NoDiscovery: true,
})
diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go
index 5cbccfe98..11c92d451 100644
--- a/cmd/utils/customflags.go
+++ b/cmd/utils/customflags.go
@@ -137,9 +137,19 @@ func (self *DirectoryFlag) Set(value string) {
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func expandPath(p string) string {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
- if user, err := user.Current(); err == nil {
- p = user.HomeDir + p[1:]
+ if home := homeDir(); home != "" {
+ p = home + p[1:]
}
}
return path.Clean(os.ExpandEnv(p))
}
+
+func homeDir() string {
+ if home := os.Getenv("HOME"); home != "" {
+ return home
+ }
+ if usr, err := user.Current(); err == nil {
+ return usr.HomeDir
+ }
+ return ""
+}
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 3ab556a8f..0be499c5b 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -105,7 +105,7 @@ var (
DataDirFlag = DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases and keystore",
- Value: DirectoryString{common.DefaultDataDir()},
+ Value: DirectoryString{node.DefaultDataDir()},
}
KeyStoreDirFlag = DirectoryFlag{
Name: "keystore",
@@ -139,7 +139,7 @@ var (
DocRootFlag = DirectoryFlag{
Name: "docroot",
Usage: "Document Root for HTTPClient file scheme",
- Value: DirectoryString{common.HomeDir()},
+ Value: DirectoryString{homeDir()},
}
CacheFlag = cli.IntFlag{
Name: "cache",
@@ -245,12 +245,12 @@ var (
RPCListenAddrFlag = cli.StringFlag{
Name: "rpcaddr",
Usage: "HTTP-RPC server listening interface",
- Value: common.DefaultHTTPHost,
+ Value: node.DefaultHTTPHost,
}
RPCPortFlag = cli.IntFlag{
Name: "rpcport",
Usage: "HTTP-RPC server listening port",
- Value: common.DefaultHTTPPort,
+ Value: node.DefaultHTTPPort,
}
RPCCORSDomainFlag = cli.StringFlag{
Name: "rpccorsdomain",
@@ -268,13 +268,13 @@ var (
}
IPCApiFlag = cli.StringFlag{
Name: "ipcapi",
- Usage: "API's offered over the IPC-RPC interface",
+ Usage: "APIs offered over the IPC-RPC interface",
Value: rpc.DefaultIPCApis,
}
IPCPathFlag = DirectoryFlag{
Name: "ipcpath",
Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)",
- Value: DirectoryString{common.DefaultIPCSocket},
+ Value: DirectoryString{"geth.ipc"},
}
WSEnabledFlag = cli.BoolFlag{
Name: "ws",
@@ -283,12 +283,12 @@ var (
WSListenAddrFlag = cli.StringFlag{
Name: "wsaddr",
Usage: "WS-RPC server listening interface",
- Value: common.DefaultWSHost,
+ Value: node.DefaultWSHost,
}
WSPortFlag = cli.IntFlag{
Name: "wsport",
Usage: "WS-RPC server listening port",
- Value: common.DefaultWSPort,
+ Value: node.DefaultWSPort,
}
WSApiFlag = cli.StringFlag{
Name: "wsapi",
@@ -396,13 +396,14 @@ var (
}
)
-// MustMakeDataDir retrieves the currently requested data directory, terminating
+// MakeDataDir retrieves the currently requested data directory, terminating
// if none (or the empty string) is specified. If the node is starting a testnet,
// the a subdirectory of the specified datadir will be used.
-func MustMakeDataDir(ctx *cli.Context) string {
+func MakeDataDir(ctx *cli.Context) string {
if path := ctx.GlobalString(DataDirFlag.Name); path != "" {
+ // TODO: choose a different location outside of the regular datadir.
if ctx.GlobalBool(TestNetFlag.Name) {
- return filepath.Join(path, "/testnet")
+ return filepath.Join(path, "testnet")
}
return path
}
@@ -447,16 +448,16 @@ func MakeNodeKey(ctx *cli.Context) *ecdsa.PrivateKey {
return key
}
-// MakeNodeName creates a node name from a base set and the command line flags.
-func MakeNodeName(client, version string, ctx *cli.Context) string {
- name := common.MakeName(client, version)
+// makeNodeUserIdent creates the user identifier from CLI flags.
+func makeNodeUserIdent(ctx *cli.Context) string {
+ var comps []string
if identity := ctx.GlobalString(IdentityFlag.Name); len(identity) > 0 {
- name += "/" + identity
+ comps = append(comps, identity)
}
if ctx.GlobalBool(VMEnableJitFlag.Name) {
- name += "/JIT"
+ comps = append(comps, "JIT")
}
- return name
+ return strings.Join(comps, "/")
}
// MakeBootstrapNodes creates a list of bootstrap nodes from the command line
@@ -612,11 +613,13 @@ func MakeNode(ctx *cli.Context, name, gitCommit string) *node.Node {
}
config := &node.Config{
- DataDir: MustMakeDataDir(ctx),
+ DataDir: MakeDataDir(ctx),
KeyStoreDir: ctx.GlobalString(KeyStoreDirFlag.Name),
UseLightweightKDF: ctx.GlobalBool(LightKDFFlag.Name),
PrivateKey: MakeNodeKey(ctx),
- Name: MakeNodeName(name, vsn, ctx),
+ Name: name,
+ Version: vsn,
+ UserIdent: makeNodeUserIdent(ctx),
NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name),
BootstrapNodes: MakeBootstrapNodes(ctx),
ListenAddr: MakeListenAddress(ctx),
@@ -674,7 +677,7 @@ func RegisterEthService(ctx *cli.Context, stack *node.Node, extra []byte) {
ethConf := &eth.Config{
Etherbase: MakeEtherbase(stack.AccountManager(), ctx),
- ChainConfig: MustMakeChainConfig(ctx),
+ ChainConfig: MakeChainConfig(ctx, stack),
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
DatabaseCache: ctx.GlobalInt(CacheFlag.Name),
DatabaseHandles: MakeDatabaseHandles(),
@@ -748,16 +751,16 @@ func SetupNetwork(ctx *cli.Context) {
params.TargetGasLimit = common.String2Big(ctx.GlobalString(TargetGasLimitFlag.Name))
}
-// MustMakeChainConfig reads the chain configuration from the database in ctx.Datadir.
-func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig {
- db := MakeChainDatabase(ctx)
+// MakeChainConfig reads the chain configuration from the database in ctx.Datadir.
+func MakeChainConfig(ctx *cli.Context, stack *node.Node) *core.ChainConfig {
+ db := MakeChainDatabase(ctx, stack)
defer db.Close()
- return MustMakeChainConfigFromDb(ctx, db)
+ return MakeChainConfigFromDb(ctx, db)
}
-// MustMakeChainConfigFromDb reads the chain configuration from the given database.
-func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfig {
+// MakeChainConfigFromDb reads the chain configuration from the given database.
+func MakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfig {
// If the chain is already initialized, use any existing chain configs
config := new(core.ChainConfig)
@@ -800,14 +803,13 @@ func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainC
}
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
-func MakeChainDatabase(ctx *cli.Context) ethdb.Database {
+func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
var (
- datadir = MustMakeDataDir(ctx)
cache = ctx.GlobalInt(CacheFlag.Name)
handles = MakeDatabaseHandles()
)
- chainDb, err := ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles)
+ chainDb, err := stack.OpenDatabase("chaindata", cache, handles)
if err != nil {
Fatalf("Could not open database: %v", err)
}
@@ -815,9 +817,9 @@ func MakeChainDatabase(ctx *cli.Context) ethdb.Database {
}
// MakeChain creates a chain manager from set command line flags.
-func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
+func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chainDb ethdb.Database) {
var err error
- chainDb = MakeChainDatabase(ctx)
+ chainDb = MakeChainDatabase(ctx, stack)
if ctx.GlobalBool(OlympicFlag.Name) {
_, err := core.WriteTestNetGenesisBlock(chainDb)
@@ -825,7 +827,7 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database
glog.Fatalln(err)
}
}
- chainConfig := MustMakeChainConfigFromDb(ctx, chainDb)
+ chainConfig := MakeChainConfigFromDb(ctx, chainDb)
pow := pow.PoW(core.FakePow{})
if !ctx.GlobalBool(FakePoWFlag.Name) {