main.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. "time"
  26. "github.com/codegangsta/cli"
  27. "github.com/ethereum/go-ethereum/accounts"
  28. "github.com/ethereum/go-ethereum/cmd/utils"
  29. "github.com/ethereum/go-ethereum/core/types"
  30. "github.com/ethereum/go-ethereum/eth"
  31. "github.com/ethereum/go-ethereum/ethutil"
  32. "github.com/ethereum/go-ethereum/logger"
  33. "github.com/ethereum/go-ethereum/state"
  34. "github.com/peterh/liner"
  35. )
  36. const (
  37. ClientIdentifier = "Ethereum(G)"
  38. Version = "0.9.0"
  39. )
  40. var (
  41. clilogger = logger.NewLogger("CLI")
  42. app = utils.NewApp(Version, "the go-ethereum command line interface")
  43. )
  44. func init() {
  45. app.Action = run
  46. app.HideVersion = true // we have a command to print the version
  47. app.Commands = []cli.Command{
  48. {
  49. Action: version,
  50. Name: "version",
  51. Usage: "print ethereum version numbers",
  52. Description: `
  53. The output of this command is supposed to be machine-readable.
  54. `,
  55. },
  56. {
  57. Action: accountList,
  58. Name: "account",
  59. Usage: "manage accounts",
  60. Subcommands: []cli.Command{
  61. {
  62. Action: accountList,
  63. Name: "list",
  64. Usage: "print account addresses",
  65. },
  66. {
  67. Action: accountCreate,
  68. Name: "new",
  69. Usage: "create a new account",
  70. },
  71. },
  72. },
  73. {
  74. Action: dump,
  75. Name: "dump",
  76. Usage: `dump a specific block from storage`,
  77. Description: `
  78. The arguments are interpreted as block numbers or hashes.
  79. Use "ethereum dump 0" to dump the genesis block.
  80. `,
  81. },
  82. {
  83. Action: runjs,
  84. Name: "js",
  85. Usage: `interactive JavaScript console`,
  86. Description: `
  87. In the console, you can use the eth object to interact
  88. with the running ethereum stack. The API does not match
  89. ethereum.js.
  90. A JavaScript file can be provided as the argument. The
  91. runtime will execute the file and exit.
  92. `,
  93. },
  94. {
  95. Action: importchain,
  96. Name: "import",
  97. Usage: `import a blockchain file`,
  98. },
  99. }
  100. app.Flags = []cli.Flag{
  101. utils.BootnodesFlag,
  102. utils.DataDirFlag,
  103. utils.ListenPortFlag,
  104. utils.LogFileFlag,
  105. utils.LogFormatFlag,
  106. utils.LogLevelFlag,
  107. utils.MaxPeersFlag,
  108. utils.MinerThreadsFlag,
  109. utils.MiningEnabledFlag,
  110. utils.NATFlag,
  111. utils.NodeKeyFileFlag,
  112. utils.NodeKeyHexFlag,
  113. utils.RPCEnabledFlag,
  114. utils.RPCListenAddrFlag,
  115. utils.RPCPortFlag,
  116. utils.VMDebugFlag,
  117. //utils.VMTypeFlag,
  118. }
  119. // missing:
  120. // flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
  121. // flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
  122. // flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
  123. // potential subcommands:
  124. // flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
  125. // flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
  126. // flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
  127. }
  128. func main() {
  129. runtime.GOMAXPROCS(runtime.NumCPU())
  130. defer logger.Flush()
  131. if err := app.Run(os.Args); err != nil {
  132. fmt.Fprintln(os.Stderr, err)
  133. os.Exit(1)
  134. }
  135. }
  136. func run(ctx *cli.Context) {
  137. fmt.Printf("Welcome to the FRONTIER\n")
  138. utils.HandleInterrupt()
  139. eth, err := utils.GetEthereum(ClientIdentifier, Version, ctx)
  140. if err == accounts.ErrNoKeys {
  141. utils.Fatalf(`No accounts configured.
  142. Please run 'ethereum account new' to create a new account.`)
  143. } else if err != nil {
  144. utils.Fatalf("%v", err)
  145. }
  146. startEth(ctx, eth)
  147. // this blocks the thread
  148. eth.WaitForShutdown()
  149. }
  150. func runjs(ctx *cli.Context) {
  151. eth, err := utils.GetEthereum(ClientIdentifier, Version, ctx)
  152. if err == accounts.ErrNoKeys {
  153. utils.Fatalf(`No accounts configured.
  154. Please run 'ethereum account new' to create a new account.`)
  155. } else if err != nil {
  156. utils.Fatalf("%v", err)
  157. }
  158. startEth(ctx, eth)
  159. repl := newJSRE(eth)
  160. if len(ctx.Args()) == 0 {
  161. repl.interactive()
  162. } else {
  163. for _, file := range ctx.Args() {
  164. repl.exec(file)
  165. }
  166. }
  167. eth.Stop()
  168. eth.WaitForShutdown()
  169. }
  170. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  171. utils.StartEthereum(eth)
  172. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  173. utils.StartRPC(eth, ctx)
  174. }
  175. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  176. eth.Miner().Start()
  177. }
  178. }
  179. func accountList(ctx *cli.Context) {
  180. am := utils.GetAccountManager(ctx)
  181. accts, err := am.Accounts()
  182. if err != nil {
  183. utils.Fatalf("Could not list accounts: %v", err)
  184. }
  185. for _, acct := range accts {
  186. fmt.Printf("Address: %#x\n", acct)
  187. }
  188. }
  189. func accountCreate(ctx *cli.Context) {
  190. am := utils.GetAccountManager(ctx)
  191. auth, err := readPassword("Passphrase: ", true)
  192. if err != nil {
  193. utils.Fatalf("%v", err)
  194. }
  195. confirm, err := readPassword("Repeat Passphrase: ", false)
  196. if err != nil {
  197. utils.Fatalf("%v", err)
  198. }
  199. if auth != confirm {
  200. utils.Fatalf("Passphrases did not match.")
  201. }
  202. acct, err := am.NewAccount(auth)
  203. if err != nil {
  204. utils.Fatalf("Could not create the account: %v", err)
  205. }
  206. fmt.Printf("Address: %#x\n", acct.Address)
  207. }
  208. func importchain(ctx *cli.Context) {
  209. if len(ctx.Args()) != 1 {
  210. utils.Fatalf("This command requires an argument.")
  211. }
  212. chain, _, _ := utils.GetChain(ctx)
  213. start := time.Now()
  214. err := utils.ImportChain(chain, ctx.Args().First())
  215. if err != nil {
  216. utils.Fatalf("Import error: %v\n", err)
  217. }
  218. fmt.Printf("Import done in", time.Since(start))
  219. return
  220. }
  221. func dump(ctx *cli.Context) {
  222. chain, _, stateDb := utils.GetChain(ctx)
  223. for _, arg := range ctx.Args() {
  224. var block *types.Block
  225. if hashish(arg) {
  226. block = chain.GetBlock(ethutil.Hex2Bytes(arg))
  227. } else {
  228. num, _ := strconv.Atoi(arg)
  229. block = chain.GetBlockByNumber(uint64(num))
  230. }
  231. if block == nil {
  232. fmt.Println("{}")
  233. utils.Fatalf("block not found")
  234. } else {
  235. statedb := state.New(block.Root(), stateDb)
  236. fmt.Printf("%s\n", statedb.Dump())
  237. // fmt.Println(block)
  238. }
  239. }
  240. }
  241. func version(c *cli.Context) {
  242. fmt.Printf(`%v %v
  243. PV=%d
  244. GOOS=%s
  245. GO=%s
  246. GOPATH=%s
  247. GOROOT=%s
  248. `, ClientIdentifier, Version, eth.ProtocolVersion, runtime.GOOS, runtime.Version(), os.Getenv("GOPATH"), runtime.GOROOT())
  249. }
  250. // hashish returns true for strings that look like hashes.
  251. func hashish(x string) bool {
  252. _, err := strconv.Atoi(x)
  253. return err != nil
  254. }
  255. func readPassword(prompt string, warnTerm bool) (string, error) {
  256. if liner.TerminalSupported() {
  257. lr := liner.NewLiner()
  258. defer lr.Close()
  259. return lr.PasswordPrompt(prompt)
  260. }
  261. if warnTerm {
  262. fmt.Println("!! Unsupported terminal, password will be echoed.")
  263. }
  264. fmt.Print(prompt)
  265. input, err := bufio.NewReader(os.Stdin).ReadString('\n')
  266. fmt.Println()
  267. return input, err
  268. }