github.com/jonkofee/go-ethereum@v1.11.1/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/jonkofee/go-ethereum" 26 "github.com/jonkofee/go-ethereum/accounts" 27 "github.com/jonkofee/go-ethereum/common" 28 "github.com/jonkofee/go-ethereum/consensus" 29 "github.com/jonkofee/go-ethereum/core" 30 "github.com/jonkofee/go-ethereum/core/bloombits" 31 "github.com/jonkofee/go-ethereum/core/rawdb" 32 "github.com/jonkofee/go-ethereum/core/state" 33 "github.com/jonkofee/go-ethereum/core/types" 34 "github.com/jonkofee/go-ethereum/core/vm" 35 "github.com/jonkofee/go-ethereum/eth/gasprice" 36 "github.com/jonkofee/go-ethereum/ethdb" 37 "github.com/jonkofee/go-ethereum/event" 38 "github.com/jonkofee/go-ethereum/miner" 39 "github.com/jonkofee/go-ethereum/params" 40 "github.com/jonkofee/go-ethereum/rpc" 41 ) 42 43 // EthAPIBackend implements ethapi.Backend for full nodes 44 type EthAPIBackend struct { 45 extRPCEnabled bool 46 allowUnprotectedTxs bool 47 eth *Ethereum 48 gpo *gasprice.Oracle 49 } 50 51 // ChainConfig returns the active chain configuration. 52 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 53 return b.eth.blockchain.Config() 54 } 55 56 func (b *EthAPIBackend) CurrentBlock() *types.Block { 57 return b.eth.blockchain.CurrentBlock() 58 } 59 60 func (b *EthAPIBackend) SetHead(number uint64) { 61 b.eth.handler.downloader.Cancel() 62 b.eth.blockchain.SetHead(number) 63 } 64 65 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 66 // Pending block is only known by the miner 67 if number == rpc.PendingBlockNumber { 68 block := b.eth.miner.PendingBlock() 69 return block.Header(), nil 70 } 71 // Otherwise resolve and return the block 72 if number == rpc.LatestBlockNumber { 73 return b.eth.blockchain.CurrentBlock().Header(), nil 74 } 75 if number == rpc.FinalizedBlockNumber { 76 block := b.eth.blockchain.CurrentFinalizedBlock() 77 if block != nil { 78 return block.Header(), nil 79 } 80 return nil, errors.New("finalized block not found") 81 } 82 if number == rpc.SafeBlockNumber { 83 block := b.eth.blockchain.CurrentSafeBlock() 84 if block != nil { 85 return block.Header(), nil 86 } 87 return nil, errors.New("safe block not found") 88 } 89 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 90 } 91 92 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 93 if blockNr, ok := blockNrOrHash.Number(); ok { 94 return b.HeaderByNumber(ctx, blockNr) 95 } 96 if hash, ok := blockNrOrHash.Hash(); ok { 97 header := b.eth.blockchain.GetHeaderByHash(hash) 98 if header == nil { 99 return nil, errors.New("header for hash not found") 100 } 101 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 102 return nil, errors.New("hash is not currently canonical") 103 } 104 return header, nil 105 } 106 return nil, errors.New("invalid arguments; neither block nor hash specified") 107 } 108 109 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 110 return b.eth.blockchain.GetHeaderByHash(hash), nil 111 } 112 113 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 114 // Pending block is only known by the miner 115 if number == rpc.PendingBlockNumber { 116 block := b.eth.miner.PendingBlock() 117 return block, nil 118 } 119 // Otherwise resolve and return the block 120 if number == rpc.LatestBlockNumber { 121 return b.eth.blockchain.CurrentBlock(), nil 122 } 123 if number == rpc.FinalizedBlockNumber { 124 return b.eth.blockchain.CurrentFinalizedBlock(), nil 125 } 126 if number == rpc.SafeBlockNumber { 127 return b.eth.blockchain.CurrentSafeBlock(), nil 128 } 129 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 130 } 131 132 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 133 return b.eth.blockchain.GetBlockByHash(hash), nil 134 } 135 136 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 137 if blockNr, ok := blockNrOrHash.Number(); ok { 138 return b.BlockByNumber(ctx, blockNr) 139 } 140 if hash, ok := blockNrOrHash.Hash(); ok { 141 header := b.eth.blockchain.GetHeaderByHash(hash) 142 if header == nil { 143 return nil, errors.New("header for hash not found") 144 } 145 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 146 return nil, errors.New("hash is not currently canonical") 147 } 148 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 149 if block == nil { 150 return nil, errors.New("header found, but block body is missing") 151 } 152 return block, nil 153 } 154 return nil, errors.New("invalid arguments; neither block nor hash specified") 155 } 156 157 func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 158 return b.eth.miner.PendingBlockAndReceipts() 159 } 160 161 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 162 // Pending state is only known by the miner 163 if number == rpc.PendingBlockNumber { 164 block, state := b.eth.miner.Pending() 165 return state, block.Header(), nil 166 } 167 // Otherwise resolve the block number and return its state 168 header, err := b.HeaderByNumber(ctx, number) 169 if err != nil { 170 return nil, nil, err 171 } 172 if header == nil { 173 return nil, nil, errors.New("header not found") 174 } 175 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 176 return stateDb, header, err 177 } 178 179 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 180 if blockNr, ok := blockNrOrHash.Number(); ok { 181 return b.StateAndHeaderByNumber(ctx, blockNr) 182 } 183 if hash, ok := blockNrOrHash.Hash(); ok { 184 header, err := b.HeaderByHash(ctx, hash) 185 if err != nil { 186 return nil, nil, err 187 } 188 if header == nil { 189 return nil, nil, errors.New("header for hash not found") 190 } 191 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 192 return nil, nil, errors.New("hash is not currently canonical") 193 } 194 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 195 return stateDb, header, err 196 } 197 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 198 } 199 200 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 201 return b.eth.blockchain.GetReceiptsByHash(hash), nil 202 } 203 204 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { 205 return rawdb.ReadLogs(b.eth.chainDb, hash, number, b.ChainConfig()), nil 206 } 207 208 func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 209 if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil { 210 return b.eth.blockchain.GetTd(hash, header.Number.Uint64()) 211 } 212 return nil 213 } 214 215 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) { 216 vmError := func() error { return nil } 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), vmError, 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) SyncProgressMap() map[string]interface{} { 296 progress := b.eth.Downloader().Progress() 297 return progress.ToMap() 298 } 299 300 func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress { 301 return b.eth.Downloader().Progress() 302 } 303 304 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 305 return b.gpo.SuggestTipCap(ctx) 306 } 307 308 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) { 309 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 310 } 311 312 func (b *EthAPIBackend) ChainDb() ethdb.Database { 313 return b.eth.ChainDb() 314 } 315 316 func (b *EthAPIBackend) EventMux() *event.TypeMux { 317 return b.eth.EventMux() 318 } 319 320 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 321 return b.eth.AccountManager() 322 } 323 324 func (b *EthAPIBackend) ExtRPCEnabled() bool { 325 return b.extRPCEnabled 326 } 327 328 func (b *EthAPIBackend) UnprotectedAllowed() bool { 329 return b.allowUnprotectedTxs 330 } 331 332 func (b *EthAPIBackend) RPCGasCap() uint64 { 333 return b.eth.config.RPCGasCap 334 } 335 336 func (b *EthAPIBackend) RPCEVMTimeout() time.Duration { 337 return b.eth.config.RPCEVMTimeout 338 } 339 340 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 341 return b.eth.config.RPCTxFeeCap 342 } 343 344 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 345 sections, _, _ := b.eth.bloomIndexer.Sections() 346 return params.BloomBitsBlocks, sections 347 } 348 349 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 350 for i := 0; i < bloomFilterThreads; i++ { 351 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 352 } 353 } 354 355 func (b *EthAPIBackend) Engine() consensus.Engine { 356 return b.eth.engine 357 } 358 359 func (b *EthAPIBackend) CurrentHeader() *types.Header { 360 return b.eth.blockchain.CurrentHeader() 361 } 362 363 func (b *EthAPIBackend) Miner() *miner.Miner { 364 return b.eth.Miner() 365 } 366 367 func (b *EthAPIBackend) StartMining(threads int) error { 368 return b.eth.StartMining(threads) 369 } 370 371 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error) { 372 return b.eth.StateAtBlock(block, reexec, base, checkLive, preferDisk) 373 } 374 375 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) { 376 return b.eth.stateAtTransaction(block, txIndex, reexec) 377 } 378 379 func (b *EthAPIBackend) FallbackClient() types.FallbackClient { 380 return nil 381 }