main.go 21.6 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"
51
	Version          = "1.3.1"
52
	VersionMajor     = 1
J
Jeffrey Wilcke 已提交
53
	VersionMinor     = 3
54
	VersionPatch     = 1
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",
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
		utils.BacktraceAtFlag,
O
obscuren 已提交
342 343
		utils.LogVModuleFlag,
		utils.LogFileFlag,
344
		utils.PProfEanbledFlag,
345
		utils.PProfPortFlag,
346
		utils.MetricsEnabledFlag,
347
		utils.SolcPathFlag,
Z
zsfelfoldi 已提交
348 349 350 351 352 353
		utils.GpoMinGasPriceFlag,
		utils.GpoMaxGasPriceFlag,
		utils.GpoFullBlockRatioFlag,
		utils.GpobaseStepDownFlag,
		utils.GpobaseStepUpFlag,
		utils.GpobaseCorrectionFactorFlag,
Z
zelig 已提交
354
		utils.ExtraDataFlag,
355
	}
356
	app.Before = func(ctx *cli.Context) error {
357
		utils.SetupLogger(ctx)
358
		utils.SetupNetwork(ctx)
359
		utils.SetupVM(ctx)
360
		if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
361 362 363
			utils.StartPProf(ctx)
		}
		return nil
364
	}
365
	// Start system runtime metrics collection
366
	go metrics.CollectProcessMetrics(3 * time.Second)
367
}
O
obscuren 已提交
368

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

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

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

406
func run(ctx *cli.Context) {
407
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
408

409
	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
K
Kobi Gurkan 已提交
410
	cfg.ExtraData = makeExtra(ctx)
411

412
	ethereum, err := eth.New(cfg)
413
	if err != nil {
414 415 416
		utils.Fatalf("%v", err)
	}

417
	startEth(ctx, ethereum)
418
	// this blocks the thread
419
	ethereum.WaitForShutdown()
420
}
421

B
Bas van Kervel 已提交
422
func attach(ctx *cli.Context) {
423
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
424

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

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

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

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

Z
CLI:  
zelig 已提交
455
func console(ctx *cli.Context) {
456
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
457

458
	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
459 460
	cfg.ExtraData = makeExtra(ctx)

461
	ethereum, err := eth.New(cfg)
462
	if err != nil {
463 464 465
		utils.Fatalf("%v", err)
	}

466 467
	client := comms.NewInProcClient(codec.JSON)

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

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

	ethereum.Stop()
	ethereum.WaitForShutdown()
}

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

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

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

512 513
	ethereum.Stop()
	ethereum.WaitForShutdown()
514
}
O
obscuren 已提交
515

Z
zelig 已提交
516 517
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 已提交
518

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

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

542
func blockRecovery(ctx *cli.Context) {
543
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
544

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

	cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
551 552
	utils.CheckLegalese(cfg.DataDir)

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

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

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

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

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

579
	am := eth.AccountManager()
Z
zelig 已提交
580
	account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
581
	accounts := strings.Split(account, " ")
Z
zelig 已提交
582
	var passphrases []string
583
	for i, account := range accounts {
584 585
		if len(account) > 0 {
			if account == "primary" {
Z
zelig 已提交
586
				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.")
587
			}
Z
zelig 已提交
588
			_, _, passphrases = unlockAccount(ctx, am, account, i, passphrases)
Z
zelig 已提交
589
		}
Z
zelig 已提交
590
	}
591
	// Start auxiliary services if enabled.
B
Bas van Kervel 已提交
592 593 594 595 596
	if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) {
		if err := utils.StartIPC(eth, ctx); err != nil {
			utils.Fatalf("Error string IPC: %v", err)
		}
	}
597
	if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
598 599 600
		if err := utils.StartRPC(eth, ctx); err != nil {
			utils.Fatalf("Error starting RPC: %v", err)
		}
601 602
	}
	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
603 604 605 606
		err := eth.StartMining(
			ctx.GlobalInt(utils.MinerThreadsFlag.Name),
			ctx.GlobalString(utils.MiningGPUFlag.Name))
		if err != nil {
607 608
			utils.Fatalf("%v", err)
		}
609 610
	}
}
O
Merge  
obscuren 已提交
611

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

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

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

644
	} else {
Z
zelig 已提交
645 646 647 648 649 650 651 652 653 654
		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")
655
		}
656 657 658 659 660
		if i >= len(passphrases) {
			passphrase = passphrases[len(passphrases)-1]
		} else {
			passphrase = passphrases[i]
		}
F
Felix Lange 已提交
661
	}
Z
zelig 已提交
662 663 664 665
	return
}

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

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

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

680 681 682 683 684 685
	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 已提交
686 687
	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)
688 689 690 691 692 693
	err := am.Update(common.HexToAddress(addr), authFrom, authTo)
	if err != nil {
		utils.Fatalf("Could not update the account: %v", err)
	}
}

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

697 698 699 700 701 702 703 704 705
	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)
	}

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

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

Z
zelig 已提交
719 720 721 722
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
723
	am := utils.MakeAccountManager(ctx)
Z
zelig 已提交
724
	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 已提交
725 726 727 728 729 730 731
	acct, err := am.Import(keyfile, passphrase)
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
	fmt.Printf("Address: %x\n", acct)
}

732
func makedag(ctx *cli.Context) {
733
	utils.CheckLegalese(utils.MustDataDir(ctx))
T
Taylor Gerring 已提交
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 760
	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()
	}
761 762
}

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

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