main.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.8.6"
  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.VMTypeFlag,
  116. }
  117. // missing:
  118. // flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
  119. // flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
  120. // flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
  121. // potential subcommands:
  122. // flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
  123. // flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
  124. // flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
  125. }
  126. func main() {
  127. runtime.GOMAXPROCS(runtime.NumCPU())
  128. defer logger.Flush()
  129. if err := app.Run(os.Args); err != nil {
  130. fmt.Fprintln(os.Stderr, err)
  131. os.Exit(1)
  132. }
  133. }
  134. func run(ctx *cli.Context) {
  135. fmt.Printf("Welcome to the FRONTIER\n")
  136. utils.HandleInterrupt()
  137. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  138. startEth(ctx, eth)
  139. // this blocks the thread
  140. eth.WaitForShutdown()
  141. }
  142. func runjs(ctx *cli.Context) {
  143. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  144. startEth(ctx, eth)
  145. if len(ctx.Args()) == 0 {
  146. runREPL(eth)
  147. eth.Stop()
  148. eth.WaitForShutdown()
  149. } else if len(ctx.Args()) == 1 {
  150. execJsFile(eth, ctx.Args()[0])
  151. } else {
  152. utils.Fatalf("This command can handle at most one argument.")
  153. }
  154. }
  155. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  156. utils.StartEthereum(eth)
  157. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  158. utils.StartRPC(eth, ctx)
  159. }
  160. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  161. eth.Miner().Start()
  162. }
  163. }
  164. func accountList(ctx *cli.Context) {
  165. am := utils.GetAccountManager(ctx)
  166. accts, err := am.Accounts()
  167. if err != nil {
  168. utils.Fatalf("Could not list accounts: %v", err)
  169. }
  170. for _, acct := range accts {
  171. fmt.Printf("Address: %#x\n", acct)
  172. }
  173. }
  174. func accountCreate(ctx *cli.Context) {
  175. am := utils.GetAccountManager(ctx)
  176. auth, err := readPassword("Passphrase: ", true)
  177. if err != nil {
  178. utils.Fatalf("%v", err)
  179. }
  180. confirm, err := readPassword("Repeat Passphrase: ", false)
  181. if err != nil {
  182. utils.Fatalf("%v", err)
  183. }
  184. if auth != confirm {
  185. utils.Fatalf("Passphrases did not match.")
  186. }
  187. acct, err := am.NewAccount(auth)
  188. if err != nil {
  189. utils.Fatalf("Could not create the account: %v", err)
  190. }
  191. fmt.Printf("Address: %#x\n", acct.Address)
  192. }
  193. func importchain(ctx *cli.Context) {
  194. if len(ctx.Args()) != 1 {
  195. utils.Fatalf("This command requires an argument.")
  196. }
  197. chain, _ := utils.GetChain(ctx)
  198. start := time.Now()
  199. err := utils.ImportChain(chain, ctx.Args().First())
  200. if err != nil {
  201. utils.Fatalf("Import error: %v\n", err)
  202. }
  203. fmt.Printf("Import done in", time.Since(start))
  204. return
  205. }
  206. func dump(ctx *cli.Context) {
  207. chain, db := utils.GetChain(ctx)
  208. for _, arg := range ctx.Args() {
  209. var block *types.Block
  210. if hashish(arg) {
  211. block = chain.GetBlock(ethutil.Hex2Bytes(arg))
  212. } else {
  213. num, _ := strconv.Atoi(arg)
  214. block = chain.GetBlockByNumber(uint64(num))
  215. }
  216. if block == nil {
  217. fmt.Println("{}")
  218. utils.Fatalf("block not found")
  219. } else {
  220. statedb := state.New(block.Root(), db)
  221. fmt.Printf("%s\n", statedb.Dump())
  222. // fmt.Println(block)
  223. }
  224. }
  225. }
  226. func version(c *cli.Context) {
  227. fmt.Printf(`%v %v
  228. PV=%d
  229. GOOS=%s
  230. GO=%s
  231. GOPATH=%s
  232. GOROOT=%s
  233. `, ClientIdentifier, Version, eth.ProtocolVersion, runtime.GOOS, runtime.Version(), os.Getenv("GOPATH"), runtime.GOROOT())
  234. }
  235. // hashish returns true for strings that look like hashes.
  236. func hashish(x string) bool {
  237. _, err := strconv.Atoi(x)
  238. return err != nil
  239. }
  240. func readPassword(prompt string, warnTerm bool) (string, error) {
  241. if liner.TerminalSupported() {
  242. lr := liner.NewLiner()
  243. defer lr.Close()
  244. return lr.PasswordPrompt(prompt)
  245. }
  246. if warnTerm {
  247. fmt.Println("!! Unsupported terminal, password will be echoed.")
  248. }
  249. fmt.Print(prompt)
  250. input, err := bufio.NewReader(os.Stdin).ReadString('\n')
  251. fmt.Println()
  252. return input, err
  253. }