api_backend.go 10 KB

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