flags.go 17 KB

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