api_backend.go 6.4 KB

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