flags.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package utils
  17. import (
  18. "crypto/ecdsa"
  19. "fmt"
  20. "log"
  21. "math/big"
  22. "net"
  23. "net/http"
  24. "os"
  25. "path/filepath"
  26. "runtime"
  27. "strconv"
  28. "github.com/codegangsta/cli"
  29. "github.com/ethereum/ethash"
  30. "github.com/ethereum/go-ethereum/accounts"
  31. "github.com/ethereum/go-ethereum/common"
  32. "github.com/ethereum/go-ethereum/core"
  33. "github.com/ethereum/go-ethereum/core/vm"
  34. "github.com/ethereum/go-ethereum/crypto"
  35. "github.com/ethereum/go-ethereum/eth"
  36. "github.com/ethereum/go-ethereum/ethdb"
  37. "github.com/ethereum/go-ethereum/event"
  38. "github.com/ethereum/go-ethereum/logger"
  39. "github.com/ethereum/go-ethereum/logger/glog"
  40. "github.com/ethereum/go-ethereum/metrics"
  41. "github.com/ethereum/go-ethereum/p2p/nat"
  42. "github.com/ethereum/go-ethereum/rpc/api"
  43. "github.com/ethereum/go-ethereum/rpc/codec"
  44. "github.com/ethereum/go-ethereum/rpc/comms"
  45. "github.com/ethereum/go-ethereum/rpc/shared"
  46. "github.com/ethereum/go-ethereum/rpc/useragent"
  47. "github.com/ethereum/go-ethereum/xeth"
  48. )
  49. func init() {
  50. cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
  51. VERSION:
  52. {{.Version}}
  53. COMMANDS:
  54. {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  55. {{end}}{{if .Flags}}
  56. GLOBAL OPTIONS:
  57. {{range .Flags}}{{.}}
  58. {{end}}{{end}}
  59. `
  60. cli.CommandHelpTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
  61. {{if .Description}}{{.Description}}
  62. {{end}}{{if .Subcommands}}
  63. SUBCOMMANDS:
  64. {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
  65. {{end}}{{end}}{{if .Flags}}
  66. OPTIONS:
  67. {{range .Flags}}{{.}}
  68. {{end}}{{end}}
  69. `
  70. }
  71. // NewApp creates an app with sane defaults.
  72. func NewApp(version, usage string) *cli.App {
  73. app := cli.NewApp()
  74. app.Name = filepath.Base(os.Args[0])
  75. app.Author = ""
  76. //app.Authors = nil
  77. app.Email = ""
  78. app.Version = version
  79. app.Usage = usage
  80. return app
  81. }
  82. // These are all the command line flags we support.
  83. // If you add to this list, please remember to include the
  84. // flag in the appropriate command definition.
  85. //
  86. // The flags are defined here so their names and help texts
  87. // are the same for all commands.
  88. var (
  89. // General settings
  90. DataDirFlag = DirectoryFlag{
  91. Name: "datadir",
  92. Usage: "Data directory to be used",
  93. Value: DirectoryString{common.DefaultDataDir()},
  94. }
  95. NetworkIdFlag = cli.IntFlag{
  96. Name: "networkid",
  97. Usage: "Network Id (integer)",
  98. Value: eth.NetworkId,
  99. }
  100. BlockchainVersionFlag = cli.IntFlag{
  101. Name: "blockchainversion",
  102. Usage: "Blockchain version (integer)",
  103. Value: core.BlockChainVersion,
  104. }
  105. GenesisNonceFlag = cli.IntFlag{
  106. Name: "genesisnonce",
  107. Usage: "Sets the genesis nonce",
  108. Value: 42,
  109. }
  110. GenesisFileFlag = cli.StringFlag{
  111. Name: "genesis",
  112. Usage: "Inserts/Overwrites the genesis block (json format)",
  113. }
  114. IdentityFlag = cli.StringFlag{
  115. Name: "identity",
  116. Usage: "Custom node name",
  117. }
  118. NatspecEnabledFlag = cli.BoolFlag{
  119. Name: "natspec",
  120. Usage: "Enable NatSpec confirmation notice",
  121. }
  122. CacheFlag = cli.IntFlag{
  123. Name: "cache",
  124. Usage: "Megabytes of memory allocated to internal caching",
  125. Value: 0,
  126. }
  127. OlympicFlag = cli.BoolFlag{
  128. Name: "olympic",
  129. Usage: "Use olympic style protocol",
  130. }
  131. EthVersionFlag = cli.IntFlag{
  132. Name: "eth",
  133. Value: 62,
  134. Usage: "Highest eth protocol to advertise (temporary, dev option)",
  135. }
  136. // miner settings
  137. MinerThreadsFlag = cli.IntFlag{
  138. Name: "minerthreads",
  139. Usage: "Number of miner threads",
  140. Value: runtime.NumCPU(),
  141. }
  142. MiningEnabledFlag = cli.BoolFlag{
  143. Name: "mine",
  144. Usage: "Enable mining",
  145. }
  146. AutoDAGFlag = cli.BoolFlag{
  147. Name: "autodag",
  148. Usage: "Enable automatic DAG pregeneration",
  149. }
  150. EtherbaseFlag = cli.StringFlag{
  151. Name: "etherbase",
  152. Usage: "Public address for block mining rewards. By default the address first created is used",
  153. Value: "0",
  154. }
  155. GasPriceFlag = cli.StringFlag{
  156. Name: "gasprice",
  157. Usage: "Sets the minimal gasprice when mining transactions",
  158. Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
  159. }
  160. UnlockedAccountFlag = cli.StringFlag{
  161. Name: "unlock",
  162. Usage: "Unlock the account given until this program exits (prompts for password). '--unlock n' unlocks the n-th account in order or creation.",
  163. Value: "",
  164. }
  165. PasswordFileFlag = cli.StringFlag{
  166. Name: "password",
  167. Usage: "Path to password file to use with options and subcommands needing a password",
  168. Value: "",
  169. }
  170. // vm flags
  171. VMDebugFlag = cli.BoolFlag{
  172. Name: "vmdebug",
  173. Usage: "Virtual Machine debug output",
  174. }
  175. VMForceJitFlag = cli.BoolFlag{
  176. Name: "forcejit",
  177. Usage: "Force the JIT VM to take precedence",
  178. }
  179. VMJitCacheFlag = cli.IntFlag{
  180. Name: "jitcache",
  181. Usage: "Amount of cached JIT VM programs",
  182. Value: 64,
  183. }
  184. VMEnableJitFlag = cli.BoolFlag{
  185. Name: "jitvm",
  186. Usage: "Enable the JIT VM",
  187. }
  188. // logging and debug settings
  189. LogFileFlag = cli.StringFlag{
  190. Name: "logfile",
  191. Usage: "Send log output to a file",
  192. }
  193. VerbosityFlag = cli.IntFlag{
  194. Name: "verbosity",
  195. Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
  196. Value: int(logger.InfoLevel),
  197. }
  198. LogJSONFlag = cli.StringFlag{
  199. Name: "logjson",
  200. Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
  201. Value: "",
  202. }
  203. LogToStdErrFlag = cli.BoolFlag{
  204. Name: "logtostderr",
  205. Usage: "Logs are written to standard error instead of to files.",
  206. }
  207. LogVModuleFlag = cli.GenericFlag{
  208. Name: "vmodule",
  209. 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 log verbosity level.",
  210. Value: glog.GetVModule(),
  211. }
  212. BacktraceAtFlag = cli.GenericFlag{
  213. Name: "backtrace_at",
  214. Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged",
  215. Value: glog.GetTraceLocation(),
  216. }
  217. PProfEanbledFlag = cli.BoolFlag{
  218. Name: "pprof",
  219. Usage: "Enable the profiling server on localhost",
  220. }
  221. PProfPortFlag = cli.IntFlag{
  222. Name: "pprofport",
  223. Usage: "Port on which the profiler should listen",
  224. Value: 6060,
  225. }
  226. MetricsEnabledFlag = cli.BoolFlag{
  227. Name: metrics.MetricsEnabledFlag,
  228. Usage: "Enables metrics collection and reporting",
  229. }
  230. // RPC settings
  231. RPCEnabledFlag = cli.BoolFlag{
  232. Name: "rpc",
  233. Usage: "Enable the JSON-RPC server",
  234. }
  235. RPCListenAddrFlag = cli.StringFlag{
  236. Name: "rpcaddr",
  237. Usage: "Listening address for the JSON-RPC server",
  238. Value: "127.0.0.1",
  239. }
  240. RPCPortFlag = cli.IntFlag{
  241. Name: "rpcport",
  242. Usage: "Port on which the JSON-RPC server should listen",
  243. Value: 8545,
  244. }
  245. RPCCORSDomainFlag = cli.StringFlag{
  246. Name: "rpccorsdomain",
  247. Usage: "Domain on which to send Access-Control-Allow-Origin header",
  248. Value: "",
  249. }
  250. RpcApiFlag = cli.StringFlag{
  251. Name: "rpcapi",
  252. Usage: "Specify the API's which are offered over the HTTP RPC interface",
  253. Value: comms.DefaultHttpRpcApis,
  254. }
  255. IPCDisabledFlag = cli.BoolFlag{
  256. Name: "ipcdisable",
  257. Usage: "Disable the IPC-RPC server",
  258. }
  259. IPCApiFlag = cli.StringFlag{
  260. Name: "ipcapi",
  261. Usage: "Specify the API's which are offered over the IPC interface",
  262. Value: comms.DefaultIpcApis,
  263. }
  264. IPCPathFlag = DirectoryFlag{
  265. Name: "ipcpath",
  266. Usage: "Filename for IPC socket/pipe",
  267. Value: DirectoryString{common.DefaultIpcPath()},
  268. }
  269. ExecFlag = cli.StringFlag{
  270. Name: "exec",
  271. Usage: "Execute javascript statement (only in combination with console/attach)",
  272. }
  273. // Network Settings
  274. MaxPeersFlag = cli.IntFlag{
  275. Name: "maxpeers",
  276. Usage: "Maximum number of network peers (network disabled if set to 0)",
  277. Value: 25,
  278. }
  279. MaxPendingPeersFlag = cli.IntFlag{
  280. Name: "maxpendpeers",
  281. Usage: "Maximum number of pending connection attempts (defaults used if set to 0)",
  282. Value: 0,
  283. }
  284. ListenPortFlag = cli.IntFlag{
  285. Name: "port",
  286. Usage: "Network listening port",
  287. Value: 30303,
  288. }
  289. BootnodesFlag = cli.StringFlag{
  290. Name: "bootnodes",
  291. Usage: "Space-separated enode URLs for p2p discovery bootstrap",
  292. Value: "",
  293. }
  294. NodeKeyFileFlag = cli.StringFlag{
  295. Name: "nodekey",
  296. Usage: "P2P node key file",
  297. }
  298. NodeKeyHexFlag = cli.StringFlag{
  299. Name: "nodekeyhex",
  300. Usage: "P2P node key as hex (for testing)",
  301. }
  302. NATFlag = cli.StringFlag{
  303. Name: "nat",
  304. Usage: "NAT port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  305. Value: "any",
  306. }
  307. NoDiscoverFlag = cli.BoolFlag{
  308. Name: "nodiscover",
  309. Usage: "Disables the peer discovery mechanism (manual peer addition)",
  310. }
  311. WhisperEnabledFlag = cli.BoolFlag{
  312. Name: "shh",
  313. Usage: "Enable whisper",
  314. }
  315. // ATM the url is left to the user and deployment to
  316. JSpathFlag = cli.StringFlag{
  317. Name: "jspath",
  318. Usage: "JS library path to be used with console and js subcommands",
  319. Value: ".",
  320. }
  321. SolcPathFlag = cli.StringFlag{
  322. Name: "solc",
  323. Usage: "solidity compiler to be used",
  324. Value: "solc",
  325. }
  326. GpoMinGasPriceFlag = cli.StringFlag{
  327. Name: "gpomin",
  328. Usage: "Minimum suggested gas price",
  329. Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
  330. }
  331. GpoMaxGasPriceFlag = cli.StringFlag{
  332. Name: "gpomax",
  333. Usage: "Maximum suggested gas price",
  334. Value: new(big.Int).Mul(big.NewInt(500), common.Shannon).String(),
  335. }
  336. GpoFullBlockRatioFlag = cli.IntFlag{
  337. Name: "gpofull",
  338. Usage: "Full block threshold for gas price calculation (%)",
  339. Value: 80,
  340. }
  341. GpobaseStepDownFlag = cli.IntFlag{
  342. Name: "gpobasedown",
  343. Usage: "Suggested gas price base step down ratio (1/1000)",
  344. Value: 10,
  345. }
  346. GpobaseStepUpFlag = cli.IntFlag{
  347. Name: "gpobaseup",
  348. Usage: "Suggested gas price base step up ratio (1/1000)",
  349. Value: 100,
  350. }
  351. GpobaseCorrectionFactorFlag = cli.IntFlag{
  352. Name: "gpobasecf",
  353. Usage: "Suggested gas price base correction factor (%)",
  354. Value: 110,
  355. }
  356. )
  357. // MakeNAT creates a port mapper from set command line flags.
  358. func MakeNAT(ctx *cli.Context) nat.Interface {
  359. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  360. if err != nil {
  361. Fatalf("Option %s: %v", NATFlag.Name, err)
  362. }
  363. return natif
  364. }
  365. // MakeNodeKey creates a node key from set command line flags.
  366. func MakeNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  367. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  368. var err error
  369. switch {
  370. case file != "" && hex != "":
  371. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  372. case file != "":
  373. if key, err = crypto.LoadECDSA(file); err != nil {
  374. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  375. }
  376. case hex != "":
  377. if key, err = crypto.HexToECDSA(hex); err != nil {
  378. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  379. }
  380. }
  381. return key
  382. }
  383. // MakeEthConfig creates ethereum options from set command line flags.
  384. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  385. customName := ctx.GlobalString(IdentityFlag.Name)
  386. if len(customName) > 0 {
  387. clientID += "/" + customName
  388. }
  389. am := MakeAccountManager(ctx)
  390. etherbase, err := ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)
  391. if err != nil {
  392. glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default")
  393. }
  394. return &eth.Config{
  395. Name: common.MakeName(clientID, version),
  396. DataDir: ctx.GlobalString(DataDirFlag.Name),
  397. GenesisNonce: ctx.GlobalInt(GenesisNonceFlag.Name),
  398. GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
  399. BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
  400. DatabaseCache: ctx.GlobalInt(CacheFlag.Name),
  401. SkipBcVersionCheck: false,
  402. NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
  403. LogFile: ctx.GlobalString(LogFileFlag.Name),
  404. Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
  405. LogJSON: ctx.GlobalString(LogJSONFlag.Name),
  406. Etherbase: common.HexToAddress(etherbase),
  407. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  408. AccountManager: am,
  409. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  410. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  411. MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
  412. Port: ctx.GlobalString(ListenPortFlag.Name),
  413. Olympic: ctx.GlobalBool(OlympicFlag.Name),
  414. NAT: MakeNAT(ctx),
  415. NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
  416. Discovery: !ctx.GlobalBool(NoDiscoverFlag.Name),
  417. NodeKey: MakeNodeKey(ctx),
  418. Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
  419. Dial: true,
  420. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  421. GasPrice: common.String2Big(ctx.GlobalString(GasPriceFlag.Name)),
  422. GpoMinGasPrice: common.String2Big(ctx.GlobalString(GpoMinGasPriceFlag.Name)),
  423. GpoMaxGasPrice: common.String2Big(ctx.GlobalString(GpoMaxGasPriceFlag.Name)),
  424. GpoFullBlockRatio: ctx.GlobalInt(GpoFullBlockRatioFlag.Name),
  425. GpobaseStepDown: ctx.GlobalInt(GpobaseStepDownFlag.Name),
  426. GpobaseStepUp: ctx.GlobalInt(GpobaseStepUpFlag.Name),
  427. GpobaseCorrectionFactor: ctx.GlobalInt(GpobaseCorrectionFactorFlag.Name),
  428. SolcPath: ctx.GlobalString(SolcPathFlag.Name),
  429. AutoDAG: ctx.GlobalBool(AutoDAGFlag.Name) || ctx.GlobalBool(MiningEnabledFlag.Name),
  430. }
  431. }
  432. // SetupLogger configures glog from the logging-related command line flags.
  433. func SetupLogger(ctx *cli.Context) {
  434. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  435. glog.CopyStandardLogTo("INFO")
  436. glog.SetToStderr(true)
  437. glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
  438. }
  439. // SetupVM configured the VM package's global settings
  440. func SetupVM(ctx *cli.Context) {
  441. vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
  442. vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
  443. vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
  444. }
  445. // SetupEth configures the eth packages global settings
  446. func SetupEth(ctx *cli.Context) {
  447. version := ctx.GlobalInt(EthVersionFlag.Name)
  448. for len(eth.ProtocolVersions) > 0 && eth.ProtocolVersions[0] > uint(version) {
  449. eth.ProtocolVersions = eth.ProtocolVersions[1:]
  450. eth.ProtocolLengths = eth.ProtocolLengths[1:]
  451. }
  452. if len(eth.ProtocolVersions) == 0 {
  453. Fatalf("No valid eth protocols remaining")
  454. }
  455. }
  456. // MakeChain creates a chain manager from set command line flags.
  457. func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb common.Database) {
  458. datadir := ctx.GlobalString(DataDirFlag.Name)
  459. cache := ctx.GlobalInt(CacheFlag.Name)
  460. var err error
  461. if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil {
  462. Fatalf("Could not open database: %v", err)
  463. }
  464. if ctx.GlobalBool(OlympicFlag.Name) {
  465. InitOlympic()
  466. _, err := core.WriteTestNetGenesisBlock(chainDb, 42)
  467. if err != nil {
  468. glog.Fatalln(err)
  469. }
  470. }
  471. eventMux := new(event.TypeMux)
  472. pow := ethash.New()
  473. //genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
  474. chain, err = core.NewChainManager(chainDb, pow, eventMux)
  475. if err != nil {
  476. Fatalf("Could not start chainmanager: %v", err)
  477. }
  478. proc := core.NewBlockProcessor(chainDb, pow, chain, eventMux)
  479. chain.SetProcessor(proc)
  480. return chain, chainDb
  481. }
  482. // MakeChain creates an account manager from set command line flags.
  483. func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
  484. dataDir := ctx.GlobalString(DataDirFlag.Name)
  485. ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"))
  486. return accounts.NewManager(ks)
  487. }
  488. func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
  489. if runtime.GOOS == "windows" {
  490. ipcpath = common.DefaultIpcPath()
  491. if ctx.GlobalIsSet(IPCPathFlag.Name) {
  492. ipcpath = ctx.GlobalString(IPCPathFlag.Name)
  493. }
  494. } else {
  495. ipcpath = common.DefaultIpcPath()
  496. if ctx.GlobalIsSet(DataDirFlag.Name) {
  497. ipcpath = filepath.Join(ctx.GlobalString(DataDirFlag.Name), "geth.ipc")
  498. }
  499. if ctx.GlobalIsSet(IPCPathFlag.Name) {
  500. ipcpath = ctx.GlobalString(IPCPathFlag.Name)
  501. }
  502. }
  503. return
  504. }
  505. func StartIPC(eth *eth.Ethereum, ctx *cli.Context) error {
  506. config := comms.IpcConfig{
  507. Endpoint: IpcSocketPath(ctx),
  508. }
  509. initializer := func(conn net.Conn) (shared.EthereumApi, error) {
  510. fe := useragent.NewRemoteFrontend(conn, eth.AccountManager())
  511. xeth := xeth.New(eth, fe)
  512. codec := codec.JSON
  513. apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec, xeth, eth)
  514. if err != nil {
  515. return nil, err
  516. }
  517. return api.Merge(apis...), nil
  518. }
  519. return comms.StartIpc(config, codec.JSON, initializer)
  520. }
  521. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) error {
  522. config := comms.HttpConfig{
  523. ListenAddress: ctx.GlobalString(RPCListenAddrFlag.Name),
  524. ListenPort: uint(ctx.GlobalInt(RPCPortFlag.Name)),
  525. CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
  526. }
  527. xeth := xeth.New(eth, nil)
  528. codec := codec.JSON
  529. apis, err := api.ParseApiString(ctx.GlobalString(RpcApiFlag.Name), codec, xeth, eth)
  530. if err != nil {
  531. return err
  532. }
  533. return comms.StartHttp(config, codec, api.Merge(apis...))
  534. }
  535. func StartPProf(ctx *cli.Context) {
  536. address := fmt.Sprintf("localhost:%d", ctx.GlobalInt(PProfPortFlag.Name))
  537. go func() {
  538. log.Println(http.ListenAndServe(address, nil))
  539. }()
  540. }
  541. func ParamToAddress(addr string, am *accounts.Manager) (addrHex string, err error) {
  542. if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
  543. index, err := strconv.Atoi(addr)
  544. if err != nil {
  545. Fatalf("Invalid account address '%s'", addr)
  546. }
  547. addrHex, err = am.AddressByIndex(index)
  548. if err != nil {
  549. return "", err
  550. }
  551. } else {
  552. addrHex = addr
  553. }
  554. return
  555. }