api_backend.go 6.6 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. "math/big"
  19. "github.com/ethereum/go-ethereum/accounts"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/eth/downloader"
  26. "github.com/ethereum/go-ethereum/eth/gasprice"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/event"
  29. "github.com/ethereum/go-ethereum/internal/ethapi"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/rpc"
  32. "golang.org/x/net/context"
  33. )
  34. // EthApiBackend implements ethapi.Backend for full nodes
  35. type EthApiBackend struct {
  36. eth *Ethereum
  37. gpo *gasprice.GasPriceOracle
  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.blockchain.SetHead(number)
  47. }
  48. func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
  49. // Pending block is only known by the miner
  50. if blockNr == rpc.PendingBlockNumber {
  51. block := b.eth.miner.PendingBlock()
  52. return block.Header(), nil
  53. }
  54. // Otherwise resolve and return the block
  55. if blockNr == rpc.LatestBlockNumber {
  56. return b.eth.blockchain.CurrentBlock().Header(), nil
  57. }
  58. return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
  59. }
  60. func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
  61. // Pending block is only known by the miner
  62. if blockNr == rpc.PendingBlockNumber {
  63. block := b.eth.miner.PendingBlock()
  64. return block, nil
  65. }
  66. // Otherwise resolve and return the block
  67. if blockNr == rpc.LatestBlockNumber {
  68. return b.eth.blockchain.CurrentBlock(), nil
  69. }
  70. return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
  71. }
  72. func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
  73. // Pending state is only known by the miner
  74. if blockNr == rpc.PendingBlockNumber {
  75. block, state := b.eth.miner.Pending()
  76. return EthApiState{state}, block.Header(), nil
  77. }
  78. // Otherwise resolve the block number and return its state
  79. header, err := b.HeaderByNumber(ctx, blockNr)
  80. if header == nil || err != nil {
  81. return nil, nil, err
  82. }
  83. stateDb, err := b.eth.BlockChain().StateAt(header.Root)
  84. return EthApiState{stateDb}, header, err
  85. }
  86. func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
  87. return b.eth.blockchain.GetBlockByHash(blockHash), nil
  88. }
  89. func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
  90. return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
  91. }
  92. func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
  93. return b.eth.blockchain.GetTdByHash(blockHash)
  94. }
  95. func (b *EthApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (*vm.EVM, func() error, error) {
  96. statedb := state.(EthApiState).state
  97. from := statedb.GetOrNewStateObject(msg.From())
  98. from.SetBalance(common.MaxBig)
  99. vmError := func() error { return nil }
  100. context := core.NewEVMContext(msg, header, b.eth.BlockChain())
  101. return vm.NewEVM(context, statedb, b.eth.chainConfig, vm.Config{}), vmError, nil
  102. }
  103. func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  104. b.eth.txMu.Lock()
  105. defer b.eth.txMu.Unlock()
  106. b.eth.txPool.SetLocal(signedTx)
  107. return b.eth.txPool.Add(signedTx)
  108. }
  109. func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
  110. b.eth.txMu.Lock()
  111. defer b.eth.txMu.Unlock()
  112. b.eth.txPool.Remove(txHash)
  113. }
  114. func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
  115. b.eth.txMu.Lock()
  116. defer b.eth.txMu.Unlock()
  117. pending, err := b.eth.txPool.Pending()
  118. if err != nil {
  119. return nil, err
  120. }
  121. var txs types.Transactions
  122. for _, batch := range pending {
  123. txs = append(txs, batch...)
  124. }
  125. return txs, nil
  126. }
  127. func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  128. b.eth.txMu.Lock()
  129. defer b.eth.txMu.Unlock()
  130. return b.eth.txPool.Get(hash)
  131. }
  132. func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  133. b.eth.txMu.Lock()
  134. defer b.eth.txMu.Unlock()
  135. return b.eth.txPool.State().GetNonce(addr), nil
  136. }
  137. func (b *EthApiBackend) Stats() (pending int, queued int) {
  138. b.eth.txMu.Lock()
  139. defer b.eth.txMu.Unlock()
  140. return b.eth.txPool.Stats()
  141. }
  142. func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  143. b.eth.txMu.Lock()
  144. defer b.eth.txMu.Unlock()
  145. return b.eth.TxPool().Content()
  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(), nil
  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. type EthApiState struct {
  166. state *state.StateDB
  167. }
  168. func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
  169. return s.state.GetBalance(addr), nil
  170. }
  171. func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
  172. return s.state.GetCode(addr), nil
  173. }
  174. func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
  175. return s.state.GetState(a, b), nil
  176. }
  177. func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
  178. return s.state.GetNonce(addr), nil
  179. }