geth.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/cmd/utils"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/eth"
  28. "github.com/ethereum/go-ethereum/ethclient"
  29. "github.com/ethereum/go-ethereum/node"
  30. "github.com/ethereum/go-ethereum/p2p/nat"
  31. "github.com/ethereum/go-ethereum/whisper"
  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. // EthereumTestnetNonces specifies whether to use account nonces from the testnet
  55. // range (2^20) or from the mainnet one (0).
  56. EthereumTestnetNonces bool
  57. // EthereumDatabaseCache is the system memory in MB to allocate for database caching.
  58. // A minimum of 16MB is always reserved.
  59. EthereumDatabaseCache int
  60. // WhisperEnabled specifies whether the node should run the Whisper protocol.
  61. WhisperEnabled bool
  62. }
  63. // defaultNodeConfig contains the default node configuration values to use if all
  64. // or some fields are missing from the user's specified list.
  65. var defaultNodeConfig = &NodeConfig{
  66. BootstrapNodes: MainnetBootnodes(),
  67. MaxPeers: 25,
  68. EthereumEnabled: true,
  69. EthereumNetworkID: 1,
  70. EthereumChainConfig: MainnetChainConfig,
  71. EthereumDatabaseCache: 16,
  72. }
  73. // NewNodeConfig creates a new node option set, initialized to the default values.
  74. func NewNodeConfig() *NodeConfig {
  75. config := *defaultNodeConfig
  76. return &config
  77. }
  78. // Node represents a Geth Ethereum node instance.
  79. type Node struct {
  80. node *node.Node
  81. }
  82. // NewNode creates and configures a new Geth node.
  83. func NewNode(datadir string, config *NodeConfig) (*Node, error) {
  84. // If no or partial configurations were specified, use defaults
  85. if config == nil {
  86. config = NewNodeConfig()
  87. }
  88. if config.MaxPeers == 0 {
  89. config.MaxPeers = defaultNodeConfig.MaxPeers
  90. }
  91. // Create the empty networking stack
  92. nodeConf := &node.Config{
  93. DataDir: datadir,
  94. KeyStoreDir: filepath.Join(datadir, "keystore"), // Mobile should never use internal keystores!
  95. Name: common.MakeName(clientIdentifier, utils.Version),
  96. BootstrapNodes: config.BootstrapNodes.nodes,
  97. ListenAddr: ":0",
  98. NAT: nat.Any(),
  99. MaxPeers: config.MaxPeers,
  100. }
  101. stack, err := node.New(nodeConf)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // Register the Ethereum protocol if requested
  106. if config.EthereumEnabled {
  107. ethConf := &eth.Config{
  108. ChainConfig: &core.ChainConfig{
  109. HomesteadBlock: big.NewInt(config.EthereumChainConfig.HomesteadBlock),
  110. DAOForkBlock: big.NewInt(config.EthereumChainConfig.DAOForkBlock),
  111. DAOForkSupport: config.EthereumChainConfig.DAOForkSupport,
  112. },
  113. Genesis: config.EthereumGenesis,
  114. FastSync: true,
  115. DatabaseCache: config.EthereumDatabaseCache,
  116. NetworkId: config.EthereumNetworkID,
  117. GasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
  118. GpoMinGasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
  119. GpoMaxGasPrice: new(big.Int).Mul(big.NewInt(500), common.Shannon),
  120. GpoFullBlockRatio: 80,
  121. GpobaseStepDown: 10,
  122. GpobaseStepUp: 100,
  123. GpobaseCorrectionFactor: 110,
  124. }
  125. if config.EthereumTestnetNonces {
  126. state.StartingNonce = 1048576 // (2**20)
  127. }
  128. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  129. return eth.New(ctx, ethConf)
  130. }); err != nil {
  131. return nil, fmt.Errorf("ethereum init: %v", err)
  132. }
  133. }
  134. // Register the Whisper protocol if requested
  135. if config.WhisperEnabled {
  136. if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
  137. return nil, fmt.Errorf("whisper init: %v", err)
  138. }
  139. }
  140. return &Node{stack}, nil
  141. }
  142. // Start creates a live P2P node and starts running it.
  143. func (n *Node) Start() error {
  144. return n.node.Start()
  145. }
  146. // Stop terminates a running node along with all it's services. In the node was
  147. // not started, an error is returned.
  148. func (n *Node) Stop() error {
  149. return n.node.Stop()
  150. }
  151. // GetEthereumClient retrieves a client to access the Ethereum subsystem.
  152. func (n *Node) GetEthereumClient() (*EthereumClient, error) {
  153. rpc, err := n.node.Attach()
  154. if err != nil {
  155. return nil, err
  156. }
  157. return &EthereumClient{ethclient.NewClient(rpc)}, nil
  158. }
  159. // GetNodeInfo gathers and returns a collection of metadata known about the host.
  160. func (n *Node) GetNodeInfo() *NodeInfo {
  161. return &NodeInfo{n.node.Server().NodeInfo()}
  162. }
  163. // GetPeersInfo returns an array of metadata objects describing connected peers.
  164. func (n *Node) GetPeersInfo() *PeerInfos {
  165. return &PeerInfos{n.node.Server().PeersInfo()}
  166. }