api_backend.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. rpc "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.Pending()
  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.Pending()
  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.Environment, 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. return core.NewEnv(statedb, b.eth.chainConfig, b.eth.blockchain, msg, header, vm.Config{}), 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 {
  114. b.eth.txMu.Lock()
  115. defer b.eth.txMu.Unlock()
  116. var txs types.Transactions
  117. for _, batch := range b.eth.txPool.Pending() {
  118. txs = append(txs, batch...)
  119. }
  120. return txs
  121. }
  122. func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  123. b.eth.txMu.Lock()
  124. defer b.eth.txMu.Unlock()
  125. return b.eth.txPool.Get(hash)
  126. }
  127. func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  128. b.eth.txMu.Lock()
  129. defer b.eth.txMu.Unlock()
  130. return b.eth.txPool.State().GetNonce(addr), nil
  131. }
  132. func (b *EthApiBackend) Stats() (pending int, queued int) {
  133. b.eth.txMu.Lock()
  134. defer b.eth.txMu.Unlock()
  135. return b.eth.txPool.Stats()
  136. }
  137. func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  138. b.eth.txMu.Lock()
  139. defer b.eth.txMu.Unlock()
  140. return b.eth.TxPool().Content()
  141. }
  142. func (b *EthApiBackend) Downloader() *downloader.Downloader {
  143. return b.eth.Downloader()
  144. }
  145. func (b *EthApiBackend) ProtocolVersion() int {
  146. return b.eth.EthVersion()
  147. }
  148. func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  149. return b.gpo.SuggestPrice(), nil
  150. }
  151. func (b *EthApiBackend) ChainDb() ethdb.Database {
  152. return b.eth.ChainDb()
  153. }
  154. func (b *EthApiBackend) EventMux() *event.TypeMux {
  155. return b.eth.EventMux()
  156. }
  157. func (b *EthApiBackend) AccountManager() *accounts.Manager {
  158. return b.eth.AccountManager()
  159. }
  160. type EthApiState struct {
  161. state *state.StateDB
  162. }
  163. func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
  164. return s.state.GetBalance(addr), nil
  165. }
  166. func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
  167. return s.state.GetCode(addr), nil
  168. }
  169. func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
  170. return s.state.GetState(a, b), nil
  171. }
  172. func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
  173. return s.state.GetNonce(addr), nil
  174. }