Skip to content

Commit

Permalink
feat: api support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Aug 12, 2024
1 parent f41e0e5 commit 16b615a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 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
4 changes: 2 additions & 2 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 511 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / unit-test (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 511 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 511 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 511 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 511 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / unit-test (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber
return header
}

Expand Down Expand Up @@ -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)

Check failure on line 566 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / unit-test (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 566 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 566 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 566 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber

Check failure on line 566 in core/headerchain.go

View workflow job for this annotation

GitHub Actions / unit-test (1.21.x, ubuntu-latest)

too many arguments in call to hc.blockArchiverService.GetBlockByNumber
if header == nil || body == nil {
break
}
Expand Down
38 changes: 7 additions & 31 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()
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
}
Expand Down

0 comments on commit 16b615a

Please sign in to comment.