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