geth.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains all the wrappers from the node package to support client side node
  17. // management on mobile platforms.
  18. package geth
  19. import (
  20. "fmt"
  21. "math/big"
  22. "path/filepath"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/ethclient"
  26. "github.com/ethereum/go-ethereum/ethstats"
  27. "github.com/ethereum/go-ethereum/les"
  28. "github.com/ethereum/go-ethereum/node"
  29. "github.com/ethereum/go-ethereum/p2p/nat"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/whisper/whisperv2"
  32. )
  33. // NodeConfig represents the collection of configuration values to fine tune the Geth
  34. // node embedded into a mobile process. The available values are a subset of the
  35. // entire API provided by go-ethereum to reduce the maintenance surface and dev
  36. // complexity.
  37. type NodeConfig struct {
  38. // Bootstrap nodes used to establish connectivity with the rest of the network.
  39. BootstrapNodes *Enodes
  40. // MaxPeers is the maximum number of peers that can be connected. If this is
  41. // set to zero, then only the configured static and trusted peers can connect.
  42. MaxPeers int
  43. // EthereumEnabled specifies whether the node should run the Ethereum protocol.
  44. EthereumEnabled bool
  45. // EthereumNetworkID is the network identifier used by the Ethereum protocol to
  46. // decide if remote peers should be accepted or not.
  47. EthereumNetworkID int
  48. // EthereumChainConfig is the default parameters of the blockchain to use. If no
  49. // configuration is specified, it defaults to the main network.
  50. EthereumChainConfig *ChainConfig
  51. // EthereumGenesis is the genesis JSON to use to seed the blockchain with. An
  52. // empty genesis state is equivalent to using the mainnet's state.
  53. EthereumGenesis string
  54. // EthereumDatabaseCache is the system memory in MB to allocate for database caching.
  55. // A minimum of 16MB is always reserved.
  56. EthereumDatabaseCache int
  57. // EthereumNetStats is a netstats connection string to use to report various
  58. // chain, transaction and node stats to a monitoring server.
  59. //
  60. // It has the form "nodename:secret@host:port"
  61. EthereumNetStats string
  62. // WhisperEnabled specifies whether the node should run the Whisper protocol.
  63. WhisperEnabled bool
  64. }
  65. // defaultNodeConfig contains the default node configuration values to use if all
  66. // or some fields are missing from the user's specified list.
  67. var defaultNodeConfig = &NodeConfig{
  68. BootstrapNodes: FoundationBootnodes(),
  69. MaxPeers: 25,
  70. EthereumEnabled: true,
  71. EthereumNetworkID: 1,
  72. EthereumChainConfig: MainnetChainConfig(),
  73. EthereumDatabaseCache: 16,
  74. }
  75. // NewNodeConfig creates a new node option set, initialized to the default values.
  76. func NewNodeConfig() *NodeConfig {
  77. config := *defaultNodeConfig
  78. return &config
  79. }
  80. // Node represents a Geth Ethereum node instance.
  81. type Node struct {
  82. node *node.Node
  83. }
  84. // NewNode creates and configures a new Geth node.
  85. func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
  86. // If no or partial configurations were specified, use defaults
  87. if config == nil {
  88. config = NewNodeConfig()
  89. }
  90. if config.MaxPeers == 0 {
  91. config.MaxPeers = defaultNodeConfig.MaxPeers
  92. }
  93. if config.BootstrapNodes == nil || config.BootstrapNodes.Size() == 0 {
  94. config.BootstrapNodes = defaultNodeConfig.BootstrapNodes
  95. }
  96. // Create the empty networking stack
  97. nodeConf := &node.Config{
  98. Name: clientIdentifier,
  99. Version: params.Version,
  100. DataDir: datadir,
  101. KeyStoreDir: filepath.Join(datadir, "keystore"), // Mobile should never use internal keystores!
  102. NoDiscovery: true,
  103. DiscoveryV5: true,
  104. DiscoveryV5Addr: ":0",
  105. BootstrapNodesV5: config.BootstrapNodes.nodes,
  106. ListenAddr: ":0",
  107. NAT: nat.Any(),
  108. MaxPeers: config.MaxPeers,
  109. }
  110. rawStack, err := node.New(nodeConf)
  111. if err != nil {
  112. return nil, err
  113. }
  114. // Register the Ethereum protocol if requested
  115. if config.EthereumEnabled {
  116. ethConf := &eth.Config{
  117. ChainConfig: &params.ChainConfig{
  118. ChainId: big.NewInt(config.EthereumChainConfig.ChainID),
  119. HomesteadBlock: big.NewInt(config.EthereumChainConfig.HomesteadBlock),
  120. DAOForkBlock: big.NewInt(config.EthereumChainConfig.DAOForkBlock),
  121. DAOForkSupport: config.EthereumChainConfig.DAOForkSupport,
  122. EIP150Block: big.NewInt(config.EthereumChainConfig.EIP150Block),
  123. EIP150Hash: config.EthereumChainConfig.EIP150Hash.hash,
  124. EIP155Block: big.NewInt(config.EthereumChainConfig.EIP155Block),
  125. EIP158Block: big.NewInt(config.EthereumChainConfig.EIP158Block),
  126. },
  127. Genesis: config.EthereumGenesis,
  128. LightMode: true,
  129. DatabaseCache: config.EthereumDatabaseCache,
  130. NetworkId: config.EthereumNetworkID,
  131. GasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
  132. GpoMinGasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
  133. GpoMaxGasPrice: new(big.Int).Mul(big.NewInt(500), common.Shannon),
  134. GpoFullBlockRatio: 80,
  135. GpobaseStepDown: 10,
  136. GpobaseStepUp: 100,
  137. GpobaseCorrectionFactor: 110,
  138. }
  139. if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  140. return les.New(ctx, ethConf)
  141. }); err != nil {
  142. return nil, fmt.Errorf("ethereum init: %v", err)
  143. }
  144. // If netstats reporting is requested, do it
  145. if config.EthereumNetStats != "" {
  146. if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  147. var lesServ *les.LightEthereum
  148. ctx.Service(&lesServ)
  149. return ethstats.New(config.EthereumNetStats, nil, lesServ)
  150. }); err != nil {
  151. return nil, fmt.Errorf("netstats init: %v", err)
  152. }
  153. }
  154. }
  155. // Register the Whisper protocol if requested
  156. if config.WhisperEnabled {
  157. if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) { return whisperv2.New(), nil }); err != nil {
  158. return nil, fmt.Errorf("whisper init: %v", err)
  159. }
  160. }
  161. return &Node{rawStack}, nil
  162. }
  163. // Start creates a live P2P node and starts running it.
  164. func (n *Node) Start() error {
  165. return n.node.Start()
  166. }
  167. // Stop terminates a running node along with all it's services. In the node was
  168. // not started, an error is returned.
  169. func (n *Node) Stop() error {
  170. return n.node.Stop()
  171. }
  172. // GetEthereumClient retrieves a client to access the Ethereum subsystem.
  173. func (n *Node) GetEthereumClient() (client *EthereumClient, _ error) {
  174. rpc, err := n.node.Attach()
  175. if err != nil {
  176. return nil, err
  177. }
  178. return &EthereumClient{ethclient.NewClient(rpc)}, nil
  179. }
  180. // GetNodeInfo gathers and returns a collection of metadata known about the host.
  181. func (n *Node) GetNodeInfo() *NodeInfo {
  182. return &NodeInfo{n.node.Server().NodeInfo()}
  183. }
  184. // GetPeersInfo returns an array of metadata objects describing connected peers.
  185. func (n *Node) GetPeersInfo() *PeerInfos {
  186. return &PeerInfos{n.node.Server().PeersInfo()}
  187. }