api_backend.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2015 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 eth
  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/params"
  33. "github.com/ethereum/go-ethereum/rpc"
  34. )
  35. // EthApiBackend implements ethapi.Backend for full nodes
  36. type EthApiBackend struct {
  37. eth *Ethereum
  38. gpo *gasprice.Oracle
  39. }
  40. func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
  41. return b.eth.chainConfig
  42. }
  43. func (b *EthApiBackend) CurrentBlock() *types.Block {
  44. return b.eth.blockchain.CurrentBlock()
  45. }
  46. func (b *EthApiBackend) SetHead(number uint64) {
  47. b.eth.protocolManager.downloader.Cancel()
  48. b.eth.blockchain.SetHead(number)
  49. }
  50. func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
  51. // Pending block is only known by the miner
  52. if blockNr == rpc.PendingBlockNumber {
  53. block := b.eth.miner.PendingBlock()
  54. return block.Header(), nil
  55. }
  56. // Otherwise resolve and return the block
  57. if blockNr == rpc.LatestBlockNumber {
  58. return b.eth.blockchain.CurrentBlock().Header(), nil
  59. }
  60. return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
  61. }
  62. func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
  63. // Pending block is only known by the miner
  64. if blockNr == rpc.PendingBlockNumber {
  65. block := b.eth.miner.PendingBlock()
  66. return block, nil
  67. }
  68. // Otherwise resolve and return the block
  69. if blockNr == rpc.LatestBlockNumber {
  70. return b.eth.blockchain.CurrentBlock(), nil
  71. }
  72. return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
  73. }
  74. func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
  75. // Pending state is only known by the miner
  76. if blockNr == rpc.PendingBlockNumber {
  77. block, state := b.eth.miner.Pending()
  78. return state, block.Header(), nil
  79. }
  80. // Otherwise resolve the block number and return its state
  81. header, err := b.HeaderByNumber(ctx, blockNr)
  82. if header == nil || err != nil {
  83. return nil, nil, err
  84. }
  85. stateDb, err := b.eth.BlockChain().StateAt(header.Root)
  86. return stateDb, header, err
  87. }
  88. func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
  89. return b.eth.blockchain.GetBlockByHash(blockHash), nil
  90. }
  91. func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
  92. return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
  93. }
  94. func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
  95. return b.eth.blockchain.GetTdByHash(blockHash)
  96. }
  97. func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
  98. state.SetBalance(msg.From(), math.MaxBig256)
  99. vmError := func() error { return nil }
  100. context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
  101. return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
  102. }
  103. func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  104. return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
  105. }
  106. func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  107. return b.eth.BlockChain().SubscribeChainEvent(ch)
  108. }
  109. func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  110. return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
  111. }
  112. func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  113. return b.eth.BlockChain().SubscribeChainSideEvent(ch)
  114. }
  115. func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  116. return b.eth.BlockChain().SubscribeLogsEvent(ch)
  117. }
  118. func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  119. return b.eth.txPool.AddLocal(signedTx)
  120. }
  121. func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
  122. pending, err := b.eth.txPool.Pending()
  123. if err != nil {
  124. return nil, err
  125. }
  126. var txs types.Transactions
  127. for _, batch := range pending {
  128. txs = append(txs, batch...)
  129. }
  130. return txs, nil
  131. }
  132. func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  133. return b.eth.txPool.Get(hash)
  134. }
  135. func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  136. return b.eth.txPool.State().GetNonce(addr), nil
  137. }
  138. func (b *EthApiBackend) Stats() (pending int, queued int) {
  139. return b.eth.txPool.Stats()
  140. }
  141. func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  142. return b.eth.TxPool().Content()
  143. }
  144. func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
  145. return b.eth.TxPool().SubscribeTxPreEvent(ch)
  146. }
  147. func (b *EthApiBackend) Downloader() *downloader.Downloader {
  148. return b.eth.Downloader()
  149. }
  150. func (b *EthApiBackend) ProtocolVersion() int {
  151. return b.eth.EthVersion()
  152. }
  153. func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  154. return b.gpo.SuggestPrice(ctx)
  155. }
  156. func (b *EthApiBackend) ChainDb() ethdb.Database {
  157. return b.eth.ChainDb()
  158. }
  159. func (b *EthApiBackend) EventMux() *event.TypeMux {
  160. return b.eth.EventMux()
  161. }
  162. func (b *EthApiBackend) AccountManager() *accounts.Manager {
  163. return b.eth.AccountManager()
  164. }
  165. func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
  166. sections, _, _ := b.eth.bloomIndexer.Sections()
  167. return params.BloomBitsBlocks, sections
  168. }
  169. func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  170. for i := 0; i < bloomFilterThreads; i++ {
  171. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  172. }
  173. }