main.go 15.8 KB
Newer Older
O
obscuren 已提交
1 2
/*
	This file is part of go-ethereum
F
Felix Lange 已提交
3

O
obscuren 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
	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/>.
*/
/**
 * @authors
 * 	Jeffrey Wilcke <i@jev.io>
 */
21 22 23
package main

import (
F
Felix Lange 已提交
24
	"bufio"
O
obscuren 已提交
25
	"fmt"
26
	"io/ioutil"
O
obscuren 已提交
27
	"os"
O
obscuren 已提交
28
	"runtime"
29
	"strconv"
O
obscuren 已提交
30
	"time"
O
obscuren 已提交
31

O
obscuren 已提交
32 33
	"path"

34
	"github.com/codegangsta/cli"
35
	"github.com/ethereum/ethash"
Z
zelig 已提交
36
	"github.com/ethereum/go-ethereum/accounts"
O
obscuren 已提交
37
	"github.com/ethereum/go-ethereum/cmd/utils"
Z
zelig 已提交
38
	"github.com/ethereum/go-ethereum/common"
39
	"github.com/ethereum/go-ethereum/core"
O
bump  
obscuren 已提交
40
	"github.com/ethereum/go-ethereum/core/state"
41
	"github.com/ethereum/go-ethereum/core/types"
O
obscuren 已提交
42
	"github.com/ethereum/go-ethereum/eth"
O
obscuren 已提交
43
	"github.com/ethereum/go-ethereum/logger"
F
Felix Lange 已提交
44
	"github.com/peterh/liner"
45
)
O
obscuren 已提交
46
import _ "net/http/pprof"
47

Z
zelig 已提交
48
const (
O
obscuren 已提交
49
	ClientIdentifier = "Geth"
O
obscuren 已提交
50
	Version          = "0.9.11"
Z
zelig 已提交
51 52
)

O
obscuren 已提交
53
var app = utils.NewApp(Version, "the go-ethereum command line interface")
54

55 56 57 58
func init() {
	app.Action = run
	app.HideVersion = true // we have a command to print the version
	app.Commands = []cli.Command{
59
		blocktestCmd,
60 61 62 63 64 65 66 67 68 69 70
		{
			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.
`,
		},
71 72 73 74 75 76 77 78
		{
			Action: version,
			Name:   "version",
			Usage:  "print ethereum version numbers",
			Description: `
The output of this command is supposed to be machine-readable.
`,
		},
79 80

		{
81 82
			Name:  "wallet",
			Usage: "ethereum presale wallet",
83 84 85 86 87 88 89 90
			Subcommands: []cli.Command{
				{
					Action: importWallet,
					Name:   "import",
					Usage:  "import ethereum presale wallet",
				},
			},
		},
F
Felix Lange 已提交
91 92 93 94
		{
			Action: accountList,
			Name:   "account",
			Usage:  "manage accounts",
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
			Description: `

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

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
between ethereum nodes.
Make sure you backup your keys regularly.

And finally. DO NOT FORGET YOUR PASSWORD.
`,
F
Felix Lange 已提交
117 118 119 120 121 122 123 124 125 126
			Subcommands: []cli.Command{
				{
					Action: accountList,
					Name:   "list",
					Usage:  "print account addresses",
				},
				{
					Action: accountCreate,
					Name:   "new",
					Usage:  "create a new account",
Z
zelig 已提交
127 128 129 130
					Description: `

    ethereum account new

131 132 133 134 135 136
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 已提交
137 138 139 140
For non-interactive use the passphrase can be specified with the --password flag:

    ethereum --password <passwordfile> account new

141 142
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 已提交
143 144 145 146 147 148 149 150 151 152
					`,
				},
				{
					Action: accountImport,
					Name:   "import",
					Usage:  "import a private key into a new account",
					Description: `

    ethereum account import <keyfile>

153 154 155
Imports an unencrypted private key from <keyfile> and creates a new account.
Prints the address.

156
The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
Z
zelig 已提交
157 158 159

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

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

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

164
    ethereum --password <passwordfile> account import <keyfile>
Z
zelig 已提交
165 166

Note:
Z
zelig 已提交
167
As you can directly copy your encrypted accounts to another ethereum instance,
168
this import mechanism is not needed when you transfer an account between
Z
zelig 已提交
169
nodes.
Z
zelig 已提交
170
					`,
F
Felix Lange 已提交
171 172 173
				},
			},
		},
174 175 176 177 178 179 180 181 182 183
		{
			Action: dump,
			Name:   "dump",
			Usage:  `dump a specific block from storage`,
			Description: `
The arguments are interpreted as block numbers or hashes.
Use "ethereum dump 0" to dump the genesis block.
`,
		},
		{
Z
CLI:  
zelig 已提交
184 185
			Action: console,
			Name:   "console",
O
obscuren 已提交
186
			Usage:  `Geth Console: interactive JavaScript environment`,
Z
CLI:  
zelig 已提交
187
			Description: `
O
obscuren 已提交
188
The Geth console is an interactive shell for the JavaScript runtime environment
Z
zelig 已提交
189
which exposes a node admin interface as well as the DAPP JavaScript API.
Z
CLI:  
zelig 已提交
190 191 192 193 194
See https://github.com/ethereum/go-ethereum/wiki/Frontier-Console
`,
		},
		{
			Action: execJSFiles,
195
			Name:   "js",
O
obscuren 已提交
196
			Usage:  `executes the given JavaScript files in the Geth JavaScript VM`,
197
			Description: `
O
obscuren 已提交
198
The JavaScript VM exposes a node admin interface as well as the DAPP
Z
zelig 已提交
199
JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
200 201 202 203 204 205 206
`,
		},
		{
			Action: importchain,
			Name:   "import",
			Usage:  `import a blockchain file`,
		},
207 208 209 210 211
		{
			Action: exportchain,
			Name:   "export",
			Usage:  `export blockchain into file`,
		},
212 213 214 215 216
		{
			Action: upgradeDb,
			Name:   "upgradedb",
			Usage:  "upgrade chainblock database",
		},
217 218
	}
	app.Flags = []cli.Flag{
219
		utils.IdentityFlag,
220
		utils.UnlockedAccountFlag,
Z
zelig 已提交
221
		utils.PasswordFileFlag,
222 223
		utils.BootnodesFlag,
		utils.DataDirFlag,
224
		utils.BlockchainVersionFlag,
Z
CLI:  
zelig 已提交
225
		utils.JSpathFlag,
226 227
		utils.ListenPortFlag,
		utils.MaxPeersFlag,
Z
zelig 已提交
228
		utils.EtherbaseFlag,
229 230 231 232 233 234 235 236
		utils.MinerThreadsFlag,
		utils.MiningEnabledFlag,
		utils.NATFlag,
		utils.NodeKeyFileFlag,
		utils.NodeKeyHexFlag,
		utils.RPCEnabledFlag,
		utils.RPCListenAddrFlag,
		utils.RPCPortFlag,
237
		utils.WhisperEnabledFlag,
238
		utils.VMDebugFlag,
Z
zelig 已提交
239 240
		utils.ProtocolVersionFlag,
		utils.NetworkIdFlag,
241
		utils.RPCCORSDomainFlag,
O
obscuren 已提交
242
		utils.LogLevelFlag,
O
obscuren 已提交
243 244
		utils.BacktraceAtFlag,
		utils.LogToStdErrFlag,
O
obscuren 已提交
245 246 247
		utils.LogVModuleFlag,
		utils.LogFileFlag,
		utils.LogJSONFlag,
248
		utils.PProfDisabledFlag,
249
		utils.PProfPortFlag,
250
	}
251 252 253 254 255 256
	app.Before = func(ctx *cli.Context) error {
		if !ctx.GlobalBool(utils.PProfDisabledFlag.Name) {
			utils.StartPProf(ctx)
		}
		return nil
	}
257 258 259 260 261

	// missing:
	// flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
	// flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
	// flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
262

263 264 265 266 267
	// potential subcommands:
	// flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
	// flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
	// flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
}
O
obscuren 已提交
268

269
func main() {
O
obscuren 已提交
270
	fmt.Printf("Welcome to the FRONTIER\n")
271 272 273 274 275 276 277
	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 已提交
278

279
func run(ctx *cli.Context) {
280
	utils.HandleInterrupt()
281 282
	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	ethereum, err := eth.New(cfg)
283
	if err != nil {
284 285 286
		utils.Fatalf("%v", err)
	}

287
	startEth(ctx, ethereum)
288
	// this blocks the thread
289
	ethereum.WaitForShutdown()
290
}
291

Z
CLI:  
zelig 已提交
292
func console(ctx *cli.Context) {
293 294
	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	ethereum, err := eth.New(cfg)
295
	if err != nil {
296 297 298
		utils.Fatalf("%v", err)
	}

299
	startEth(ctx, ethereum)
300
	repl := newJSRE(ethereum, ctx.String(utils.JSpathFlag.Name), true)
Z
CLI:  
zelig 已提交
301 302 303 304 305 306 307 308 309 310 311
	repl.interactive()

	ethereum.Stop()
	ethereum.WaitForShutdown()
}

func execJSFiles(ctx *cli.Context) {
	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	ethereum, err := eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v", err)
O
obscuren 已提交
312
	}
Z
CLI:  
zelig 已提交
313 314

	startEth(ctx, ethereum)
315
	repl := newJSRE(ethereum, ctx.String(utils.JSpathFlag.Name), false)
Z
CLI:  
zelig 已提交
316 317 318 319
	for _, file := range ctx.Args() {
		repl.exec(file)
	}

320 321
	ethereum.Stop()
	ethereum.WaitForShutdown()
322
}
O
obscuren 已提交
323

Z
zelig 已提交
324
func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (passphrase string) {
325 326 327 328 329 330 331 332 333 334 335
	var err error
	// Load startup keys. XXX we are going to need a different format
	// Attempt to unlock the account
	passphrase = getPassPhrase(ctx, "", false)
	accbytes := common.FromHex(account)
	if len(accbytes) == 0 {
		utils.Fatalf("Invalid account address '%s'", account)
	}
	err = am.Unlock(accbytes, passphrase)
	if err != nil {
		utils.Fatalf("Unlock account failed '%v'", err)
336
	}
Z
zelig 已提交
337 338 339 340
	return
}

func startEth(ctx *cli.Context, eth *eth.Ethereum) {
341
	// Start Ethereum itself
Z
zelig 已提交
342 343 344 345 346
	utils.StartEthereum(eth)
	am := eth.AccountManager()

	account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
	if len(account) > 0 {
Z
zelig 已提交
347 348
		if account == "primary" {
			accbytes, err := am.Primary()
349
			if err != nil {
Z
zelig 已提交
350
				utils.Fatalf("no primary account: %v", err)
351 352
			}
			account = common.ToHex(accbytes)
Z
zelig 已提交
353
		}
Z
zelig 已提交
354 355
		unlockAccount(ctx, am, account)
	}
356
	// Start auxiliary services if enabled.
357
	if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
358
		utils.StartRPC(eth, ctx)
359 360
	}
	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
361
		eth.StartMining()
362 363
	}
}
O
Merge  
obscuren 已提交
364

F
Felix Lange 已提交
365 366 367 368 369 370 371
func accountList(ctx *cli.Context) {
	am := utils.GetAccountManager(ctx)
	accts, err := am.Accounts()
	if err != nil {
		utils.Fatalf("Could not list accounts: %v", err)
	}
	for _, acct := range accts {
O
obscuren 已提交
372
		fmt.Printf("Address: %x\n", acct)
F
Felix Lange 已提交
373 374 375
	}
}

376
func getPassPhrase(ctx *cli.Context, desc string, confirmation bool) (passphrase string) {
377 378 379 380 381 382 383 384 385
	passfile := ctx.GlobalString(utils.PasswordFileFlag.Name)
	if len(passfile) == 0 {
		fmt.Println(desc)
		auth, err := readPassword("Passphrase: ", true)
		if err != nil {
			utils.Fatalf("%v", err)
		}
		if confirmation {
			confirm, err := readPassword("Repeat Passphrase: ", false)
Z
zelig 已提交
386 387 388
			if err != nil {
				utils.Fatalf("%v", err)
			}
389 390
			if auth != confirm {
				utils.Fatalf("Passphrases did not match.")
Z
zelig 已提交
391
			}
392 393
		}
		passphrase = auth
Z
zelig 已提交
394

395 396 397 398
	} else {
		passbytes, err := ioutil.ReadFile(passfile)
		if err != nil {
			utils.Fatalf("Unable to read password file '%s': %v", passfile, err)
399
		}
400
		passphrase = string(passbytes)
F
Felix Lange 已提交
401
	}
Z
zelig 已提交
402 403 404 405 406
	return
}

func accountCreate(ctx *cli.Context) {
	am := utils.GetAccountManager(ctx)
407
	passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
408
	acct, err := am.NewAccount(passphrase)
F
Felix Lange 已提交
409 410 411
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
Z
zelig 已提交
412 413 414
	fmt.Printf("Address: %x\n", acct)
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
func importWallet(ctx *cli.Context) {
	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)
	}

	am := utils.GetAccountManager(ctx)
	passphrase := getPassPhrase(ctx, "", false)

	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 已提交
435 436 437 438 439 440
func accountImport(ctx *cli.Context) {
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
	am := utils.GetAccountManager(ctx)
441
	passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
Z
zelig 已提交
442 443 444 445 446 447 448
	acct, err := am.Import(keyfile, passphrase)
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
	fmt.Printf("Address: %x\n", acct)
}

449 450 451 452
func importchain(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		utils.Fatalf("This command requires an argument.")
	}
453 454 455 456 457 458 459 460 461 462

	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	cfg.SkipBcVersionCheck = true

	ethereum, err := eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v\n", err)
	}

	chainmgr := ethereum.ChainManager()
463
	start := time.Now()
464
	err = utils.ImportChain(chainmgr, ctx.Args().First())
O
obscuren 已提交
465
	if err != nil {
466
		utils.Fatalf("Import error: %v\n", err)
O
obscuren 已提交
467
	}
468 469 470 471 472 473

	// force database flush
	ethereum.BlockDb().Close()
	ethereum.StateDb().Close()
	ethereum.ExtraDb().Close()

474
	fmt.Printf("Import done in %v", time.Since(start))
475

476 477 478 479 480 481 482
	return
}

func exportchain(ctx *cli.Context) {
	if len(ctx.Args()) != 1 {
		utils.Fatalf("This command requires an argument.")
	}
483 484 485 486 487 488 489 490 491 492

	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	cfg.SkipBcVersionCheck = true

	ethereum, err := eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v\n", err)
	}

	chainmgr := ethereum.ChainManager()
493
	start := time.Now()
494
	err = utils.ExportChain(chainmgr, ctx.Args().First())
495 496 497 498
	if err != nil {
		utils.Fatalf("Export error: %v\n", err)
	}
	fmt.Printf("Export done in %v", time.Since(start))
499 500
	return
}
O
Merge  
obscuren 已提交
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
func upgradeDb(ctx *cli.Context) {
	fmt.Println("Upgrade blockchain DB")

	cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
	cfg.SkipBcVersionCheck = true

	ethereum, err := eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v\n", err)
	}

	v, _ := ethereum.BlockDb().Get([]byte("BlockchainVersion"))
	bcVersion := int(common.NewValue(v).Uint())

	if bcVersion == 0 {
		bcVersion = core.BlockChainVersion
	}

	filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("2006-01-02_15:04:05"))
	exportFile := path.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename)

	err = utils.ExportChain(ethereum.ChainManager(), exportFile)
	if err != nil {
		utils.Fatalf("Unable to export chain for reimport %s\n", err)
	}

	ethereum.BlockDb().Close()
	ethereum.StateDb().Close()
	ethereum.ExtraDb().Close()

	os.RemoveAll(path.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain"))

	ethereum, err = eth.New(cfg)
	if err != nil {
		utils.Fatalf("%v\n", err)
	}

	ethereum.BlockDb().Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes())

	err = utils.ImportChain(ethereum.ChainManager(), exportFile)
	if err != nil {
		utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)\n", err, exportFile)
	}

	// force database flush
	ethereum.BlockDb().Close()
	ethereum.StateDb().Close()
	ethereum.ExtraDb().Close()

	os.Remove(exportFile)

	fmt.Println("Import finished")
}

556
func dump(ctx *cli.Context) {
557
	chainmgr, _, stateDb := utils.GetChain(ctx)
558
	for _, arg := range ctx.Args() {
O
obscuren 已提交
559
		var block *types.Block
560
		if hashish(arg) {
O
obscuren 已提交
561
			block = chainmgr.GetBlock(common.HexToHash(arg))
O
obscuren 已提交
562
		} else {
563
			num, _ := strconv.Atoi(arg)
564
			block = chainmgr.GetBlockByNumber(uint64(num))
O
obscuren 已提交
565 566 567
		}
		if block == nil {
			fmt.Println("{}")
568 569
			utils.Fatalf("block not found")
		} else {
570
			statedb := state.New(block.Root(), stateDb)
571
			fmt.Printf("%s\n", statedb.Dump())
O
obscuren 已提交
572
		}
O
obscuren 已提交
573
	}
574
}
O
obscuren 已提交
575

576 577 578 579
func makedag(ctx *cli.Context) {
	chain, _, _ := utils.GetChain(ctx)
	pow := ethash.New(chain)
	fmt.Println("making cache")
O
obscuren 已提交
580
	pow.UpdateCache(0, true)
581 582 583 584
	fmt.Println("making DAG")
	pow.UpdateDAG()
}

585
func version(c *cli.Context) {
Z
zelig 已提交
586 587 588 589 590 591
	fmt.Printf(`%v
Version: %v
Protocol Version: %d
Network Id: %d
GO: %s
OS: %s
O
obscuren 已提交
592 593
GOPATH=%s
GOROOT=%s
Z
zelig 已提交
594
`, ClientIdentifier, Version, c.GlobalInt(utils.ProtocolVersionFlag.Name), c.GlobalInt(utils.NetworkIdFlag.Name), runtime.Version(), runtime.GOOS, os.Getenv("GOPATH"), runtime.GOROOT())
O
obscuren 已提交
595
}
F
Felix Lange 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

// hashish returns true for strings that look like hashes.
func hashish(x string) bool {
	_, err := strconv.Atoi(x)
	return err != nil
}

func readPassword(prompt string, warnTerm bool) (string, error) {
	if liner.TerminalSupported() {
		lr := liner.NewLiner()
		defer lr.Close()
		return lr.PasswordPrompt(prompt)
	}
	if warnTerm {
		fmt.Println("!! Unsupported terminal, password will be echoed.")
	}
	fmt.Print(prompt)
	input, err := bufio.NewReader(os.Stdin).ReadString('\n')
	fmt.Println()
	return input, err
}