main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "fmt"
  21. "os"
  22. "runtime"
  23. "strconv"
  24. "time"
  25. "github.com/codegangsta/cli"
  26. "github.com/ethereum/go-ethereum/cmd/utils"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/eth"
  29. "github.com/ethereum/go-ethereum/ethutil"
  30. "github.com/ethereum/go-ethereum/logger"
  31. "github.com/ethereum/go-ethereum/state"
  32. )
  33. const (
  34. ClientIdentifier = "Ethereum(G)"
  35. Version = "0.8.6"
  36. )
  37. var (
  38. clilogger = logger.NewLogger("CLI")
  39. app = cli.NewApp()
  40. )
  41. func init() {
  42. app.Version = Version
  43. app.Usage = "the go-ethereum command-line client"
  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: dump,
  57. Name: "dump",
  58. Usage: `dump a specific block from storage`,
  59. Description: `
  60. The arguments are interpreted as block numbers or hashes.
  61. Use "ethereum dump 0" to dump the genesis block.
  62. `,
  63. },
  64. {
  65. Action: runjs,
  66. Name: "js",
  67. Usage: `interactive JavaScript console`,
  68. Description: `
  69. In the console, you can use the eth object to interact
  70. with the running ethereum stack. The API does not match
  71. ethereum.js.
  72. A JavaScript file can be provided as the argument. The
  73. runtime will execute the file and exit.
  74. `,
  75. },
  76. {
  77. Action: importchain,
  78. Name: "import",
  79. Usage: `import a blockchain file`,
  80. },
  81. }
  82. app.Author = ""
  83. app.Email = ""
  84. app.Flags = []cli.Flag{
  85. utils.BootnodesFlag,
  86. utils.DataDirFlag,
  87. utils.KeyRingFlag,
  88. utils.KeyStoreFlag,
  89. utils.ListenPortFlag,
  90. utils.LogFileFlag,
  91. utils.LogFormatFlag,
  92. utils.LogLevelFlag,
  93. utils.MaxPeersFlag,
  94. utils.MinerThreadsFlag,
  95. utils.MiningEnabledFlag,
  96. utils.NATFlag,
  97. utils.NodeKeyFileFlag,
  98. utils.NodeKeyHexFlag,
  99. utils.RPCEnabledFlag,
  100. utils.RPCListenAddrFlag,
  101. utils.RPCPortFlag,
  102. utils.VMDebugFlag,
  103. //utils.VMTypeFlag,
  104. }
  105. // missing:
  106. // flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
  107. // flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
  108. // flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
  109. // potential subcommands:
  110. // flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
  111. // flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
  112. // flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
  113. }
  114. func main() {
  115. runtime.GOMAXPROCS(runtime.NumCPU())
  116. defer logger.Flush()
  117. if err := app.Run(os.Args); err != nil {
  118. fmt.Fprintln(os.Stderr, err)
  119. os.Exit(1)
  120. }
  121. }
  122. func run(ctx *cli.Context) {
  123. fmt.Printf("Welcome to the FRONTIER\n")
  124. utils.HandleInterrupt()
  125. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  126. startEth(ctx, eth)
  127. // this blocks the thread
  128. eth.WaitForShutdown()
  129. }
  130. func runjs(ctx *cli.Context) {
  131. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  132. startEth(ctx, eth)
  133. if len(ctx.Args()) == 0 {
  134. runREPL(eth)
  135. eth.Stop()
  136. eth.WaitForShutdown()
  137. } else if len(ctx.Args()) == 1 {
  138. execJsFile(eth, ctx.Args()[0])
  139. } else {
  140. utils.Fatalf("This command can handle at most one argument.")
  141. }
  142. }
  143. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  144. utils.StartEthereum(eth)
  145. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  146. addr := ctx.GlobalString(utils.RPCListenAddrFlag.Name)
  147. port := ctx.GlobalInt(utils.RPCPortFlag.Name)
  148. utils.StartRpc(eth, addr, port)
  149. }
  150. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  151. eth.Miner().Start()
  152. }
  153. }
  154. func importchain(ctx *cli.Context) {
  155. if len(ctx.Args()) != 1 {
  156. utils.Fatalf("This command requires an argument.")
  157. }
  158. chain, _ := utils.GetChain(ctx)
  159. start := time.Now()
  160. err := utils.ImportChain(chain, ctx.Args().First())
  161. if err != nil {
  162. utils.Fatalf("Import error: %v\n", err)
  163. }
  164. fmt.Printf("Import done in", time.Since(start))
  165. return
  166. }
  167. func dump(ctx *cli.Context) {
  168. chain, db := utils.GetChain(ctx)
  169. for _, arg := range ctx.Args() {
  170. var block *types.Block
  171. if hashish(arg) {
  172. block = chain.GetBlock(ethutil.Hex2Bytes(arg))
  173. } else {
  174. num, _ := strconv.Atoi(arg)
  175. block = chain.GetBlockByNumber(uint64(num))
  176. }
  177. if block == nil {
  178. fmt.Println("{}")
  179. utils.Fatalf("block not found")
  180. } else {
  181. statedb := state.New(block.Root(), db)
  182. fmt.Printf("%s\n", statedb.Dump())
  183. // fmt.Println(block)
  184. }
  185. }
  186. }
  187. // hashish returns true for strings that look like hashes.
  188. func hashish(x string) bool {
  189. _, err := strconv.Atoi(x)
  190. return err != nil
  191. }
  192. func version(c *cli.Context) {
  193. fmt.Printf(`%v %v
  194. PV=%d
  195. GOOS=%s
  196. GO=%s
  197. GOPATH=%s
  198. GOROOT=%s
  199. `, ClientIdentifier, Version, eth.ProtocolVersion, runtime.GOOS, runtime.Version(), os.Getenv("GOPATH"), runtime.GOROOT())
  200. }