backend.go 6.7 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. "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. NatSpec bool
  63. netVersionId int
  64. netRPCService *ethapi.PublicNetAPI
  65. }
  66. func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
  67. chainDb, err := eth.CreateDB(ctx, config, "lightchaindata")
  68. if err != nil {
  69. return nil, err
  70. }
  71. if err := eth.SetupGenesisBlock(&chainDb, config); err != nil {
  72. return nil, err
  73. }
  74. pow, err := eth.CreatePoW(config)
  75. if err != nil {
  76. return nil, err
  77. }
  78. odr := NewLesOdr(chainDb)
  79. relay := NewLesTxRelay()
  80. eth := &LightEthereum{
  81. odr: odr,
  82. relay: relay,
  83. chainDb: chainDb,
  84. eventMux: ctx.EventMux,
  85. accountManager: ctx.AccountManager,
  86. pow: pow,
  87. shutdownChan: make(chan bool),
  88. netVersionId: config.NetworkId,
  89. NatSpec: config.NatSpec,
  90. solcPath: config.SolcPath,
  91. }
  92. if config.ChainConfig == nil {
  93. return nil, errors.New("missing chain config")
  94. }
  95. eth.chainConfig = config.ChainConfig
  96. eth.blockchain, err = light.NewLightChain(odr, eth.chainConfig, eth.pow, eth.eventMux)
  97. if err != nil {
  98. if err == core.ErrNoGenesis {
  99. return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`)
  100. }
  101. return nil, err
  102. }
  103. eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay)
  104. if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil {
  105. return nil, err
  106. }
  107. eth.ApiBackend = &LesApiBackend{eth, nil}
  108. eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend)
  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) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
  161. func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
  162. func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
  163. // Protocols implements node.Service, returning all the currently configured
  164. // network protocols to start.
  165. func (s *LightEthereum) Protocols() []p2p.Protocol {
  166. return s.protocolManager.SubProtocols
  167. }
  168. // Start implements node.Service, starting all internal goroutines needed by the
  169. // Ethereum protocol implementation.
  170. func (s *LightEthereum) Start(srvr *p2p.Server) error {
  171. glog.V(logger.Info).Infof("WARNING: light client mode is an experimental feature")
  172. s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId)
  173. s.protocolManager.Start(srvr)
  174. return nil
  175. }
  176. // Stop implements node.Service, terminating all internal goroutines used by the
  177. // Ethereum protocol.
  178. func (s *LightEthereum) Stop() error {
  179. s.odr.Stop()
  180. s.blockchain.Stop()
  181. s.protocolManager.Stop()
  182. s.txPool.Stop()
  183. s.eventMux.Stop()
  184. time.Sleep(time.Millisecond * 200)
  185. s.chainDb.Close()
  186. close(s.shutdownChan)
  187. return nil
  188. }