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