api_backend.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright 2016 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 les
  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/light"
  36. "github.com/ethereum/go-ethereum/params"
  37. "github.com/ethereum/go-ethereum/rpc"
  38. )
  39. type LesApiBackend struct {
  40. extRPCEnabled bool
  41. allowUnprotectedTxs bool
  42. eth *LightEthereum
  43. gpo *gasprice.Oracle
  44. }
  45. func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
  46. return b.eth.chainConfig
  47. }
  48. func (b *LesApiBackend) CurrentBlock() *types.Block {
  49. return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
  50. }
  51. func (b *LesApiBackend) SetHead(number uint64) {
  52. b.eth.handler.downloader.Cancel()
  53. b.eth.blockchain.SetHead(number)
  54. }
  55. func (b *LesApiBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
  56. // Return the latest current as the pending one since there
  57. // is no pending notion in the light client. TODO(rjl493456442)
  58. // unify the behavior of `HeaderByNumber` and `PendingBlockAndReceipts`.
  59. if number == rpc.PendingBlockNumber {
  60. return b.eth.blockchain.CurrentHeader(), nil
  61. }
  62. if number == rpc.LatestBlockNumber {
  63. return b.eth.blockchain.CurrentHeader(), nil
  64. }
  65. return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(number))
  66. }
  67. func (b *LesApiBackend) 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, err := b.HeaderByHash(ctx, hash)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if header == nil {
  77. return nil, errors.New("header for hash not found")
  78. }
  79. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  80. return nil, errors.New("hash is not currently canonical")
  81. }
  82. return header, nil
  83. }
  84. return nil, errors.New("invalid arguments; neither block nor hash specified")
  85. }
  86. func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  87. return b.eth.blockchain.GetHeaderByHash(hash), nil
  88. }
  89. func (b *LesApiBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
  90. header, err := b.HeaderByNumber(ctx, number)
  91. if header == nil || err != nil {
  92. return nil, err
  93. }
  94. return b.BlockByHash(ctx, header.Hash())
  95. }
  96. func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  97. return b.eth.blockchain.GetBlockByHash(ctx, hash)
  98. }
  99. func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
  100. if blockNr, ok := blockNrOrHash.Number(); ok {
  101. return b.BlockByNumber(ctx, blockNr)
  102. }
  103. if hash, ok := blockNrOrHash.Hash(); ok {
  104. block, err := b.BlockByHash(ctx, hash)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if block == nil {
  109. return nil, errors.New("header found, but block body is missing")
  110. }
  111. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(block.NumberU64()) != hash {
  112. return nil, errors.New("hash is not currently canonical")
  113. }
  114. return block, nil
  115. }
  116. return nil, errors.New("invalid arguments; neither block nor hash specified")
  117. }
  118. func (b *LesApiBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
  119. return nil, nil
  120. }
  121. func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
  122. header, err := b.HeaderByNumber(ctx, number)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. if header == nil {
  127. return nil, nil, errors.New("header not found")
  128. }
  129. return light.NewState(ctx, header, b.eth.odr), header, nil
  130. }
  131. func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
  132. if blockNr, ok := blockNrOrHash.Number(); ok {
  133. return b.StateAndHeaderByNumber(ctx, blockNr)
  134. }
  135. if hash, ok := blockNrOrHash.Hash(); ok {
  136. header := b.eth.blockchain.GetHeaderByHash(hash)
  137. if header == nil {
  138. return nil, nil, errors.New("header for hash not found")
  139. }
  140. if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
  141. return nil, nil, errors.New("hash is not currently canonical")
  142. }
  143. return light.NewState(ctx, header, b.eth.odr), header, nil
  144. }
  145. return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
  146. }
  147. func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
  148. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  149. return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
  150. }
  151. return nil, nil
  152. }
  153. func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
  154. return light.GetBlockLogs(ctx, b.eth.odr, hash, number)
  155. }
  156. func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
  157. if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
  158. return b.eth.blockchain.GetTdOdr(ctx, hash, *number)
  159. }
  160. return nil
  161. }
  162. func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
  163. if vmConfig == nil {
  164. vmConfig = new(vm.Config)
  165. }
  166. txContext := core.NewEVMTxContext(msg)
  167. context := core.NewEVMBlockContext(header, b.eth.blockchain, nil)
  168. return vm.NewEVM(context, txContext, state, b.eth.chainConfig, *vmConfig), state.Error, nil
  169. }
  170. func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  171. return b.eth.txPool.Add(ctx, signedTx)
  172. }
  173. func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
  174. b.eth.txPool.RemoveTx(txHash)
  175. }
  176. func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
  177. return b.eth.txPool.GetTransactions()
  178. }
  179. func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
  180. return b.eth.txPool.GetTransaction(txHash)
  181. }
  182. func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
  183. return light.GetTransaction(ctx, b.eth.odr, txHash)
  184. }
  185. func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  186. return b.eth.txPool.GetNonce(ctx, addr)
  187. }
  188. func (b *LesApiBackend) Stats() (pending int, queued int) {
  189. return b.eth.txPool.Stats(), 0
  190. }
  191. func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  192. return b.eth.txPool.Content()
  193. }
  194. func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
  195. return b.eth.txPool.ContentFrom(addr)
  196. }
  197. func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
  198. return b.eth.txPool.SubscribeNewTxsEvent(ch)
  199. }
  200. func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
  201. return b.eth.blockchain.SubscribeChainEvent(ch)
  202. }
  203. func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
  204. return b.eth.blockchain.SubscribeChainHeadEvent(ch)
  205. }
  206. func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
  207. return b.eth.blockchain.SubscribeChainSideEvent(ch)
  208. }
  209. func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  210. return b.eth.blockchain.SubscribeLogsEvent(ch)
  211. }
  212. func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
  213. return event.NewSubscription(func(quit <-chan struct{}) error {
  214. <-quit
  215. return nil
  216. })
  217. }
  218. func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
  219. return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
  220. }
  221. func (b *LesApiBackend) SyncProgress() ethereum.SyncProgress {
  222. return b.eth.Downloader().Progress()
  223. }
  224. func (b *LesApiBackend) ProtocolVersion() int {
  225. return b.eth.LesVersion() + 10000
  226. }
  227. func (b *LesApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
  228. return b.gpo.SuggestTipCap(ctx)
  229. }
  230. func (b *LesApiBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
  231. return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
  232. }
  233. func (b *LesApiBackend) ChainDb() ethdb.Database {
  234. return b.eth.chainDb
  235. }
  236. func (b *LesApiBackend) AccountManager() *accounts.Manager {
  237. return b.eth.accountManager
  238. }
  239. func (b *LesApiBackend) ExtRPCEnabled() bool {
  240. return b.extRPCEnabled
  241. }
  242. func (b *LesApiBackend) UnprotectedAllowed() bool {
  243. return b.allowUnprotectedTxs
  244. }
  245. func (b *LesApiBackend) RPCGasCap() uint64 {
  246. return b.eth.config.RPCGasCap
  247. }
  248. func (b *LesApiBackend) RPCEVMTimeout() time.Duration {
  249. return b.eth.config.RPCEVMTimeout
  250. }
  251. func (b *LesApiBackend) RPCTxFeeCap() float64 {
  252. return b.eth.config.RPCTxFeeCap
  253. }
  254. func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
  255. if b.eth.bloomIndexer == nil {
  256. return 0, 0
  257. }
  258. sections, _, _ := b.eth.bloomIndexer.Sections()
  259. return params.BloomBitsBlocksClient, sections
  260. }
  261. func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
  262. for i := 0; i < bloomFilterThreads; i++ {
  263. go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
  264. }
  265. }
  266. func (b *LesApiBackend) Engine() consensus.Engine {
  267. return b.eth.engine
  268. }
  269. func (b *LesApiBackend) CurrentHeader() *types.Header {
  270. return b.eth.blockchain.CurrentHeader()
  271. }
  272. func (b *LesApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) {
  273. return b.eth.stateAtBlock(ctx, block, reexec)
  274. }
  275. func (b *LesApiBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {
  276. return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
  277. }