github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/inspector/validation/errors.go (about)

     1  package validation
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  
     9  	"github.com/onflow/flow-go/network/channels"
    10  	p2pmsg "github.com/onflow/flow-go/network/p2p/message"
    11  )
    12  
    13  // IWantDuplicateMsgIDThresholdErr indicates that the amount of duplicate message ids exceeds the allowed threshold.
    14  type IWantDuplicateMsgIDThresholdErr struct {
    15  	duplicates int
    16  	sampleSize uint
    17  	threshold  int
    18  }
    19  
    20  func (e IWantDuplicateMsgIDThresholdErr) Error() string {
    21  	return fmt.Sprintf("%d/%d iWant duplicate message ids exceeds the allowed threshold: %d", e.duplicates, e.sampleSize, e.threshold)
    22  }
    23  
    24  // NewIWantDuplicateMsgIDThresholdErr returns a new IWantDuplicateMsgIDThresholdErr.
    25  func NewIWantDuplicateMsgIDThresholdErr(duplicates int, sampleSize uint, threshold int) IWantDuplicateMsgIDThresholdErr {
    26  	return IWantDuplicateMsgIDThresholdErr{duplicates, sampleSize, threshold}
    27  }
    28  
    29  // IsIWantDuplicateMsgIDThresholdErr returns true if an error is IWantDuplicateMsgIDThresholdErr
    30  func IsIWantDuplicateMsgIDThresholdErr(err error) bool {
    31  	var e IWantDuplicateMsgIDThresholdErr
    32  	return errors.As(err, &e)
    33  }
    34  
    35  // IWantCacheMissThresholdErr indicates that the amount of cache misses exceeds the allowed threshold.
    36  type IWantCacheMissThresholdErr struct {
    37  	cacheMissCount int // total iwant cache misses
    38  	sampleSize     uint
    39  	threshold      int
    40  }
    41  
    42  func (e IWantCacheMissThresholdErr) Error() string {
    43  	return fmt.Sprintf("%d/%d iWant cache misses exceeds the allowed threshold: %d", e.cacheMissCount, e.sampleSize, e.threshold)
    44  }
    45  
    46  // NewIWantCacheMissThresholdErr returns a new IWantCacheMissThresholdErr.
    47  func NewIWantCacheMissThresholdErr(cacheMissCount int, sampleSize uint, threshold int) IWantCacheMissThresholdErr {
    48  	return IWantCacheMissThresholdErr{cacheMissCount, sampleSize, threshold}
    49  }
    50  
    51  // IsIWantCacheMissThresholdErr returns true if an error is IWantCacheMissThresholdErr
    52  func IsIWantCacheMissThresholdErr(err error) bool {
    53  	var e IWantCacheMissThresholdErr
    54  	return errors.As(err, &e)
    55  }
    56  
    57  // DuplicateTopicErr error that indicates a duplicate has been detected. This can be duplicate topic or message ID tracking.
    58  type DuplicateTopicErr struct {
    59  	topic   string                    // the topic that is duplicated
    60  	count   int                       // the number of times the topic has been duplicated
    61  	msgType p2pmsg.ControlMessageType // the control message type that the topic was found in
    62  }
    63  
    64  func (e DuplicateTopicErr) Error() string {
    65  	return fmt.Sprintf("duplicate topic found in %s control message type: %s", e.msgType, e.topic)
    66  }
    67  
    68  // NewDuplicateTopicErr returns a new DuplicateTopicErr.
    69  // Args:
    70  //
    71  //	topic: the topic that is duplicated
    72  //	count: the number of times the topic has been duplicated
    73  //	msgType: the control message type that the topic was found in
    74  //
    75  // Returns:
    76  //
    77  //	A new DuplicateTopicErr.
    78  func NewDuplicateTopicErr(topic string, count int, msgType p2pmsg.ControlMessageType) DuplicateTopicErr {
    79  
    80  	return DuplicateTopicErr{topic, count, msgType}
    81  }
    82  
    83  // IsDuplicateTopicErr returns true if an error is DuplicateTopicErr.
    84  func IsDuplicateTopicErr(err error) bool {
    85  	var e DuplicateTopicErr
    86  	return errors.As(err, &e)
    87  }
    88  
    89  // DuplicateMessageIDErr error that indicates a duplicate message ID has been detected in a IHAVE or IWANT control message.
    90  type DuplicateMessageIDErr struct {
    91  	id      string                    // id of the message that is duplicated
    92  	count   int                       // the number of times the message ID has been duplicated
    93  	msgType p2pmsg.ControlMessageType // the control message type that the message ID was found in
    94  }
    95  
    96  func (e DuplicateMessageIDErr) Error() string {
    97  	return fmt.Sprintf("duplicate message ID foud in %s control message type: %s", e.msgType, e.id)
    98  }
    99  
   100  // NewDuplicateMessageIDErr returns a new DuplicateMessageIDErr.
   101  // Args:
   102  //
   103  //	id: id of the message that is duplicated
   104  //	count: the number of times the message ID has been duplicated
   105  //	msgType: the control message type that the message ID was found in.
   106  func NewDuplicateMessageIDErr(id string, count int, msgType p2pmsg.ControlMessageType) DuplicateMessageIDErr {
   107  	return DuplicateMessageIDErr{id, count, msgType}
   108  }
   109  
   110  // IsDuplicateMessageIDErr returns true if an error is DuplicateMessageIDErr.
   111  func IsDuplicateMessageIDErr(err error) bool {
   112  	var e DuplicateMessageIDErr
   113  	return errors.As(err, &e)
   114  }
   115  
   116  // ErrActiveClusterIdsNotSet error that indicates a cluster prefixed control message has been received but the cluster IDs have not been set yet.
   117  type ErrActiveClusterIdsNotSet struct {
   118  	topic channels.Topic
   119  }
   120  
   121  func (e ErrActiveClusterIdsNotSet) Error() string {
   122  	return fmt.Errorf("failed to validate cluster prefixed topic %s no active cluster IDs set", e.topic).Error()
   123  }
   124  
   125  // NewActiveClusterIdsNotSetErr returns a new ErrActiveClusterIdsNotSet.
   126  func NewActiveClusterIdsNotSetErr(topic channels.Topic) ErrActiveClusterIdsNotSet {
   127  	return ErrActiveClusterIdsNotSet{topic: topic}
   128  }
   129  
   130  // IsErrActiveClusterIDsNotSet returns true if an error is ErrActiveClusterIdsNotSet.
   131  func IsErrActiveClusterIDsNotSet(err error) bool {
   132  	var e ErrActiveClusterIdsNotSet
   133  	return errors.As(err, &e)
   134  }
   135  
   136  // ErrUnstakedPeer error that indicates a cluster prefixed control message has been from an unstaked peer.
   137  type ErrUnstakedPeer struct {
   138  	pid peer.ID
   139  }
   140  
   141  func (e ErrUnstakedPeer) Error() string {
   142  	return fmt.Sprintf("unstaked peer: %s", e.pid)
   143  }
   144  
   145  // NewUnstakedPeerErr returns a new ErrUnstakedPeer.
   146  func NewUnstakedPeerErr(pid peer.ID) ErrUnstakedPeer {
   147  	return ErrUnstakedPeer{pid: pid}
   148  }
   149  
   150  // IsErrUnstakedPeer returns true if an error is ErrUnstakedPeer.
   151  func IsErrUnstakedPeer(err error) bool {
   152  	var e ErrUnstakedPeer
   153  	return errors.As(err, &e)
   154  }
   155  
   156  // ErrEjectedPeer error that indicates a cluster prefixed control message has been received from an ejected peer.
   157  type ErrEjectedPeer struct {
   158  	pid peer.ID
   159  }
   160  
   161  func (e ErrEjectedPeer) Error() string {
   162  	return fmt.Sprintf("ejected peer: %s", e.pid)
   163  }
   164  
   165  // NewEjectedPeerErr returns a new ErrEjectedPeer.
   166  func NewEjectedPeerErr(pid peer.ID) ErrEjectedPeer {
   167  	return ErrEjectedPeer{pid: pid}
   168  }
   169  
   170  // IsErrEjectedPeer returns true if an error is ErrEjectedPeer.
   171  func IsErrEjectedPeer(err error) bool {
   172  	var e ErrEjectedPeer
   173  	return errors.As(err, &e)
   174  }
   175  
   176  // InvalidRpcPublishMessagesErr error indicates that rpc publish message validation failed.
   177  type InvalidRpcPublishMessagesErr struct {
   178  	// err the original error returned by the calling func.
   179  	err error
   180  	// count the number of times this err was encountered.
   181  	count int
   182  }
   183  
   184  func (e InvalidRpcPublishMessagesErr) Error() string {
   185  	return fmt.Errorf("rpc publish messages validation failed %d error(s) encountered: %w", e.count, e.err).Error()
   186  }
   187  
   188  // NewInvalidRpcPublishMessagesErr returns a new InvalidRpcPublishMessagesErr.
   189  func NewInvalidRpcPublishMessagesErr(err error, count int) InvalidRpcPublishMessagesErr {
   190  	return InvalidRpcPublishMessagesErr{err: err, count: count}
   191  }
   192  
   193  // IsInvalidRpcPublishMessagesErr returns true if an error is InvalidRpcPublishMessagesErr.
   194  func IsInvalidRpcPublishMessagesErr(err error) bool {
   195  	var e InvalidRpcPublishMessagesErr
   196  	return errors.As(err, &e)
   197  }
   198  
   199  // DuplicateTopicIDThresholdExceeded indicates that the number of duplicate topic IDs exceeds the allowed threshold.
   200  type DuplicateTopicIDThresholdExceeded struct {
   201  	duplicates int
   202  	sampleSize int
   203  	threshold  int
   204  }
   205  
   206  func (e DuplicateTopicIDThresholdExceeded) Error() string {
   207  	return fmt.Sprintf("%d/%d duplicate topic IDs exceed the allowed threshold: %d", e.duplicates, e.sampleSize, e.threshold)
   208  }
   209  
   210  // NewDuplicateTopicIDThresholdExceeded returns a new DuplicateTopicIDThresholdExceeded error.
   211  func NewDuplicateTopicIDThresholdExceeded(duplicates int, sampleSize int, threshold int) DuplicateTopicIDThresholdExceeded {
   212  	return DuplicateTopicIDThresholdExceeded{duplicates, sampleSize, threshold}
   213  }
   214  
   215  // IsDuplicateTopicIDThresholdExceeded returns true if an error is DuplicateTopicIDThresholdExceeded
   216  func IsDuplicateTopicIDThresholdExceeded(err error) bool {
   217  	var e DuplicateTopicIDThresholdExceeded
   218  	return errors.As(err, &e)
   219  }
   220  
   221  // InvalidTopicIDThresholdExceeded indicates that the number of invalid topic IDs exceeds the allowed threshold.
   222  type InvalidTopicIDThresholdExceeded struct {
   223  	invalidCount int
   224  	threshold    int
   225  }
   226  
   227  func (e InvalidTopicIDThresholdExceeded) Error() string {
   228  	return fmt.Sprintf("%d invalid topic IDs exceed the allowed threshold: %d", e.invalidCount, e.threshold)
   229  }
   230  
   231  // NewInvalidTopicIDThresholdExceeded returns a new InvalidTopicIDThresholdExceeded error.
   232  func NewInvalidTopicIDThresholdExceeded(invalidCount, threshold int) InvalidTopicIDThresholdExceeded {
   233  	return InvalidTopicIDThresholdExceeded{invalidCount, threshold}
   234  }
   235  
   236  // IsInvalidTopicIDThresholdExceeded returns true if an error is InvalidTopicIDThresholdExceeded.
   237  func IsInvalidTopicIDThresholdExceeded(err error) bool {
   238  	var e InvalidTopicIDThresholdExceeded
   239  	return errors.As(err, &e)
   240  }