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

     1  package invalid
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/onflow/flow-go/model/flow"
     8  	"github.com/onflow/flow-go/state"
     9  	"github.com/onflow/flow-go/state/protocol"
    10  )
    11  
    12  // Epoch represents an epoch that does not exist or could not be retrieved.
    13  type Epoch struct {
    14  	err error
    15  }
    16  
    17  // NewEpoch returns a new invalid epoch, containing an error describing why the
    18  // epoch could not be retrieved. The following are expected errors when constructing
    19  // an invalid Epoch:
    20  //   - protocol.ErrNoPreviousEpoch - if the epoch represents a previous epoch which does not exist.
    21  //     This happens when the previous epoch is queried within the first epoch of a spork.
    22  //   - protocol.ErrNextEpochNotSetup - if the epoch represents a next epoch which has not been set up.
    23  //     This happens when the next epoch is queried within the EpochStaking phase of any epoch.
    24  //   - state.ErrUnknownSnapshotReference - if the epoch is queried from an unresolvable snapshot.
    25  //   - generic error in case of unexpected critical internal corruption or bugs
    26  func NewEpoch(err error) *Epoch {
    27  	if errors.Is(err, protocol.ErrNoPreviousEpoch) {
    28  		return &Epoch{err: err}
    29  	}
    30  	if errors.Is(err, protocol.ErrNextEpochNotSetup) {
    31  		return &Epoch{err: err}
    32  	}
    33  	if errors.Is(err, state.ErrUnknownSnapshotReference) {
    34  		return &Epoch{err: err}
    35  	}
    36  	return &Epoch{err: fmt.Errorf("critical unexpected error querying epoch: %w", err)}
    37  }
    38  
    39  // NewEpochf is NewEpoch with ergonomic error formatting.
    40  func NewEpochf(msg string, args ...interface{}) *Epoch {
    41  	return NewEpoch(fmt.Errorf(msg, args...))
    42  }
    43  
    44  func (u *Epoch) Counter() (uint64, error) {
    45  	return 0, u.err
    46  }
    47  
    48  func (u *Epoch) FirstView() (uint64, error) {
    49  	return 0, u.err
    50  }
    51  
    52  func (u *Epoch) FinalView() (uint64, error) {
    53  	return 0, u.err
    54  }
    55  
    56  func (u *Epoch) DKGPhase1FinalView() (uint64, error) {
    57  	return 0, u.err
    58  }
    59  
    60  func (u *Epoch) DKGPhase2FinalView() (uint64, error) {
    61  	return 0, u.err
    62  }
    63  
    64  func (u *Epoch) DKGPhase3FinalView() (uint64, error) {
    65  	return 0, u.err
    66  }
    67  
    68  func (u *Epoch) InitialIdentities() (flow.IdentitySkeletonList, error) {
    69  	return nil, u.err
    70  }
    71  
    72  func (u *Epoch) Clustering() (flow.ClusterList, error) {
    73  	return nil, u.err
    74  }
    75  
    76  func (u *Epoch) Cluster(uint) (protocol.Cluster, error) {
    77  	return nil, u.err
    78  }
    79  
    80  func (u *Epoch) ClusterByChainID(chainID flow.ChainID) (protocol.Cluster, error) {
    81  	return nil, u.err
    82  }
    83  
    84  func (u *Epoch) DKG() (protocol.DKG, error) {
    85  	return nil, u.err
    86  }
    87  
    88  func (u *Epoch) RandomSource() ([]byte, error) {
    89  	return nil, u.err
    90  }
    91  
    92  func (u *Epoch) TargetDuration() (uint64, error) {
    93  	return 0, u.err
    94  }
    95  
    96  func (u *Epoch) TargetEndTime() (uint64, error) {
    97  	return 0, u.err
    98  }
    99  
   100  func (u *Epoch) FirstHeight() (uint64, error) {
   101  	return 0, u.err
   102  }
   103  
   104  func (u *Epoch) FinalHeight() (uint64, error) {
   105  	return 0, u.err
   106  }
   107  
   108  // Epochs is an epoch query for an invalid snapshot.
   109  type Epochs struct {
   110  	err error
   111  }
   112  
   113  func (u *Snapshot) Epochs() protocol.EpochQuery {
   114  	return &Epochs{err: u.err}
   115  }
   116  
   117  func (u *Epochs) Current() protocol.Epoch {
   118  	return NewEpoch(u.err)
   119  }
   120  
   121  func (u *Epochs) Next() protocol.Epoch {
   122  	return NewEpoch(u.err)
   123  }
   124  
   125  func (u *Epochs) Previous() protocol.Epoch {
   126  	return NewEpoch(u.err)
   127  }