diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/geth/main.go | 1 | ||||
-rw-r--r-- | cmd/geth/usage.go | 1 | ||||
-rw-r--r-- | cmd/utils/fdlimit_test.go | 35 | ||||
-rw-r--r-- | cmd/utils/fdlimit_unix.go | 50 | ||||
-rw-r--r-- | cmd/utils/fdlimit_windows.go | 41 | ||||
-rw-r--r-- | cmd/utils/flags.go | 41 | ||||
-rw-r--r-- | cmd/utils/jeth.go | 24 |
7 files changed, 180 insertions, 13 deletions
diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 8594d18c5..68e09912b 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -284,6 +284,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso utils.GenesisFileFlag, utils.BootnodesFlag, utils.DataDirFlag, + utils.KeyStoreDirFlag, utils.BlockchainVersionFlag, utils.OlympicFlag, utils.FastSyncFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 051c51878..d2f76eaa6 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -63,6 +63,7 @@ var AppHelpFlagGroups = []flagGroup{ Name: "ETHEREUM", Flags: []cli.Flag{ utils.DataDirFlag, + utils.KeyStoreDirFlag, utils.NetworkIdFlag, utils.OlympicFlag, utils.TestNetFlag, diff --git a/cmd/utils/fdlimit_test.go b/cmd/utils/fdlimit_test.go new file mode 100644 index 000000000..0a950a6c9 --- /dev/null +++ b/cmd/utils/fdlimit_test.go @@ -0,0 +1,35 @@ +// Copyright 2016 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 utils + +import "testing" + +// TestFileDescriptorLimits simply tests whether the file descriptor allowance +// per this process can be retrieved. +func TestFileDescriptorLimits(t *testing.T) { + target := 4096 + + if limit, err := getFdLimit(); err != nil || limit <= 0 { + t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err) + } + if err := raiseFdLimit(uint64(target)); err != nil { + t.Fatalf("failed to raise file allowance") + } + if limit, err := getFdLimit(); err != nil || limit < target { + t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err) + } +} diff --git a/cmd/utils/fdlimit_unix.go b/cmd/utils/fdlimit_unix.go new file mode 100644 index 000000000..2a6dffc8f --- /dev/null +++ b/cmd/utils/fdlimit_unix.go @@ -0,0 +1,50 @@ +// Copyright 2016 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/>. + +// +build linux darwin + +package utils + +import "syscall" + +// raiseFdLimit tries to maximize the file descriptor allowance of this process +// to the maximum hard-limit allowed by the OS. +func raiseFdLimit(max uint64) error { + // Get the current limit + var limit syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return err + } + // Try to update the limit to the max allowance + limit.Cur = limit.Max + if limit.Cur > max { + limit.Cur = max + } + if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return err + } + return nil +} + +// getFdLimit retrieves the number of file descriptors allowed to be opened by this +// process. +func getFdLimit() (int, error) { + var limit syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return 0, err + } + return int(limit.Cur), nil +} diff --git a/cmd/utils/fdlimit_windows.go b/cmd/utils/fdlimit_windows.go new file mode 100644 index 000000000..53aad3d7a --- /dev/null +++ b/cmd/utils/fdlimit_windows.go @@ -0,0 +1,41 @@ +// Copyright 2016 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 utils + +import "errors" + +// raiseFdLimit tries to maximize the file descriptor allowance of this process +// to the maximum hard-limit allowed by the OS. +func raiseFdLimit(max uint64) error { + // This method is NOP by design: + // * Linux/Darwin counterparts need to manually increase per process limits + // * On Windows Go uses the CreateFile API, which is limited to 16K files, non + // changeable from within a running process + // This way we can always "request" raising the limits, which will either have + // or not have effect based on the platform we're running on. + if max > 16384 { + return errors.New("file descriptor limit (16384) reached") + } + return nil +} + +// getFdLimit retrieves the number of file descriptors allowed to be opened by this +// process. +func getFdLimit() (int, error) { + // Please see raiseFdLimit for the reason why we use hard coded 16K as the limit + return 16384, nil +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index adcc0adca..69fb0b9db 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -102,6 +102,10 @@ var ( Usage: "Data directory for the databases and keystore", Value: DirectoryString{common.DefaultDataDir()}, } + KeyStoreDirFlag = DirectoryFlag{ + Name: "keystore", + Usage: "Directory for the keystore (default = inside the datadir)", + } NetworkIdFlag = cli.IntFlag{ Name: "networkid", Usage: "Network identifier (integer, 0=Olympic, 1=Frontier, 2=Morden)", @@ -139,7 +143,7 @@ var ( CacheFlag = cli.IntFlag{ Name: "cache", Usage: "Megabytes of memory allocated to internal caching (min 16MB / database forced)", - Value: 0, + Value: 128, } BlockchainVersionFlag = cli.IntFlag{ Name: "blockchainversion", @@ -393,6 +397,16 @@ func MustMakeDataDir(ctx *cli.Context) string { return "" } +// MakeKeyStoreDir resolves the folder to use for storing the account keys from the +// set command line flags, returning the explicitly requested path, or one inside +// the data directory otherwise. +func MakeKeyStoreDir(datadir string, ctx *cli.Context) string { + if path := ctx.GlobalString(KeyStoreDirFlag.Name); path != "" { + return path + } + return filepath.Join(datadir, "keystore") +} + // MakeIPCPath creates an IPC path configuration from the set command line flags, // returning an empty string if IPC was explicitly disabled, or the set path. func MakeIPCPath(ctx *cli.Context) string { @@ -513,6 +527,22 @@ func MakeGenesisBlock(ctx *cli.Context) string { return string(data) } +// MakeDatabaseHandles raises out the number of allowed file handles per process +// for Geth and returns half of the allowance to assign to the database. +func MakeDatabaseHandles() int { + if err := raiseFdLimit(2048); err != nil { + Fatalf("Failed to raise file descriptor allowance: %v", err) + } + limit, err := getFdLimit() + if err != nil { + Fatalf("Failed to retrieve file descriptor allowance: %v", err) + } + if limit > 2048 { // cap database file descriptors even if more is available + limit = 2048 + } + return limit / 2 // Leave half for networking and other stuff +} + // MakeAccountManager creates an account manager from set command line flags. func MakeAccountManager(ctx *cli.Context) *accounts.Manager { // Create the keystore crypto primitive, light if requested @@ -525,8 +555,9 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager { } // Assemble an account manager using the configured datadir var ( - datadir = MustMakeDataDir(ctx) - keystore = crypto.NewKeyStorePassphrase(filepath.Join(datadir, "keystore"), scryptN, scryptP) + datadir = MustMakeDataDir(ctx) + keystoredir = MakeKeyStoreDir(datadir, ctx) + keystore = crypto.NewKeyStorePassphrase(keystoredir, scryptN, scryptP) ) return accounts.NewManager(keystore) } @@ -634,6 +665,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node. FastSync: ctx.GlobalBool(FastSyncFlag.Name), BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), + DatabaseHandles: MakeDatabaseHandles(), NetworkId: ctx.GlobalInt(NetworkIdFlag.Name), AccountManager: accman, Etherbase: MakeEtherbase(accman, ctx), @@ -748,9 +780,10 @@ func SetupVM(ctx *cli.Context) { func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) { datadir := MustMakeDataDir(ctx) cache := ctx.GlobalInt(CacheFlag.Name) + handles := MakeDatabaseHandles() var err error - if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil { + if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles); err != nil { Fatalf("Could not open database: %v", err) } if ctx.GlobalBool(OlympicFlag.Name) { diff --git a/cmd/utils/jeth.go b/cmd/utils/jeth.go index b460597c1..e5e520db2 100644 --- a/cmd/utils/jeth.go +++ b/cmd/utils/jeth.go @@ -58,7 +58,7 @@ func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id *int64) ( // UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) { - var cmd, account, passwd string + var account, passwd string timeout := int64(300) var ok bool @@ -92,8 +92,7 @@ func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) { } } - cmd = fmt.Sprintf("jeth.unlockAccount('%s', '%s', %d)", account, passwd, timeout) - if val, err := call.Otto.Run(cmd); err == nil { + if val, err := call.Otto.Call("jeth.unlockAccount", nil, account, passwd, timeout); err == nil { return val } @@ -102,8 +101,10 @@ func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) { // NewAccount asks the user for the password and than executes the jeth.newAccount callback in the jsre func (self *Jeth) NewAccount(call otto.FunctionCall) (response otto.Value) { + var passwd string if len(call.ArgumentList) == 0 { - passwd, err := PromptPassword("Passphrase: ", true) + var err error + passwd, err = PromptPassword("Passphrase: ", true) if err != nil { return otto.FalseValue() } @@ -116,13 +117,18 @@ func (self *Jeth) NewAccount(call otto.FunctionCall) (response otto.Value) { fmt.Println("Passphrases don't match") return otto.FalseValue() } + } else if len(call.ArgumentList) == 1 && call.Argument(0).IsString() { + passwd, _ = call.Argument(0).ToString() + } else { + fmt.Println("expected 0 or 1 string argument") + return otto.FalseValue() + } - cmd := fmt.Sprintf("jeth.newAccount('%s')", passwd) - if val, err := call.Otto.Run(cmd); err == nil { - return val - } + if ret, err := call.Otto.Call("jeth.newAccount", nil, passwd); err == nil { + return ret } else { - fmt.Println("New account doesn't expect argument(s), you will be prompted for a password") + fmt.Printf("%v\n", err) + return otto.FalseValue() } return otto.FalseValue() |