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