backend.go 6.8 KB

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