github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/api_backend.go (about) 1 // Copyright 2015 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 eth 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 "time" 24 25 "github.com/ethereum/go-ethereum" 26 "github.com/ethereum/go-ethereum/accounts" 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/consensus" 29 "github.com/ethereum/go-ethereum/consensus/misc/eip4844" 30 "github.com/ethereum/go-ethereum/core" 31 "github.com/ethereum/go-ethereum/core/bloombits" 32 "github.com/ethereum/go-ethereum/core/rawdb" 33 "github.com/ethereum/go-ethereum/core/state" 34 "github.com/ethereum/go-ethereum/core/txpool" 35 "github.com/ethereum/go-ethereum/core/types" 36 "github.com/ethereum/go-ethereum/core/vm" 37 "github.com/ethereum/go-ethereum/eth/gasprice" 38 "github.com/ethereum/go-ethereum/eth/tracers" 39 "github.com/ethereum/go-ethereum/ethdb" 40 "github.com/ethereum/go-ethereum/event" 41 "github.com/ethereum/go-ethereum/params" 42 "github.com/ethereum/go-ethereum/rpc" 43 ) 44 45 // EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes 46 type EthAPIBackend struct { 47 extRPCEnabled bool 48 allowUnprotectedTxs bool 49 eth *Ethereum 50 gpo *gasprice.Oracle 51 } 52 53 // ChainConfig returns the active chain configuration. 54 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 55 return b.eth.blockchain.Config() 56 } 57 58 func (b *EthAPIBackend) CurrentBlock() *types.Header { 59 return b.eth.blockchain.CurrentBlock() 60 } 61 62 func (b *EthAPIBackend) SetHead(number uint64) { 63 b.eth.handler.downloader.Cancel() 64 b.eth.blockchain.SetHead(number) 65 } 66 67 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 68 // Pending block is only known by the miner 69 if number == rpc.PendingBlockNumber { 70 block, _, _ := b.eth.miner.Pending() 71 if block == nil { 72 return nil, errors.New("pending block is not available") 73 } 74 return block.Header(), nil 75 } 76 // Otherwise resolve and return the block 77 if number == rpc.LatestBlockNumber { 78 return b.eth.blockchain.CurrentBlock(), nil 79 } 80 if number == rpc.FinalizedBlockNumber { 81 block := b.eth.blockchain.CurrentFinalBlock() 82 if block == nil { 83 return nil, errors.New("finalized block not found") 84 } 85 return block, nil 86 } 87 if number == rpc.SafeBlockNumber { 88 block := b.eth.blockchain.CurrentSafeBlock() 89 if block == nil { 90 return nil, errors.New("safe block not found") 91 } 92 return block, nil 93 } 94 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 95 } 96 97 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 98 if blockNr, ok := blockNrOrHash.Number(); ok { 99 return b.HeaderByNumber(ctx, blockNr) 100 } 101 if hash, ok := blockNrOrHash.Hash(); ok { 102 header := b.eth.blockchain.GetHeaderByHash(hash) 103 if header == nil { 104 return nil, errors.New("header for hash not found") 105 } 106 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 107 return nil, errors.New("hash is not currently canonical") 108 } 109 return header, nil 110 } 111 return nil, errors.New("invalid arguments; neither block nor hash specified") 112 } 113 114 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 115 return b.eth.blockchain.GetHeaderByHash(hash), nil 116 } 117 118 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 119 // Pending block is only known by the miner 120 if number == rpc.PendingBlockNumber { 121 block, _, _ := b.eth.miner.Pending() 122 if block == nil { 123 return nil, errors.New("pending block is not available") 124 } 125 return block, nil 126 } 127 // Otherwise resolve and return the block 128 if number == rpc.LatestBlockNumber { 129 header := b.eth.blockchain.CurrentBlock() 130 return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil 131 } 132 if number == rpc.FinalizedBlockNumber { 133 header := b.eth.blockchain.CurrentFinalBlock() 134 if header == nil { 135 return nil, errors.New("finalized block not found") 136 } 137 return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil 138 } 139 if number == rpc.SafeBlockNumber { 140 header := b.eth.blockchain.CurrentSafeBlock() 141 if header == nil { 142 return nil, errors.New("safe block not found") 143 } 144 return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil 145 } 146 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 147 } 148 149 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 150 return b.eth.blockchain.GetBlockByHash(hash), nil 151 } 152 153 // GetBody returns body of a block. It does not resolve special block numbers. 154 func (b *EthAPIBackend) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { 155 if number < 0 || hash == (common.Hash{}) { 156 return nil, errors.New("invalid arguments; expect hash and no special block numbers") 157 } 158 if body := b.eth.blockchain.GetBody(hash); body != nil { 159 return body, nil 160 } 161 return nil, errors.New("block body not found") 162 } 163 164 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 165 if blockNr, ok := blockNrOrHash.Number(); ok { 166 return b.BlockByNumber(ctx, blockNr) 167 } 168 if hash, ok := blockNrOrHash.Hash(); ok { 169 header := b.eth.blockchain.GetHeaderByHash(hash) 170 if header == nil { 171 return nil, errors.New("header for hash not found") 172 } 173 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 174 return nil, errors.New("hash is not currently canonical") 175 } 176 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 177 if block == nil { 178 return nil, errors.New("header found, but block body is missing") 179 } 180 return block, nil 181 } 182 return nil, errors.New("invalid arguments; neither block nor hash specified") 183 } 184 185 func (b *EthAPIBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) { 186 return b.eth.miner.Pending() 187 } 188 189 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 190 // Pending state is only known by the miner 191 if number == rpc.PendingBlockNumber { 192 block, _, state := b.eth.miner.Pending() 193 if block == nil || state == nil { 194 return nil, nil, errors.New("pending state is not available") 195 } 196 return state, block.Header(), nil 197 } 198 // Otherwise resolve the block number and return its state 199 header, err := b.HeaderByNumber(ctx, number) 200 if err != nil { 201 return nil, nil, err 202 } 203 if header == nil { 204 return nil, nil, errors.New("header not found") 205 } 206 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 207 if err != nil { 208 return nil, nil, err 209 } 210 return stateDb, header, nil 211 } 212 213 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 214 if blockNr, ok := blockNrOrHash.Number(); ok { 215 return b.StateAndHeaderByNumber(ctx, blockNr) 216 } 217 if hash, ok := blockNrOrHash.Hash(); ok { 218 header, err := b.HeaderByHash(ctx, hash) 219 if err != nil { 220 return nil, nil, err 221 } 222 if header == nil { 223 return nil, nil, errors.New("header for hash not found") 224 } 225 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 226 return nil, nil, errors.New("hash is not currently canonical") 227 } 228 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 229 if err != nil { 230 return nil, nil, err 231 } 232 return stateDb, header, nil 233 } 234 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 235 } 236 237 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 238 return b.eth.blockchain.GetReceiptsByHash(hash), nil 239 } 240 241 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { 242 return rawdb.ReadLogs(b.eth.chainDb, hash, number), nil 243 } 244 245 func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 246 if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil { 247 return b.eth.blockchain.GetTd(hash, header.Number.Uint64()) 248 } 249 return nil 250 } 251 252 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM { 253 if vmConfig == nil { 254 vmConfig = b.eth.blockchain.GetVMConfig() 255 } 256 txContext := core.NewEVMTxContext(msg) 257 var context vm.BlockContext 258 if blockCtx != nil { 259 context = *blockCtx 260 } else { 261 context = core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) 262 } 263 return vm.NewEVM(context, txContext, state, b.ChainConfig(), *vmConfig) 264 } 265 266 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 267 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 268 } 269 270 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 271 return b.eth.BlockChain().SubscribeChainEvent(ch) 272 } 273 274 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 275 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 276 } 277 278 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 279 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 280 } 281 282 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 283 return b.eth.BlockChain().SubscribeLogsEvent(ch) 284 } 285 286 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 287 return b.eth.txPool.Add([]*types.Transaction{signedTx}, true, false)[0] 288 } 289 290 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 291 pending := b.eth.txPool.Pending(txpool.PendingFilter{}) 292 var txs types.Transactions 293 for _, batch := range pending { 294 for _, lazy := range batch { 295 if tx := lazy.Resolve(); tx != nil { 296 txs = append(txs, tx) 297 } 298 } 299 } 300 return txs, nil 301 } 302 303 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 304 return b.eth.txPool.Get(hash) 305 } 306 307 // GetTransaction retrieves the lookup along with the transaction itself associate 308 // with the given transaction hash. 309 // 310 // An error will be returned if the transaction is not found, and background 311 // indexing for transactions is still in progress. The error is used to indicate the 312 // scenario explicitly that the transaction might be reachable shortly. 313 // 314 // A null will be returned in the transaction is not found and background transaction 315 // indexing is already finished. The transaction is not existent from the perspective 316 // of node. 317 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (bool, *types.Transaction, common.Hash, uint64, uint64, error) { 318 lookup, tx, err := b.eth.blockchain.GetTransactionLookup(txHash) 319 if err != nil { 320 return false, nil, common.Hash{}, 0, 0, err 321 } 322 if lookup == nil || tx == nil { 323 return false, nil, common.Hash{}, 0, 0, nil 324 } 325 return true, tx, lookup.BlockHash, lookup.BlockIndex, lookup.Index, nil 326 } 327 328 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 329 return b.eth.txPool.Nonce(addr), nil 330 } 331 332 func (b *EthAPIBackend) Stats() (runnable int, blocked int) { 333 return b.eth.txPool.Stats() 334 } 335 336 func (b *EthAPIBackend) TxPoolContent() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) { 337 return b.eth.txPool.Content() 338 } 339 340 func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) { 341 return b.eth.txPool.ContentFrom(addr) 342 } 343 344 func (b *EthAPIBackend) TxPool() *txpool.TxPool { 345 return b.eth.txPool 346 } 347 348 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 349 return b.eth.txPool.SubscribeTransactions(ch, true) 350 } 351 352 func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress { 353 prog := b.eth.Downloader().Progress() 354 if txProg, err := b.eth.blockchain.TxIndexProgress(); err == nil { 355 prog.TxIndexFinishedBlocks = txProg.Indexed 356 prog.TxIndexRemainingBlocks = txProg.Remaining 357 } 358 return prog 359 } 360 361 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 362 return b.gpo.SuggestTipCap(ctx) 363 } 364 365 func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, baseFeePerBlobGas []*big.Int, blobGasUsedRatio []float64, err error) { 366 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 367 } 368 369 func (b *EthAPIBackend) BlobBaseFee(ctx context.Context) *big.Int { 370 if excess := b.CurrentHeader().ExcessBlobGas; excess != nil { 371 return eip4844.CalcBlobFee(*excess) 372 } 373 return nil 374 } 375 376 func (b *EthAPIBackend) ChainDb() ethdb.Database { 377 return b.eth.ChainDb() 378 } 379 380 func (b *EthAPIBackend) EventMux() *event.TypeMux { 381 return b.eth.EventMux() 382 } 383 384 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 385 return b.eth.AccountManager() 386 } 387 388 func (b *EthAPIBackend) ExtRPCEnabled() bool { 389 return b.extRPCEnabled 390 } 391 392 func (b *EthAPIBackend) UnprotectedAllowed() bool { 393 return b.allowUnprotectedTxs 394 } 395 396 func (b *EthAPIBackend) RPCGasCap() uint64 { 397 return b.eth.config.RPCGasCap 398 } 399 400 func (b *EthAPIBackend) RPCEVMTimeout() time.Duration { 401 return b.eth.config.RPCEVMTimeout 402 } 403 404 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 405 return b.eth.config.RPCTxFeeCap 406 } 407 408 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 409 sections, _, _ := b.eth.bloomIndexer.Sections() 410 return params.BloomBitsBlocks, sections 411 } 412 413 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 414 for i := 0; i < bloomFilterThreads; i++ { 415 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 416 } 417 } 418 419 func (b *EthAPIBackend) Engine() consensus.Engine { 420 return b.eth.engine 421 } 422 423 func (b *EthAPIBackend) CurrentHeader() *types.Header { 424 return b.eth.blockchain.CurrentHeader() 425 } 426 427 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) { 428 return b.eth.stateAtBlock(ctx, block, reexec, base, readOnly, preferDisk) 429 } 430 431 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) { 432 return b.eth.stateAtTransaction(ctx, block, txIndex, reexec) 433 }