github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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/core" 30 "github.com/ethereum/go-ethereum/core/bloombits" 31 "github.com/ethereum/go-ethereum/core/rawdb" 32 "github.com/ethereum/go-ethereum/core/state" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/core/vm" 35 "github.com/ethereum/go-ethereum/eth/gasprice" 36 "github.com/ethereum/go-ethereum/eth/tracers" 37 "github.com/ethereum/go-ethereum/ethdb" 38 "github.com/ethereum/go-ethereum/event" 39 "github.com/ethereum/go-ethereum/miner" 40 "github.com/ethereum/go-ethereum/params" 41 "github.com/ethereum/go-ethereum/rpc" 42 ) 43 44 // EthAPIBackend implements ethapi.Backend for full nodes 45 type EthAPIBackend struct { 46 extRPCEnabled bool 47 allowUnprotectedTxs bool 48 eth *Ethereum 49 gpo *gasprice.Oracle 50 } 51 52 // ChainConfig returns the active chain configuration. 53 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 54 return b.eth.blockchain.Config() 55 } 56 57 func (b *EthAPIBackend) CurrentBlock() *types.Block { 58 return b.eth.blockchain.CurrentBlock() 59 } 60 61 func (b *EthAPIBackend) SetHead(number uint64) { 62 b.eth.handler.downloader.Cancel() 63 b.eth.blockchain.SetHead(number) 64 } 65 66 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 67 // Pending block is only known by the miner 68 if number == rpc.PendingBlockNumber { 69 block := b.eth.miner.PendingBlock() 70 return block.Header(), nil 71 } 72 // Otherwise resolve and return the block 73 if number == rpc.LatestBlockNumber { 74 return b.eth.blockchain.CurrentBlock().Header(), nil 75 } 76 if number == rpc.FinalizedBlockNumber { 77 block := b.eth.blockchain.CurrentFinalizedBlock() 78 if block != nil { 79 return block.Header(), nil 80 } 81 return nil, errors.New("finalized block not found") 82 } 83 if number == rpc.SafeBlockNumber { 84 block := b.eth.blockchain.CurrentSafeBlock() 85 if block != nil { 86 return block.Header(), nil 87 } 88 return nil, errors.New("safe block not found") 89 } 90 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 91 } 92 93 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 94 if blockNr, ok := blockNrOrHash.Number(); ok { 95 return b.HeaderByNumber(ctx, blockNr) 96 } 97 if hash, ok := blockNrOrHash.Hash(); ok { 98 header := b.eth.blockchain.GetHeaderByHash(hash) 99 if header == nil { 100 return nil, errors.New("header for hash not found") 101 } 102 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 103 return nil, errors.New("hash is not currently canonical") 104 } 105 return header, nil 106 } 107 return nil, errors.New("invalid arguments; neither block nor hash specified") 108 } 109 110 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 111 return b.eth.blockchain.GetHeaderByHash(hash), nil 112 } 113 114 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 115 // Pending block is only known by the miner 116 if number == rpc.PendingBlockNumber { 117 block := b.eth.miner.PendingBlock() 118 return block, nil 119 } 120 // Otherwise resolve and return the block 121 if number == rpc.LatestBlockNumber { 122 return b.eth.blockchain.CurrentBlock(), nil 123 } 124 if number == rpc.FinalizedBlockNumber { 125 return b.eth.blockchain.CurrentFinalizedBlock(), nil 126 } 127 if number == rpc.SafeBlockNumber { 128 return b.eth.blockchain.CurrentSafeBlock(), nil 129 } 130 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 131 } 132 133 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 134 return b.eth.blockchain.GetBlockByHash(hash), nil 135 } 136 137 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 138 if blockNr, ok := blockNrOrHash.Number(); ok { 139 return b.BlockByNumber(ctx, blockNr) 140 } 141 if hash, ok := blockNrOrHash.Hash(); ok { 142 header := b.eth.blockchain.GetHeaderByHash(hash) 143 if header == nil { 144 return nil, errors.New("header for hash not found") 145 } 146 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 147 return nil, errors.New("hash is not currently canonical") 148 } 149 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 150 if block == nil { 151 return nil, errors.New("header found, but block body is missing") 152 } 153 return block, nil 154 } 155 return nil, errors.New("invalid arguments; neither block nor hash specified") 156 } 157 158 func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 159 return b.eth.miner.PendingBlockAndReceipts() 160 } 161 162 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 163 // Pending state is only known by the miner 164 if number == rpc.PendingBlockNumber { 165 block, state := b.eth.miner.Pending() 166 return state, block.Header(), nil 167 } 168 // Otherwise resolve the block number and return its state 169 header, err := b.HeaderByNumber(ctx, number) 170 if err != nil { 171 return nil, nil, err 172 } 173 if header == nil { 174 return nil, nil, errors.New("header not found") 175 } 176 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 177 return stateDb, header, err 178 } 179 180 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 181 if blockNr, ok := blockNrOrHash.Number(); ok { 182 return b.StateAndHeaderByNumber(ctx, blockNr) 183 } 184 if hash, ok := blockNrOrHash.Hash(); ok { 185 header, err := b.HeaderByHash(ctx, hash) 186 if err != nil { 187 return nil, nil, err 188 } 189 if header == nil { 190 return nil, nil, errors.New("header for hash not found") 191 } 192 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 193 return nil, nil, errors.New("hash is not currently canonical") 194 } 195 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 196 return stateDb, header, err 197 } 198 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 199 } 200 201 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 202 return b.eth.blockchain.GetReceiptsByHash(hash), nil 203 } 204 205 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { 206 return rawdb.ReadLogs(b.eth.chainDb, hash, number, b.ChainConfig()), nil 207 } 208 209 func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 210 if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil { 211 return b.eth.blockchain.GetTd(hash, header.Number.Uint64()) 212 } 213 return nil 214 } 215 216 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) { 217 if vmConfig == nil { 218 vmConfig = b.eth.blockchain.GetVMConfig() 219 } 220 txContext := core.NewEVMTxContext(msg) 221 context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) 222 return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), state.Error, nil 223 } 224 225 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 226 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 227 } 228 229 func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 230 return b.eth.miner.SubscribePendingLogs(ch) 231 } 232 233 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 234 return b.eth.BlockChain().SubscribeChainEvent(ch) 235 } 236 237 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 238 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 239 } 240 241 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 242 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 243 } 244 245 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 246 return b.eth.BlockChain().SubscribeLogsEvent(ch) 247 } 248 249 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 250 return b.eth.txPool.AddLocal(signedTx) 251 } 252 253 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 254 pending := b.eth.txPool.Pending(false) 255 var txs types.Transactions 256 for _, batch := range pending { 257 txs = append(txs, batch...) 258 } 259 return txs, nil 260 } 261 262 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 263 return b.eth.txPool.Get(hash) 264 } 265 266 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 267 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) 268 return tx, blockHash, blockNumber, index, nil 269 } 270 271 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 272 return b.eth.txPool.Nonce(addr), nil 273 } 274 275 func (b *EthAPIBackend) Stats() (pending int, queued int) { 276 return b.eth.txPool.Stats() 277 } 278 279 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 280 return b.eth.TxPool().Content() 281 } 282 283 func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) { 284 return b.eth.TxPool().ContentFrom(addr) 285 } 286 287 func (b *EthAPIBackend) TxPool() *core.TxPool { 288 return b.eth.TxPool() 289 } 290 291 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 292 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 293 } 294 295 func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress { 296 return b.eth.Downloader().Progress() 297 } 298 299 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 300 return b.gpo.SuggestTipCap(ctx) 301 } 302 303 func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { 304 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 305 } 306 307 func (b *EthAPIBackend) ChainDb() ethdb.Database { 308 return b.eth.ChainDb() 309 } 310 311 func (b *EthAPIBackend) EventMux() *event.TypeMux { 312 return b.eth.EventMux() 313 } 314 315 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 316 return b.eth.AccountManager() 317 } 318 319 func (b *EthAPIBackend) ExtRPCEnabled() bool { 320 return b.extRPCEnabled 321 } 322 323 func (b *EthAPIBackend) UnprotectedAllowed() bool { 324 return b.allowUnprotectedTxs 325 } 326 327 func (b *EthAPIBackend) RPCGasCap() uint64 { 328 return b.eth.config.RPCGasCap 329 } 330 331 func (b *EthAPIBackend) RPCEVMTimeout() time.Duration { 332 return b.eth.config.RPCEVMTimeout 333 } 334 335 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 336 return b.eth.config.RPCTxFeeCap 337 } 338 339 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 340 sections, _, _ := b.eth.bloomIndexer.Sections() 341 return params.BloomBitsBlocks, sections 342 } 343 344 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 345 for i := 0; i < bloomFilterThreads; i++ { 346 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 347 } 348 } 349 350 func (b *EthAPIBackend) Engine() consensus.Engine { 351 return b.eth.engine 352 } 353 354 func (b *EthAPIBackend) CurrentHeader() *types.Header { 355 return b.eth.blockchain.CurrentHeader() 356 } 357 358 func (b *EthAPIBackend) Miner() *miner.Miner { 359 return b.eth.Miner() 360 } 361 362 func (b *EthAPIBackend) StartMining(threads int) error { 363 return b.eth.StartMining(threads) 364 } 365 366 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) { 367 return b.eth.StateAtBlock(block, reexec, base, readOnly, preferDisk) 368 } 369 370 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) { 371 return b.eth.stateAtTransaction(block, txIndex, reexec) 372 }