github.com/dim4egster/coreth@v0.10.2/eth/api_backend.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package eth 28 29 import ( 30 "context" 31 "errors" 32 "math/big" 33 "time" 34 35 "github.com/dim4egster/coreth/accounts" 36 "github.com/dim4egster/coreth/consensus" 37 "github.com/dim4egster/coreth/consensus/dummy" 38 "github.com/dim4egster/coreth/core" 39 "github.com/dim4egster/coreth/core/bloombits" 40 "github.com/dim4egster/coreth/core/rawdb" 41 "github.com/dim4egster/coreth/core/state" 42 "github.com/dim4egster/coreth/core/types" 43 "github.com/dim4egster/coreth/core/vm" 44 "github.com/dim4egster/coreth/eth/gasprice" 45 "github.com/dim4egster/coreth/ethdb" 46 "github.com/dim4egster/coreth/params" 47 "github.com/dim4egster/coreth/rpc" 48 "github.com/ethereum/go-ethereum/common" 49 "github.com/ethereum/go-ethereum/event" 50 ) 51 52 var ErrUnfinalizedData = errors.New("cannot query unfinalized data") 53 54 // EthAPIBackend implements ethapi.Backend for full nodes 55 type EthAPIBackend struct { 56 extRPCEnabled bool 57 allowUnprotectedTxs bool 58 eth *Ethereum 59 gpo *gasprice.Oracle 60 } 61 62 // ChainConfig returns the active chain configuration. 63 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 64 return b.eth.blockchain.Config() 65 } 66 67 func (b *EthAPIBackend) GetVMConfig() *vm.Config { 68 return b.eth.blockchain.GetVMConfig() 69 } 70 71 func (b *EthAPIBackend) CurrentBlock() *types.Block { 72 return b.eth.blockchain.CurrentBlock() 73 } 74 75 func (b *EthAPIBackend) LastAcceptedBlock() *types.Block { 76 return b.eth.LastAcceptedBlock() 77 } 78 79 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 80 if err := ctx.Err(); err != nil { 81 return nil, err 82 } 83 // Treat requests for the pending, latest, or accepted block 84 // identically. 85 acceptedBlock := b.eth.LastAcceptedBlock() 86 if number.IsAccepted() { 87 return acceptedBlock.Header(), nil 88 } 89 90 if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil { 91 if number.Int64() > acceptedBlock.Number().Int64() { 92 return nil, ErrUnfinalizedData 93 } 94 } 95 96 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 97 } 98 99 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 100 if err := ctx.Err(); err != nil { 101 return nil, err 102 } 103 104 header := b.eth.blockchain.GetHeaderByHash(hash) 105 if header == nil { 106 return nil, nil 107 } 108 109 if b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 110 return nil, nil 111 } 112 113 acceptedBlock := b.eth.LastAcceptedBlock() 114 if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil { 115 if header.Number.Cmp(acceptedBlock.Number()) > 0 { 116 return nil, ErrUnfinalizedData 117 } 118 } 119 return header, nil 120 } 121 122 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 123 if blockNr, ok := blockNrOrHash.Number(); ok { 124 return b.HeaderByNumber(ctx, blockNr) 125 } 126 if hash, ok := blockNrOrHash.Hash(); ok { 127 header, err := b.HeaderByHash(ctx, hash) 128 if err != nil { 129 return nil, err 130 } 131 if header == nil { 132 return nil, errors.New("header for hash not found") 133 } 134 return header, nil 135 } 136 return nil, errors.New("invalid arguments; neither block nor hash specified") 137 } 138 139 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 140 if err := ctx.Err(); err != nil { 141 return nil, err 142 } 143 // Treat requests for the pending, latest, or accepted block 144 // identically. 145 acceptedBlock := b.eth.LastAcceptedBlock() 146 if number.IsAccepted() { 147 return acceptedBlock, nil 148 } 149 150 if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil { 151 if number.Int64() > acceptedBlock.Number().Int64() { 152 return nil, ErrUnfinalizedData 153 } 154 } 155 156 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 157 } 158 159 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 160 if err := ctx.Err(); err != nil { 161 return nil, err 162 } 163 164 block := b.eth.blockchain.GetBlockByHash(hash) 165 if block == nil { 166 return nil, nil 167 } 168 169 number := block.Number() 170 if b.eth.blockchain.GetCanonicalHash(number.Uint64()) != hash { 171 return nil, nil 172 } 173 174 acceptedBlock := b.eth.LastAcceptedBlock() 175 if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil { 176 if number.Cmp(acceptedBlock.Number()) > 0 { 177 return nil, ErrUnfinalizedData 178 } 179 } 180 return block, nil 181 } 182 183 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 184 if blockNr, ok := blockNrOrHash.Number(); ok { 185 return b.BlockByNumber(ctx, blockNr) 186 } 187 if err := ctx.Err(); err != nil { 188 return nil, err 189 } 190 if hash, ok := blockNrOrHash.Hash(); ok { 191 block, err := b.BlockByHash(ctx, hash) 192 if err != nil { 193 return nil, err 194 } 195 if block == nil { 196 return nil, errors.New("header for hash not found") 197 } 198 return block, nil 199 } 200 return nil, errors.New("invalid arguments; neither block nor hash specified") 201 } 202 203 func (b *EthAPIBackend) BadBlocks() ([]*types.Block, []*core.BadBlockReason) { 204 return b.eth.blockchain.BadBlocks() 205 } 206 207 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 208 // Request the block by its number and retrieve its state 209 header, err := b.HeaderByNumber(ctx, number) 210 if err != nil { 211 return nil, nil, err 212 } 213 if header == nil { 214 return nil, nil, errors.New("header not found") 215 } 216 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 217 return stateDb, header, err 218 } 219 220 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 221 if blockNr, ok := blockNrOrHash.Number(); ok { 222 return b.StateAndHeaderByNumber(ctx, blockNr) 223 } 224 if err := ctx.Err(); err != nil { 225 return nil, nil, err 226 } 227 if hash, ok := blockNrOrHash.Hash(); ok { 228 header, err := b.HeaderByHash(ctx, hash) 229 if err != nil { 230 return nil, nil, err 231 } 232 if header == nil { 233 return nil, nil, errors.New("header for hash not found") 234 } 235 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 236 return stateDb, header, err 237 } 238 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 239 } 240 241 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 242 if err := ctx.Err(); err != nil { 243 return nil, err 244 } 245 return b.eth.blockchain.GetReceiptsByHash(hash), nil 246 } 247 248 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { 249 return rawdb.ReadLogs(b.eth.chainDb, hash, number), nil 250 } 251 252 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) { 253 vmError := func() error { return nil } 254 if vmConfig == nil { 255 vmConfig = b.eth.blockchain.GetVMConfig() 256 } 257 txContext := core.NewEVMTxContext(msg) 258 context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) 259 return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil 260 } 261 262 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 263 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 264 } 265 266 func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 267 return b.eth.miner.SubscribePendingLogs(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) SubscribeChainAcceptedEvent(ch chan<- core.ChainEvent) event.Subscription { 275 return b.eth.BlockChain().SubscribeChainAcceptedEvent(ch) 276 } 277 278 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 279 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 280 } 281 282 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 283 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 284 } 285 286 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 287 return b.eth.BlockChain().SubscribeLogsEvent(ch) 288 } 289 290 func (b *EthAPIBackend) SubscribeAcceptedLogsEvent(ch chan<- []*types.Log) event.Subscription { 291 return b.eth.BlockChain().SubscribeAcceptedLogsEvent(ch) 292 } 293 294 func (b *EthAPIBackend) SubscribeAcceptedTransactionEvent(ch chan<- core.NewTxsEvent) event.Subscription { 295 return b.eth.BlockChain().SubscribeAcceptedTransactionEvent(ch) 296 } 297 298 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 299 if err := ctx.Err(); err != nil { 300 return err 301 } 302 return b.eth.txPool.AddLocal(signedTx) 303 } 304 305 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 306 pending := b.eth.txPool.Pending(false) 307 var txs types.Transactions 308 for _, batch := range pending { 309 txs = append(txs, batch...) 310 } 311 return txs, nil 312 } 313 314 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 315 return b.eth.txPool.Get(hash) 316 } 317 318 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 319 // Note: we only index transactions during Accept, so the below check against unfinalized queries is technically redundant, but 320 // we keep it for defense in depth. 321 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) 322 323 // Respond as if the transaction does not exist if it is not yet in an 324 // accepted block. We explicitly choose not to error here to avoid breaking 325 // expectations with clients (expect an empty response when a transaction 326 // does not exist). 327 acceptedBlock := b.eth.LastAcceptedBlock() 328 if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil && tx != nil { 329 if blockNumber > acceptedBlock.NumberU64() { 330 return nil, common.Hash{}, 0, 0, nil 331 } 332 } 333 334 return tx, blockHash, blockNumber, index, nil 335 } 336 337 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 338 return b.eth.txPool.Nonce(addr), nil 339 } 340 341 func (b *EthAPIBackend) Stats() (pending int, queued int) { 342 return b.eth.txPool.Stats() 343 } 344 345 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 346 return b.eth.txPool.Content() 347 } 348 349 func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) { 350 return b.eth.txPool.ContentFrom(addr) 351 } 352 353 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 354 return b.eth.txPool.SubscribeNewTxsEvent(ch) 355 } 356 357 func (b *EthAPIBackend) EstimateBaseFee(ctx context.Context) (*big.Int, error) { 358 return b.gpo.EstimateBaseFee(ctx) 359 } 360 361 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 362 return b.gpo.SuggestPrice(ctx) 363 } 364 365 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 366 return b.gpo.SuggestTipCap(ctx) 367 } 368 369 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) { 370 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 371 } 372 373 func (b *EthAPIBackend) ChainDb() ethdb.Database { 374 return b.eth.ChainDb() 375 } 376 377 func (b *EthAPIBackend) EventMux() *event.TypeMux { 378 return b.eth.EventMux() 379 } 380 381 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 382 return b.eth.AccountManager() 383 } 384 385 func (b *EthAPIBackend) ExtRPCEnabled() bool { 386 return b.extRPCEnabled 387 } 388 389 func (b *EthAPIBackend) UnprotectedAllowed() bool { 390 return b.allowUnprotectedTxs 391 } 392 393 func (b *EthAPIBackend) RPCGasCap() uint64 { 394 return b.eth.config.RPCGasCap 395 } 396 397 func (b *EthAPIBackend) RPCEVMTimeout() time.Duration { 398 return b.eth.config.RPCEVMTimeout 399 } 400 401 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 402 return b.eth.config.RPCTxFeeCap 403 } 404 405 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 406 sections, _, _ := b.eth.bloomIndexer.Sections() 407 return params.BloomBitsBlocks, sections 408 } 409 410 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 411 for i := 0; i < bloomFilterThreads; i++ { 412 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 413 } 414 } 415 416 func (b *EthAPIBackend) Engine() consensus.Engine { 417 return b.eth.engine 418 } 419 420 func (b *EthAPIBackend) CurrentHeader() *types.Header { 421 return b.eth.blockchain.CurrentHeader() 422 } 423 424 func (b *EthAPIBackend) GetMaxBlocksPerRequest() int64 { 425 return b.eth.settings.MaxBlocksPerRequest 426 } 427 428 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) { 429 return b.eth.StateAtBlock(block, reexec, base, checkLive, preferDisk) 430 } 431 432 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) { 433 return b.eth.stateAtTransaction(block, txIndex, reexec) 434 } 435 436 func (b *EthAPIBackend) MinRequiredTip(ctx context.Context, header *types.Header) (*big.Int, error) { 437 return dummy.MinRequiredTip(b.ChainConfig(), header) 438 }