geth.go 7.5 KB

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