github.com/onflow/flow-go@v0.33.17/network/p2p/inspector/validation/errors.go (about)

     1  package validation
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/onflow/flow-go/network/channels"
     8  	p2pmsg "github.com/onflow/flow-go/network/p2p/message"
     9  )
    10  
    11  // IWantDuplicateMsgIDThresholdErr indicates that the amount of duplicate message ids exceeds the allowed threshold.
    12  type IWantDuplicateMsgIDThresholdErr struct {
    13  	duplicates int
    14  	sampleSize uint
    15  	threshold  int
    16  }
    17  
    18  func (e IWantDuplicateMsgIDThresholdErr) Error() string {
    19  	return fmt.Sprintf("%d/%d iWant duplicate message ids exceeds the allowed threshold: %d", e.duplicates, e.sampleSize, e.threshold)
    20  }
    21  
    22  // NewIWantDuplicateMsgIDThresholdErr returns a new IWantDuplicateMsgIDThresholdErr.
    23  func NewIWantDuplicateMsgIDThresholdErr(duplicates int, sampleSize uint, threshold int) IWantDuplicateMsgIDThresholdErr {
    24  	return IWantDuplicateMsgIDThresholdErr{duplicates, sampleSize, threshold}
    25  }
    26  
    27  // IsIWantDuplicateMsgIDThresholdErr returns true if an error is IWantDuplicateMsgIDThresholdErr
    28  func IsIWantDuplicateMsgIDThresholdErr(err error) bool {
    29  	var e IWantDuplicateMsgIDThresholdErr
    30  	return errors.As(err, &e)
    31  }
    32  
    33  // IWantCacheMissThresholdErr indicates that the amount of cache misses exceeds the allowed threshold.
    34  type IWantCacheMissThresholdErr struct {
    35  	cacheMissCount int // total iwant cache misses
    36  	sampleSize     uint
    37  	threshold      int
    38  }
    39  
    40  func (e IWantCacheMissThresholdErr) Error() string {
    41  	return fmt.Sprintf("%d/%d iWant cache misses exceeds the allowed threshold: %d", e.cacheMissCount, e.sampleSize, e.threshold)
    42  }
    43  
    44  // NewIWantCacheMissThresholdErr returns a new IWantCacheMissThresholdErr.
    45  func NewIWantCacheMissThresholdErr(cacheMissCount int, sampleSize uint, threshold int) IWantCacheMissThresholdErr {
    46  	return IWantCacheMissThresholdErr{cacheMissCount, sampleSize, threshold}
    47  }
    48  
    49  // IsIWantCacheMissThresholdErr returns true if an error is IWantCacheMissThresholdErr
    50  func IsIWantCacheMissThresholdErr(err error) bool {
    51  	var e IWantCacheMissThresholdErr
    52  	return errors.As(err, &e)
    53  }
    54  
    55  // DuplicateTopicErr error that indicates a duplicate has been detected. This can be duplicate topic or message ID tracking.
    56  type DuplicateTopicErr struct {
    57  	topic   string                    // the topic that is duplicated
    58  	count   int                       // the number of times the topic has been duplicated
    59  	msgType p2pmsg.ControlMessageType // the control message type that the topic was found in
    60  }
    61  
    62  func (e DuplicateTopicErr) Error() string {
    63  	return fmt.Sprintf("duplicate topic found in %s control message type: %s", e.msgType, e.topic)
    64  }
    65  
    66  // NewDuplicateTopicErr returns a new DuplicateTopicErr.
    67  // Args:
    68  //
    69  //	topic: the topic that is duplicated
    70  //	count: the number of times the topic has been duplicated
    71  //	msgType: the control message type that the topic was found in
    72  //
    73  // Returns:
    74  //
    75  //	A new DuplicateTopicErr.
    76  func NewDuplicateTopicErr(topic string, count int, msgType p2pmsg.ControlMessageType) DuplicateTopicErr {
    77  
    78  	return DuplicateTopicErr{topic, count, msgType}
    79  }
    80  
    81  // IsDuplicateTopicErr returns true if an error is DuplicateTopicErr.
    82  func IsDuplicateTopicErr(err error) bool {
    83  	var e DuplicateTopicErr
    84  	return errors.As(err, &e)
    85  }
    86  
    87  // DuplicateMessageIDErr error that indicates a duplicate message ID has been detected in a IHAVE or IWANT control message.
    88  type DuplicateMessageIDErr struct {
    89  	id      string                    // id of the message that is duplicated
    90  	count   int                       // the number of times the message ID has been duplicated
    91  	msgType p2pmsg.ControlMessageType // the control message type that the message ID was found in
    92  }
    93  
    94  func (e DuplicateMessageIDErr) Error() string {
    95  	return fmt.Sprintf("duplicate message ID foud in %s control message type: %s", e.msgType, e.id)
    96  }
    97  
    98  // NewDuplicateMessageIDErr returns a new DuplicateMessageIDErr.
    99  // Args:
   100  //
   101  //	id: id of the message that is duplicated
   102  //	count: the number of times the message ID has been duplicated
   103  //	msgType: the control message type that the message ID was found in.
   104  func NewDuplicateMessageIDErr(id string, count int, msgType p2pmsg.ControlMessageType) DuplicateMessageIDErr {
   105  	return DuplicateMessageIDErr{id, count, msgType}
   106  }
   107  
   108  // IsDuplicateMessageIDErr returns true if an error is DuplicateMessageIDErr.
   109  func IsDuplicateMessageIDErr(err error) bool {
   110  	var e DuplicateMessageIDErr
   111  	return errors.As(err, &e)
   112  }
   113  
   114  // ErrActiveClusterIdsNotSet error that indicates a cluster prefixed control message has been received but the cluster IDs have not been set yet.
   115  type ErrActiveClusterIdsNotSet struct {
   116  	topic channels.Topic
   117  }
   118  
   119  func (e ErrActiveClusterIdsNotSet) Error() string {
   120  	return fmt.Errorf("failed to validate cluster prefixed topic %s no active cluster IDs set", e.topic).Error()
   121  }
   122  
   123  // NewActiveClusterIdsNotSetErr returns a new ErrActiveClusterIdsNotSet.
   124  func NewActiveClusterIdsNotSetErr(topic channels.Topic) ErrActiveClusterIdsNotSet {
   125  	return ErrActiveClusterIdsNotSet{topic: topic}
   126  }
   127  
   128  // IsErrActiveClusterIDsNotSet returns true if an error is ErrActiveClusterIdsNotSet.
   129  func IsErrActiveClusterIDsNotSet(err error) bool {
   130  	var e ErrActiveClusterIdsNotSet
   131  	return errors.As(err, &e)
   132  }
   133  
   134  // ErrUnstakedPeer error that indicates a cluster prefixed control message has been from an unstaked peer.
   135  type ErrUnstakedPeer struct {
   136  	err error
   137  }
   138  
   139  func (e ErrUnstakedPeer) Error() string {
   140  	return e.err.Error()
   141  }
   142  
   143  // NewUnstakedPeerErr returns a new ErrUnstakedPeer.
   144  func NewUnstakedPeerErr(err error) ErrUnstakedPeer {
   145  	return ErrUnstakedPeer{err: err}
   146  }
   147  
   148  // IsErrUnstakedPeer returns true if an error is ErrUnstakedPeer.
   149  func IsErrUnstakedPeer(err error) bool {
   150  	var e ErrUnstakedPeer
   151  	return errors.As(err, &e)
   152  }
   153  
   154  // InvalidRpcPublishMessagesErr error indicates that rpc publish message validation failed.
   155  type InvalidRpcPublishMessagesErr struct {
   156  	// err the original error returned by the calling func.
   157  	err error
   158  	// count the number of times this err was encountered.
   159  	count int
   160  }
   161  
   162  func (e InvalidRpcPublishMessagesErr) Error() string {
   163  	return fmt.Errorf("rpc publish messages validation failed %d error(s) encountered: %w", e.count, e.err).Error()
   164  }
   165  
   166  // NewInvalidRpcPublishMessagesErr returns a new InvalidRpcPublishMessagesErr.
   167  func NewInvalidRpcPublishMessagesErr(err error, count int) InvalidRpcPublishMessagesErr {
   168  	return InvalidRpcPublishMessagesErr{err: err, count: count}
   169  }
   170  
   171  // IsInvalidRpcPublishMessagesErr returns true if an error is InvalidRpcPublishMessagesErr.
   172  func IsInvalidRpcPublishMessagesErr(err error) bool {
   173  	var e InvalidRpcPublishMessagesErr
   174  	return errors.As(err, &e)
   175  }