backend.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // Package les implements the Light Ethereum Subprotocol.
  17. package les
  18. import (
  19. "fmt"
  20. "time"
  21. "github.com/ethereum/go-ethereum/accounts"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/compiler"
  24. "github.com/ethereum/go-ethereum/common/hexutil"
  25. "github.com/ethereum/go-ethereum/consensus"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/eth"
  29. "github.com/ethereum/go-ethereum/eth/downloader"
  30. "github.com/ethereum/go-ethereum/eth/filters"
  31. "github.com/ethereum/go-ethereum/eth/gasprice"
  32. "github.com/ethereum/go-ethereum/ethdb"
  33. "github.com/ethereum/go-ethereum/event"
  34. "github.com/ethereum/go-ethereum/internal/ethapi"
  35. "github.com/ethereum/go-ethereum/light"
  36. "github.com/ethereum/go-ethereum/log"
  37. "github.com/ethereum/go-ethereum/node"
  38. "github.com/ethereum/go-ethereum/p2p"
  39. "github.com/ethereum/go-ethereum/params"
  40. rpc "github.com/ethereum/go-ethereum/rpc"
  41. )
  42. type LightEthereum struct {
  43. odr *LesOdr
  44. relay *LesTxRelay
  45. chainConfig *params.ChainConfig
  46. // Channel for shutting down the service
  47. shutdownChan chan bool
  48. // Handlers
  49. txPool *light.TxPool
  50. blockchain *light.LightChain
  51. protocolManager *ProtocolManager
  52. // DB interfaces
  53. chainDb ethdb.Database // Block chain database
  54. ApiBackend *LesApiBackend
  55. eventMux *event.TypeMux
  56. engine consensus.Engine
  57. accountManager *accounts.Manager
  58. solcPath string
  59. solc *compiler.Solidity
  60. netVersionId int
  61. netRPCService *ethapi.PublicNetAPI
  62. }
  63. func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
  64. chainDb, err := eth.CreateDB(ctx, config, "lightchaindata")
  65. if err != nil {
  66. return nil, err
  67. }
  68. chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
  69. if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
  70. return nil, genesisErr
  71. }
  72. log.Info("Initialised chain configuration", "config", chainConfig)
  73. odr := NewLesOdr(chainDb)
  74. relay := NewLesTxRelay()
  75. eth := &LightEthereum{
  76. odr: odr,
  77. relay: relay,
  78. chainDb: chainDb,
  79. chainConfig: chainConfig,
  80. eventMux: ctx.EventMux,
  81. accountManager: ctx.AccountManager,
  82. engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb),
  83. shutdownChan: make(chan bool),
  84. netVersionId: config.NetworkId,
  85. solcPath: config.SolcPath,
  86. }
  87. if eth.blockchain, err = light.NewLightChain(odr, eth.chainConfig, eth.engine, eth.eventMux); err != nil {
  88. return nil, err
  89. }
  90. // Rewind the chain in case of an incompatible config upgrade.
  91. if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
  92. log.Warn("Rewinding chain to upgrade configuration", "err", compat)
  93. eth.blockchain.SetHead(compat.RewindTo)
  94. core.WriteChainConfig(chainDb, genesisHash, chainConfig)
  95. }
  96. eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay)
  97. if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.engine, eth.blockchain, nil, chainDb, odr, relay); err != nil {
  98. return nil, err
  99. }
  100. relay.ps = eth.protocolManager.peers
  101. relay.reqDist = eth.protocolManager.reqDist
  102. eth.ApiBackend = &LesApiBackend{eth, nil}
  103. gpoParams := gasprice.Config{
  104. Blocks: config.GpoBlocks,
  105. Percentile: config.GpoPercentile,
  106. Default: config.GasPrice,
  107. }
  108. eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams)
  109. return eth, nil
  110. }
  111. type LightDummyAPI struct{}
  112. // Etherbase is the address that mining rewards will be send to
  113. func (s *LightDummyAPI) Etherbase() (common.Address, error) {
  114. return common.Address{}, fmt.Errorf("not supported")
  115. }
  116. // Coinbase is the address that mining rewards will be send to (alias for Etherbase)
  117. func (s *LightDummyAPI) Coinbase() (common.Address, error) {
  118. return common.Address{}, fmt.Errorf("not supported")
  119. }
  120. // Hashrate returns the POW hashrate
  121. func (s *LightDummyAPI) Hashrate() hexutil.Uint {
  122. return 0
  123. }
  124. // Mining returns an indication if this node is currently mining.
  125. func (s *LightDummyAPI) Mining() bool {
  126. return false
  127. }
  128. // APIs returns the collection of RPC services the ethereum package offers.
  129. // NOTE, some of these services probably need to be moved to somewhere else.
  130. func (s *LightEthereum) APIs() []rpc.API {
  131. return append(ethapi.GetAPIs(s.ApiBackend, s.solcPath), []rpc.API{
  132. {
  133. Namespace: "eth",
  134. Version: "1.0",
  135. Service: &LightDummyAPI{},
  136. Public: true,
  137. }, {
  138. Namespace: "eth",
  139. Version: "1.0",
  140. Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
  141. Public: true,
  142. }, {
  143. Namespace: "eth",
  144. Version: "1.0",
  145. Service: filters.NewPublicFilterAPI(s.ApiBackend, true),
  146. Public: true,
  147. }, {
  148. Namespace: "net",
  149. Version: "1.0",
  150. Service: s.netRPCService,
  151. Public: true,
  152. },
  153. }...)
  154. }
  155. func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
  156. s.blockchain.ResetWithGenesisBlock(gb)
  157. }
  158. func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
  159. func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
  160. func (s *LightEthereum) Engine() consensus.Engine { return s.engine }
  161. func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
  162. func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
  163. func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
  164. // Protocols implements node.Service, returning all the currently configured
  165. // network protocols to start.
  166. func (s *LightEthereum) Protocols() []p2p.Protocol {
  167. return s.protocolManager.SubProtocols
  168. }
  169. // Start implements node.Service, starting all internal goroutines needed by the
  170. // Ethereum protocol implementation.
  171. func (s *LightEthereum) Start(srvr *p2p.Server) error {
  172. log.Warn("Light client mode is an experimental feature")
  173. s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId)
  174. s.protocolManager.Start(srvr)
  175. return nil
  176. }
  177. // Stop implements node.Service, terminating all internal goroutines used by the
  178. // Ethereum protocol.
  179. func (s *LightEthereum) Stop() error {
  180. s.odr.Stop()
  181. s.blockchain.Stop()
  182. s.protocolManager.Stop()
  183. s.txPool.Stop()
  184. s.eventMux.Stop()
  185. time.Sleep(time.Millisecond * 200)
  186. s.chainDb.Close()
  187. close(s.shutdownChan)
  188. return nil
  189. }