main.go 6.7 KB

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