github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/chain_state.go (about)

     1  package protocol
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  )
     8  
     9  // State represents the full protocol state of the local node. It allows us to
    10  // obtain snapshots of the state at any point of the protocol state history.
    11  type State interface {
    12  
    13  	// Params gives access to a number of stable parameters of the protocol state.
    14  	Params() Params
    15  
    16  	// Final returns the snapshot of the persistent protocol state at the latest
    17  	// finalized block, and the returned snapshot is therefore immutable over
    18  	// time.
    19  	Final() Snapshot
    20  
    21  	// Sealed returns the snapshot of the persistent protocol state at the
    22  	// latest sealed block, and the returned snapshot is therefore immutable
    23  	// over time.
    24  	Sealed() Snapshot
    25  
    26  	// AtHeight returns the snapshot of the persistent protocol state at the
    27  	// given block number. It is only available for finalized blocks and the
    28  	// returned snapshot is therefore immutable over time.
    29  	AtHeight(height uint64) Snapshot
    30  
    31  	// AtBlockID returns the snapshot of the persistent protocol state at the
    32  	// given block ID. It is available for any block that was introduced into
    33  	// the protocol state, and can thus represent an ambiguous state that was or
    34  	// will never be finalized.
    35  	AtBlockID(blockID flow.Identifier) Snapshot
    36  }
    37  
    38  // FollowerState is a mutable protocol state used by nodes following main consensus (ie. non-consensus nodes).
    39  // All blocks must have a certifying QC when being added to the state to guarantee they are valid,
    40  // so there is a one-block lag between block production and incorporation into the FollowerState.
    41  // However, since all blocks are certified upon insertion, they are immediately processable by other components.
    42  type FollowerState interface {
    43  	State
    44  
    45  	// ExtendCertified introduces the block with the given ID into the persistent
    46  	// protocol state without modifying the current finalized state. It allows us
    47  	// to execute fork-aware queries against the known protocol state. The caller
    48  	// must pass a QC for candidate block to prove that the candidate block has
    49  	// been certified, and it's safe to add it to the protocol state. The QC
    50  	// cannot be nil and must certify candidate block:
    51  	//   candidate.View == qc.View && candidate.BlockID == qc.BlockID
    52  	// The `candidate` block and its QC _must be valid_ (otherwise, the state will
    53  	// be corrupted). ExtendCertified inserts any given block, as long as its
    54  	// parent is already in the protocol state. Also orphaned blocks are excepted.
    55  	// No errors are expected during normal operations.
    56  	ExtendCertified(ctx context.Context, candidate *flow.Block, qc *flow.QuorumCertificate) error
    57  
    58  	// Finalize finalizes the block with the given hash.
    59  	// At this level, we can only finalize one block at a time. This implies
    60  	// that the parent of the pending block that is to be finalized has
    61  	// to be the last finalized block.
    62  	// It modifies the persistent immutable protocol state accordingly and
    63  	// forwards the pointer to the latest finalized state.
    64  	// No errors are expected during normal operations.
    65  	Finalize(ctx context.Context, blockID flow.Identifier) error
    66  }
    67  
    68  // ParticipantState is a mutable protocol state used by active consensus participants (consensus nodes).
    69  // All blocks are validated in full, including payload validation, prior to insertion. Only valid blocks are inserted.
    70  type ParticipantState interface {
    71  	FollowerState
    72  
    73  	// Extend introduces the block with the given ID into the persistent
    74  	// protocol state without modifying the current finalized state. It allows
    75  	// us to execute fork-aware queries against ambiguous protocol state, while
    76  	// still checking that the given block is a valid extension of the protocol state.
    77  	// The candidate block must have passed HotStuff validation before being passed to Extend.
    78  	// Expected errors during normal operations:
    79  	//  * state.OutdatedExtensionError if the candidate block is outdated (e.g. orphaned)
    80  	//  * state.InvalidExtensionError if the candidate block is invalid
    81  	Extend(ctx context.Context, candidate *flow.Block) error
    82  }