diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index f48786974e..fb11f2cee1 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -163,36 +163,42 @@ func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool { // GetBlock retrieves a block from the database by hash and number, // caching it if found. func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { - // Short circuit if the block's already in the cache, retrieve otherwise - if block, ok := bc.blockCache.Get(hash); ok { - return block + var ( + body *types.Body + header *types.Header + ) + if hash == (common.Hash{}) { + body, header, _ = bc.blockArchiverService.GetBlockByNumber(number) + } else { + body, header, _ = bc.blockArchiverService.GetBlockByHash(hash) } - block := rawdb.ReadBlock(bc.db, hash, number) - if block == nil { + if body == nil || header == nil { return nil } - // Cache the found block for next time and return - bc.blockCache.Add(block.Hash(), block) - return block + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals) } // GetBlockByHash retrieves a block from the database by hash, caching it if found. func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { - number := bc.hc.GetBlockNumber(hash) - if number == nil { + body, header, _ := bc.blockArchiverService.GetBlockByHash(hash) + if body == nil || header == nil { return nil } - return bc.GetBlock(hash, *number) + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals) } // GetBlockByNumber retrieves a block from the database by number, caching it // (associated with its hash) if found. func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { - hash := rawdb.ReadCanonicalHash(bc.db, number) - if hash == (common.Hash{}) { + if number == 0 { + genesisHash := rawdb.ReadCanonicalHash(bc.db, 0) + return rawdb.ReadBlock(bc.db, genesisHash, number) + } + body, header, _ := bc.blockArchiverService.GetBlockByNumber(number) + if body == nil || header == nil { return nil } - return bc.GetBlock(hash, number) + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithWithdrawals(body.Withdrawals) } // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors. diff --git a/eth/api_backend.go b/eth/api_backend.go index ae698ade36..671664d4a0 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -68,29 +68,17 @@ func (b *EthAPIBackend) SetHead(number uint64) { func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { // Pending block is only known by the miner if number == rpc.PendingBlockNumber { - block := b.eth.miner.PendingBlock() - if block == nil { - return nil, errors.New("pending block is not available") - } - return block.Header(), nil + return nil, errors.New("get pending header is not supported") } // Otherwise resolve and return the block if number == rpc.LatestBlockNumber { return b.eth.blockchain.CurrentBlock(), nil } if number == rpc.FinalizedBlockNumber { - block := b.eth.blockchain.CurrentFinalBlock() - if block == nil { - return nil, errors.New("finalized block not found") - } - return block, nil + return nil, errors.New("get finalized header is not supported") } if number == rpc.SafeBlockNumber { - block := b.eth.blockchain.CurrentSafeBlock() - if block == nil { - return nil, errors.New("safe block not found") - } - return block, nil + return nil, errors.New("get safe header is not supported") } return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil } @@ -119,11 +107,7 @@ func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*ty func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { // Pending block is only known by the miner if number == rpc.PendingBlockNumber { - block := b.eth.miner.PendingBlock() - if block == nil { - return nil, errors.New("pending block is not available") - } - return block, nil + return nil, errors.New("get pending block is not supported") } // Otherwise resolve and return the block if number == rpc.LatestBlockNumber { @@ -131,18 +115,10 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil } if number == rpc.FinalizedBlockNumber { - header := b.eth.blockchain.CurrentFinalBlock() - if header == nil { - return nil, errors.New("finalized block not found") - } - return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil + return nil, errors.New("get finalized block is not supported") } if number == rpc.SafeBlockNumber { - header := b.eth.blockchain.CurrentSafeBlock() - if header == nil { - return nil, errors.New("safe block not found") - } - return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil + return nil, errors.New("get safe block is not supported") } return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil }