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

     1  package model
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/onflow/flow-go/model/flow"
     8  )
     9  
    10  var (
    11  	ErrUnverifiableBlock = errors.New("block proposal can't be verified, because its view is above the finalized view, but its QC is below the finalized view")
    12  	ErrInvalidSignature  = errors.New("invalid signature")
    13  	// ErrViewForUnknownEpoch is returned when Epoch information is queried for a view that is
    14  	// outside of all cached epochs. This can happen when a query is made for a view in the
    15  	// next epoch, if that epoch is not committed yet. This can also happen when an
    16  	// old epoch is queried (>3 in the past), even if that epoch does exist in storage.
    17  	ErrViewForUnknownEpoch = fmt.Errorf("by-view query for unknown epoch")
    18  )
    19  
    20  // NoVoteError contains the reason why hotstuff.SafetyRules refused to generate a `Vote` for the current view.
    21  type NoVoteError struct {
    22  	Err error
    23  }
    24  
    25  func (e NoVoteError) Error() string { return fmt.Sprintf("not voting - %s", e.Err.Error()) }
    26  
    27  func (e NoVoteError) Unwrap() error {
    28  	return e.Err
    29  }
    30  
    31  // IsNoVoteError returns whether an error is NoVoteError
    32  func IsNoVoteError(err error) bool {
    33  	var e NoVoteError
    34  	return errors.As(err, &e)
    35  }
    36  
    37  func NewNoVoteErrorf(msg string, args ...interface{}) error {
    38  	return NoVoteError{Err: fmt.Errorf(msg, args...)}
    39  }
    40  
    41  // NoTimeoutError contains the reason why hotstuff.SafetyRules refused to generate a `TimeoutObject` [TO] for the current view.
    42  type NoTimeoutError struct {
    43  	Err error
    44  }
    45  
    46  func (e NoTimeoutError) Error() string {
    47  	return fmt.Sprintf("conditions not satisfied to generate valid TimeoutObject: %s", e.Err.Error())
    48  }
    49  
    50  func (e NoTimeoutError) Unwrap() error {
    51  	return e.Err
    52  }
    53  
    54  // IsNoTimeoutError returns whether an error is NoTimeoutError
    55  func IsNoTimeoutError(err error) bool {
    56  	var e NoTimeoutError
    57  	return errors.As(err, &e)
    58  }
    59  
    60  func NewNoTimeoutErrorf(msg string, args ...interface{}) error {
    61  	return NoTimeoutError{Err: fmt.Errorf(msg, args...)}
    62  }
    63  
    64  // InvalidFormatError indicates that some data has an incompatible format.
    65  type InvalidFormatError struct {
    66  	err error
    67  }
    68  
    69  func NewInvalidFormatError(err error) error {
    70  	return InvalidFormatError{err}
    71  }
    72  
    73  func NewInvalidFormatErrorf(msg string, args ...interface{}) error {
    74  	return InvalidFormatError{fmt.Errorf(msg, args...)}
    75  }
    76  
    77  func (e InvalidFormatError) Error() string { return e.err.Error() }
    78  func (e InvalidFormatError) Unwrap() error { return e.err }
    79  
    80  // IsInvalidFormatError returns whether err is a InvalidFormatError
    81  func IsInvalidFormatError(err error) bool {
    82  	var e InvalidFormatError
    83  	return errors.As(err, &e)
    84  }
    85  
    86  // ConfigurationError indicates that a constructor or component was initialized with
    87  // invalid or inconsistent parameters.
    88  type ConfigurationError struct {
    89  	err error
    90  }
    91  
    92  func NewConfigurationError(err error) error {
    93  	return ConfigurationError{err}
    94  }
    95  
    96  func NewConfigurationErrorf(msg string, args ...interface{}) error {
    97  	return ConfigurationError{fmt.Errorf(msg, args...)}
    98  }
    99  
   100  func (e ConfigurationError) Error() string { return e.err.Error() }
   101  func (e ConfigurationError) Unwrap() error { return e.err }
   102  
   103  // IsConfigurationError returns whether err is a ConfigurationError
   104  func IsConfigurationError(err error) bool {
   105  	var e ConfigurationError
   106  	return errors.As(err, &e)
   107  }
   108  
   109  // MissingBlockError indicates that no block with identifier `BlockID` is known
   110  type MissingBlockError struct {
   111  	View    uint64
   112  	BlockID flow.Identifier
   113  }
   114  
   115  func (e MissingBlockError) Error() string {
   116  	return fmt.Sprintf("missing Proposal at view %d with ID %v", e.View, e.BlockID)
   117  }
   118  
   119  // IsMissingBlockError returns whether an error is MissingBlockError
   120  func IsMissingBlockError(err error) bool {
   121  	var e MissingBlockError
   122  	return errors.As(err, &e)
   123  }
   124  
   125  // InvalidQCError indicates that the QC for block identified by `BlockID` and `View` is invalid
   126  type InvalidQCError struct {
   127  	BlockID flow.Identifier
   128  	View    uint64
   129  	Err     error
   130  }
   131  
   132  func (e InvalidQCError) Error() string {
   133  	return fmt.Sprintf("invalid QC for block %x at view %d: %s", e.BlockID, e.View, e.Err.Error())
   134  }
   135  
   136  // IsInvalidQCError returns whether an error is InvalidQCError
   137  func IsInvalidQCError(err error) bool {
   138  	var e InvalidQCError
   139  	return errors.As(err, &e)
   140  }
   141  
   142  func (e InvalidQCError) Unwrap() error {
   143  	return e.Err
   144  }
   145  
   146  // InvalidTCError indicates that the TC for view identified by `View` is invalid
   147  type InvalidTCError struct {
   148  	View uint64
   149  	Err  error
   150  }
   151  
   152  func (e InvalidTCError) Error() string {
   153  	return fmt.Sprintf("invalid TC at view %d: %s", e.View, e.Err.Error())
   154  }
   155  
   156  // IsInvalidTCError returns whether an error is InvalidQCError
   157  func IsInvalidTCError(err error) bool {
   158  	var e InvalidTCError
   159  	return errors.As(err, &e)
   160  }
   161  
   162  func (e InvalidTCError) Unwrap() error {
   163  	return e.Err
   164  }
   165  
   166  // InvalidProposalError indicates that the proposal is invalid
   167  type InvalidProposalError struct {
   168  	InvalidProposal *Proposal
   169  	Err             error
   170  }
   171  
   172  func NewInvalidProposalErrorf(proposal *Proposal, msg string, args ...interface{}) error {
   173  	return InvalidProposalError{
   174  		InvalidProposal: proposal,
   175  		Err:             fmt.Errorf(msg, args...),
   176  	}
   177  }
   178  
   179  func (e InvalidProposalError) Error() string {
   180  	return fmt.Sprintf(
   181  		"invalid proposal %x at view %d: %s",
   182  		e.InvalidProposal.Block.BlockID,
   183  		e.InvalidProposal.Block.View,
   184  		e.Err.Error(),
   185  	)
   186  }
   187  
   188  func (e InvalidProposalError) Unwrap() error {
   189  	return e.Err
   190  }
   191  
   192  // IsInvalidProposalError returns whether an error is InvalidProposalError
   193  func IsInvalidProposalError(err error) bool {
   194  	var e InvalidProposalError
   195  	return errors.As(err, &e)
   196  }
   197  
   198  // AsInvalidProposalError determines whether the given error is a InvalidProposalError
   199  // (potentially wrapped). It follows the same semantics as a checked type cast.
   200  func AsInvalidProposalError(err error) (*InvalidProposalError, bool) {
   201  	var e InvalidProposalError
   202  	ok := errors.As(err, &e)
   203  	if ok {
   204  		return &e, true
   205  	}
   206  	return nil, false
   207  }
   208  
   209  // InvalidBlockError indicates that the block is invalid
   210  type InvalidBlockError struct {
   211  	InvalidBlock *Block
   212  	Err          error
   213  }
   214  
   215  func NewInvalidBlockErrorf(block *Block, msg string, args ...interface{}) error {
   216  	return InvalidBlockError{
   217  		InvalidBlock: block,
   218  		Err:          fmt.Errorf(msg, args...),
   219  	}
   220  }
   221  
   222  func (e InvalidBlockError) Error() string {
   223  	return fmt.Sprintf(
   224  		"invalid block %x at view %d: %s",
   225  		e.InvalidBlock.BlockID,
   226  		e.InvalidBlock.View,
   227  		e.Err.Error(),
   228  	)
   229  }
   230  
   231  // IsInvalidBlockError returns whether an error is InvalidBlockError
   232  func IsInvalidBlockError(err error) bool {
   233  	var e InvalidBlockError
   234  	return errors.As(err, &e)
   235  }
   236  
   237  // AsInvalidBlockError determines whether the given error is a InvalidProposalError
   238  // (potentially wrapped). It follows the same semantics as a checked type cast.
   239  func AsInvalidBlockError(err error) (*InvalidBlockError, bool) {
   240  	var e InvalidBlockError
   241  	ok := errors.As(err, &e)
   242  	if ok {
   243  		return &e, true
   244  	}
   245  	return nil, false
   246  }
   247  
   248  func (e InvalidBlockError) Unwrap() error {
   249  	return e.Err
   250  }
   251  
   252  // InvalidVoteError indicates that the vote with identifier `VoteID` is invalid
   253  type InvalidVoteError struct {
   254  	Vote *Vote
   255  	Err  error
   256  }
   257  
   258  func NewInvalidVoteErrorf(vote *Vote, msg string, args ...interface{}) error {
   259  	return InvalidVoteError{
   260  		Vote: vote,
   261  		Err:  fmt.Errorf(msg, args...),
   262  	}
   263  }
   264  
   265  func (e InvalidVoteError) Error() string {
   266  	return fmt.Sprintf("invalid vote at view %d for block %x: %s", e.Vote.View, e.Vote.BlockID, e.Err.Error())
   267  }
   268  
   269  // IsInvalidVoteError returns whether an error is InvalidVoteError
   270  func IsInvalidVoteError(err error) bool {
   271  	var e InvalidVoteError
   272  	return errors.As(err, &e)
   273  }
   274  
   275  // AsInvalidVoteError determines whether the given error is a InvalidVoteError
   276  // (potentially wrapped). It follows the same semantics as a checked type cast.
   277  func AsInvalidVoteError(err error) (*InvalidVoteError, bool) {
   278  	var e InvalidVoteError
   279  	ok := errors.As(err, &e)
   280  	if ok {
   281  		return &e, true
   282  	}
   283  	return nil, false
   284  }
   285  
   286  func (e InvalidVoteError) Unwrap() error {
   287  	return e.Err
   288  }
   289  
   290  // ByzantineThresholdExceededError is raised if HotStuff detects malicious conditions, which
   291  // prove that the Byzantine threshold of consensus replicas has been exceeded. Per definition,
   292  // this is the case when there are byzantine consensus replicas with ≥ 1/3 of the committee's
   293  // total weight. In this scenario, foundational consensus safety guarantees fail.
   294  // Generally, the protocol cannot continue in such conditions.
   295  // We represent this exception as with a dedicated type, so its occurrence can be detected by
   296  // higher-level logic and escalated to the node operator.
   297  type ByzantineThresholdExceededError struct {
   298  	Evidence string
   299  }
   300  
   301  func (e ByzantineThresholdExceededError) Error() string {
   302  	return e.Evidence
   303  }
   304  
   305  func IsByzantineThresholdExceededError(err error) bool {
   306  	var target ByzantineThresholdExceededError
   307  	return errors.As(err, &target)
   308  }
   309  
   310  // DoubleVoteError indicates that a consensus replica has voted for two different
   311  // blocks, or has provided two semantically different votes for the same block.
   312  type DoubleVoteError struct {
   313  	FirstVote       *Vote
   314  	ConflictingVote *Vote
   315  	err             error
   316  }
   317  
   318  func (e DoubleVoteError) Error() string {
   319  	return e.err.Error()
   320  }
   321  
   322  // IsDoubleVoteError returns whether an error is DoubleVoteError
   323  func IsDoubleVoteError(err error) bool {
   324  	var e DoubleVoteError
   325  	return errors.As(err, &e)
   326  }
   327  
   328  // AsDoubleVoteError determines whether the given error is a DoubleVoteError
   329  // (potentially wrapped). It follows the same semantics as a checked type cast.
   330  func AsDoubleVoteError(err error) (*DoubleVoteError, bool) {
   331  	var e DoubleVoteError
   332  	ok := errors.As(err, &e)
   333  	if ok {
   334  		return &e, true
   335  	}
   336  	return nil, false
   337  }
   338  
   339  func (e DoubleVoteError) Unwrap() error {
   340  	return e.err
   341  }
   342  
   343  func NewDoubleVoteErrorf(firstVote, conflictingVote *Vote, msg string, args ...interface{}) error {
   344  	return DoubleVoteError{
   345  		FirstVote:       firstVote,
   346  		ConflictingVote: conflictingVote,
   347  		err:             fmt.Errorf(msg, args...),
   348  	}
   349  }
   350  
   351  // DuplicatedSignerError indicates that a signature from the same node ID has already been added
   352  type DuplicatedSignerError struct {
   353  	err error
   354  }
   355  
   356  func NewDuplicatedSignerError(err error) error {
   357  	return DuplicatedSignerError{err}
   358  }
   359  
   360  func NewDuplicatedSignerErrorf(msg string, args ...interface{}) error {
   361  	return DuplicatedSignerError{err: fmt.Errorf(msg, args...)}
   362  }
   363  
   364  func (e DuplicatedSignerError) Error() string { return e.err.Error() }
   365  func (e DuplicatedSignerError) Unwrap() error { return e.err }
   366  
   367  // IsDuplicatedSignerError returns whether err is an DuplicatedSignerError
   368  func IsDuplicatedSignerError(err error) bool {
   369  	var e DuplicatedSignerError
   370  	return errors.As(err, &e)
   371  }
   372  
   373  // InvalidSignatureIncludedError indicates that some signatures, included via TrustedAdd, are invalid
   374  type InvalidSignatureIncludedError struct {
   375  	err error
   376  }
   377  
   378  func NewInvalidSignatureIncludedError(err error) error {
   379  	return InvalidSignatureIncludedError{err}
   380  }
   381  
   382  func NewInvalidSignatureIncludedErrorf(msg string, args ...interface{}) error {
   383  	return InvalidSignatureIncludedError{fmt.Errorf(msg, args...)}
   384  }
   385  
   386  func (e InvalidSignatureIncludedError) Error() string { return e.err.Error() }
   387  func (e InvalidSignatureIncludedError) Unwrap() error { return e.err }
   388  
   389  // IsInvalidSignatureIncludedError returns whether err is an InvalidSignatureIncludedError
   390  func IsInvalidSignatureIncludedError(err error) bool {
   391  	var e InvalidSignatureIncludedError
   392  	return errors.As(err, &e)
   393  }
   394  
   395  // InvalidAggregatedKeyError indicates that the aggregated key is invalid
   396  // which makes any aggregated signature invalid.
   397  type InvalidAggregatedKeyError struct {
   398  	error
   399  }
   400  
   401  func NewInvalidAggregatedKeyError(err error) error {
   402  	return InvalidAggregatedKeyError{err}
   403  }
   404  
   405  func NewInvalidAggregatedKeyErrorf(msg string, args ...interface{}) error {
   406  	return InvalidAggregatedKeyError{fmt.Errorf(msg, args...)}
   407  }
   408  
   409  func (e InvalidAggregatedKeyError) Unwrap() error { return e.error }
   410  
   411  // IsInvalidAggregatedKeyError returns whether err is an InvalidAggregatedKeyError
   412  func IsInvalidAggregatedKeyError(err error) bool {
   413  	var e InvalidAggregatedKeyError
   414  	return errors.As(err, &e)
   415  }
   416  
   417  // InsufficientSignaturesError indicates that not enough signatures have been stored to complete the operation.
   418  type InsufficientSignaturesError struct {
   419  	err error
   420  }
   421  
   422  func NewInsufficientSignaturesError(err error) error {
   423  	return InsufficientSignaturesError{err}
   424  }
   425  
   426  func NewInsufficientSignaturesErrorf(msg string, args ...interface{}) error {
   427  	return InsufficientSignaturesError{fmt.Errorf(msg, args...)}
   428  }
   429  
   430  func (e InsufficientSignaturesError) Error() string { return e.err.Error() }
   431  func (e InsufficientSignaturesError) Unwrap() error { return e.err }
   432  
   433  // IsInsufficientSignaturesError returns whether err is an InsufficientSignaturesError
   434  func IsInsufficientSignaturesError(err error) bool {
   435  	var e InsufficientSignaturesError
   436  	return errors.As(err, &e)
   437  }
   438  
   439  // InvalidSignerError indicates that the signer is not authorized or unknown
   440  type InvalidSignerError struct {
   441  	err error
   442  }
   443  
   444  func NewInvalidSignerError(err error) error {
   445  	return InvalidSignerError{err}
   446  }
   447  
   448  func NewInvalidSignerErrorf(msg string, args ...interface{}) error {
   449  	return InvalidSignerError{fmt.Errorf(msg, args...)}
   450  }
   451  
   452  func (e InvalidSignerError) Error() string { return e.err.Error() }
   453  func (e InvalidSignerError) Unwrap() error { return e.err }
   454  
   455  // IsInvalidSignerError returns whether err is an InvalidSignerError
   456  func IsInvalidSignerError(err error) bool {
   457  	var e InvalidSignerError
   458  	return errors.As(err, &e)
   459  }
   460  
   461  // DoubleTimeoutError indicates that a consensus replica has created two different
   462  // timeout objects for same view.
   463  type DoubleTimeoutError struct {
   464  	FirstTimeout       *TimeoutObject
   465  	ConflictingTimeout *TimeoutObject
   466  	err                error
   467  }
   468  
   469  func (e DoubleTimeoutError) Error() string {
   470  	return e.err.Error()
   471  }
   472  
   473  // IsDoubleTimeoutError returns whether an error is DoubleTimeoutError
   474  func IsDoubleTimeoutError(err error) bool {
   475  	var e DoubleTimeoutError
   476  	return errors.As(err, &e)
   477  }
   478  
   479  // AsDoubleTimeoutError determines whether the given error is a DoubleTimeoutError
   480  // (potentially wrapped). It follows the same semantics as a checked type cast.
   481  func AsDoubleTimeoutError(err error) (*DoubleTimeoutError, bool) {
   482  	var e DoubleTimeoutError
   483  	ok := errors.As(err, &e)
   484  	if ok {
   485  		return &e, true
   486  	}
   487  	return nil, false
   488  }
   489  
   490  func (e DoubleTimeoutError) Unwrap() error {
   491  	return e.err
   492  }
   493  
   494  func NewDoubleTimeoutErrorf(firstTimeout, conflictingTimeout *TimeoutObject, msg string, args ...interface{}) error {
   495  	return DoubleTimeoutError{
   496  		FirstTimeout:       firstTimeout,
   497  		ConflictingTimeout: conflictingTimeout,
   498  		err:                fmt.Errorf(msg, args...),
   499  	}
   500  }
   501  
   502  // InvalidTimeoutError indicates that the embedded timeout object is invalid
   503  type InvalidTimeoutError struct {
   504  	Timeout *TimeoutObject
   505  	Err     error
   506  }
   507  
   508  func NewInvalidTimeoutErrorf(timeout *TimeoutObject, msg string, args ...interface{}) error {
   509  	return InvalidTimeoutError{
   510  		Timeout: timeout,
   511  		Err:     fmt.Errorf(msg, args...),
   512  	}
   513  }
   514  
   515  func (e InvalidTimeoutError) Error() string {
   516  	return fmt.Sprintf("invalid timeout %x for view %d: %s", e.Timeout.ID(), e.Timeout.View, e.Err.Error())
   517  }
   518  
   519  // IsInvalidTimeoutError returns whether an error is InvalidTimeoutError
   520  func IsInvalidTimeoutError(err error) bool {
   521  	var e InvalidTimeoutError
   522  	return errors.As(err, &e)
   523  }
   524  
   525  // AsInvalidTimeoutError determines whether the given error is a InvalidTimeoutError
   526  // (potentially wrapped). It follows the same semantics as a checked type cast.
   527  func AsInvalidTimeoutError(err error) (*InvalidTimeoutError, bool) {
   528  	var e InvalidTimeoutError
   529  	ok := errors.As(err, &e)
   530  	if ok {
   531  		return &e, true
   532  	}
   533  	return nil, false
   534  }
   535  
   536  func (e InvalidTimeoutError) Unwrap() error {
   537  	return e.Err
   538  }