Skip to content

Commit

Permalink
Merge pull request bnb-chain#3 from alexgao001/api-support
Browse files Browse the repository at this point in the history
feat: api support
  • Loading branch information
unclezoro committed Aug 12, 2024
2 parents 21a070b + 7fd847c commit fcb9ed8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
34 changes: 20 additions & 14 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 6 additions & 30 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
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
}
Expand Down

0 comments on commit fcb9ed8

Please sign in to comment.