api_backend.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
  95. receipts := core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
  96. if receipts == nil {
  97. return nil, nil
  98. }
  99. logs := make([][]*types.Log, len(receipts))
  100. for i, receipt := range receipts {
  101. logs[i] = receipt.Logs
  102. }
  103. return logs, nil
  104. }
  105. func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
  106. return b.eth.blockchain.GetTdByHash(blockHash)
  107. }
  108. func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
  109. state.SetBalance(msg.From(), math.MaxBig256)
  110. vmError := func() error { return nil }
  111. context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
  112. return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
  113. }
  114. func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  115. return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
  116. }
  117. func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  118. return b.eth.BlockChain().SubscribeChainEvent(ch)
  119. }
  120. func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  121. return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
  122. }
  123. func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  124. return b.eth.BlockChain().SubscribeChainSideEvent(ch)
  125. }
  126. func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  127. return b.eth.BlockChain().SubscribeLogsEvent(ch)
  128. }
  129. func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  130. return b.eth.txPool.AddLocal(signedTx)
  131. }
  132. func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
  133. pending, err := b.eth.txPool.Pending()
  134. if err != nil {
  135. return nil, err
  136. }
  137. var txs types.Transactions
  138. for _, batch := range pending {
  139. txs = append(txs, batch...)
  140. }
  141. return txs, nil
  142. }
  143. func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  144. return b.eth.txPool.Get(hash)
  145. }
  146. func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  147. return b.eth.txPool.State().GetNonce(addr), nil
  148. }
  149. func (b *EthApiBackend) Stats() (pending int, queued int) {
  150. return b.eth.txPool.Stats()
  151. }
  152. func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  153. return b.eth.TxPool().Content()
  154. }
  155. func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
  156. return b.eth.TxPool().SubscribeTxPreEvent(ch)
  157. }
  158. func (b *EthApiBackend) Downloader() *downloader.Downloader {
  159. return b.eth.Downloader()
  160. }
  161. func (b *EthApiBackend) ProtocolVersion() int {
  162. return b.eth.EthVersion()
  163. }
  164. func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  165. return b.gpo.SuggestPrice(ctx)
  166. }
  167. func (b *EthApiBackend) ChainDb() ethdb.Database {
  168. return b.eth.ChainDb()
  169. }
  170. func (b *EthApiBackend) EventMux() *event.TypeMux {
  171. return b.eth.EventMux()
  172. }
  173. func (b *EthApiBackend) AccountManager() *accounts.Manager {
  174. return b.eth.AccountManager()
  175. }
  176. func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
  177. sections, _, _ := b.eth.bloomIndexer.Sections()
  178. return params.BloomBitsBlocks, sections
  179. }
  180. func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  181. for i := 0; i < bloomFilterThreads; i++ {
  182. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  183. }
  184. }