main.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // 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/>.
  16. // geth is the official command-line client for Ethereum.
  17. package main
  18. import (
  19. "fmt"
  20. "io/ioutil"
  21. _ "net/http/pprof"
  22. "os"
  23. "path/filepath"
  24. "runtime"
  25. "strconv"
  26. "strings"
  27. "time"
  28. "github.com/codegangsta/cli"
  29. "github.com/ethereum/ethash"
  30. "github.com/ethereum/go-ethereum/accounts"
  31. "github.com/ethereum/go-ethereum/cmd/utils"
  32. "github.com/ethereum/go-ethereum/common"
  33. "github.com/ethereum/go-ethereum/core"
  34. "github.com/ethereum/go-ethereum/core/types"
  35. "github.com/ethereum/go-ethereum/eth"
  36. "github.com/ethereum/go-ethereum/ethdb"
  37. "github.com/ethereum/go-ethereum/logger"
  38. "github.com/ethereum/go-ethereum/logger/glog"
  39. "github.com/ethereum/go-ethereum/metrics"
  40. "github.com/ethereum/go-ethereum/params"
  41. "github.com/ethereum/go-ethereum/rlp"
  42. "github.com/ethereum/go-ethereum/rpc/codec"
  43. "github.com/ethereum/go-ethereum/rpc/comms"
  44. )
  45. const (
  46. ClientIdentifier = "Geth"
  47. Version = "1.3.0-dev"
  48. VersionMajor = 1
  49. VersionMinor = 3
  50. VersionPatch = 0
  51. )
  52. var (
  53. gitCommit string // set via linker flagg
  54. nodeNameVersion string
  55. app *cli.App
  56. )
  57. func init() {
  58. if gitCommit == "" {
  59. nodeNameVersion = Version
  60. } else {
  61. nodeNameVersion = Version + "-" + gitCommit[:8]
  62. }
  63. app = utils.NewApp(Version, "the go-ethereum command line interface")
  64. app.Action = run
  65. app.HideVersion = true // we have a command to print the version
  66. app.Commands = []cli.Command{
  67. {
  68. Action: blockRecovery,
  69. Name: "recover",
  70. Usage: "attempts to recover a corrupted database by setting a new block by number or hash. See help recover.",
  71. Description: `
  72. The recover commands will attempt to read out the last
  73. block based on that.
  74. recover #number recovers by number
  75. recover <hex> recovers by hash
  76. `,
  77. },
  78. blocktestCommand,
  79. importCommand,
  80. exportCommand,
  81. upgradedbCommand,
  82. removedbCommand,
  83. dumpCommand,
  84. monitorCommand,
  85. {
  86. Action: makedag,
  87. Name: "makedag",
  88. Usage: "generate ethash dag (for testing)",
  89. Description: `
  90. The makedag command generates an ethash DAG in /tmp/dag.
  91. This command exists to support the system testing project.
  92. Regular users do not need to execute it.
  93. `,
  94. },
  95. {
  96. Action: gpuinfo,
  97. Name: "gpuinfo",
  98. Usage: "gpuinfo",
  99. Description: `
  100. Prints OpenCL device info for all found GPUs.
  101. `,
  102. },
  103. {
  104. Action: gpubench,
  105. Name: "gpubench",
  106. Usage: "benchmark GPU",
  107. Description: `
  108. Runs quick benchmark on first GPU found.
  109. `,
  110. },
  111. {
  112. Action: version,
  113. Name: "version",
  114. Usage: "print ethereum version numbers",
  115. Description: `
  116. The output of this command is supposed to be machine-readable.
  117. `,
  118. },
  119. {
  120. Name: "wallet",
  121. Usage: "ethereum presale wallet",
  122. Subcommands: []cli.Command{
  123. {
  124. Action: importWallet,
  125. Name: "import",
  126. Usage: "import ethereum presale wallet",
  127. },
  128. },
  129. Description: `
  130. get wallet import /path/to/my/presale.wallet
  131. will prompt for your password and imports your ether presale account.
  132. It can be used non-interactively with the --password option taking a
  133. passwordfile as argument containing the wallet password in plaintext.
  134. `},
  135. {
  136. Action: accountList,
  137. Name: "account",
  138. Usage: "manage accounts",
  139. Description: `
  140. Manage accounts lets you create new accounts, list all existing accounts,
  141. import a private key into a new account.
  142. ' help' shows a list of subcommands or help for one subcommand.
  143. It supports interactive mode, when you are prompted for password as well as
  144. non-interactive mode where passwords are supplied via a given password file.
  145. Non-interactive mode is only meant for scripted use on test networks or known
  146. safe environments.
  147. Make sure you remember the password you gave when creating a new account (with
  148. either new or import). Without it you are not able to unlock your account.
  149. Note that exporting your key in unencrypted format is NOT supported.
  150. Keys are stored under <DATADIR>/keys.
  151. It is safe to transfer the entire directory or the individual keys therein
  152. between ethereum nodes by simply copying.
  153. Make sure you backup your keys regularly.
  154. In order to use your account to send transactions, you need to unlock them using
  155. the '--unlock' option. The argument is a space separated list of addresses or
  156. indexes. If used non-interactively with a passwordfile, the file should contain
  157. the respective passwords one per line. If you unlock n accounts and the password
  158. file contains less than n entries, then the last password is meant to apply to
  159. all remaining accounts.
  160. And finally. DO NOT FORGET YOUR PASSWORD.
  161. `,
  162. Subcommands: []cli.Command{
  163. {
  164. Action: accountList,
  165. Name: "list",
  166. Usage: "print account addresses",
  167. },
  168. {
  169. Action: accountCreate,
  170. Name: "new",
  171. Usage: "create a new account",
  172. Description: `
  173. ethereum account new
  174. Creates a new account. Prints the address.
  175. The account is saved in encrypted format, you are prompted for a passphrase.
  176. You must remember this passphrase to unlock your account in the future.
  177. For non-interactive use the passphrase can be specified with the --password flag:
  178. ethereum --password <passwordfile> account new
  179. Note, this is meant to be used for testing only, it is a bad idea to save your
  180. password to file or expose in any other way.
  181. `,
  182. },
  183. {
  184. Action: accountUpdate,
  185. Name: "update",
  186. Usage: "update an existing account",
  187. Description: `
  188. ethereum account update <address>
  189. Update an existing account.
  190. The account is saved in the newest version in encrypted format, you are prompted
  191. for a passphrase to unlock the account and another to save the updated file.
  192. This same command can therefore be used to migrate an account of a deprecated
  193. format to the newest format or change the password for an account.
  194. For non-interactive use the passphrase can be specified with the --password flag:
  195. ethereum --password <passwordfile> account update <address>
  196. Since only one password can be given, only format update can be performed,
  197. changing your password is only possible interactively.
  198. Note that account update has the a side effect that the order of your accounts
  199. changes.
  200. `,
  201. },
  202. {
  203. Action: accountImport,
  204. Name: "import",
  205. Usage: "import a private key into a new account",
  206. Description: `
  207. ethereum account import <keyfile>
  208. Imports an unencrypted private key from <keyfile> and creates a new account.
  209. Prints the address.
  210. The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
  211. The account is saved in encrypted format, you are prompted for a passphrase.
  212. You must remember this passphrase to unlock your account in the future.
  213. For non-interactive use the passphrase can be specified with the -password flag:
  214. ethereum --password <passwordfile> account import <keyfile>
  215. Note:
  216. As you can directly copy your encrypted accounts to another ethereum instance,
  217. this import mechanism is not needed when you transfer an account between
  218. nodes.
  219. `,
  220. },
  221. },
  222. },
  223. {
  224. Action: console,
  225. Name: "console",
  226. Usage: `Geth Console: interactive JavaScript environment`,
  227. Description: `
  228. The Geth console is an interactive shell for the JavaScript runtime environment
  229. which exposes a node admin interface as well as the Ðapp JavaScript API.
  230. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
  231. `},
  232. {
  233. Action: attach,
  234. Name: "attach",
  235. Usage: `Geth Console: interactive JavaScript environment (connect to node)`,
  236. Description: `
  237. The Geth console is an interactive shell for the JavaScript runtime environment
  238. which exposes a node admin interface as well as the Ðapp JavaScript API.
  239. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console.
  240. This command allows to open a console on a running geth node.
  241. `,
  242. },
  243. {
  244. Action: execJSFiles,
  245. Name: "js",
  246. Usage: `executes the given JavaScript files in the Geth JavaScript VM`,
  247. Description: `
  248. The JavaScript VM exposes a node admin interface as well as the Ðapp
  249. JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Console
  250. `,
  251. },
  252. }
  253. app.Flags = []cli.Flag{
  254. utils.IdentityFlag,
  255. utils.UnlockedAccountFlag,
  256. utils.PasswordFileFlag,
  257. utils.GenesisFileFlag,
  258. utils.BootnodesFlag,
  259. utils.DataDirFlag,
  260. utils.BlockchainVersionFlag,
  261. utils.OlympicFlag,
  262. utils.FastSyncFlag,
  263. utils.CacheFlag,
  264. utils.JSpathFlag,
  265. utils.ListenPortFlag,
  266. utils.MaxPeersFlag,
  267. utils.MaxPendingPeersFlag,
  268. utils.EtherbaseFlag,
  269. utils.GasPriceFlag,
  270. utils.MinerThreadsFlag,
  271. utils.MiningEnabledFlag,
  272. utils.MiningGPUFlag,
  273. utils.AutoDAGFlag,
  274. utils.NATFlag,
  275. utils.NatspecEnabledFlag,
  276. utils.NoDiscoverFlag,
  277. utils.NodeKeyFileFlag,
  278. utils.NodeKeyHexFlag,
  279. utils.RPCEnabledFlag,
  280. utils.RPCListenAddrFlag,
  281. utils.RPCPortFlag,
  282. utils.RpcApiFlag,
  283. utils.IPCDisabledFlag,
  284. utils.IPCApiFlag,
  285. utils.IPCPathFlag,
  286. utils.ExecFlag,
  287. utils.WhisperEnabledFlag,
  288. utils.DevModeFlag,
  289. utils.TestNetFlag,
  290. utils.VMDebugFlag,
  291. utils.VMForceJitFlag,
  292. utils.VMJitCacheFlag,
  293. utils.VMEnableJitFlag,
  294. utils.NetworkIdFlag,
  295. utils.RPCCORSDomainFlag,
  296. utils.VerbosityFlag,
  297. utils.BacktraceAtFlag,
  298. utils.LogToStdErrFlag,
  299. utils.LogVModuleFlag,
  300. utils.LogFileFlag,
  301. utils.LogJSONFlag,
  302. utils.PProfEanbledFlag,
  303. utils.PProfPortFlag,
  304. utils.MetricsEnabledFlag,
  305. utils.SolcPathFlag,
  306. utils.GpoMinGasPriceFlag,
  307. utils.GpoMaxGasPriceFlag,
  308. utils.GpoFullBlockRatioFlag,
  309. utils.GpobaseStepDownFlag,
  310. utils.GpobaseStepUpFlag,
  311. utils.GpobaseCorrectionFactorFlag,
  312. utils.ExtraDataFlag,
  313. }
  314. app.Before = func(ctx *cli.Context) error {
  315. utils.SetupLogger(ctx)
  316. utils.SetupNetwork(ctx)
  317. utils.SetupVM(ctx)
  318. if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
  319. utils.StartPProf(ctx)
  320. }
  321. return nil
  322. }
  323. // Start system runtime metrics collection
  324. go metrics.CollectProcessMetrics(3 * time.Second)
  325. }
  326. func main() {
  327. runtime.GOMAXPROCS(runtime.NumCPU())
  328. defer logger.Flush()
  329. if err := app.Run(os.Args); err != nil {
  330. fmt.Fprintln(os.Stderr, err)
  331. os.Exit(1)
  332. }
  333. }
  334. // makeExtra resolves extradata for the miner from a flag or returns a default.
  335. func makeExtra(ctx *cli.Context) []byte {
  336. if ctx.GlobalIsSet(utils.ExtraDataFlag.Name) {
  337. return []byte(ctx.GlobalString(utils.ExtraDataFlag.Name))
  338. }
  339. return makeDefaultExtra()
  340. }
  341. func makeDefaultExtra() []byte {
  342. var clientInfo = struct {
  343. Version uint
  344. Name string
  345. GoVersion string
  346. Os string
  347. }{uint(VersionMajor<<16 | VersionMinor<<8 | VersionPatch), ClientIdentifier, runtime.Version(), runtime.GOOS}
  348. extra, err := rlp.EncodeToBytes(clientInfo)
  349. if err != nil {
  350. glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
  351. }
  352. if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
  353. glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
  354. glog.V(logger.Debug).Infof("extra: %x\n", extra)
  355. return nil
  356. }
  357. return extra
  358. }
  359. func run(ctx *cli.Context) {
  360. utils.CheckLegalese(utils.MustDataDir(ctx))
  361. cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
  362. cfg.ExtraData = makeExtra(ctx)
  363. ethereum, err := eth.New(cfg)
  364. if err != nil {
  365. utils.Fatalf("%v", err)
  366. }
  367. startEth(ctx, ethereum)
  368. // this blocks the thread
  369. ethereum.WaitForShutdown()
  370. }
  371. func attach(ctx *cli.Context) {
  372. utils.CheckLegalese(utils.MustDataDir(ctx))
  373. var client comms.EthereumClient
  374. var err error
  375. if ctx.Args().Present() {
  376. client, err = comms.ClientFromEndpoint(ctx.Args().First(), codec.JSON)
  377. } else {
  378. cfg := comms.IpcConfig{
  379. Endpoint: utils.IpcSocketPath(ctx),
  380. }
  381. client, err = comms.NewIpcClient(cfg, codec.JSON)
  382. }
  383. if err != nil {
  384. utils.Fatalf("Unable to attach to geth node - %v", err)
  385. }
  386. repl := newLightweightJSRE(
  387. ctx.GlobalString(utils.JSpathFlag.Name),
  388. client,
  389. ctx.GlobalString(utils.DataDirFlag.Name),
  390. true,
  391. )
  392. if ctx.GlobalString(utils.ExecFlag.Name) != "" {
  393. repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
  394. } else {
  395. repl.welcome()
  396. repl.interactive()
  397. }
  398. }
  399. func console(ctx *cli.Context) {
  400. utils.CheckLegalese(utils.MustDataDir(ctx))
  401. cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
  402. cfg.ExtraData = makeExtra(ctx)
  403. ethereum, err := eth.New(cfg)
  404. if err != nil {
  405. utils.Fatalf("%v", err)
  406. }
  407. client := comms.NewInProcClient(codec.JSON)
  408. startEth(ctx, ethereum)
  409. repl := newJSRE(
  410. ethereum,
  411. ctx.GlobalString(utils.JSpathFlag.Name),
  412. ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
  413. client,
  414. true,
  415. nil,
  416. )
  417. if ctx.GlobalString(utils.ExecFlag.Name) != "" {
  418. repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
  419. } else {
  420. repl.welcome()
  421. repl.interactive()
  422. }
  423. ethereum.Stop()
  424. ethereum.WaitForShutdown()
  425. }
  426. func execJSFiles(ctx *cli.Context) {
  427. utils.CheckLegalese(utils.MustDataDir(ctx))
  428. cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
  429. ethereum, err := eth.New(cfg)
  430. if err != nil {
  431. utils.Fatalf("%v", err)
  432. }
  433. client := comms.NewInProcClient(codec.JSON)
  434. startEth(ctx, ethereum)
  435. repl := newJSRE(
  436. ethereum,
  437. ctx.GlobalString(utils.JSpathFlag.Name),
  438. ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
  439. client,
  440. false,
  441. nil,
  442. )
  443. for _, file := range ctx.Args() {
  444. repl.exec(file)
  445. }
  446. ethereum.Stop()
  447. ethereum.WaitForShutdown()
  448. }
  449. func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int, inputpassphrases []string) (addrHex, auth string, passphrases []string) {
  450. utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
  451. var err error
  452. passphrases = inputpassphrases
  453. addrHex, err = utils.ParamToAddress(addr, am)
  454. if err == nil {
  455. // Attempt to unlock the account 3 times
  456. attempts := 3
  457. for tries := 0; tries < attempts; tries++ {
  458. msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts)
  459. auth, passphrases = getPassPhrase(ctx, msg, false, i, passphrases)
  460. err = am.Unlock(common.HexToAddress(addrHex), auth)
  461. if err == nil || passphrases != nil {
  462. break
  463. }
  464. }
  465. }
  466. if err != nil {
  467. utils.Fatalf("Unlock account '%s' (%v) failed: %v", addr, addrHex, err)
  468. }
  469. fmt.Printf("Account '%s' (%v) unlocked.\n", addr, addrHex)
  470. return
  471. }
  472. func blockRecovery(ctx *cli.Context) {
  473. utils.CheckLegalese(utils.MustDataDir(ctx))
  474. arg := ctx.Args().First()
  475. if len(ctx.Args()) < 1 && len(arg) > 0 {
  476. glog.Fatal("recover requires block number or hash")
  477. }
  478. cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
  479. utils.CheckLegalese(cfg.DataDir)
  480. blockDb, err := ethdb.NewLDBDatabase(filepath.Join(cfg.DataDir, "blockchain"), cfg.DatabaseCache)
  481. if err != nil {
  482. glog.Fatalln("could not open db:", err)
  483. }
  484. var block *types.Block
  485. if arg[0] == '#' {
  486. block = core.GetBlock(blockDb, core.GetCanonicalHash(blockDb, common.String2Big(arg[1:]).Uint64()))
  487. } else {
  488. block = core.GetBlock(blockDb, common.HexToHash(arg))
  489. }
  490. if block == nil {
  491. glog.Fatalln("block not found. Recovery failed")
  492. }
  493. if err = core.WriteHeadBlockHash(blockDb, block.Hash()); err != nil {
  494. glog.Fatalln("block write err", err)
  495. }
  496. glog.Infof("Recovery succesful. New HEAD %x\n", block.Hash())
  497. }
  498. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  499. // Start Ethereum itself
  500. utils.StartEthereum(eth)
  501. am := eth.AccountManager()
  502. account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
  503. accounts := strings.Split(account, " ")
  504. var passphrases []string
  505. for i, account := range accounts {
  506. if len(account) > 0 {
  507. if account == "primary" {
  508. 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.")
  509. }
  510. _, _, passphrases = unlockAccount(ctx, am, account, i, passphrases)
  511. }
  512. }
  513. // Start auxiliary services if enabled.
  514. if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) {
  515. if err := utils.StartIPC(eth, ctx); err != nil {
  516. utils.Fatalf("Error string IPC: %v", err)
  517. }
  518. }
  519. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  520. if err := utils.StartRPC(eth, ctx); err != nil {
  521. utils.Fatalf("Error starting RPC: %v", err)
  522. }
  523. }
  524. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  525. err := eth.StartMining(
  526. ctx.GlobalInt(utils.MinerThreadsFlag.Name),
  527. ctx.GlobalString(utils.MiningGPUFlag.Name))
  528. if err != nil {
  529. utils.Fatalf("%v", err)
  530. }
  531. }
  532. }
  533. func accountList(ctx *cli.Context) {
  534. utils.CheckLegalese(utils.MustDataDir(ctx))
  535. am := utils.MakeAccountManager(ctx)
  536. accts, err := am.Accounts()
  537. if err != nil {
  538. utils.Fatalf("Could not list accounts: %v", err)
  539. }
  540. for i, acct := range accts {
  541. fmt.Printf("Account #%d: %x\n", i, acct)
  542. }
  543. }
  544. func getPassPhrase(ctx *cli.Context, desc string, confirmation bool, i int, inputpassphrases []string) (passphrase string, passphrases []string) {
  545. passfile := ctx.GlobalString(utils.PasswordFileFlag.Name)
  546. if len(passfile) == 0 {
  547. fmt.Println(desc)
  548. auth, err := utils.PromptPassword("Passphrase: ", true)
  549. if err != nil {
  550. utils.Fatalf("%v", err)
  551. }
  552. if confirmation {
  553. confirm, err := utils.PromptPassword("Repeat Passphrase: ", false)
  554. if err != nil {
  555. utils.Fatalf("%v", err)
  556. }
  557. if auth != confirm {
  558. utils.Fatalf("Passphrases did not match.")
  559. }
  560. }
  561. passphrase = auth
  562. } else {
  563. passphrases = inputpassphrases
  564. if passphrases == nil {
  565. passbytes, err := ioutil.ReadFile(passfile)
  566. if err != nil {
  567. utils.Fatalf("Unable to read password file '%s': %v", passfile, err)
  568. }
  569. // this is backwards compatible if the same password unlocks several accounts
  570. // it also has the consequence that trailing newlines will not count as part
  571. // of the password, so --password <(echo -n 'pass') will now work without -n
  572. passphrases = strings.Split(string(passbytes), "\n")
  573. }
  574. if i >= len(passphrases) {
  575. passphrase = passphrases[len(passphrases)-1]
  576. } else {
  577. passphrase = passphrases[i]
  578. }
  579. }
  580. return
  581. }
  582. func accountCreate(ctx *cli.Context) {
  583. utils.CheckLegalese(utils.MustDataDir(ctx))
  584. am := utils.MakeAccountManager(ctx)
  585. passphrase, _ := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, nil)
  586. acct, err := am.NewAccount(passphrase)
  587. if err != nil {
  588. utils.Fatalf("Could not create the account: %v", err)
  589. }
  590. fmt.Printf("Address: %x\n", acct)
  591. }
  592. func accountUpdate(ctx *cli.Context) {
  593. utils.CheckLegalese(utils.MustDataDir(ctx))
  594. am := utils.MakeAccountManager(ctx)
  595. arg := ctx.Args().First()
  596. if len(arg) == 0 {
  597. utils.Fatalf("account address or index must be given as argument")
  598. }
  599. addr, authFrom, passphrases := unlockAccount(ctx, am, arg, 0, nil)
  600. authTo, _ := getPassPhrase(ctx, "Please give a new password. Do not forget this password.", true, 0, passphrases)
  601. err := am.Update(common.HexToAddress(addr), authFrom, authTo)
  602. if err != nil {
  603. utils.Fatalf("Could not update the account: %v", err)
  604. }
  605. }
  606. func importWallet(ctx *cli.Context) {
  607. utils.CheckLegalese(utils.MustDataDir(ctx))
  608. keyfile := ctx.Args().First()
  609. if len(keyfile) == 0 {
  610. utils.Fatalf("keyfile must be given as argument")
  611. }
  612. keyJson, err := ioutil.ReadFile(keyfile)
  613. if err != nil {
  614. utils.Fatalf("Could not read wallet file: %v", err)
  615. }
  616. am := utils.MakeAccountManager(ctx)
  617. passphrase, _ := getPassPhrase(ctx, "", false, 0, nil)
  618. acct, err := am.ImportPreSaleKey(keyJson, passphrase)
  619. if err != nil {
  620. utils.Fatalf("Could not create the account: %v", err)
  621. }
  622. fmt.Printf("Address: %x\n", acct)
  623. }
  624. func accountImport(ctx *cli.Context) {
  625. utils.CheckLegalese(utils.MustDataDir(ctx))
  626. keyfile := ctx.Args().First()
  627. if len(keyfile) == 0 {
  628. utils.Fatalf("keyfile must be given as argument")
  629. }
  630. am := utils.MakeAccountManager(ctx)
  631. passphrase, _ := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, nil)
  632. acct, err := am.Import(keyfile, passphrase)
  633. if err != nil {
  634. utils.Fatalf("Could not create the account: %v", err)
  635. }
  636. fmt.Printf("Address: %x\n", acct)
  637. }
  638. func makedag(ctx *cli.Context) {
  639. utils.CheckLegalese(utils.MustDataDir(ctx))
  640. args := ctx.Args()
  641. wrongArgs := func() {
  642. utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
  643. }
  644. switch {
  645. case len(args) == 2:
  646. blockNum, err := strconv.ParseUint(args[0], 0, 64)
  647. dir := args[1]
  648. if err != nil {
  649. wrongArgs()
  650. } else {
  651. dir = filepath.Clean(dir)
  652. // seems to require a trailing slash
  653. if !strings.HasSuffix(dir, "/") {
  654. dir = dir + "/"
  655. }
  656. _, err = ioutil.ReadDir(dir)
  657. if err != nil {
  658. utils.Fatalf("Can't find dir")
  659. }
  660. fmt.Println("making DAG, this could take awhile...")
  661. ethash.MakeDAG(blockNum, dir)
  662. }
  663. default:
  664. wrongArgs()
  665. }
  666. }
  667. func gpuinfo(ctx *cli.Context) {
  668. eth.PrintOpenCLDevices()
  669. }
  670. func gpubench(ctx *cli.Context) {
  671. args := ctx.Args()
  672. wrongArgs := func() {
  673. utils.Fatalf(`Usage: geth gpubench <gpu number>`)
  674. }
  675. switch {
  676. case len(args) == 1:
  677. n, err := strconv.ParseUint(args[0], 0, 64)
  678. if err != nil {
  679. wrongArgs()
  680. }
  681. eth.GPUBench(n)
  682. case len(args) == 0:
  683. eth.GPUBench(0)
  684. default:
  685. wrongArgs()
  686. }
  687. }
  688. func version(c *cli.Context) {
  689. fmt.Println(ClientIdentifier)
  690. fmt.Println("Version:", Version)
  691. if gitCommit != "" {
  692. fmt.Println("Git Commit:", gitCommit)
  693. }
  694. fmt.Println("Protocol Versions:", eth.ProtocolVersions)
  695. fmt.Println("Network Id:", c.GlobalInt(utils.NetworkIdFlag.Name))
  696. fmt.Println("Go Version:", runtime.Version())
  697. fmt.Println("OS:", runtime.GOOS)
  698. fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
  699. fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
  700. }