backend.go 6.8 KB

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