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