flags.go 7.4 KB

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