api_backend.go 13 KB

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