flags.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. // miner settings
  132. MinerThreadsFlag = cli.IntFlag{
  133. Name: "minerthreads",
  134. Usage: "Number of miner threads",
  135. Value: runtime.NumCPU(),
  136. }
  137. MiningEnabledFlag = cli.BoolFlag{
  138. Name: "mine",
  139. Usage: "Enable mining",
  140. }
  141. AutoDAGFlag = cli.BoolFlag{
  142. Name: "autodag",
  143. Usage: "Enable automatic DAG pregeneration",
  144. }
  145. EtherbaseFlag = cli.StringFlag{
  146. Name: "etherbase",
  147. Usage: "Public address for block mining rewards. By default the address first created is used",
  148. Value: "0",
  149. }
  150. GasPriceFlag = cli.StringFlag{
  151. Name: "gasprice",
  152. Usage: "Sets the minimal gasprice when mining transactions",
  153. Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
  154. }
  155. UnlockedAccountFlag = cli.StringFlag{
  156. Name: "unlock",
  157. Usage: "Unlock the account given until this program exits (prompts for password). '--unlock n' unlocks the n-th account in order or creation.",
  158. Value: "",
  159. }
  160. PasswordFileFlag = cli.StringFlag{
  161. Name: "password",
  162. Usage: "Path to password file to use with options and subcommands needing a password",
  163. Value: "",
  164. }
  165. // vm flags
  166. VMDebugFlag = cli.BoolFlag{
  167. Name: "vmdebug",
  168. Usage: "Virtual Machine debug output",
  169. }
  170. VMForceJitFlag = cli.BoolFlag{
  171. Name: "forcejit",
  172. Usage: "Force the JIT VM to take precedence",
  173. }
  174. VMJitCacheFlag = cli.IntFlag{
  175. Name: "jitcache",
  176. Usage: "Amount of cached JIT VM programs",
  177. Value: 64,
  178. }
  179. VMEnableJitFlag = cli.BoolFlag{
  180. Name: "jitvm",
  181. Usage: "Enable the JIT VM",
  182. }
  183. // logging and debug settings
  184. LogFileFlag = cli.StringFlag{
  185. Name: "logfile",
  186. Usage: "Send log output to a file",
  187. }
  188. VerbosityFlag = cli.IntFlag{
  189. Name: "verbosity",
  190. Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
  191. Value: int(logger.InfoLevel),
  192. }
  193. LogJSONFlag = cli.StringFlag{
  194. Name: "logjson",
  195. Usage: "Send json structured log output to a file or '-' for standard output (default: no json output)",
  196. Value: "",
  197. }
  198. LogToStdErrFlag = cli.BoolFlag{
  199. Name: "logtostderr",
  200. Usage: "Logs are written to standard error instead of to files.",
  201. }
  202. LogVModuleFlag = cli.GenericFlag{
  203. Name: "vmodule",
  204. 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.",
  205. Value: glog.GetVModule(),
  206. }
  207. BacktraceAtFlag = cli.GenericFlag{
  208. Name: "backtrace_at",
  209. Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged",
  210. Value: glog.GetTraceLocation(),
  211. }
  212. PProfEanbledFlag = cli.BoolFlag{
  213. Name: "pprof",
  214. Usage: "Enable the profiling server on localhost",
  215. }
  216. PProfPortFlag = cli.IntFlag{
  217. Name: "pprofport",
  218. Usage: "Port on which the profiler should listen",
  219. Value: 6060,
  220. }
  221. MetricsEnabledFlag = cli.BoolFlag{
  222. Name: metrics.MetricsEnabledFlag,
  223. Usage: "Enables metrics collection and reporting",
  224. }
  225. // RPC settings
  226. RPCEnabledFlag = cli.BoolFlag{
  227. Name: "rpc",
  228. Usage: "Enable the JSON-RPC server",
  229. }
  230. RPCListenAddrFlag = cli.StringFlag{
  231. Name: "rpcaddr",
  232. Usage: "Listening address for the JSON-RPC server",
  233. Value: "127.0.0.1",
  234. }
  235. RPCPortFlag = cli.IntFlag{
  236. Name: "rpcport",
  237. Usage: "Port on which the JSON-RPC server should listen",
  238. Value: 8545,
  239. }
  240. RPCCORSDomainFlag = cli.StringFlag{
  241. Name: "rpccorsdomain",
  242. Usage: "Domain on which to send Access-Control-Allow-Origin header",
  243. Value: "",
  244. }
  245. RpcApiFlag = cli.StringFlag{
  246. Name: "rpcapi",
  247. Usage: "Specify the API's which are offered over the HTTP RPC interface",
  248. Value: comms.DefaultHttpRpcApis,
  249. }
  250. IPCDisabledFlag = cli.BoolFlag{
  251. Name: "ipcdisable",
  252. Usage: "Disable the IPC-RPC server",
  253. }
  254. IPCApiFlag = cli.StringFlag{
  255. Name: "ipcapi",
  256. Usage: "Specify the API's which are offered over the IPC interface",
  257. Value: comms.DefaultIpcApis,
  258. }
  259. IPCPathFlag = DirectoryFlag{
  260. Name: "ipcpath",
  261. Usage: "Filename for IPC socket/pipe",
  262. Value: DirectoryString{common.DefaultIpcPath()},
  263. }
  264. ExecFlag = cli.StringFlag{
  265. Name: "exec",
  266. Usage: "Execute javascript statement (only in combination with console/attach)",
  267. }
  268. // Network Settings
  269. MaxPeersFlag = cli.IntFlag{
  270. Name: "maxpeers",
  271. Usage: "Maximum number of network peers (network disabled if set to 0)",
  272. Value: 25,
  273. }
  274. MaxPendingPeersFlag = cli.IntFlag{
  275. Name: "maxpendpeers",
  276. Usage: "Maximum number of pending connection attempts (defaults used if set to 0)",
  277. Value: 0,
  278. }
  279. ListenPortFlag = cli.IntFlag{
  280. Name: "port",
  281. Usage: "Network listening port",
  282. Value: 30303,
  283. }
  284. BootnodesFlag = cli.StringFlag{
  285. Name: "bootnodes",
  286. Usage: "Space-separated enode URLs for p2p discovery bootstrap",
  287. Value: "",
  288. }
  289. NodeKeyFileFlag = cli.StringFlag{
  290. Name: "nodekey",
  291. Usage: "P2P node key file",
  292. }
  293. NodeKeyHexFlag = cli.StringFlag{
  294. Name: "nodekeyhex",
  295. Usage: "P2P node key as hex (for testing)",
  296. }
  297. NATFlag = cli.StringFlag{
  298. Name: "nat",
  299. Usage: "NAT port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
  300. Value: "any",
  301. }
  302. NoDiscoverFlag = cli.BoolFlag{
  303. Name: "nodiscover",
  304. Usage: "Disables the peer discovery mechanism (manual peer addition)",
  305. }
  306. WhisperEnabledFlag = cli.BoolFlag{
  307. Name: "shh",
  308. Usage: "Enable whisper",
  309. }
  310. // ATM the url is left to the user and deployment to
  311. JSpathFlag = cli.StringFlag{
  312. Name: "jspath",
  313. Usage: "JS library path to be used with console and js subcommands",
  314. Value: ".",
  315. }
  316. SolcPathFlag = cli.StringFlag{
  317. Name: "solc",
  318. Usage: "solidity compiler to be used",
  319. Value: "solc",
  320. }
  321. GpoMinGasPriceFlag = cli.StringFlag{
  322. Name: "gpomin",
  323. Usage: "Minimum suggested gas price",
  324. Value: new(big.Int).Mul(big.NewInt(50), common.Shannon).String(),
  325. }
  326. GpoMaxGasPriceFlag = cli.StringFlag{
  327. Name: "gpomax",
  328. Usage: "Maximum suggested gas price",
  329. Value: new(big.Int).Mul(big.NewInt(500), common.Shannon).String(),
  330. }
  331. GpoFullBlockRatioFlag = cli.IntFlag{
  332. Name: "gpofull",
  333. Usage: "Full block threshold for gas price calculation (%)",
  334. Value: 80,
  335. }
  336. GpobaseStepDownFlag = cli.IntFlag{
  337. Name: "gpobasedown",
  338. Usage: "Suggested gas price base step down ratio (1/1000)",
  339. Value: 10,
  340. }
  341. GpobaseStepUpFlag = cli.IntFlag{
  342. Name: "gpobaseup",
  343. Usage: "Suggested gas price base step up ratio (1/1000)",
  344. Value: 100,
  345. }
  346. GpobaseCorrectionFactorFlag = cli.IntFlag{
  347. Name: "gpobasecf",
  348. Usage: "Suggested gas price base correction factor (%)",
  349. Value: 110,
  350. }
  351. )
  352. // MakeNAT creates a port mapper from set command line flags.
  353. func MakeNAT(ctx *cli.Context) nat.Interface {
  354. natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
  355. if err != nil {
  356. Fatalf("Option %s: %v", NATFlag.Name, err)
  357. }
  358. return natif
  359. }
  360. // MakeNodeKey creates a node key from set command line flags.
  361. func MakeNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
  362. hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
  363. var err error
  364. switch {
  365. case file != "" && hex != "":
  366. Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
  367. case file != "":
  368. if key, err = crypto.LoadECDSA(file); err != nil {
  369. Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
  370. }
  371. case hex != "":
  372. if key, err = crypto.HexToECDSA(hex); err != nil {
  373. Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
  374. }
  375. }
  376. return key
  377. }
  378. // MakeEthConfig creates ethereum options from set command line flags.
  379. func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
  380. customName := ctx.GlobalString(IdentityFlag.Name)
  381. if len(customName) > 0 {
  382. clientID += "/" + customName
  383. }
  384. am := MakeAccountManager(ctx)
  385. etherbase, err := ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)
  386. if err != nil {
  387. glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default")
  388. }
  389. return &eth.Config{
  390. Name: common.MakeName(clientID, version),
  391. DataDir: ctx.GlobalString(DataDirFlag.Name),
  392. GenesisNonce: ctx.GlobalInt(GenesisNonceFlag.Name),
  393. GenesisFile: ctx.GlobalString(GenesisFileFlag.Name),
  394. BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
  395. DatabaseCache: ctx.GlobalInt(CacheFlag.Name),
  396. SkipBcVersionCheck: false,
  397. NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
  398. LogFile: ctx.GlobalString(LogFileFlag.Name),
  399. Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
  400. LogJSON: ctx.GlobalString(LogJSONFlag.Name),
  401. Etherbase: common.HexToAddress(etherbase),
  402. MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
  403. AccountManager: am,
  404. VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
  405. MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
  406. MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
  407. Port: ctx.GlobalString(ListenPortFlag.Name),
  408. Olympic: ctx.GlobalBool(OlympicFlag.Name),
  409. NAT: MakeNAT(ctx),
  410. NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
  411. Discovery: !ctx.GlobalBool(NoDiscoverFlag.Name),
  412. NodeKey: MakeNodeKey(ctx),
  413. Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
  414. Dial: true,
  415. BootNodes: ctx.GlobalString(BootnodesFlag.Name),
  416. GasPrice: common.String2Big(ctx.GlobalString(GasPriceFlag.Name)),
  417. GpoMinGasPrice: common.String2Big(ctx.GlobalString(GpoMinGasPriceFlag.Name)),
  418. GpoMaxGasPrice: common.String2Big(ctx.GlobalString(GpoMaxGasPriceFlag.Name)),
  419. GpoFullBlockRatio: ctx.GlobalInt(GpoFullBlockRatioFlag.Name),
  420. GpobaseStepDown: ctx.GlobalInt(GpobaseStepDownFlag.Name),
  421. GpobaseStepUp: ctx.GlobalInt(GpobaseStepUpFlag.Name),
  422. GpobaseCorrectionFactor: ctx.GlobalInt(GpobaseCorrectionFactorFlag.Name),
  423. SolcPath: ctx.GlobalString(SolcPathFlag.Name),
  424. AutoDAG: ctx.GlobalBool(AutoDAGFlag.Name) || ctx.GlobalBool(MiningEnabledFlag.Name),
  425. }
  426. }
  427. // SetupLogger configures glog from the logging-related command line flags.
  428. func SetupLogger(ctx *cli.Context) {
  429. glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
  430. glog.CopyStandardLogTo("INFO")
  431. glog.SetToStderr(true)
  432. glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
  433. }
  434. // SetupVM configured the VM package's global settings
  435. func SetupVM(ctx *cli.Context) {
  436. vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
  437. vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
  438. vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
  439. }
  440. // MakeChain creates a chain manager from set command line flags.
  441. func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb common.Database) {
  442. datadir := ctx.GlobalString(DataDirFlag.Name)
  443. cache := ctx.GlobalInt(CacheFlag.Name)
  444. var err error
  445. if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil {
  446. Fatalf("Could not open database: %v", err)
  447. }
  448. if ctx.GlobalBool(OlympicFlag.Name) {
  449. InitOlympic()
  450. _, err := core.WriteTestNetGenesisBlock(chainDb, 42)
  451. if err != nil {
  452. glog.Fatalln(err)
  453. }
  454. }
  455. eventMux := new(event.TypeMux)
  456. pow := ethash.New()
  457. //genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
  458. chain, err = core.NewChainManager(chainDb, pow, eventMux)
  459. if err != nil {
  460. Fatalf("Could not start chainmanager: %v", err)
  461. }
  462. proc := core.NewBlockProcessor(chainDb, pow, chain, eventMux)
  463. chain.SetProcessor(proc)
  464. return chain, chainDb
  465. }
  466. // MakeChain creates an account manager from set command line flags.
  467. func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
  468. dataDir := ctx.GlobalString(DataDirFlag.Name)
  469. ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"))
  470. return accounts.NewManager(ks)
  471. }
  472. func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
  473. if runtime.GOOS == "windows" {
  474. ipcpath = common.DefaultIpcPath()
  475. if ctx.GlobalIsSet(IPCPathFlag.Name) {
  476. ipcpath = ctx.GlobalString(IPCPathFlag.Name)
  477. }
  478. } else {
  479. ipcpath = common.DefaultIpcPath()
  480. if ctx.GlobalIsSet(DataDirFlag.Name) {
  481. ipcpath = filepath.Join(ctx.GlobalString(DataDirFlag.Name), "geth.ipc")
  482. }
  483. if ctx.GlobalIsSet(IPCPathFlag.Name) {
  484. ipcpath = ctx.GlobalString(IPCPathFlag.Name)
  485. }
  486. }
  487. return
  488. }
  489. func StartIPC(eth *eth.Ethereum, ctx *cli.Context) error {
  490. config := comms.IpcConfig{
  491. Endpoint: IpcSocketPath(ctx),
  492. }
  493. initializer := func(conn net.Conn) (shared.EthereumApi, error) {
  494. fe := useragent.NewRemoteFrontend(conn, eth.AccountManager())
  495. xeth := xeth.New(eth, fe)
  496. codec := codec.JSON
  497. apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec, xeth, eth)
  498. if err != nil {
  499. return nil, err
  500. }
  501. return api.Merge(apis...), nil
  502. }
  503. return comms.StartIpc(config, codec.JSON, initializer)
  504. }
  505. func StartRPC(eth *eth.Ethereum, ctx *cli.Context) error {
  506. config := comms.HttpConfig{
  507. ListenAddress: ctx.GlobalString(RPCListenAddrFlag.Name),
  508. ListenPort: uint(ctx.GlobalInt(RPCPortFlag.Name)),
  509. CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
  510. }
  511. xeth := xeth.New(eth, nil)
  512. codec := codec.JSON
  513. apis, err := api.ParseApiString(ctx.GlobalString(RpcApiFlag.Name), codec, xeth, eth)
  514. if err != nil {
  515. return err
  516. }
  517. return comms.StartHttp(config, codec, api.Merge(apis...))
  518. }
  519. func StartPProf(ctx *cli.Context) {
  520. address := fmt.Sprintf("localhost:%d", ctx.GlobalInt(PProfPortFlag.Name))
  521. go func() {
  522. log.Println(http.ListenAndServe(address, nil))
  523. }()
  524. }
  525. func ParamToAddress(addr string, am *accounts.Manager) (addrHex string, err error) {
  526. if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
  527. index, err := strconv.Atoi(addr)
  528. if err != nil {
  529. Fatalf("Invalid account address '%s'", addr)
  530. }
  531. addrHex, err = am.AddressByIndex(index)
  532. if err != nil {
  533. return "", err
  534. }
  535. } else {
  536. addrHex = addr
  537. }
  538. return
  539. }