api_backend.go 11 KB

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