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