main.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "bufio"
  21. "fmt"
  22. "os"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "github.com/codegangsta/cli"
  28. "github.com/ethereum/ethash"
  29. "github.com/ethereum/go-ethereum/cmd/utils"
  30. "github.com/ethereum/go-ethereum/common"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/eth"
  33. "github.com/ethereum/go-ethereum/logger"
  34. "github.com/ethereum/go-ethereum/state"
  35. "github.com/peterh/liner"
  36. )
  37. const (
  38. ClientIdentifier = "Ethereum(G)"
  39. Version = "0.9.1"
  40. )
  41. var (
  42. clilogger = logger.NewLogger("CLI")
  43. app = utils.NewApp(Version, "the go-ethereum command line interface")
  44. )
  45. func init() {
  46. app.Action = run
  47. app.HideVersion = true // we have a command to print the version
  48. app.Commands = []cli.Command{
  49. blocktestCmd,
  50. {
  51. Action: makedag,
  52. Name: "makedag",
  53. Usage: "generate ethash dag (for testing)",
  54. Description: `
  55. The makedag command generates an ethash DAG in /tmp/dag.
  56. This command exists to support the system testing project.
  57. Regular users do not need to execute it.
  58. `,
  59. },
  60. {
  61. Action: version,
  62. Name: "version",
  63. Usage: "print ethereum version numbers",
  64. Description: `
  65. The output of this command is supposed to be machine-readable.
  66. `,
  67. },
  68. {
  69. Action: accountList,
  70. Name: "account",
  71. Usage: "manage accounts",
  72. Subcommands: []cli.Command{
  73. {
  74. Action: accountList,
  75. Name: "list",
  76. Usage: "print account addresses",
  77. },
  78. {
  79. Action: accountCreate,
  80. Name: "new",
  81. Usage: "create a new account",
  82. },
  83. },
  84. },
  85. {
  86. Action: dump,
  87. Name: "dump",
  88. Usage: `dump a specific block from storage`,
  89. Description: `
  90. The arguments are interpreted as block numbers or hashes.
  91. Use "ethereum dump 0" to dump the genesis block.
  92. `,
  93. },
  94. {
  95. Action: console,
  96. Name: "console",
  97. Usage: `Ethereum Console: interactive JavaScript environment`,
  98. Description: `
  99. Console is an interactive shell for the Ethereum JavaScript runtime environment which exposes a node admin interface as well as the DAPP JavaScript API.
  100. See https://github.com/ethereum/go-ethereum/wiki/Frontier-Console
  101. `,
  102. },
  103. {
  104. Action: execJSFiles,
  105. Name: "js",
  106. Usage: `executes the given JavaScript files in the Ethereum Frontier JavaScript VM`,
  107. Description: `
  108. The Ethereum JavaScript VM exposes a node admin interface as well as the DAPP JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Frontier-Console
  109. `,
  110. },
  111. {
  112. Action: importchain,
  113. Name: "import",
  114. Usage: `import a blockchain file`,
  115. },
  116. {
  117. Action: exportchain,
  118. Name: "export",
  119. Usage: `export blockchain into file`,
  120. },
  121. }
  122. app.Flags = []cli.Flag{
  123. utils.UnlockedAccountFlag,
  124. utils.BootnodesFlag,
  125. utils.DataDirFlag,
  126. utils.JSpathFlag,
  127. utils.ListenPortFlag,
  128. utils.LogFileFlag,
  129. utils.LogFormatFlag,
  130. utils.LogLevelFlag,
  131. utils.MaxPeersFlag,
  132. utils.MinerThreadsFlag,
  133. utils.MiningEnabledFlag,
  134. utils.NATFlag,
  135. utils.NodeKeyFileFlag,
  136. utils.NodeKeyHexFlag,
  137. utils.RPCEnabledFlag,
  138. utils.RPCListenAddrFlag,
  139. utils.RPCPortFlag,
  140. utils.UnencryptedKeysFlag,
  141. utils.VMDebugFlag,
  142. utils.ProtocolVersionFlag,
  143. utils.NetworkIdFlag,
  144. }
  145. // missing:
  146. // flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
  147. // flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
  148. // flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
  149. // potential subcommands:
  150. // flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
  151. // flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
  152. // flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
  153. }
  154. func main() {
  155. runtime.GOMAXPROCS(runtime.NumCPU())
  156. defer logger.Flush()
  157. if err := app.Run(os.Args); err != nil {
  158. fmt.Fprintln(os.Stderr, err)
  159. os.Exit(1)
  160. }
  161. }
  162. func run(ctx *cli.Context) {
  163. fmt.Printf("Welcome to the FRONTIER\n")
  164. utils.HandleInterrupt()
  165. cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
  166. ethereum, err := eth.New(cfg)
  167. if err != nil {
  168. utils.Fatalf("%v", err)
  169. }
  170. startEth(ctx, ethereum)
  171. // this blocks the thread
  172. ethereum.WaitForShutdown()
  173. }
  174. func console(ctx *cli.Context) {
  175. cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
  176. ethereum, err := eth.New(cfg)
  177. if err != nil {
  178. utils.Fatalf("%v", err)
  179. }
  180. startEth(ctx, ethereum)
  181. repl := newJSRE(ethereum, ctx.String(utils.JSpathFlag.Name))
  182. repl.interactive()
  183. ethereum.Stop()
  184. ethereum.WaitForShutdown()
  185. }
  186. func execJSFiles(ctx *cli.Context) {
  187. cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
  188. ethereum, err := eth.New(cfg)
  189. if err != nil {
  190. utils.Fatalf("%v", err)
  191. }
  192. startEth(ctx, ethereum)
  193. repl := newJSRE(ethereum, ctx.String(utils.JSpathFlag.Name))
  194. for _, file := range ctx.Args() {
  195. repl.exec(file)
  196. }
  197. ethereum.Stop()
  198. ethereum.WaitForShutdown()
  199. }
  200. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  201. utils.StartEthereum(eth)
  202. // Load startup keys. XXX we are going to need a different format
  203. account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
  204. if len(account) > 0 {
  205. split := strings.Split(account, ":")
  206. if len(split) != 2 {
  207. utils.Fatalf("Illegal 'unlock' format (address:password)")
  208. }
  209. am := eth.AccountManager()
  210. // Attempt to unlock the account
  211. err := am.Unlock(common.FromHex(split[0]), split[1])
  212. if err != nil {
  213. utils.Fatalf("Unlock account failed '%v'", err)
  214. }
  215. }
  216. // Start auxiliary services if enabled.
  217. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  218. utils.StartRPC(eth, ctx)
  219. }
  220. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  221. eth.StartMining()
  222. }
  223. }
  224. func accountList(ctx *cli.Context) {
  225. am := utils.GetAccountManager(ctx)
  226. accts, err := am.Accounts()
  227. if err != nil {
  228. utils.Fatalf("Could not list accounts: %v", err)
  229. }
  230. for _, acct := range accts {
  231. fmt.Printf("Address: %#x\n", acct)
  232. }
  233. }
  234. func accountCreate(ctx *cli.Context) {
  235. am := utils.GetAccountManager(ctx)
  236. passphrase := ""
  237. if !ctx.GlobalBool(utils.UnencryptedKeysFlag.Name) {
  238. fmt.Println("The new account will be encrypted with a passphrase.")
  239. fmt.Println("Please enter a passphrase now.")
  240. auth, err := readPassword("Passphrase: ", true)
  241. if err != nil {
  242. utils.Fatalf("%v", err)
  243. }
  244. confirm, err := readPassword("Repeat Passphrase: ", false)
  245. if err != nil {
  246. utils.Fatalf("%v", err)
  247. }
  248. if auth != confirm {
  249. utils.Fatalf("Passphrases did not match.")
  250. }
  251. passphrase = auth
  252. }
  253. acct, err := am.NewAccount(passphrase)
  254. if err != nil {
  255. utils.Fatalf("Could not create the account: %v", err)
  256. }
  257. fmt.Printf("Address: %#x\n", acct.Address)
  258. }
  259. func importchain(ctx *cli.Context) {
  260. if len(ctx.Args()) != 1 {
  261. utils.Fatalf("This command requires an argument.")
  262. }
  263. chainmgr, _, _ := utils.GetChain(ctx)
  264. start := time.Now()
  265. err := utils.ImportChain(chainmgr, ctx.Args().First())
  266. if err != nil {
  267. utils.Fatalf("Import error: %v\n", err)
  268. }
  269. fmt.Printf("Import done in %v", time.Since(start))
  270. return
  271. }
  272. func exportchain(ctx *cli.Context) {
  273. if len(ctx.Args()) != 1 {
  274. utils.Fatalf("This command requires an argument.")
  275. }
  276. chainmgr, _, _ := utils.GetChain(ctx)
  277. start := time.Now()
  278. err := utils.ExportChain(chainmgr, ctx.Args().First())
  279. if err != nil {
  280. utils.Fatalf("Export error: %v\n", err)
  281. }
  282. fmt.Printf("Export done in %v", time.Since(start))
  283. return
  284. }
  285. func dump(ctx *cli.Context) {
  286. chainmgr, _, stateDb := utils.GetChain(ctx)
  287. for _, arg := range ctx.Args() {
  288. var block *types.Block
  289. if hashish(arg) {
  290. block = chainmgr.GetBlock(common.HexToHash(arg))
  291. } else {
  292. num, _ := strconv.Atoi(arg)
  293. block = chainmgr.GetBlockByNumber(uint64(num))
  294. }
  295. if block == nil {
  296. fmt.Println("{}")
  297. utils.Fatalf("block not found")
  298. } else {
  299. statedb := state.New(block.Root(), stateDb)
  300. fmt.Printf("%s\n", statedb.Dump())
  301. // fmt.Println(block)
  302. }
  303. }
  304. }
  305. func makedag(ctx *cli.Context) {
  306. chain, _, _ := utils.GetChain(ctx)
  307. pow := ethash.New(chain)
  308. fmt.Println("making cache")
  309. pow.UpdateCache(true)
  310. fmt.Println("making DAG")
  311. pow.UpdateDAG()
  312. }
  313. func version(c *cli.Context) {
  314. fmt.Printf(`%v
  315. Version: %v
  316. Protocol Version: %d
  317. Network Id: %d
  318. GO: %s
  319. OS: %s
  320. GOPATH=%s
  321. GOROOT=%s
  322. `, ClientIdentifier, Version, c.GlobalInt(utils.ProtocolVersionFlag.Name), c.GlobalInt(utils.NetworkIdFlag.Name), runtime.Version(), runtime.GOOS, os.Getenv("GOPATH"), runtime.GOROOT())
  323. }
  324. // hashish returns true for strings that look like hashes.
  325. func hashish(x string) bool {
  326. _, err := strconv.Atoi(x)
  327. return err != nil
  328. }
  329. func readPassword(prompt string, warnTerm bool) (string, error) {
  330. if liner.TerminalSupported() {
  331. lr := liner.NewLiner()
  332. defer lr.Close()
  333. return lr.PasswordPrompt(prompt)
  334. }
  335. if warnTerm {
  336. fmt.Println("!! Unsupported terminal, password will be echoed.")
  337. }
  338. fmt.Print(prompt)
  339. input, err := bufio.NewReader(os.Stdin).ReadString('\n')
  340. fmt.Println()
  341. return input, err
  342. }