api_backend.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. 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. rpc "github.com/ethereum/go-ethereum/rpc"
  31. "golang.org/x/net/context"
  32. )
  33. // EthApiBackend implements ethapi.Backend for full nodes
  34. type EthApiBackend struct {
  35. eth *FullNodeService
  36. gpo *gasprice.GasPriceOracle
  37. }
  38. func (b *EthApiBackend) SetHead(number uint64) {
  39. b.eth.blockchain.SetHead(number)
  40. }
  41. func (b *EthApiBackend) HeaderByNumber(blockNr rpc.BlockNumber) *types.Header {
  42. // Pending block is only known by the miner
  43. if blockNr == rpc.PendingBlockNumber {
  44. block, _ := b.eth.miner.Pending()
  45. return block.Header()
  46. }
  47. // Otherwise resolve and return the block
  48. if blockNr == rpc.LatestBlockNumber {
  49. return b.eth.blockchain.CurrentBlock().Header()
  50. }
  51. return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr))
  52. }
  53. func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
  54. // Pending block is only known by the miner
  55. if blockNr == rpc.PendingBlockNumber {
  56. block, _ := b.eth.miner.Pending()
  57. return block, nil
  58. }
  59. // Otherwise resolve and return the block
  60. if blockNr == rpc.LatestBlockNumber {
  61. return b.eth.blockchain.CurrentBlock(), nil
  62. }
  63. return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
  64. }
  65. func (b *EthApiBackend) StateAndHeaderByNumber(blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
  66. // Pending state is only known by the miner
  67. if blockNr == rpc.PendingBlockNumber {
  68. block, state := b.eth.miner.Pending()
  69. return EthApiState{state}, block.Header(), nil
  70. }
  71. // Otherwise resolve the block number and return its state
  72. header := b.HeaderByNumber(blockNr)
  73. if header == nil {
  74. return nil, nil, nil
  75. }
  76. stateDb, err := state.New(header.Root, b.eth.chainDb)
  77. return EthApiState{stateDb}, header, err
  78. }
  79. func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
  80. return b.eth.blockchain.GetBlockByHash(blockHash), nil
  81. }
  82. func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
  83. return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
  84. }
  85. func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
  86. return b.eth.blockchain.GetTdByHash(blockHash)
  87. }
  88. func (b *EthApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (vm.Environment, func() error, error) {
  89. stateDb := state.(EthApiState).state.Copy()
  90. addr, _ := msg.From()
  91. from := stateDb.GetOrNewStateObject(addr)
  92. from.SetBalance(common.MaxBig)
  93. vmError := func() error { return nil }
  94. return core.NewEnv(stateDb, b.eth.chainConfig, b.eth.blockchain, msg, header, b.eth.chainConfig.VmConfig), vmError, nil
  95. }
  96. func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  97. b.eth.txMu.Lock()
  98. defer b.eth.txMu.Unlock()
  99. b.eth.txPool.SetLocal(signedTx)
  100. return b.eth.txPool.Add(signedTx)
  101. }
  102. func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
  103. b.eth.txMu.Lock()
  104. defer b.eth.txMu.Unlock()
  105. b.eth.txPool.RemoveTx(txHash)
  106. }
  107. func (b *EthApiBackend) GetPoolTransactions() types.Transactions {
  108. b.eth.txMu.Lock()
  109. defer b.eth.txMu.Unlock()
  110. return b.eth.txPool.GetTransactions()
  111. }
  112. func (b *EthApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
  113. b.eth.txMu.Lock()
  114. defer b.eth.txMu.Unlock()
  115. return b.eth.txPool.GetTransaction(txHash)
  116. }
  117. func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  118. b.eth.txMu.Lock()
  119. defer b.eth.txMu.Unlock()
  120. return b.eth.txPool.State().GetNonce(addr), nil
  121. }
  122. func (b *EthApiBackend) Stats() (pending int, queued int) {
  123. b.eth.txMu.Lock()
  124. defer b.eth.txMu.Unlock()
  125. return b.eth.txPool.Stats()
  126. }
  127. func (b *EthApiBackend) TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) {
  128. b.eth.txMu.Lock()
  129. defer b.eth.txMu.Unlock()
  130. return b.eth.TxPool().Content()
  131. }
  132. func (b *EthApiBackend) Downloader() *downloader.Downloader {
  133. return b.eth.Downloader()
  134. }
  135. func (b *EthApiBackend) ProtocolVersion() int {
  136. return b.eth.EthVersion()
  137. }
  138. func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  139. return b.gpo.SuggestPrice(), nil
  140. }
  141. func (b *EthApiBackend) ChainDb() ethdb.Database {
  142. return b.eth.ChainDb()
  143. }
  144. func (b *EthApiBackend) EventMux() *event.TypeMux {
  145. return b.eth.EventMux()
  146. }
  147. func (b *EthApiBackend) AccountManager() *accounts.Manager {
  148. return b.eth.AccountManager()
  149. }
  150. type EthApiState struct {
  151. state *state.StateDB
  152. }
  153. func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
  154. return s.state.GetBalance(addr), nil
  155. }
  156. func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
  157. return s.state.GetCode(addr), nil
  158. }
  159. func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
  160. return s.state.GetState(a, b), nil
  161. }
  162. func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
  163. return s.state.GetNonce(addr), nil
  164. }