flags.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package utils
  2. import (
  3. "crypto/ecdsa"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "path"
  9. "runtime"
  10. "github.com/codegangsta/cli"
  11. "github.com/ethereum/go-ethereum/accounts"
  12. "github.com/ethereum/go-ethereum/common"
  13. "github.com/ethereum/go-ethereum/core"
  14. "github.com/ethereum/go-ethereum/crypto"
  15. "github.com/ethereum/go-ethereum/eth"
  16. "github.com/ethereum/go-ethereum/ethdb"
  17. "github.com/ethereum/go-ethereum/event"
  18. "github.com/ethereum/go-ethereum/logger"
  19. "github.com/ethereum/go-ethereum/p2p/nat"
  20. "github.com/ethereum/go-ethereum/rpc"
  21. "github.com/ethereum/go-ethereum/xeth"
  22. )
  23. func init() {
  24. cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
  25. VERSION:
  26. {{.Version}}
  27. COMMANDS:
  28. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  29. {{end}}{{if .Flags}}
  30. GLOBAL OPTIONS:
  31. {{range .Flags}}{{.}}
  32. {{end}}{{end}}
  33. `
  34. cli.CommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
  35. {{if .Description}}{{.Description}}
  36. {{end}}{{if .Subcommands}}
  37. SUBCOMMANDS:
  38. {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  39. {{end}}{{end}}{{if .Flags}}
  40. OPTIONS:
  41. {{range .Flags}}{{.}}
  42. {{end}}{{end}}
  43. `
  44. }
  45. // NewApp creates an app with sane defaults.
  46. func NewApp(version, usage string) *cli.App {
  47. app := cli.NewApp()
  48. app.Name = path.Base(os.Args[0])
  49. app.Author = ""
  50. //app.Authors = nil
  51. app.Email = ""
  52. app.Version = version
  53. app.Usage = usage
  54. return app
  55. }
  56. // These are all the command line flags we support.
  57. // If you add to this list, please remember to include the
  58. // flag in the appropriate command definition.
  59. //
  60. // The flags are defined here so their names and help texts
  61. // are the same for all commands.
  62. var (
  63. // General settings
  64. DataDirFlag = cli.StringFlag{
  65. Name: "datadir",
  66. Usage: "Data directory to be used",
  67. Value: common.DefaultDataDir(),
  68. }
  69. ProtocolVersionFlag = cli.IntFlag{
  70. Name: "protocolversion",
  71. Usage: "ETH protocol version",
  72. Value: eth.ProtocolVersion,
  73. }
  74. NetworkIdFlag = cli.IntFlag{
  75. Name: "networkid",
  76. Usage: "Network Id",
  77. Value: eth.NetworkId,
  78. }
  79. // miner settings
  80. MinerThreadsFlag = cli.IntFlag{
  81. Name: "minerthreads",
  82. Usage: "Number of miner threads",
  83. Value: runtime.NumCPU(),
  84. }
  85. MiningEnabledFlag = cli.BoolFlag{
  86. Name: "mine",
  87. Usage: "Enable mining",
  88. }
  89. EtherbaseFlag = cli.StringFlag{
  90. Name: "Etherbase",
  91. Usage: "public address for block mining rewards. By default the address of your primary account is used",
  92. Value: "primary",
  93. }
  94. UnlockedAccountFlag = cli.StringFlag{
  95. Name: "unlock",
  96. Usage: "unlock the account given until this program exits (prompts for password). '--unlock primary' unlocks the primary account",
  97. Value: "",
  98. }
  99. PasswordFileFlag = cli.StringFlag{
  100. Name: "password",
  101. Usage: "Path to password file for (un)locking an existing account.",
  102. Value: "",
  103. }
  104. // logging and debug settings
  105. LogFileFlag = cli.StringFlag{
  106. Name: "logfile",
  107. Usage: "Send log output to a file",
  108. }
  109. LogLevelFlag = cli.IntFlag{
  110. Name: "loglevel",
  111. Usage: "0-5 (silent, error, warn, info, debug, debug detail)",
  112. Value: int(logger.InfoLevel),
  113. }
  114. LogJSONFlag = cli.StringFlag{
  115. Name: "logjson",
  116. Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
  117. Value: "",
  118. }
  119. VMDebugFlag = cli.BoolFlag{
  120. Name: "vmdebug",
  121. Usage: "Virtual Machine debug output",
  122. }
  123. // RPC settings
  124. RPCEnabledFlag = cli.BoolFlag{
  125. Name: "rpc",
  126. Usage: "Whether RPC server is enabled",
  127. }
  128. RPCListenAddrFlag = cli.StringFlag{
  129. Name: "rpcaddr",
  130. Usage: "Listening address for the JSON-RPC server",
  131. Value: "127.0.0.1",
  132. }
  133. RPCPortFlag = cli.IntFlag{
  134. Name: "rpcport",
  135. Usage: "Port on which the JSON-RPC server should listen",
  136. Value: 8545,
  137. }
  138. // Network Settings
  139. MaxPeersFlag = cli.IntFlag{
  140. Name: "maxpeers",
  141. Usage: "Maximum number of network peers",
  142. Value: 16,
  143. }
  144. ListenPortFlag = cli.IntFlag{
  145. Name: "port",
  146. Usage: "Network listening port",
  147. Value: 30303,
  148. }
  149. BootnodesFlag = cli.StringFlag{
  150. Name: "bootnodes",
  151. Usage: "Space-separated enode URLs for discovery bootstrap",
  152. Value: "",
  153. }
  154. NodeKeyFileFlag = cli.StringFlag{
  155. Name: "nodekey",
  156. Usage: "P2P node key file",
  157. }
  158. NodeKeyHexFlag = cli.StringFlag{
  159. Name: "nodekeyhex",
  160. Usage: "P2P node key as hex (for testing)",
  161. }
  162. NATFlag = cli.StringFlag{
  163. Name: "nat",
  164. Usage: "Port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  165. Value: "any",
  166. }
  167. JSpathFlag = cli.StringFlag{
  168. Name: "jspath",
  169. Usage: "JS library path to be used with console and js subcommands",
  170. Value: ".",
  171. }
  172. )
  173. func GetNAT(ctx *cli.Context) nat.Interface {
  174. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  175. if err != nil {
  176. Fatalf("Option %s: %v", NATFlag.Name, err)
  177. }
  178. return natif
  179. }
  180. func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  181. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  182. var err error
  183. switch {
  184. case file != "" && hex != "":
  185. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  186. case file != "":
  187. if key, err = crypto.LoadECDSA(file); err != nil {
  188. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  189. }
  190. case hex != "":
  191. if key, err = crypto.HexToECDSA(hex); err != nil {
  192. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  193. }
  194. }
  195. return key
  196. }
  197. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  198. return &eth.Config{
  199. Name: common.MakeName(clientID, version),
  200. DataDir: ctx.GlobalString(DataDirFlag.Name),
  201. ProtocolVersion: ctx.GlobalInt(ProtocolVersionFlag.Name),
  202. NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
  203. LogFile: ctx.GlobalString(LogFileFlag.Name),
  204. LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
  205. LogJSON: ctx.GlobalString(LogJSONFlag.Name),
  206. Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
  207. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  208. AccountManager: GetAccountManager(ctx),
  209. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  210. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  211. Port: ctx.GlobalString(ListenPortFlag.Name),
  212. NAT: GetNAT(ctx),
  213. NodeKey: GetNodeKey(ctx),
  214. Shh: true,
  215. Dial: true,
  216. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  217. }
  218. }
  219. func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
  220. dataDir := ctx.GlobalString(DataDirFlag.Name)
  221. blockDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "blockchain"))
  222. if err != nil {
  223. Fatalf("Could not open database: %v", err)
  224. }
  225. stateDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "state"))
  226. if err != nil {
  227. Fatalf("Could not open database: %v", err)
  228. }
  229. return core.NewChainManager(blockDb, stateDb, new(event.TypeMux)), blockDb, stateDb
  230. }
  231. func GetAccountManager(ctx *cli.Context) *accounts.Manager {
  232. dataDir := ctx.GlobalString(DataDirFlag.Name)
  233. ks := crypto.NewKeyStorePassphrase(path.Join(dataDir, "keys"))
  234. return accounts.NewManager(ks)
  235. }
  236. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
  237. addr := ctx.GlobalString(RPCListenAddrFlag.Name)
  238. port := ctx.GlobalInt(RPCPortFlag.Name)
  239. dataDir := ctx.GlobalString(DataDirFlag.Name)
  240. fmt.Println("Starting RPC on port: ", port)
  241. l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
  242. if err != nil {
  243. Fatalf("Can't listen on %s:%d: %v", addr, port, err)
  244. }
  245. go http.Serve(l, rpc.JSONRPC(xeth.New(eth, nil), dataDir))
  246. }