api_backend.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "math/big"
  19. "github.com/ethereum/go-ethereum/accounts"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/eth/downloader"
  26. "github.com/ethereum/go-ethereum/eth/gasprice"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/event"
  29. "github.com/ethereum/go-ethereum/internal/ethapi"
  30. "github.com/ethereum/go-ethereum/light"
  31. "github.com/ethereum/go-ethereum/params"
  32. "github.com/ethereum/go-ethereum/rpc"
  33. "golang.org/x/net/context"
  34. )
  35. type LesApiBackend struct {
  36. eth *LightEthereum
  37. gpo *gasprice.LightPriceOracle
  38. }
  39. func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
  40. return b.eth.chainConfig
  41. }
  42. func (b *LesApiBackend) CurrentBlock() *types.Block {
  43. return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
  44. }
  45. func (b *LesApiBackend) SetHead(number uint64) {
  46. b.eth.blockchain.SetHead(number)
  47. }
  48. func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
  49. if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
  50. return b.eth.blockchain.CurrentHeader(), nil
  51. }
  52. return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
  53. }
  54. func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
  55. header, err := b.HeaderByNumber(ctx, blockNr)
  56. if header == nil || err != nil {
  57. return nil, err
  58. }
  59. return b.GetBlock(ctx, header.Hash())
  60. }
  61. func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
  62. header, err := b.HeaderByNumber(ctx, blockNr)
  63. if header == nil || err != nil {
  64. return nil, nil, err
  65. }
  66. return light.NewLightState(light.StateTrieID(header), b.eth.odr), header, nil
  67. }
  68. func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
  69. return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
  70. }
  71. func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
  72. return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
  73. }
  74. func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
  75. return b.eth.blockchain.GetTdByHash(blockHash)
  76. }
  77. func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
  78. stateDb := state.(*light.LightState).Copy()
  79. addr := msg.From()
  80. from, err := stateDb.GetOrNewStateObject(ctx, addr)
  81. if err != nil {
  82. return nil, nil, err
  83. }
  84. from.SetBalance(math.MaxBig256)
  85. vmstate := light.NewVMState(ctx, stateDb)
  86. context := core.NewEVMContext(msg, header, b.eth.blockchain)
  87. return vm.NewEVM(context, vmstate, b.eth.chainConfig, vmCfg), vmstate.Error, nil
  88. }
  89. func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
  90. return b.eth.txPool.Add(ctx, signedTx)
  91. }
  92. func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
  93. b.eth.txPool.RemoveTx(txHash)
  94. }
  95. func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
  96. return b.eth.txPool.GetTransactions()
  97. }
  98. func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
  99. return b.eth.txPool.GetTransaction(txHash)
  100. }
  101. func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
  102. return b.eth.txPool.GetNonce(ctx, addr)
  103. }
  104. func (b *LesApiBackend) Stats() (pending int, queued int) {
  105. return b.eth.txPool.Stats(), 0
  106. }
  107. func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  108. return b.eth.txPool.Content()
  109. }
  110. func (b *LesApiBackend) Downloader() *downloader.Downloader {
  111. return b.eth.Downloader()
  112. }
  113. func (b *LesApiBackend) ProtocolVersion() int {
  114. return b.eth.LesVersion() + 10000
  115. }
  116. func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
  117. return b.gpo.SuggestPrice(ctx)
  118. }
  119. func (b *LesApiBackend) ChainDb() ethdb.Database {
  120. return b.eth.chainDb
  121. }
  122. func (b *LesApiBackend) EventMux() *event.TypeMux {
  123. return b.eth.eventMux
  124. }
  125. func (b *LesApiBackend) AccountManager() *accounts.Manager {
  126. return b.eth.accountManager
  127. }