flags.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // miner settings
  84. MinerThreadsFlag = cli.IntFlag{
  85. Name: "minerthreads",
  86. Usage: "Number of miner threads",
  87. Value: runtime.NumCPU(),
  88. }
  89. MiningEnabledFlag = cli.BoolFlag{
  90. Name: "mine",
  91. Usage: "Enable mining",
  92. }
  93. EtherbaseFlag = cli.StringFlag{
  94. Name: "etherbase",
  95. Usage: "public address for block mining rewards. By default the address of your primary account is used",
  96. Value: "primary",
  97. }
  98. UnlockedAccountFlag = cli.StringFlag{
  99. Name: "unlock",
  100. Usage: "unlock the account given until this program exits (prompts for password). '--unlock primary' unlocks the primary account",
  101. Value: "",
  102. }
  103. PasswordFileFlag = cli.StringFlag{
  104. Name: "password",
  105. Usage: "Path to password file for (un)locking an existing account.",
  106. Value: "",
  107. }
  108. // logging and debug settings
  109. LogFileFlag = cli.StringFlag{
  110. Name: "logfile",
  111. Usage: "Send log output to a file",
  112. }
  113. LogLevelFlag = cli.IntFlag{
  114. Name: "loglevel",
  115. Usage: "0-5 (silent, error, warn, info, debug, debug detail)",
  116. Value: int(logger.InfoLevel),
  117. }
  118. LogJSONFlag = cli.StringFlag{
  119. Name: "logjson",
  120. Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
  121. Value: "",
  122. }
  123. VMDebugFlag = cli.BoolFlag{
  124. Name: "vmdebug",
  125. Usage: "Virtual Machine debug output",
  126. }
  127. // RPC settings
  128. RPCEnabledFlag = cli.BoolFlag{
  129. Name: "rpc",
  130. Usage: "Whether RPC server is enabled",
  131. }
  132. RPCListenAddrFlag = cli.StringFlag{
  133. Name: "rpcaddr",
  134. Usage: "Listening address for the JSON-RPC server",
  135. Value: "127.0.0.1",
  136. }
  137. RPCPortFlag = cli.IntFlag{
  138. Name: "rpcport",
  139. Usage: "Port on which the JSON-RPC server should listen",
  140. Value: 8545,
  141. }
  142. RPCCORSDomainFlag = cli.StringFlag{
  143. Name: "rpccorsdomain",
  144. Usage: "Domain on which to send Access-Control-Allow-Origin header",
  145. Value: "",
  146. }
  147. // Network Settings
  148. MaxPeersFlag = cli.IntFlag{
  149. Name: "maxpeers",
  150. Usage: "Maximum number of network peers",
  151. Value: 16,
  152. }
  153. ListenPortFlag = cli.IntFlag{
  154. Name: "port",
  155. Usage: "Network listening port",
  156. Value: 30303,
  157. }
  158. BootnodesFlag = cli.StringFlag{
  159. Name: "bootnodes",
  160. Usage: "Space-separated enode URLs for discovery bootstrap",
  161. Value: "",
  162. }
  163. NodeKeyFileFlag = cli.StringFlag{
  164. Name: "nodekey",
  165. Usage: "P2P node key file",
  166. }
  167. NodeKeyHexFlag = cli.StringFlag{
  168. Name: "nodekeyhex",
  169. Usage: "P2P node key as hex (for testing)",
  170. }
  171. NATFlag = cli.StringFlag{
  172. Name: "nat",
  173. Usage: "Port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  174. Value: "any",
  175. }
  176. JSpathFlag = cli.StringFlag{
  177. Name: "jspath",
  178. Usage: "JS library path to be used with console and js subcommands",
  179. Value: ".",
  180. }
  181. BacktraceAtFlag = cli.GenericFlag{
  182. Name: "backtrace_at",
  183. Usage: "When set to a file and line number holding a logging statement a stack trace will be written to the Info log",
  184. Value: glog.GetTraceLocation(),
  185. }
  186. LogToStdErrFlag = cli.BoolFlag{
  187. Name: "logtostderr",
  188. Usage: "Logs are written to standard error instead of to files.",
  189. }
  190. LogVModuleFlag = cli.GenericFlag{
  191. Name: "vmodule",
  192. 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.",
  193. Value: glog.GetVModule(),
  194. }
  195. )
  196. func GetNAT(ctx *cli.Context) nat.Interface {
  197. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  198. if err != nil {
  199. Fatalf("Option %s: %v", NATFlag.Name, err)
  200. }
  201. return natif
  202. }
  203. func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  204. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  205. var err error
  206. switch {
  207. case file != "" && hex != "":
  208. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  209. case file != "":
  210. if key, err = crypto.LoadECDSA(file); err != nil {
  211. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  212. }
  213. case hex != "":
  214. if key, err = crypto.HexToECDSA(hex); err != nil {
  215. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  216. }
  217. }
  218. return key
  219. }
  220. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  221. // Set verbosity on glog
  222. glog.SetV(ctx.GlobalInt(LogLevelFlag.Name))
  223. // Set the log type
  224. //glog.SetToStderr(ctx.GlobalBool(LogToStdErrFlag.Name))
  225. glog.SetToStderr(true)
  226. // Set the log dir
  227. glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
  228. return &eth.Config{
  229. Name: common.MakeName(clientID, version),
  230. DataDir: ctx.GlobalString(DataDirFlag.Name),
  231. ProtocolVersion: ctx.GlobalInt(ProtocolVersionFlag.Name),
  232. BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
  233. SkipBcVersionCheck: false,
  234. NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
  235. LogFile: ctx.GlobalString(LogFileFlag.Name),
  236. LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
  237. LogJSON: ctx.GlobalString(LogJSONFlag.Name),
  238. Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
  239. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  240. AccountManager: GetAccountManager(ctx),
  241. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  242. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  243. Port: ctx.GlobalString(ListenPortFlag.Name),
  244. NAT: GetNAT(ctx),
  245. NodeKey: GetNodeKey(ctx),
  246. Shh: true,
  247. Dial: true,
  248. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  249. }
  250. }
  251. func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
  252. dataDir := ctx.GlobalString(DataDirFlag.Name)
  253. blockDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "blockchain"))
  254. if err != nil {
  255. Fatalf("Could not open database: %v", err)
  256. }
  257. stateDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "state"))
  258. if err != nil {
  259. Fatalf("Could not open database: %v", err)
  260. }
  261. extraDb, err := ethdb.NewLDBDatabase(path.Join(dataDir, "extra"))
  262. if err != nil {
  263. Fatalf("Could not open database: %v", err)
  264. }
  265. eventMux := new(event.TypeMux)
  266. chainManager := core.NewChainManager(blockDb, stateDb, eventMux)
  267. pow := ethash.New(chainManager)
  268. txPool := core.NewTxPool(eventMux, chainManager.State)
  269. blockProcessor := core.NewBlockProcessor(stateDb, extraDb, pow, txPool, chainManager, eventMux)
  270. chainManager.SetProcessor(blockProcessor)
  271. return chainManager, blockDb, stateDb
  272. }
  273. func GetAccountManager(ctx *cli.Context) *accounts.Manager {
  274. dataDir := ctx.GlobalString(DataDirFlag.Name)
  275. ks := crypto.NewKeyStorePassphrase(path.Join(dataDir, "keys"))
  276. return accounts.NewManager(ks)
  277. }
  278. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
  279. config := rpc.RpcConfig{
  280. ListenAddress: ctx.GlobalString(RPCListenAddrFlag.Name),
  281. ListenPort: uint(ctx.GlobalInt(RPCPortFlag.Name)),
  282. CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
  283. }
  284. xeth := xeth.New(eth, nil)
  285. _ = rpc.Start(xeth, config)
  286. }