flags.go 17 KB

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