flags.go 20 KB

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