api_backend.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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
  17. import (
  18. "context"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/accounts"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/bloombits"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/eth/downloader"
  30. "github.com/ethereum/go-ethereum/eth/gasprice"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. "github.com/ethereum/go-ethereum/event"
  33. "github.com/ethereum/go-ethereum/light"
  34. "github.com/ethereum/go-ethereum/params"
  35. "github.com/ethereum/go-ethereum/rpc"
  36. )
  37. type LesApiBackend struct {
  38. extRPCEnabled bool
  39. eth *LightEthereum
  40. gpo *gasprice.Oracle
  41. }
  42. func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
  43. return b.eth.chainConfig
  44. }
  45. func (b *LesApiBackend) CurrentBlock() *types.Block {
  46. return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
  47. }
  48. func (b *LesApiBackend) SetHead(number uint64) {
  49. b.eth.protocolManager.downloader.Cancel()
  50. b.eth.blockchain.SetHead(number)
  51. }
  52. func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
  53. if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
  54. return b.eth.blockchain.CurrentHeader(), nil
  55. }
  56. return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
  57. }
  58. func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  59. return b.eth.blockchain.GetHeaderByHash(hash), nil
  60. }
  61. func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
  62. header, err := b.HeaderByNumber(ctx, blockNr)
  63. if header == nil || err != nil {
  64. return nil, err
  65. }
  66. return b.GetBlock(ctx, header.Hash())
  67. }
  68. func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
  69. header, err := b.HeaderByNumber(ctx, blockNr)
  70. if header == nil || err != nil {
  71. return nil, nil, err
  72. }
  73. return light.NewState(ctx, header, b.eth.odr), header, nil
  74. }
  75. func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
  76. return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
  77. }
  78. func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
  79. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  80. return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
  81. }
  82. return nil, nil
  83. }
  84. func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
  85. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  86. return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
  87. }
  88. return nil, nil
  89. }
  90. func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int {
  91. return b.eth.blockchain.GetTdByHash(hash)
  92. }
  93. func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
  94. state.SetBalance(msg.From(), math.MaxBig256)
  95. context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
  96. return vm.NewEVM(context, state, b.eth.chainConfig, vm.Config{}), state.Error, nil
  97. }
  98. func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  99. return b.eth.txPool.Add(ctx, signedTx)
  100. }
  101. func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
  102. b.eth.txPool.RemoveTx(txHash)
  103. }
  104. func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
  105. return b.eth.txPool.GetTransactions()
  106. }
  107. func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
  108. return b.eth.txPool.GetTransaction(txHash)
  109. }
  110. func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  111. return b.eth.txPool.GetNonce(ctx, addr)
  112. }
  113. func (b *LesApiBackend) Stats() (pending int, queued int) {
  114. return b.eth.txPool.Stats(), 0
  115. }
  116. func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  117. return b.eth.txPool.Content()
  118. }
  119. func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
  120. return b.eth.txPool.SubscribeNewTxsEvent(ch)
  121. }
  122. func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  123. return b.eth.blockchain.SubscribeChainEvent(ch)
  124. }
  125. func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  126. return b.eth.blockchain.SubscribeChainHeadEvent(ch)
  127. }
  128. func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  129. return b.eth.blockchain.SubscribeChainSideEvent(ch)
  130. }
  131. func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  132. return b.eth.blockchain.SubscribeLogsEvent(ch)
  133. }
  134. func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  135. return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
  136. }
  137. func (b *LesApiBackend) Downloader() *downloader.Downloader {
  138. return b.eth.Downloader()
  139. }
  140. func (b *LesApiBackend) ProtocolVersion() int {
  141. return b.eth.LesVersion() + 10000
  142. }
  143. func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  144. return b.gpo.SuggestPrice(ctx)
  145. }
  146. func (b *LesApiBackend) ChainDb() ethdb.Database {
  147. return b.eth.chainDb
  148. }
  149. func (b *LesApiBackend) EventMux() *event.TypeMux {
  150. return b.eth.eventMux
  151. }
  152. func (b *LesApiBackend) AccountManager() *accounts.Manager {
  153. return b.eth.accountManager
  154. }
  155. func (b *LesApiBackend) ExtRPCEnabled() bool {
  156. return b.extRPCEnabled
  157. }
  158. func (b *LesApiBackend) RPCGasCap() *big.Int {
  159. return b.eth.config.RPCGasCap
  160. }
  161. func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
  162. if b.eth.bloomIndexer == nil {
  163. return 0, 0
  164. }
  165. sections, _, _ := b.eth.bloomIndexer.Sections()
  166. return params.BloomBitsBlocksClient, sections
  167. }
  168. func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  169. for i := 0; i < bloomFilterThreads; i++ {
  170. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  171. }
  172. }