main.go 21.1 KB
Newer Older
F
Felix Lange 已提交
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 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
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

17
// geth is the official command-line client for Ethereum.
18 19 20
package main

import (
O
obscuren 已提交
21
	"fmt"
22
	"io/ioutil"
23
	_ "net/http/pprof"
O
obscuren 已提交
24
	"os"
25
	"path/filepath"
O
obscuren 已提交
26
	"runtime"
27
	"strconv"
28
	"strings"
29
	"time"
O
obscuren 已提交
30

31
	"github.com/codegangsta/cli"
32
	"github.com/ethereum/ethash"
Z
zelig 已提交
33
	"github.com/ethereum/go-ethereum/accounts"
O
obscuren 已提交
34
	"github.com/ethereum/go-ethereum/cmd/utils"
Z
zelig 已提交
35
	"github.com/ethereum/go-ethereum/common"
36 37
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
O
obscuren 已提交
38
	"github.com/ethereum/go-ethereum/eth"
39
	"github.com/ethereum/go-ethereum/ethdb"
O
obscuren 已提交
40
	"github.com/ethereum/go-ethereum/logger"
41
	"github.com/ethereum/go-ethereum/logger/glog"
42
	"github.com/ethereum/go-ethereum/metrics"
43 44
	"github.com/ethereum/go-ethereum/params"
	"github.com/ethereum/go-ethereum/rlp"
45 46
	"github.com/ethereum/go-ethereum/rpc/codec"
	"github.com/ethereum/go-ethereum/rpc/comms"
47 48
)

Z
zelig 已提交
49
const (
50
	ClientIdentifier = "Geth"
J
Jeffrey Wilcke 已提交
51
	Version          = "1.3.0-dev"
52
	VersionMajor     = 1
J
Jeffrey Wilcke 已提交
53
	VersionMinor     = 3
J
Jeffrey Wilcke 已提交
54
	VersionPatch     = 0
Z
zelig 已提交
55 56
)

57
var (
J
Jeffrey Wilcke 已提交
58
	gitCommit       string // set via linker flagg
59 60
	nodeNameVersion string
	app             *cli.App
K
Kobi Gurkan 已提交
61 62 63 64 65

	ExtraDataFlag = cli.StringFlag{
		Name:  "extradata",
		Usage: "Extra data for the miner",
	}
66
)
67

68
func init() {
69 70 71 72 73 74 75
	if gitCommit == "" {
		nodeNameVersion = Version
	} else {
		nodeNameVersion = Version + "-" + gitCommit[:8]
	}

	app = utils.NewApp(Version, "the go-ethereum command line interface")
76 77 78
	app.Action = run
	app.HideVersion = true // we have a command to print the version
	app.Commands = []cli.Command{
79 80 81
		{
			Action: blockRecovery,
			Name:   "recover",
82
			Usage:  "attempts to recover a corrupted database by setting a new block by number or hash. See help recover.",
83 84 85
			Description: `
The recover commands will attempt to read out the last
block based on that.
86 87 88

recover #number recovers by number
recover <hex> recovers by hash
89 90
`,
		},
91 92 93 94 95 96
		blocktestCommand,
		importCommand,
		exportCommand,
		upgradedbCommand,
		removedbCommand,
		dumpCommand,
97
		monitorCommand,
98 99 100 101 102 103 104 105 106
		{
			Action: makedag,
			Name:   "makedag",
			Usage:  "generate ethash dag (for testing)",
			Description: `
The makedag command generates an ethash DAG in /tmp/dag.

This command exists to support the system testing project.
Regular users do not need to execute it.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
`,
		},
		{
			Action: gpuinfo,
			Name:   "gpuinfo",
			Usage:  "gpuinfo",
			Description: `
Prints OpenCL device info for all found GPUs.
`,
		},
		{
			Action: gpubench,
			Name:   "gpubench",
			Usage:  "benchmark GPU",
			Description: `
Runs quick benchmark on first GPU found.
123 124
`,
		},
125 126 127 128 129 130 131 132
		{
			Action: version,
			Name:   "version",
			Usage:  "print ethereum version numbers",
			Description: `
The output of this command is supposed to be machine-readable.
`,
		},
133 134

		{
135 136
			Name:  "wallet",
			Usage: "ethereum presale wallet",
137 138 139 140 141 142 143
			Subcommands: []cli.Command{
				{
					Action: importWallet,
					Name:   "import",
					Usage:  "import ethereum presale wallet",
				},
			},
Z
zelig 已提交
144 145 146 147 148 149 150 151 152
			Description: `

    get wallet import /path/to/my/presale.wallet

will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
passwordfile as argument containing the wallet password in plaintext.

`},
F
Felix Lange 已提交
153 154 155 156
		{
			Action: accountList,
			Name:   "account",
			Usage:  "manage accounts",
157 158 159 160 161
			Description: `

Manage accounts lets you create new accounts, list all existing accounts,
import a private key into a new account.

162
'            help' shows a list of subcommands or help for one subcommand.
163

164 165 166 167 168 169 170 171 172 173 174 175
It supports interactive mode, when you are prompted for password as well as
non-interactive mode where passwords are supplied via a given password file.
Non-interactive mode is only meant for scripted use on test networks or known
safe environments.

Make sure you remember the password you gave when creating a new account (with
either new or import). Without it you are not able to unlock your account.

Note that exporting your key in unencrypted format is NOT supported.

Keys are stored under <DATADIR>/keys.
It is safe to transfer the entire directory or the individual keys therein
176
between ethereum nodes by simply copying.
177 178
Make sure you backup your keys regularly.

179 180 181
In order to use your account to send transactions, you need to unlock them using the
'--unlock' option. The argument is a comma

182 183
And finally. DO NOT FORGET YOUR PASSWORD.
`,
F
Felix Lange 已提交
184 185 186 187 188 189 190 191 192 193
			Subcommands: []cli.Command{
				{
					Action: accountList,
					Name:   "list",
					Usage:  "print account addresses",
				},
				{
					Action: accountCreate,
					Name:   "new",
					Usage:  "create a new account",
Z
zelig 已提交
194 195 196 197
					Description: `

    ethereum account new

198 199 200 201 202 203
Creates a new account. Prints the address.

The account is saved in encrypted format, you are prompted for a passphrase.

You must remember this passphrase to unlock your account in the future.

Z
zelig 已提交
204 205 206 207
For non-interactive use the passphrase can be specified with the --password flag:

    ethereum --password <passwordfile> account new

208 209
Note, this is meant to be used for testing only, it is a bad idea to save your
password to file or expose in any other way.
Z
zelig 已提交
210 211
					`,
				},
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
				{
					Action: accountUpdate,
					Name:   "update",
					Usage:  "update an existing account",
					Description: `

    ethereum account update <address>

Update an existing account.

The account is saved in the newest version in encrypted format, you are prompted
for a passphrase to unlock the account and another to save the updated file.

This same command can therefore be used to migrate an account of a deprecated
format to the newest format or change the password for an account.

For non-interactive use the passphrase can be specified with the --password flag:

    ethereum --password <passwordfile> account new

Since only one password can be given, only format update can be performed,
changing your password is only possible interactively.

Note that account update has the a side effect that the order of your accounts
changes.
					`,
				},
Z
zelig 已提交
239 240 241 242 243 244 245 246
				{
					Action: accountImport,
					Name:   "import",
					Usage:  "import a private key into a new account",
					Description: `

    ethereum account import <keyfile>

247 248 249
Imports an unencrypted private key from <keyfile> and creates a new account.
Prints the address.

250
The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
Z
zelig 已提交
251 252 253

The account is saved in encrypted format, you are prompted for a passphrase.

254
You must remember this passphrase to unlock your account in the future.
Z
zelig 已提交
255

256
For non-interactive use the passphrase can be specified with the -password flag:
Z
zelig 已提交
257

258
    ethereum --password <passwordfile> account import <keyfile>
Z
zelig 已提交
259 260

Note:
Z
zelig 已提交
261
As you can directly copy your encrypted accounts to another ethereum instance,
262
this import mechanism is not needed when you transfer an account between
Z
zelig 已提交
263
nodes.
Z
zelig 已提交
264
					`,
F
Felix Lange 已提交
265 266 267
				},
			},
		},
268
		{
Z
CLI:  
zelig 已提交
269 270
			Action: console,
			Name:   "console",
O
obscuren 已提交
271
			Usage:  `Geth Console: interactive JavaScript environment`,
Z
CLI:  
zelig 已提交
272
			Description: `
O
obscuren 已提交
273
The Geth console is an interactive shell for the JavaScript runtime environment
274 275
which exposes a node admin interface as well as the Ðapp JavaScript API.
See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
B
Bas van Kervel 已提交
276 277 278 279
`},
		{
			Action: attach,
			Name:   "attach",
B
Bas van Kervel 已提交
280
			Usage:  `Geth Console: interactive JavaScript environment (connect to node)`,
B
Bas van Kervel 已提交
281 282 283 284 285
			Description: `
The Geth console is an interactive shell for the JavaScript runtime environment
which exposes a node admin interface as well as the Ðapp JavaScript API.
See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console.
This command allows to open a console on a running geth node.
Z
CLI:  
zelig 已提交
286 287 288 289
`,
		},
		{
			Action: execJSFiles,
290
			Name:   "js",
O
obscuren 已提交
291
			Usage:  `executes the given JavaScript files in the Geth JavaScript VM`,
292
			Description: `
293
The JavaScript VM exposes a node admin interface as well as the Ðapp
Z
zelig 已提交
294
JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
295 296 297 298
`,
		},
	}
	app.Flags = []cli.Flag{
299
		utils.IdentityFlag,
300
		utils.UnlockedAccountFlag,
Z
zelig 已提交
301
		utils.PasswordFileFlag,
302
		utils.GenesisFileFlag,
303 304
		utils.BootnodesFlag,
		utils.DataDirFlag,
305
		utils.BlockchainVersionFlag,
306
		utils.OlympicFlag,
307
		utils.FastSyncFlag,
308
		utils.CacheFlag,
Z
CLI:  
zelig 已提交
309
		utils.JSpathFlag,
310 311
		utils.ListenPortFlag,
		utils.MaxPeersFlag,
312
		utils.MaxPendingPeersFlag,
Z
zelig 已提交
313
		utils.EtherbaseFlag,
314
		utils.GasPriceFlag,
315 316
		utils.MinerThreadsFlag,
		utils.MiningEnabledFlag,
317
		utils.MiningGPUFlag,
318
		utils.AutoDAGFlag,
319
		utils.NATFlag,
320
		utils.NatspecEnabledFlag,
321
		utils.NoDiscoverFlag,
322 323 324 325 326
		utils.NodeKeyFileFlag,
		utils.NodeKeyHexFlag,
		utils.RPCEnabledFlag,
		utils.RPCListenAddrFlag,
		utils.RPCPortFlag,
327
		utils.RpcApiFlag,
B
Bas van Kervel 已提交
328 329 330
		utils.IPCDisabledFlag,
		utils.IPCApiFlag,
		utils.IPCPathFlag,
331
		utils.ExecFlag,
332
		utils.WhisperEnabledFlag,
333
		utils.DevModeFlag,
334
		utils.TestNetFlag,
335
		utils.VMDebugFlag,
336 337 338
		utils.VMForceJitFlag,
		utils.VMJitCacheFlag,
		utils.VMEnableJitFlag,
Z
zelig 已提交
339
		utils.NetworkIdFlag,
340
		utils.RPCCORSDomainFlag,
341
		utils.VerbosityFlag,
O
obscuren 已提交
342 343
		utils.BacktraceAtFlag,
		utils.LogToStdErrFlag,
O
obscuren 已提交
344 345 346
		utils.LogVModuleFlag,
		utils.LogFileFlag,
		utils.LogJSONFlag,
347
		utils.PProfEanbledFlag,
348
		utils.PProfPortFlag,
349
		utils.MetricsEnabledFlag,
350
		utils.SolcPathFlag,
Z
zsfelfoldi 已提交
351 352 353 354 355 356
		utils.GpoMinGasPriceFlag,
		utils.GpoMaxGasPriceFlag,
		utils.GpoFullBlockRatioFlag,
		utils.GpobaseStepDownFlag,
		utils.GpobaseStepUpFlag,
		utils.GpobaseCorrectionFactorFlag,
K
Kobi Gurkan 已提交
357
		ExtraDataFlag,
358
	}
359
	app.Before = func(ctx *cli.Context) error {
360
		utils.SetupLogger(ctx)
361
		utils.SetupNetwork(ctx)
362
		utils.SetupVM(ctx)
363
		if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
364 365 366
			utils.StartPProf(ctx)
		}
		return nil
367
	}
368
	// Start system runtime metrics collection
369
	go metrics.CollectProcessMetrics(3 * time.Second)
370
}
O
obscuren 已提交
371

372 373 374 375 376 377 378 379
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	defer logger.Flush()
	if err := app.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
Z
zelig 已提交
380

381
// makeExtra resolves extradata for the miner from a flag or returns a default.
K
Kobi Gurkan 已提交
382 383 384 385 386 387 388
func makeExtra(ctx *cli.Context) []byte {
	if ctx.GlobalIsSet(ExtraDataFlag.Name) {
		return []byte(ctx.GlobalString(ExtraDataFlag.Name))
	}
	return makeDefaultExtra()
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
func makeDefaultExtra() []byte {
	var clientInfo = struct {
		Version   uint
		Name      string
		GoVersion string
		Os        string
	}{uint(VersionMajor<<16 | VersionMinor<<8 | VersionPatch), ClientIdentifier, runtime.Version(), runtime.GOOS}
	extra, err := rlp.EncodeToBytes(clientInfo)
	if err != nil {
		glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
	}

	if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
		glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
		glog.V(logger.Debug).Infof("extra: %x\n", extra)
		return nil
	}

	return extra
}

410
func run(ctx *cli.Context) {
411
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
412

413
	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
K
Kobi Gurkan 已提交
414
	cfg.ExtraData = makeExtra(ctx)
415

416
	ethereum, err := eth.New(cfg)
417
	if err != nil {
418 419 420
		utils.Fatalf("%v", err)
	}

421
	startEth(ctx, ethereum)
422
	// this blocks the thread
423
	ethereum.WaitForShutdown()
424
}
425

B
Bas van Kervel 已提交
426
func attach(ctx *cli.Context) {
427
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
428

B
Bas van Kervel 已提交
429 430 431 432 433 434
	var client comms.EthereumClient
	var err error
	if ctx.Args().Present() {
		client, err = comms.ClientFromEndpoint(ctx.Args().First(), codec.JSON)
	} else {
		cfg := comms.IpcConfig{
435
			Endpoint: utils.IpcSocketPath(ctx),
B
Bas van Kervel 已提交
436 437 438 439 440 441 442 443 444
		}
		client, err = comms.NewIpcClient(cfg, codec.JSON)
	}

	if err != nil {
		utils.Fatalf("Unable to attach to geth node - %v", err)
	}

	repl := newLightweightJSRE(
445
		ctx.GlobalString(utils.JSpathFlag.Name),
B
Bas van Kervel 已提交
446
		client,
B
Bas van Kervel 已提交
447
		ctx.GlobalString(utils.DataDirFlag.Name),
B
Bas van Kervel 已提交
448
		true,
B
Bas van Kervel 已提交
449
	)
B
Bas van Kervel 已提交
450

451 452 453 454 455 456
	if ctx.GlobalString(utils.ExecFlag.Name) != "" {
		repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
	} else {
		repl.welcome()
		repl.interactive()
	}
B
Bas van Kervel 已提交
457 458
}

Z
CLI:  
zelig 已提交
459
func console(ctx *cli.Context) {
460
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
461

462
	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
463 464
	cfg.ExtraData = makeExtra(ctx)

465
	ethereum, err := eth.New(cfg)
466
	if err != nil {
467 468 469
		utils.Fatalf("%v", err)
	}

470 471
	client := comms.NewInProcClient(codec.JSON)

472
	startEth(ctx, ethereum)
473 474
	repl := newJSRE(
		ethereum,
475
		ctx.GlobalString(utils.JSpathFlag.Name),
476
		ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
477
		client,
478 479 480
		true,
		nil,
	)
B
Bas van Kervel 已提交
481

482 483 484 485 486 487
	if ctx.GlobalString(utils.ExecFlag.Name) != "" {
		repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
	} else {
		repl.welcome()
		repl.interactive()
	}
Z
CLI:  
zelig 已提交
488 489 490 491 492 493

	ethereum.Stop()
	ethereum.WaitForShutdown()
}

func execJSFiles(ctx *cli.Context) {
494
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
495

496
	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
Z
CLI:  
zelig 已提交
497 498 499
	ethereum, err := eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v", err)
O
obscuren 已提交
500
	}
Z
CLI:  
zelig 已提交
501

502
	client := comms.NewInProcClient(codec.JSON)
Z
CLI:  
zelig 已提交
503
	startEth(ctx, ethereum)
504 505
	repl := newJSRE(
		ethereum,
506
		ctx.GlobalString(utils.JSpathFlag.Name),
507
		ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
508
		client,
509 510 511
		false,
		nil,
	)
Z
CLI:  
zelig 已提交
512 513 514 515
	for _, file := range ctx.Args() {
		repl.exec(file)
	}

516 517
	ethereum.Stop()
	ethereum.WaitForShutdown()
518
}
O
obscuren 已提交
519

520
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
521
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
522

523
	var err error
524 525 526 527 528 529 530 531 532 533 534
	addrHex, err = utils.ParamToAddress(addr, am)
	if err == nil {
		// Attempt to unlock the account 3 times
		attempts := 3
		for tries := 0; tries < attempts; tries++ {
			msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts)
			auth = getPassPhrase(ctx, msg, false, i)
			err = am.Unlock(common.HexToAddress(addrHex), auth)
			if err == nil {
				break
			}
535 536
		}
	}
537

538 539
	if err != nil {
		utils.Fatalf("Unlock account failed '%v'", err)
540
	}
541 542
	fmt.Printf("Account '%s' unlocked.\n", addr)
	return
Z
zelig 已提交
543 544
}

545
func blockRecovery(ctx *cli.Context) {
546
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
547

548 549 550
	arg := ctx.Args().First()
	if len(ctx.Args()) < 1 && len(arg) > 0 {
		glog.Fatal("recover requires block number or hash")
551 552 553
	}

	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
554 555
	utils.CheckLegalese(cfg.DataDir)

556
	blockDb, err := ethdb.NewLDBDatabase(filepath.Join(cfg.DataDir, "blockchain"), cfg.DatabaseCache)
557
	if err != nil {
558 559 560 561 562
		glog.Fatalln("could not open db:", err)
	}

	var block *types.Block
	if arg[0] == '#' {
563
		block = core.GetBlock(blockDb, core.GetCanonicalHash(blockDb, common.String2Big(arg[1:]).Uint64()))
564
	} else {
565
		block = core.GetBlock(blockDb, common.HexToHash(arg))
566 567 568 569 570 571
	}

	if block == nil {
		glog.Fatalln("block not found. Recovery failed")
	}

572
	if err = core.WriteHeadBlockHash(blockDb, block.Hash()); err != nil {
573
		glog.Fatalln("block write err", err)
574
	}
575
	glog.Infof("Recovery succesful. New HEAD %x\n", block.Hash())
576 577
}

Z
zelig 已提交
578
func startEth(ctx *cli.Context, eth *eth.Ethereum) {
579
	// Start Ethereum itself
Z
zelig 已提交
580 581
	utils.StartEthereum(eth)

582
	am := eth.AccountManager()
Z
zelig 已提交
583
	account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
584
	accounts := strings.Split(account, " ")
585
	for i, account := range accounts {
586 587
		if len(account) > 0 {
			if account == "primary" {
Z
zelig 已提交
588
				utils.Fatalf("the 'primary' keyword is deprecated. You can use integer indexes, but the indexes are not permanent, they can change if you add external keys, export your keys or copy your keystore to another node.")
589
			}
590
			unlockAccount(ctx, am, account, i)
Z
zelig 已提交
591
		}
Z
zelig 已提交
592
	}
593
	// Start auxiliary services if enabled.
B
Bas van Kervel 已提交
594 595 596 597 598
	if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) {
		if err := utils.StartIPC(eth, ctx); err != nil {
			utils.Fatalf("Error string IPC: %v", err)
		}
	}
599
	if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
600 601 602
		if err := utils.StartRPC(eth, ctx); err != nil {
			utils.Fatalf("Error starting RPC: %v", err)
		}
603 604
	}
	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
605 606 607 608
		err := eth.StartMining(
			ctx.GlobalInt(utils.MinerThreadsFlag.Name),
			ctx.GlobalString(utils.MiningGPUFlag.Name))
		if err != nil {
609 610
			utils.Fatalf("%v", err)
		}
611 612
	}
}
O
Merge  
obscuren 已提交
613

F
Felix Lange 已提交
614
func accountList(ctx *cli.Context) {
615
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
616

617
	am := utils.MakeAccountManager(ctx)
F
Felix Lange 已提交
618 619 620 621
	accts, err := am.Accounts()
	if err != nil {
		utils.Fatalf("Could not list accounts: %v", err)
	}
622
	for i, acct := range accts {
Z
zelig 已提交
623
		fmt.Printf("Account #%d: %x\n", i, acct)
F
Felix Lange 已提交
624 625 626
	}
}

627
func getPassPhrase(ctx *cli.Context, desc string, confirmation bool, i int) (passphrase string) {
628 629 630
	passfile := ctx.GlobalString(utils.PasswordFileFlag.Name)
	if len(passfile) == 0 {
		fmt.Println(desc)
631
		auth, err := utils.PromptPassword("Passphrase: ", true)
632 633 634 635
		if err != nil {
			utils.Fatalf("%v", err)
		}
		if confirmation {
636
			confirm, err := utils.PromptPassword("Repeat Passphrase: ", false)
Z
zelig 已提交
637 638 639
			if err != nil {
				utils.Fatalf("%v", err)
			}
640 641
			if auth != confirm {
				utils.Fatalf("Passphrases did not match.")
Z
zelig 已提交
642
			}
643 644
		}
		passphrase = auth
Z
zelig 已提交
645

646 647 648 649
	} else {
		passbytes, err := ioutil.ReadFile(passfile)
		if err != nil {
			utils.Fatalf("Unable to read password file '%s': %v", passfile, err)
650
		}
651 652 653 654 655 656 657 658 659
		// this is backwards compatible if the same password unlocks several accounts
		// it also has the consequence that trailing newlines will not count as part
		// of the password, so --password <(echo -n 'pass') will now work without -n
		passphrases := strings.Split(string(passbytes), "\n")
		if i >= len(passphrases) {
			passphrase = passphrases[len(passphrases)-1]
		} else {
			passphrase = passphrases[i]
		}
F
Felix Lange 已提交
660
	}
Z
zelig 已提交
661 662 663 664
	return
}

func accountCreate(ctx *cli.Context) {
665
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
666

667
	am := utils.MakeAccountManager(ctx)
668
	passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0)
669
	acct, err := am.NewAccount(passphrase)
F
Felix Lange 已提交
670 671 672
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
Z
zelig 已提交
673 674 675
	fmt.Printf("Address: %x\n", acct)
}

676
func accountUpdate(ctx *cli.Context) {
677
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
678

679 680 681 682 683 684 685 686 687 688 689 690 691 692
	am := utils.MakeAccountManager(ctx)
	arg := ctx.Args().First()
	if len(arg) == 0 {
		utils.Fatalf("account address or index must be given as argument")
	}

	addr, authFrom := unlockAccount(ctx, am, arg, 0)
	authTo := getPassPhrase(ctx, "Please give a new password. Do not forget this password.", true, 0)
	err := am.Update(common.HexToAddress(addr), authFrom, authTo)
	if err != nil {
		utils.Fatalf("Could not update the account: %v", err)
	}
}

693
func importWallet(ctx *cli.Context) {
694
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
695

696 697 698 699 700 701 702 703 704
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
	keyJson, err := ioutil.ReadFile(keyfile)
	if err != nil {
		utils.Fatalf("Could not read wallet file: %v", err)
	}

705
	am := utils.MakeAccountManager(ctx)
706
	passphrase := getPassPhrase(ctx, "", false, 0)
707 708 709 710 711 712 713 714

	acct, err := am.ImportPreSaleKey(keyJson, passphrase)
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
	fmt.Printf("Address: %x\n", acct)
}

Z
zelig 已提交
715
func accountImport(ctx *cli.Context) {
716
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
717

Z
zelig 已提交
718 719 720 721
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
722
	am := utils.MakeAccountManager(ctx)
723
	passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0)
Z
zelig 已提交
724 725 726 727 728 729 730
	acct, err := am.Import(keyfile, passphrase)
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
	fmt.Printf("Address: %x\n", acct)
}

731
func makedag(ctx *cli.Context) {
732
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
733

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	args := ctx.Args()
	wrongArgs := func() {
		utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
	}
	switch {
	case len(args) == 2:
		blockNum, err := strconv.ParseUint(args[0], 0, 64)
		dir := args[1]
		if err != nil {
			wrongArgs()
		} else {
			dir = filepath.Clean(dir)
			// seems to require a trailing slash
			if !strings.HasSuffix(dir, "/") {
				dir = dir + "/"
			}
			_, err = ioutil.ReadDir(dir)
			if err != nil {
				utils.Fatalf("Can't find dir")
			}
			fmt.Println("making DAG, this could take awhile...")
			ethash.MakeDAG(blockNum, dir)
		}
	default:
		wrongArgs()
	}
760 761
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
func gpuinfo(ctx *cli.Context) {
	eth.PrintOpenCLDevices()
}

func gpubench(ctx *cli.Context) {
	args := ctx.Args()
	wrongArgs := func() {
		utils.Fatalf(`Usage: geth gpubench <gpu number>`)
	}
	switch {
	case len(args) == 1:
		n, err := strconv.ParseUint(args[0], 0, 64)
		if err != nil {
			wrongArgs()
		}
		eth.GPUBench(n)
	case len(args) == 0:
		eth.GPUBench(0)
	default:
		wrongArgs()
	}
}

785
func version(c *cli.Context) {
786 787 788 789 790
	fmt.Println(ClientIdentifier)
	fmt.Println("Version:", Version)
	if gitCommit != "" {
		fmt.Println("Git Commit:", gitCommit)
	}
791
	fmt.Println("Protocol Versions:", eth.ProtocolVersions)
792 793 794 795 796
	fmt.Println("Network Id:", c.GlobalInt(utils.NetworkIdFlag.Name))
	fmt.Println("Go Version:", runtime.Version())
	fmt.Println("OS:", runtime.GOOS)
	fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
	fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
O
obscuren 已提交
797
}