From 16b615aca0ce299846b388ffd430987f5296b547 Mon Sep 17 00:00:00 2001 From: Alexgao001 Date: Mon, 12 Aug 2024 16:03:49 +0800 Subject: [PATCH] feat: api support --- core/blockchain_reader.go | 34 ++++++++++++++++++++-------------- core/headerchain.go | 4 ++-- eth/api_backend.go | 38 +++++++------------------------------- 3 files changed, 29 insertions(+), 47 deletions(-) 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/core/headerchain.go b/core/headerchain.go index 38d07d8265..62ff37c31c 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -508,7 +508,7 @@ func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header } // get header from block archiver, and the headerCache will be updated there - _, header, _ := hc.blockArchiverService.GetBlockByNumber(number) + _, header, _ := hc.blockArchiverService.GetBlockByNumber(number, false) return header } @@ -563,7 +563,7 @@ func (hc *HeaderChain) GetHeadersFrom(number, count uint64) []rlp.RawValue { var headers []rlp.RawValue for count > 0 { // GetBlockByNumber gets the header from block archiver service, cache is updated there - body, header, _ := hc.blockArchiverService.GetBlockByNumber(number) + body, header, _ := hc.blockArchiverService.GetBlockByNumber(number, false) if header == nil || body == nil { break } diff --git a/eth/api_backend.go b/eth/api_backend.go index ae698ade36..38b3fd40a1 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,30 +107,18 @@ 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 { - header := b.eth.blockchain.CurrentBlock() + header := b.eth.blockchain.CurrentBlock() // get from cache 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 }