main.go 21.7 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 61
	nodeNameVersion string
	app             *cli.App
)
62

63
func init() {
64 65 66 67 68 69 70
	if gitCommit == "" {
		nodeNameVersion = Version
	} else {
		nodeNameVersion = Version + "-" + gitCommit[:8]
	}

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

recover #number recovers by number
recover <hex> recovers by hash
84 85
`,
		},
86 87 88 89 90 91
		blocktestCommand,
		importCommand,
		exportCommand,
		upgradedbCommand,
		removedbCommand,
		dumpCommand,
92
		monitorCommand,
93 94 95 96 97 98 99 100 101
		{
			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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
`,
		},
		{
			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.
118 119
`,
		},
120 121 122 123 124 125 126 127
		{
			Action: version,
			Name:   "version",
			Usage:  "print ethereum version numbers",
			Description: `
The output of this command is supposed to be machine-readable.
`,
		},
128 129

		{
130 131
			Name:  "wallet",
			Usage: "ethereum presale wallet",
132 133 134 135 136 137 138
			Subcommands: []cli.Command{
				{
					Action: importWallet,
					Name:   "import",
					Usage:  "import ethereum presale wallet",
				},
			},
Z
zelig 已提交
139 140 141 142 143 144 145 146 147
			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 已提交
148 149 150 151
		{
			Action: accountList,
			Name:   "account",
			Usage:  "manage accounts",
152 153 154 155 156
			Description: `

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

157
'            help' shows a list of subcommands or help for one subcommand.
158

159 160 161 162 163 164 165 166 167 168 169 170
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
171
between ethereum nodes by simply copying.
172 173
Make sure you backup your keys regularly.

Z
zelig 已提交
174 175 176 177 178 179
In order to use your account to send transactions, you need to unlock them using
the '--unlock' option. The argument is a space separated list of addresses or
indexes. If used non-interactively with a passwordfile, the file should contain
the respective passwords one per line. If you unlock n accounts and the password
file contains less than n entries, then the last password is meant to apply to
all remaining accounts.
180

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

    ethereum account new

197 198 199 200 201 202
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 已提交
203 204 205 206
For non-interactive use the passphrase can be specified with the --password flag:

    ethereum --password <passwordfile> account new

207 208
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 已提交
209 210
					`,
				},
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
				{
					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:

Z
zelig 已提交
229
    ethereum --password <passwordfile> account update <address>
230 231 232 233 234 235 236 237

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 已提交
238 239 240 241 242 243 244 245
				{
					Action: accountImport,
					Name:   "import",
					Usage:  "import a private key into a new account",
					Description: `

    ethereum account import <keyfile>

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

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

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

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

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

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

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

371 372 373 374 375 376 377 378
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 已提交
379

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

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
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
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	ethereum.Stop()
	ethereum.WaitForShutdown()
}

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

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

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

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

Z
zelig 已提交
519 520
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int, inputpassphrases []string) (addrHex, auth string, passphrases []string) {
	utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
T
Taylor Gerring 已提交
521

522
	var err error
Z
zelig 已提交
523
	passphrases = inputpassphrases
524 525 526 527 528 529
	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)
Z
zelig 已提交
530
			auth, passphrases = getPassPhrase(ctx, msg, false, i, passphrases)
531
			err = am.Unlock(common.HexToAddress(addrHex), auth)
Z
zelig 已提交
532
			if err == nil || passphrases != nil {
533 534
				break
			}
535 536
		}
	}
537

538
	if err != nil {
Z
zelig 已提交
539
		utils.Fatalf("Unlock account '%s' (%v) failed: %v", addr, addrHex, err)
540
	}
Z
zelig 已提交
541
	fmt.Printf("Account '%s' (%v) unlocked.\n", addr, addrHex)
542
	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, " ")
Z
zelig 已提交
585
	var passphrases []string
586
	for i, account := range accounts {
587 588
		if len(account) > 0 {
			if account == "primary" {
Z
zelig 已提交
589
				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.")
590
			}
Z
zelig 已提交
591
			_, _, passphrases = unlockAccount(ctx, am, account, i, passphrases)
Z
zelig 已提交
592
		}
Z
zelig 已提交
593
	}
594
	// Start auxiliary services if enabled.
B
Bas van Kervel 已提交
595 596 597 598 599
	if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) {
		if err := utils.StartIPC(eth, ctx); err != nil {
			utils.Fatalf("Error string IPC: %v", err)
		}
	}
600
	if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
601 602 603
		if err := utils.StartRPC(eth, ctx); err != nil {
			utils.Fatalf("Error starting RPC: %v", err)
		}
604 605
	}
	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
606 607 608 609
		err := eth.StartMining(
			ctx.GlobalInt(utils.MinerThreadsFlag.Name),
			ctx.GlobalString(utils.MiningGPUFlag.Name))
		if err != nil {
610 611
			utils.Fatalf("%v", err)
		}
612 613
	}
}
O
Merge  
obscuren 已提交
614

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

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

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

647
	} else {
Z
zelig 已提交
648 649 650 651 652 653 654 655 656 657
		passphrases = inputpassphrases
		if passphrases == nil {
			passbytes, err := ioutil.ReadFile(passfile)
			if err != nil {
				utils.Fatalf("Unable to read password file '%s': %v", passfile, err)
			}
			// 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")
658
		}
659 660 661 662 663
		if i >= len(passphrases) {
			passphrase = passphrases[len(passphrases)-1]
		} else {
			passphrase = passphrases[i]
		}
F
Felix Lange 已提交
664
	}
Z
zelig 已提交
665 666 667 668
	return
}

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

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

680
func accountUpdate(ctx *cli.Context) {
681
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
682

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

Z
zelig 已提交
689 690
	addr, authFrom, passphrases := unlockAccount(ctx, am, arg, 0, nil)
	authTo, _ := getPassPhrase(ctx, "Please give a new password. Do not forget this password.", true, 0, passphrases)
691 692 693 694 695 696
	err := am.Update(common.HexToAddress(addr), authFrom, authTo)
	if err != nil {
		utils.Fatalf("Could not update the account: %v", err)
	}
}

697
func importWallet(ctx *cli.Context) {
698
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
699

700 701 702 703 704 705 706 707 708
	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)
	}

709
	am := utils.MakeAccountManager(ctx)
Z
zelig 已提交
710
	passphrase, _ := getPassPhrase(ctx, "", false, 0, nil)
711 712 713 714 715 716 717 718

	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 已提交
719
func accountImport(ctx *cli.Context) {
720
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
721

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

735
func makedag(ctx *cli.Context) {
736
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
737

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	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()
	}
764 765
}

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
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()
	}
}

789
func version(c *cli.Context) {
790 791 792 793 794
	fmt.Println(ClientIdentifier)
	fmt.Println("Version:", Version)
	if gitCommit != "" {
		fmt.Println("Git Commit:", gitCommit)
	}
795
	fmt.Println("Protocol Versions:", eth.ProtocolVersions)
796 797 798 799 800
	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 已提交
801
}