Skip to content

Commit

Permalink
rpc: add GetFinalizedBlock to simplify using the fast finality feature
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC committed Jul 31, 2023
1 parent f616c36 commit 79bf687
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,10 +1758,12 @@ func (p *Parlia) GetFinalizedHeader(chain consensus.ChainHeaderReader, header *t
return nil
}

if snap.Attestation != nil {
return chain.GetHeader(snap.Attestation.SourceHash, snap.Attestation.SourceNumber)
// snap.Attestation is nil after plato upgrade, only can happen in local testnet
if snap.Attestation == nil {
return chain.GetHeaderByNumber(0) // keep consistent with GetJustifiedNumberAndHash
}
return nil

return chain.GetHeader(snap.Attestation.SourceHash, snap.Attestation.SourceNumber)
}

// =========================== utility function ==========================
Expand Down
29 changes: 29 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ import (

const UnHealthyTimeout = 5 * time.Second

// max is a helper function which returns the larger of the two given integers.
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}

// PublicEthereumAPI provides an API to access Ethereum related information.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicEthereumAPI struct {
Expand Down Expand Up @@ -773,6 +781,27 @@ func (s *PublicBlockChainAPI) Health() bool {
return true
}

// GetFinalizedBlock returns the requested finalized block.
// * probabilisticFinalized should be in range [2,21], then the block with number `max(voteFinalized, latest-probabilisticFinalized)` is returned
func (s *PublicBlockChainAPI) GetFinalizedBlock(ctx context.Context, probabilisticFinalized int64, fullTx bool) (map[string]interface{}, error) {
if probabilisticFinalized < 2 || probabilisticFinalized > 21 {
return nil, fmt.Errorf("%d out of range [2,21]", probabilisticFinalized)
}

var err error
voteFinalizedHeader, err := s.b.HeaderByNumber(ctx, rpc.FinalizedBlockNumber)
if err != nil { // impossible
return nil, err
}
latestHeader, err := s.b.HeaderByNumber(ctx, rpc.LatestBlockNumber)
if err != nil { // impossible
return nil, err
}
finalizedBlockNumber := max(voteFinalizedHeader.Number.Int64(), latestHeader.Number.Int64()-probabilisticFinalized)

return s.GetBlockByNumber(ctx, rpc.BlockNumber(finalizedBlockNumber), fullTx)
}

// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index.
func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {
block, err := s.b.BlockByNumber(ctx, blockNr)
Expand Down

0 comments on commit 79bf687

Please sign in to comment.