api_backend.go 7.4 KB

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