backend.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend)
  104. return eth, nil
  105. }
  106. type LightDummyAPI struct{}
  107. // Etherbase is the address that mining rewards will be send to
  108. func (s *LightDummyAPI) Etherbase() (common.Address, error) {
  109. return common.Address{}, fmt.Errorf("not supported")
  110. }
  111. // Coinbase is the address that mining rewards will be send to (alias for Etherbase)
  112. func (s *LightDummyAPI) Coinbase() (common.Address, error) {
  113. return common.Address{}, fmt.Errorf("not supported")
  114. }
  115. // Hashrate returns the POW hashrate
  116. func (s *LightDummyAPI) Hashrate() hexutil.Uint {
  117. return 0
  118. }
  119. // Mining returns an indication if this node is currently mining.
  120. func (s *LightDummyAPI) Mining() bool {
  121. return false
  122. }
  123. // APIs returns the collection of RPC services the ethereum package offers.
  124. // NOTE, some of these services probably need to be moved to somewhere else.
  125. func (s *LightEthereum) APIs() []rpc.API {
  126. return append(ethapi.GetAPIs(s.ApiBackend, s.solcPath), []rpc.API{
  127. {
  128. Namespace: "eth",
  129. Version: "1.0",
  130. Service: &LightDummyAPI{},
  131. Public: true,
  132. }, {
  133. Namespace: "eth",
  134. Version: "1.0",
  135. Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
  136. Public: true,
  137. }, {
  138. Namespace: "eth",
  139. Version: "1.0",
  140. Service: filters.NewPublicFilterAPI(s.ApiBackend, true),
  141. Public: true,
  142. }, {
  143. Namespace: "net",
  144. Version: "1.0",
  145. Service: s.netRPCService,
  146. Public: true,
  147. },
  148. }...)
  149. }
  150. func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
  151. s.blockchain.ResetWithGenesisBlock(gb)
  152. }
  153. func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
  154. func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
  155. func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
  156. func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
  157. func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
  158. // Protocols implements node.Service, returning all the currently configured
  159. // network protocols to start.
  160. func (s *LightEthereum) Protocols() []p2p.Protocol {
  161. return s.protocolManager.SubProtocols
  162. }
  163. // Start implements node.Service, starting all internal goroutines needed by the
  164. // Ethereum protocol implementation.
  165. func (s *LightEthereum) Start(srvr *p2p.Server) error {
  166. log.Warn("Light client mode is an experimental feature")
  167. s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId)
  168. s.protocolManager.Start(srvr)
  169. return nil
  170. }
  171. // Stop implements node.Service, terminating all internal goroutines used by the
  172. // Ethereum protocol.
  173. func (s *LightEthereum) Stop() error {
  174. s.odr.Stop()
  175. s.blockchain.Stop()
  176. s.protocolManager.Stop()
  177. s.txPool.Stop()
  178. s.eventMux.Stop()
  179. time.Sleep(time.Millisecond * 200)
  180. s.chainDb.Close()
  181. close(s.shutdownChan)
  182. return nil
  183. }