github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/forks.go (about)

     1  package hotstuff
     2  
     3  import (
     4  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     5  	"github.com/onflow/flow-go/model/flow"
     6  )
     7  
     8  // FinalityProof represents a finality proof for a Block. By convention, a FinalityProof
     9  // is immutable. Finality in Jolteon/HotStuff is determined by the 2-chain rule:
    10  //
    11  //	There exists a _certified_ block C, such that Block.View + 1 = C.View
    12  type FinalityProof struct {
    13  	Block          *model.Block
    14  	CertifiedChild model.CertifiedBlock
    15  }
    16  
    17  // Forks maintains an in-memory data-structure of all blocks whose view-number is larger or equal to
    18  // the latest finalized block. The latest finalized block is defined as the finalized block with the largest view number.
    19  // When adding blocks, Forks automatically updates its internal state (including finalized blocks).
    20  // Furthermore, blocks whose view number is smaller than the latest finalized block are pruned automatically.
    21  //
    22  // PREREQUISITES:
    23  // Forks expects that only blocks are added that can be connected to its latest finalized block
    24  // (without missing interim ancestors). If this condition is violated, Forks will raise an error
    25  // and ignore the block.
    26  type Forks interface {
    27  
    28  	// GetBlocksForView returns all known blocks for the given view
    29  	GetBlocksForView(view uint64) []*model.Block
    30  
    31  	// GetBlock returns (BlockProposal, true) if the block with the specified
    32  	// id was found and (nil, false) otherwise.
    33  	GetBlock(blockID flow.Identifier) (*model.Block, bool)
    34  
    35  	// FinalizedView returns the largest view number where a finalized block is known
    36  	FinalizedView() uint64
    37  
    38  	// FinalizedBlock returns the finalized block with the largest view number
    39  	FinalizedBlock() *model.Block
    40  
    41  	// FinalityProof returns the latest finalized block and a certified child from
    42  	// the subsequent view, which proves finality.
    43  	// CAUTION: method returns (nil, false), when Forks has not yet finalized any
    44  	// blocks beyond the finalized root block it was initialized with.
    45  	FinalityProof() (*FinalityProof, bool)
    46  
    47  	// AddValidatedBlock appends the validated block to the tree of pending
    48  	// blocks and updates the latest finalized block (if applicable). Unless the parent is
    49  	// below the pruning threshold (latest finalized view), we require that the parent is
    50  	// already stored in Forks. Calling this method with previously processed blocks
    51  	// leaves the consensus state invariant (though, it will potentially cause some
    52  	// duplicate processing).
    53  	// Notes:
    54  	//   - Method `AddCertifiedBlock(..)` should be used preferably, if a QC certifying
    55  	//     `block` is already known. This is generally the case for the consensus follower.
    56  	//     Method `AddValidatedBlock` is intended for active consensus participants, which fully
    57  	//     validate blocks (incl. payload), i.e. QCs are processed as part of validated proposals.
    58  	//
    59  	// Possible error returns:
    60  	//   - model.MissingBlockError if the parent does not exist in the forest (but is above
    61  	//     the pruned view). From the perspective of Forks, this error is benign (no-op).
    62  	//   - model.InvalidBlockError if the block is invalid (see `Forks.EnsureBlockIsValidExtension`
    63  	//     for details). From the perspective of Forks, this error is benign (no-op). However, we
    64  	//     assume all blocks are fully verified, i.e. they should satisfy all consistency
    65  	//     requirements. Hence, this error is likely an indicator of a bug in the compliance layer.
    66  	//   - model.ByzantineThresholdExceededError if conflicting QCs or conflicting finalized
    67  	//     blocks have been detected (violating a foundational consensus guarantees). This
    68  	//     indicates that there are 1/3+ Byzantine nodes (weighted by stake) in the network,
    69  	//     breaking the safety guarantees of HotStuff (or there is a critical bug / data
    70  	//     corruption). Forks cannot recover from this exception.
    71  	//   - All other errors are potential symptoms of bugs or state corruption.
    72  	AddValidatedBlock(proposal *model.Block) error
    73  
    74  	// AddCertifiedBlock appends the given certified block to the tree of pending
    75  	// blocks and updates the latest finalized block (if finalization progressed).
    76  	// Unless the parent is below the pruning threshold (latest finalized view), we
    77  	// require that the parent is already stored in Forks. Calling this method with
    78  	// previously processed blocks leaves the consensus state invariant (though,
    79  	// it will potentially cause some duplicate processing).
    80  	//
    81  	// Possible error returns:
    82  	//   - model.MissingBlockError if the parent does not exist in the forest (but is above
    83  	//     the pruned view). From the perspective of Forks, this error is benign (no-op).
    84  	//   - model.InvalidBlockError if the block is invalid (see `Forks.EnsureBlockIsValidExtension`
    85  	//     for details). From the perspective of Forks, this error is benign (no-op). However, we
    86  	//     assume all blocks are fully verified, i.e. they should satisfy all consistency
    87  	//     requirements. Hence, this error is likely an indicator of a bug in the compliance layer.
    88  	//   - model.ByzantineThresholdExceededError if conflicting QCs or conflicting finalized
    89  	//     blocks have been detected (violating a foundational consensus guarantees). This
    90  	//     indicates that there are 1/3+ Byzantine nodes (weighted by stake) in the network,
    91  	//     breaking the safety guarantees of HotStuff (or there is a critical bug / data
    92  	//     corruption). Forks cannot recover from this exception.
    93  	//   - All other errors are potential symptoms of bugs or state corruption.
    94  	AddCertifiedBlock(certifiedBlock *model.CertifiedBlock) error
    95  }