api_backend.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/accounts"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/consensus"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/bloombits"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/eth/downloader"
  31. "github.com/ethereum/go-ethereum/eth/gasprice"
  32. "github.com/ethereum/go-ethereum/ethdb"
  33. "github.com/ethereum/go-ethereum/event"
  34. "github.com/ethereum/go-ethereum/miner"
  35. "github.com/ethereum/go-ethereum/params"
  36. "github.com/ethereum/go-ethereum/rpc"
  37. )
  38. // EthAPIBackend implements ethapi.Backend for full nodes
  39. type EthAPIBackend struct {
  40. extRPCEnabled bool
  41. eth *Ethereum
  42. gpo *gasprice.Oracle
  43. }
  44. // ChainConfig returns the active chain configuration.
  45. func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
  46. return b.eth.blockchain.Config()
  47. }
  48. func (b *EthAPIBackend) CurrentBlock() *types.Block {
  49. return b.eth.blockchain.CurrentBlock()
  50. }
  51. func (b *EthAPIBackend) SetHead(number uint64) {
  52. b.eth.handler.downloader.Cancel()
  53. b.eth.blockchain.SetHead(number)
  54. }
  55. func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
  56. // Pending block is only known by the miner
  57. if number == rpc.PendingBlockNumber {
  58. block := b.eth.miner.PendingBlock()
  59. return block.Header(), nil
  60. }
  61. // Otherwise resolve and return the block
  62. if number == rpc.LatestBlockNumber {
  63. return b.eth.blockchain.CurrentBlock().Header(), nil
  64. }
  65. return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
  66. }
  67. func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
  68. if blockNr, ok := blockNrOrHash.Number(); ok {
  69. return b.HeaderByNumber(ctx, blockNr)
  70. }
  71. if hash, ok := blockNrOrHash.Hash(); ok {
  72. header := b.eth.blockchain.GetHeaderByHash(hash)
  73. if header == nil {
  74. return nil, errors.New("header for hash not found")
  75. }
  76. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  77. return nil, errors.New("hash is not currently canonical")
  78. }
  79. return header, nil
  80. }
  81. return nil, errors.New("invalid arguments; neither block nor hash specified")
  82. }
  83. func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  84. return b.eth.blockchain.GetHeaderByHash(hash), nil
  85. }
  86. func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
  87. // Pending block is only known by the miner
  88. if number == rpc.PendingBlockNumber {
  89. block := b.eth.miner.PendingBlock()
  90. return block, nil
  91. }
  92. // Otherwise resolve and return the block
  93. if number == rpc.LatestBlockNumber {
  94. return b.eth.blockchain.CurrentBlock(), nil
  95. }
  96. return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
  97. }
  98. func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  99. return b.eth.blockchain.GetBlockByHash(hash), nil
  100. }
  101. func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
  102. if blockNr, ok := blockNrOrHash.Number(); ok {
  103. return b.BlockByNumber(ctx, blockNr)
  104. }
  105. if hash, ok := blockNrOrHash.Hash(); ok {
  106. header := b.eth.blockchain.GetHeaderByHash(hash)
  107. if header == nil {
  108. return nil, errors.New("header for hash not found")
  109. }
  110. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  111. return nil, errors.New("hash is not currently canonical")
  112. }
  113. block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
  114. if block == nil {
  115. return nil, errors.New("header found, but block body is missing")
  116. }
  117. return block, nil
  118. }
  119. return nil, errors.New("invalid arguments; neither block nor hash specified")
  120. }
  121. func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
  122. // Pending state is only known by the miner
  123. if number == rpc.PendingBlockNumber {
  124. block, state := b.eth.miner.Pending()
  125. return state, block.Header(), nil
  126. }
  127. // Otherwise resolve the block number and return its state
  128. header, err := b.HeaderByNumber(ctx, number)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. if header == nil {
  133. return nil, nil, errors.New("header not found")
  134. }
  135. stateDb, err := b.eth.BlockChain().StateAt(header.Root)
  136. return stateDb, header, err
  137. }
  138. func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
  139. if blockNr, ok := blockNrOrHash.Number(); ok {
  140. return b.StateAndHeaderByNumber(ctx, blockNr)
  141. }
  142. if hash, ok := blockNrOrHash.Hash(); ok {
  143. header, err := b.HeaderByHash(ctx, hash)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. if header == nil {
  148. return nil, nil, errors.New("header for hash not found")
  149. }
  150. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  151. return nil, nil, errors.New("hash is not currently canonical")
  152. }
  153. stateDb, err := b.eth.BlockChain().StateAt(header.Root)
  154. return stateDb, header, err
  155. }
  156. return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
  157. }
  158. func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
  159. return b.eth.blockchain.GetReceiptsByHash(hash), nil
  160. }
  161. func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
  162. receipts := b.eth.blockchain.GetReceiptsByHash(hash)
  163. if receipts == nil {
  164. return nil, nil
  165. }
  166. logs := make([][]*types.Log, len(receipts))
  167. for i, receipt := range receipts {
  168. logs[i] = receipt.Logs
  169. }
  170. return logs, nil
  171. }
  172. func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
  173. return b.eth.blockchain.GetTdByHash(hash)
  174. }
  175. func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
  176. vmError := func() error { return nil }
  177. txContext := core.NewEVMTxContext(msg)
  178. context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
  179. return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
  180. }
  181. func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  182. return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
  183. }
  184. func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
  185. return b.eth.miner.SubscribePendingLogs(ch)
  186. }
  187. func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  188. return b.eth.BlockChain().SubscribeChainEvent(ch)
  189. }
  190. func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  191. return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
  192. }
  193. func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  194. return b.eth.BlockChain().SubscribeChainSideEvent(ch)
  195. }
  196. func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  197. return b.eth.BlockChain().SubscribeLogsEvent(ch)
  198. }
  199. func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  200. return b.eth.txPool.AddLocal(signedTx)
  201. }
  202. func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
  203. pending, err := b.eth.txPool.Pending()
  204. if err != nil {
  205. return nil, err
  206. }
  207. var txs types.Transactions
  208. for _, batch := range pending {
  209. txs = append(txs, batch...)
  210. }
  211. return txs, nil
  212. }
  213. func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
  214. return b.eth.txPool.Get(hash)
  215. }
  216. func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
  217. tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
  218. return tx, blockHash, blockNumber, index, nil
  219. }
  220. func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  221. return b.eth.txPool.Nonce(addr), nil
  222. }
  223. func (b *EthAPIBackend) Stats() (pending int, queued int) {
  224. return b.eth.txPool.Stats()
  225. }
  226. func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  227. return b.eth.TxPool().Content()
  228. }
  229. func (b *EthAPIBackend) TxPool() *core.TxPool {
  230. return b.eth.TxPool()
  231. }
  232. func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
  233. return b.eth.TxPool().SubscribeNewTxsEvent(ch)
  234. }
  235. func (b *EthAPIBackend) Downloader() *downloader.Downloader {
  236. return b.eth.Downloader()
  237. }
  238. func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  239. return b.gpo.SuggestPrice(ctx)
  240. }
  241. func (b *EthAPIBackend) ChainDb() ethdb.Database {
  242. return b.eth.ChainDb()
  243. }
  244. func (b *EthAPIBackend) EventMux() *event.TypeMux {
  245. return b.eth.EventMux()
  246. }
  247. func (b *EthAPIBackend) AccountManager() *accounts.Manager {
  248. return b.eth.AccountManager()
  249. }
  250. func (b *EthAPIBackend) ExtRPCEnabled() bool {
  251. return b.extRPCEnabled
  252. }
  253. func (b *EthAPIBackend) RPCGasCap() uint64 {
  254. return b.eth.config.RPCGasCap
  255. }
  256. func (b *EthAPIBackend) RPCTxFeeCap() float64 {
  257. return b.eth.config.RPCTxFeeCap
  258. }
  259. func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
  260. sections, _, _ := b.eth.bloomIndexer.Sections()
  261. return params.BloomBitsBlocks, sections
  262. }
  263. func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  264. for i := 0; i < bloomFilterThreads; i++ {
  265. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  266. }
  267. }
  268. func (b *EthAPIBackend) Engine() consensus.Engine {
  269. return b.eth.engine
  270. }
  271. func (b *EthAPIBackend) CurrentHeader() *types.Header {
  272. return b.eth.blockchain.CurrentHeader()
  273. }
  274. func (b *EthAPIBackend) Miner() *miner.Miner {
  275. return b.eth.Miner()
  276. }
  277. func (b *EthAPIBackend) StartMining(threads int) error {
  278. return b.eth.StartMining(threads)
  279. }
  280. func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64) (*state.StateDB, func(), error) {
  281. return b.eth.stateAtBlock(block, reexec)
  282. }
  283. func (b *EthAPIBackend) StatesInRange(ctx context.Context, fromBlock *types.Block, toBlock *types.Block, reexec uint64) ([]*state.StateDB, func(), error) {
  284. return b.eth.statesInRange(fromBlock, toBlock, reexec)
  285. }
  286. func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, func(), error) {
  287. return b.eth.stateAtTransaction(block, txIndex, reexec)
  288. }