main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.9.0"
  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. Action: exportchain,
  83. Name: "export",
  84. Usage: `export blockchain into file`,
  85. },
  86. }
  87. app.Author = ""
  88. app.Email = ""
  89. app.Flags = []cli.Flag{
  90. utils.BootnodesFlag,
  91. utils.DataDirFlag,
  92. utils.KeyRingFlag,
  93. utils.KeyStoreFlag,
  94. utils.ListenPortFlag,
  95. utils.LogFileFlag,
  96. utils.LogFormatFlag,
  97. utils.LogLevelFlag,
  98. utils.MaxPeersFlag,
  99. utils.MinerThreadsFlag,
  100. utils.MiningEnabledFlag,
  101. utils.NATFlag,
  102. utils.NodeKeyFileFlag,
  103. utils.NodeKeyHexFlag,
  104. utils.RPCEnabledFlag,
  105. utils.RPCListenAddrFlag,
  106. utils.RPCPortFlag,
  107. utils.VMDebugFlag,
  108. //utils.VMTypeFlag,
  109. }
  110. // missing:
  111. // flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
  112. // flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
  113. // flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
  114. // potential subcommands:
  115. // flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
  116. // flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
  117. // flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
  118. }
  119. func main() {
  120. runtime.GOMAXPROCS(runtime.NumCPU())
  121. defer logger.Flush()
  122. if err := app.Run(os.Args); err != nil {
  123. fmt.Fprintln(os.Stderr, err)
  124. os.Exit(1)
  125. }
  126. }
  127. func run(ctx *cli.Context) {
  128. fmt.Printf("Welcome to the FRONTIER\n")
  129. utils.HandleInterrupt()
  130. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  131. startEth(ctx, eth)
  132. // this blocks the thread
  133. eth.WaitForShutdown()
  134. }
  135. func runjs(ctx *cli.Context) {
  136. eth := utils.GetEthereum(ClientIdentifier, Version, ctx)
  137. startEth(ctx, eth)
  138. if len(ctx.Args()) == 0 {
  139. runREPL(eth)
  140. eth.Stop()
  141. eth.WaitForShutdown()
  142. } else if len(ctx.Args()) == 1 {
  143. execJsFile(eth, ctx.Args()[0])
  144. } else {
  145. utils.Fatalf("This command can handle at most one argument.")
  146. }
  147. }
  148. func startEth(ctx *cli.Context, eth *eth.Ethereum) {
  149. utils.StartEthereum(eth)
  150. if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
  151. addr := ctx.GlobalString(utils.RPCListenAddrFlag.Name)
  152. port := ctx.GlobalInt(utils.RPCPortFlag.Name)
  153. utils.StartRpc(eth, addr, port)
  154. }
  155. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  156. eth.Miner().Start()
  157. }
  158. }
  159. func importchain(ctx *cli.Context) {
  160. if len(ctx.Args()) != 1 {
  161. utils.Fatalf("This command requires an argument.")
  162. }
  163. chainmgr, _, _ := utils.GetChain(ctx)
  164. start := time.Now()
  165. err := utils.ImportChain(chainmgr, ctx.Args().First())
  166. if err != nil {
  167. utils.Fatalf("Import error: %v\n", err)
  168. }
  169. fmt.Printf("Import done in %v", time.Since(start))
  170. return
  171. }
  172. func exportchain(ctx *cli.Context) {
  173. if len(ctx.Args()) != 1 {
  174. utils.Fatalf("This command requires an argument.")
  175. }
  176. chainmgr, _, _ := utils.GetChain(ctx)
  177. start := time.Now()
  178. err := utils.ExportChain(chainmgr, ctx.Args().First())
  179. if err != nil {
  180. utils.Fatalf("Export error: %v\n", err)
  181. }
  182. fmt.Printf("Export done in %v", time.Since(start))
  183. return
  184. }
  185. func dump(ctx *cli.Context) {
  186. chainmgr, _, stateDb := utils.GetChain(ctx)
  187. for _, arg := range ctx.Args() {
  188. var block *types.Block
  189. if hashish(arg) {
  190. block = chainmgr.GetBlock(ethutil.Hex2Bytes(arg))
  191. } else {
  192. num, _ := strconv.Atoi(arg)
  193. block = chainmgr.GetBlockByNumber(uint64(num))
  194. }
  195. if block == nil {
  196. fmt.Println("{}")
  197. utils.Fatalf("block not found")
  198. } else {
  199. statedb := state.New(block.Root(), stateDb)
  200. fmt.Printf("%s\n", statedb.Dump())
  201. // fmt.Println(block)
  202. }
  203. }
  204. }
  205. // hashish returns true for strings that look like hashes.
  206. func hashish(x string) bool {
  207. _, err := strconv.Atoi(x)
  208. return err != nil
  209. }
  210. func version(c *cli.Context) {
  211. fmt.Printf(`%v %v
  212. PV=%d
  213. GOOS=%s
  214. GO=%s
  215. GOPATH=%s
  216. GOROOT=%s
  217. `, ClientIdentifier, Version, eth.ProtocolVersion, runtime.GOOS, runtime.Version(), os.Getenv("GOPATH"), runtime.GOROOT())
  218. }