api_backend.go 9.7 KB

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