Skip to content

Commit

Permalink
internal/ethapi: support GetFinalizedBlock by common ratio validators
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Jan 6, 2025
1 parent 574379e commit bc25982
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
14 changes: 4 additions & 10 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,24 +253,18 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H
}

// GetFinalizedHeader returns the requested finalized block header.
// - probabilisticFinalized should be in range [2,21],
// then the block header with number `max(fastFinalized, latest-probabilisticFinalized)` is returned
func (ec *Client) FinalizedHeader(ctx context.Context, probabilisticFinalized int64) (*types.Header, error) {
func (ec *Client) FinalizedHeader(ctx context.Context, verifiedValidatorNum int64) (*types.Header, error) {
var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getFinalizedHeader", probabilisticFinalized)
err := ec.c.CallContext(ctx, &head, "eth_getFinalizedHeader", verifiedValidatorNum)
if err == nil && head == nil {
err = ethereum.NotFound
}
return head, err
}

// GetFinalizedBlock returns the requested finalized block.
// - probabilisticFinalized should be in range [2,21],
// then the block with number `max(fastFinalized, latest-probabilisticFinalized)` is returned
// - When fullTx is true all transactions in the block are returned, otherwise
// only the transaction hash is returned.
func (ec *Client) FinalizedBlock(ctx context.Context, probabilisticFinalized int64, fullTx bool) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getFinalizedBlock", probabilisticFinalized, true)
func (ec *Client) FinalizedBlock(ctx context.Context, verifiedValidatorNum int64, fullTx bool) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getFinalizedBlock", verifiedValidatorNum, fullTx)
}

func (ec *Client) GetRootByDiffHash(ctx context.Context, blockNr *big.Int, blockHash common.Hash, diffHash common.Hash) (*core.VerifyResult, error) {
Expand Down
21 changes: 17 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/common/gopool"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -549,8 +550,14 @@ func (api *BlockChainAPI) getFinalizedNumber(ctx context.Context, verifiedValida
if err != nil { // impossible
return 0, err
}
valLen := int64(len(curValidators))
if verifiedValidatorNum < 1 || verifiedValidatorNum > valLen {
valLen := len(curValidators)
if verifiedValidatorNum == -1 {
verifiedValidatorNum = int64(cmath.CeilDiv(valLen, 2))
} else if verifiedValidatorNum == -2 {
verifiedValidatorNum = int64(cmath.CeilDiv(valLen*2, 3))
} else if verifiedValidatorNum == -3 {
verifiedValidatorNum = int64(valLen)
} else if verifiedValidatorNum < 1 || verifiedValidatorNum > int64(valLen) {
return 0, fmt.Errorf("%d out of range [1,%d]", verifiedValidatorNum, valLen)
}

Expand Down Expand Up @@ -582,7 +589,10 @@ func (api *BlockChainAPI) getFinalizedNumber(ctx context.Context, verifiedValida
}

// GetFinalizedHeader returns the finalized block header based on the specified parameters.
// - `verifiedValidatorNum` must be within the range [1, len(currentValidators)].
// - `verifiedValidatorNum` must be within the range [1, len(currentValidators)],with the exception that:
// -1 represents at least len(currentValidators) * 1/2
// -2 represents at least len(currentValidators) * 2/3
// -3 represents at least len(currentValidators)
// - The function calculates `probabilisticFinalizedHeight` as the highest height of the block verified by `verifiedValidatorNum` validators,
// it then returns the block header with a height equal to `max(fastFinalizedHeight, probabilisticFinalizedHeight)`.
// - The height of the returned block header is guaranteed to be monotonically increasing.
Expand All @@ -595,7 +605,10 @@ func (api *BlockChainAPI) GetFinalizedHeader(ctx context.Context, verifiedValida
}

// GetFinalizedBlock returns the finalized block based on the specified parameters.
// - `verifiedValidatorNum` must be within the range [1, len(currentValidators)].
// - `verifiedValidatorNum` must be within the range [1, len(currentValidators)],with the exception that:
// -1 represents at least len(currentValidators) * 1/2
// -2 represents at least len(currentValidators) * 2/3
// -3 represents at least len(currentValidators)
// - The function calculates `probabilisticFinalizedHeight` as the highest height of the block verified by `verifiedValidatorNum` validators,
// it then returns the block with a height equal to `max(fastFinalizedHeight, probabilisticFinalizedHeight)`.
// - If `fullTx` is true, the block includes all transactions; otherwise, only transaction hashes are included.
Expand Down

0 comments on commit bc25982

Please sign in to comment.