main.go 20 KB

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