flags.go 9.2 KB

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