backend.go 6.7 KB

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