api_backend.go 11 KB

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