api_backend.go 6.0 KB

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