github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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 "context" 21 "math/big" 22 23 "github.com/vntchain/go-vnt/accounts" 24 "github.com/vntchain/go-vnt/common" 25 "github.com/vntchain/go-vnt/common/math" 26 "github.com/vntchain/go-vnt/core" 27 "github.com/vntchain/go-vnt/core/bloombits" 28 "github.com/vntchain/go-vnt/core/rawdb" 29 "github.com/vntchain/go-vnt/core/state" 30 "github.com/vntchain/go-vnt/core/types" 31 "github.com/vntchain/go-vnt/core/vm" 32 "github.com/vntchain/go-vnt/event" 33 "github.com/vntchain/go-vnt/light" 34 "github.com/vntchain/go-vnt/params" 35 "github.com/vntchain/go-vnt/rpc" 36 "github.com/vntchain/go-vnt/vnt/downloader" 37 "github.com/vntchain/go-vnt/vnt/gasprice" 38 "github.com/vntchain/go-vnt/vntdb" 39 ) 40 41 type LesApiBackend struct { 42 vnt *LightVnt 43 gpo *gasprice.Oracle 44 } 45 46 func (b *LesApiBackend) ChainConfig() *params.ChainConfig { 47 return b.vnt.chainConfig 48 } 49 50 func (b *LesApiBackend) CurrentBlock() *types.Block { 51 return types.NewBlockWithHeader(b.vnt.BlockChain().CurrentHeader()) 52 } 53 54 func (b *LesApiBackend) SetHead(number uint64) { 55 b.vnt.protocolManager.downloader.Cancel() 56 b.vnt.blockchain.SetHead(number) 57 } 58 59 func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 60 if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber { 61 return b.vnt.blockchain.CurrentHeader(), nil 62 } 63 64 return b.vnt.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr)) 65 } 66 67 func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 68 header, err := b.HeaderByNumber(ctx, blockNr) 69 if header == nil || err != nil { 70 return nil, err 71 } 72 return b.GetBlock(ctx, header.Hash()) 73 } 74 75 func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 76 header, err := b.HeaderByNumber(ctx, blockNr) 77 if header == nil || err != nil { 78 return nil, nil, err 79 } 80 return light.NewState(ctx, header, b.vnt.odr), header, nil 81 } 82 83 func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) { 84 return b.vnt.blockchain.GetBlockByHash(ctx, blockHash) 85 } 86 87 func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 88 if number := rawdb.ReadHeaderNumber(b.vnt.chainDb, hash); number != nil { 89 return light.GetBlockReceipts(ctx, b.vnt.odr, hash, *number) 90 } 91 return nil, nil 92 } 93 94 func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 95 if number := rawdb.ReadHeaderNumber(b.vnt.chainDb, hash); number != nil { 96 return light.GetBlockLogs(ctx, b.vnt.odr, hash, *number) 97 } 98 return nil, nil 99 } 100 101 func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int { 102 return b.vnt.blockchain.GetTdByHash(hash) 103 } 104 105 func (b *LesApiBackend) GetVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (vm.VM, func() error, error) { 106 state.SetBalance(msg.From(), math.MaxBig256) 107 context := core.NewVMContext(msg, header, b.vnt.blockchain, nil) 108 vmEnv := core.GetVM(msg, context, state, b.vnt.chainConfig, vmCfg) 109 return vmEnv, state.Error, nil 110 111 } 112 113 func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 114 return b.vnt.txPool.Add(ctx, signedTx) 115 } 116 117 func (b *LesApiBackend) RemoveTx(txHash common.Hash) { 118 b.vnt.txPool.RemoveTx(txHash) 119 } 120 121 func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) { 122 return b.vnt.txPool.GetTransactions() 123 } 124 125 func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction { 126 return b.vnt.txPool.GetTransaction(txHash) 127 } 128 129 func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 130 return b.vnt.txPool.GetNonce(ctx, addr) 131 } 132 133 func (b *LesApiBackend) Stats() (pending int, queued int) { 134 return b.vnt.txPool.Stats(), 0 135 } 136 137 func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 138 return b.vnt.txPool.Content() 139 } 140 141 func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 142 return b.vnt.txPool.SubscribeNewTxsEvent(ch) 143 } 144 145 func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 146 return b.vnt.blockchain.SubscribeChainEvent(ch) 147 } 148 149 func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 150 return b.vnt.blockchain.SubscribeChainHeadEvent(ch) 151 } 152 153 func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 154 return b.vnt.blockchain.SubscribeChainSideEvent(ch) 155 } 156 157 func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 158 return b.vnt.blockchain.SubscribeLogsEvent(ch) 159 } 160 161 func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 162 return b.vnt.blockchain.SubscribeRemovedLogsEvent(ch) 163 } 164 165 func (b *LesApiBackend) Downloader() *downloader.Downloader { 166 return b.vnt.Downloader() 167 } 168 169 func (b *LesApiBackend) ProtocolVersion() int { 170 return b.vnt.LesVersion() + 10000 171 } 172 173 func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 174 return b.gpo.SuggestPrice(ctx) 175 } 176 177 func (b *LesApiBackend) ChainDb() vntdb.Database { 178 return b.vnt.chainDb 179 } 180 181 func (b *LesApiBackend) EventMux() *event.TypeMux { 182 return b.vnt.eventMux 183 } 184 185 func (b *LesApiBackend) AccountManager() *accounts.Manager { 186 return b.vnt.accountManager 187 } 188 189 func (b *LesApiBackend) BloomStatus() (uint64, uint64) { 190 if b.vnt.bloomIndexer == nil { 191 return 0, 0 192 } 193 sections, _, _ := b.vnt.bloomIndexer.Sections() 194 return light.BloomTrieFrequency, sections 195 } 196 197 func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 198 for i := 0; i < bloomFilterThreads; i++ { 199 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.vnt.bloomRequests) 200 } 201 }