api_backend.go 7.3 KB

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