main.go 8.2 KB

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