github.com/onflow/flow-go@v0.33.17/state/protocol/snapshot.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package protocol
     4  
     5  import (
     6  	"github.com/onflow/flow-go/model/flow"
     7  )
     8  
     9  // Snapshot represents an immutable snapshot of the protocol state
    10  // at a specific block, denoted as the Head block.
    11  // The Snapshot is fork-specific and only accounts for the information contained
    12  // in blocks along this fork up to (including) Head.
    13  // It allows us to read the parameters at the selected block in a deterministic manner.
    14  //
    15  // TODO Epoch / Snapshot API Structure:  Currently Epoch and Snapshot APIs
    16  // are structured to allow chained queries to be used without error checking
    17  // at each call where errors might occur. Instead, errors are cached in the
    18  // resulting struct (eg. invalid.Epoch) until the query chain ends with a
    19  // function which can return an error. This has some negative effects:
    20  //  1. Cached intermediary errors result in more complex error handling
    21  //     a) each final call of the chained query needs to handle all intermediary errors, every time
    22  //     b) intermediary errors must be handled by dependencies on the final call of the query chain (eg. conversion functions)
    23  //  2. The error caching pattern encourages potentially dangerous snapshot query patterns
    24  //
    25  // See https://github.com/dapperlabs/flow-go/issues/6368 for details and proposal
    26  //
    27  // A snapshot with an unknown reference block will return state.ErrUnknownSnapshotReference for all methods.
    28  //
    29  // TODO document error returns
    30  type Snapshot interface {
    31  
    32  	// Head returns the latest block at the selected point of the protocol state
    33  	// history. It can represent either a finalized or ambiguous block,
    34  	// depending on our selection criteria. Either way, it's the block on which
    35  	// we should build the next block in the context of the selected state.
    36  	// TODO document error returns
    37  	Head() (*flow.Header, error)
    38  
    39  	// QuorumCertificate returns a valid quorum certificate for the header at
    40  	// this snapshot, if one exists.
    41  	// Expected error returns:
    42  	//   - storage.ErrNotFound is returned if the QC is unknown.
    43  	//   - state.ErrUnknownSnapshotReference if the snapshot reference block is unknown
    44  	// All other errors should be treated as exceptions.
    45  	QuorumCertificate() (*flow.QuorumCertificate, error)
    46  
    47  	// Identities returns a list of identities at the selected point of the
    48  	// protocol state history. At the beginning of an epoch, this list includes
    49  	// identities from the previous epoch that are un-staking during the current
    50  	// epoch. At the end of an epoch, this includes identities scheduled to join
    51  	// in the next epoch but are not active yet.
    52  	//
    53  	// Identities are guaranteed to be returned in canonical order (flow.Canonical).
    54  	//
    55  	// It allows us to provide optional upfront filters which can be used by the
    56  	// implementation to speed up database lookups.
    57  	// TODO document error returns
    58  	Identities(selector flow.IdentityFilter) (flow.IdentityList, error)
    59  
    60  	// Identity attempts to retrieve the node with the given identifier at the
    61  	// selected point of the protocol state history. It will error if it doesn't exist.
    62  	// TODO document error returns
    63  	Identity(nodeID flow.Identifier) (*flow.Identity, error)
    64  
    65  	// SealedResult returns the most recent included seal as of this block and
    66  	// the corresponding execution result. The seal may have been included in a
    67  	// parent block, if this block is empty. If this block contains multiple
    68  	// seals, this returns the seal for the block with the greatest height.
    69  	// TODO document error returns
    70  	SealedResult() (*flow.ExecutionResult, *flow.Seal, error)
    71  
    72  	// Commit returns the state commitment of the most recently included seal
    73  	// as of this block. It represents the sealed state.
    74  	// TODO document error returns
    75  	Commit() (flow.StateCommitment, error)
    76  
    77  	// SealingSegment returns the chain segment such that the highest block
    78  	// is this snapshot's reference block and the lowest block
    79  	// is the most recently sealed block as of this snapshot (ie. the block
    80  	// referenced by LatestSeal). The segment is in ascending height order.
    81  	//
    82  	// TAIL <- B1 <- ... <- BN <- HEAD
    83  	//
    84  	// NOTE 1: TAIL is not always sealed by HEAD. In the case that the head of
    85  	// the snapshot contains no seals, TAIL must be sealed by the first ancestor
    86  	// of HEAD which contains any seal.
    87  	//
    88  	// NOTE 2: In the special case of a root snapshot generated for a spork,
    89  	// the sealing segment has exactly one block (the root block for the spork).
    90  	// For all other snapshots, the sealing segment contains at least 2 blocks.
    91  	//
    92  	// NOTE 3: It is often the case that a block inside the segment will contain
    93  	// an execution receipt in its payload that references an execution result
    94  	// missing from the payload. These missing execution results are stored on the
    95  	// flow.SealingSegment.ExecutionResults field.
    96  	// Expected errors during normal operations:
    97  	//  - protocol.ErrSealingSegmentBelowRootBlock if sealing segment would stretch beyond the node's local history cut-off
    98  	//  - protocol.UnfinalizedSealingSegmentError if sealing segment would contain unfinalized blocks (including orphaned blocks)
    99  	//  - state.ErrUnknownSnapshotReference if the snapshot reference block is unknown
   100  	SealingSegment() (*flow.SealingSegment, error)
   101  
   102  	// Descendants returns the IDs of all descendants of the Head block.
   103  	// The IDs are ordered such that parents are included before their children.
   104  	// Since all blocks are fully validated before being inserted to the state,
   105  	// all returned blocks are validated.
   106  	// No errors are expected under normal operation.
   107  	Descendants() ([]flow.Identifier, error)
   108  
   109  	// RandomSource returns the source of randomness _for_ the snapshot's Head block.
   110  	// Note that the source of randomness for a block `H`, is contained in the
   111  	// QuorumCertificate [QC] for block `H` (QCs for H are distributed as part of child
   112  	// blocks, timeout messages or timeout certificates). While there might be different
   113  	// QCs for block H, they all yield exactly the same source of randomness (feature of
   114  	// threshold signatures used here). Therefore, it is a possibility that there is no
   115  	// QC known (yet) for the head block.
   116  	// NOTE: not to be confused with the epoch source of randomness!
   117  	// Expected error returns:
   118  	//  - storage.ErrNotFound is returned if the QC is unknown.
   119  	//  - state.ErrUnknownSnapshotReference if the snapshot reference block is unknown
   120  	// All other errors should be treated as exceptions.
   121  	RandomSource() ([]byte, error)
   122  
   123  	// Phase returns the epoch phase for the current epoch, as of the Head block.
   124  	// TODO document error returns
   125  	Phase() (flow.EpochPhase, error)
   126  
   127  	// Epochs returns a query object enabling querying detailed information about
   128  	// various epochs.
   129  	//
   130  	// For epochs that are in the future w.r.t. the Head block, some of Epoch's
   131  	// methods may return errors, since the Epoch Preparation Protocol may be
   132  	// in-progress and incomplete for the epoch.
   133  	// Returns invalid.Epoch with state.ErrUnknownSnapshotReference if snapshot reference block is unknown.
   134  	Epochs() EpochQuery
   135  
   136  	// Params returns global parameters of the state this snapshot is taken from.
   137  	// Returns invalid.Params with state.ErrUnknownSnapshotReference if snapshot reference block is unknown.
   138  	Params() GlobalParams
   139  
   140  	// VersionBeacon returns the latest sealed version beacon.
   141  	// If no version beacon has been sealed so far during the current spork, returns nil.
   142  	// The latest VersionBeacon is only updated for finalized blocks. This means that, when
   143  	// querying an un-finalized fork, `VersionBeacon` will have the same value as querying
   144  	// the snapshot for the latest finalized block, even if a newer version beacon is included
   145  	// in a seal along the un-finalized fork.
   146  	VersionBeacon() (*flow.SealedVersionBeacon, error)
   147  }