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

     1  package p2pconfig
     2  
     3  import "time"
     4  
     5  const (
     6  	SpamRecordCacheKey          = "spam-record-cache"
     7  	ScoringRegistryKey          = "scoring-registry"
     8  	AppSpecificScoreRegistryKey = "app-specific-score"
     9  	StartupSilenceDurationKey   = "startup-silence-duration"
    10  )
    11  
    12  type ScoringRegistryParameters struct {
    13  	// StartupSilenceDuration defines the duration of time, after the node startup,
    14  	// during which the scoring registry remains inactive before penalizing nodes.
    15  	// Throughout this startup silence period, the application-specific penalty
    16  	// for all nodes will be set to 0, and any invalid control message notifications
    17  	// will be ignored.
    18  	//
    19  	// This configuration allows nodes to stabilize and initialize before
    20  	// applying penalties or responding processing invalid control message notifications.
    21  	StartupSilenceDuration time.Duration              `validate:"gt=10m" mapstructure:"startup-silence-duration"`
    22  	AppSpecificScore       AppSpecificScoreParameters `validate:"required" mapstructure:"app-specific-score"`
    23  	SpamRecordCache        SpamRecordCacheParameters  `validate:"required" mapstructure:"spam-record-cache"`
    24  	MisbehaviourPenalties  MisbehaviourPenalties      `validate:"required" mapstructure:"misbehaviour-penalties"`
    25  }
    26  
    27  const (
    28  	ScoreUpdateWorkerNumKey                       = "score-update-worker-num"
    29  	ScoreUpdateRequestQueueSizeKey                = "score-update-request-queue-size"
    30  	ScoreTTLKey                                   = "score-ttl"
    31  	InvalidControlMessageNotificationQueueSizeKey = "invalid-control-message-notification-queue-size"
    32  )
    33  
    34  // AppSpecificScoreParameters is the parameters for the GossipSubAppSpecificScoreRegistry.
    35  // Parameters are "numerical values" that are used to compute or build components that compute or maintain the application specific score of peers.
    36  type AppSpecificScoreParameters struct {
    37  	// ScoreUpdateWorkerNum is the number of workers in the worker pool for handling the application specific score update of peers in a non-blocking way.
    38  	ScoreUpdateWorkerNum int `validate:"gt=0" mapstructure:"score-update-worker-num"`
    39  
    40  	// ScoreUpdateRequestQueueSize is the size of the worker pool for handling the application specific score update of peers in a non-blocking way.
    41  	ScoreUpdateRequestQueueSize uint32 `validate:"gt=0" mapstructure:"score-update-request-queue-size"`
    42  
    43  	// InvalidControlMessageNotificationQueueSize is the size of the queue for handling invalid control message notifications in a non-blocking way.
    44  	InvalidControlMessageNotificationQueueSize uint32 `validate:"gt=0" mapstructure:"invalid-control-message-notification-queue-size"`
    45  
    46  	// ScoreTTL is the time to live of the application specific score of a peer; the registry keeps a cached copy of the
    47  	// application specific score of a peer for this duration. When the duration expires, the application specific score
    48  	// of the peer is updated asynchronously. As long as the update is in progress, the cached copy of the application
    49  	// specific score of the peer is used even if it is expired.
    50  	ScoreTTL time.Duration `validate:"required" mapstructure:"score-ttl"`
    51  }
    52  
    53  const (
    54  	DecayKey = "decay"
    55  )
    56  
    57  type SpamRecordCacheParameters struct {
    58  	// CacheSize is size of the cache used to store the spam records of peers.
    59  	// The spam records are used to penalize peers that send invalid messages.
    60  	CacheSize uint32               `validate:"gt=0" mapstructure:"cache-size"`
    61  	Decay     SpamRecordCacheDecay `validate:"required" mapstructure:"decay"`
    62  }
    63  
    64  const (
    65  	PenaltyDecaySlowdownThresholdKey = "penalty-decay-slowdown-threshold"
    66  	DecayRateReductionFactorKey      = "penalty-decay-rate-reduction-factor"
    67  	PenaltyDecayEvaluationPeriodKey  = "penalty-decay-evaluation-period"
    68  	MinimumSpamPenaltyDecayFactorKey = "minimum-spam-penalty-decay-factor"
    69  	MaximumSpamPenaltyDecayFactorKey = "maximum-spam-penalty-decay-factor"
    70  	SkipDecayThresholdKey            = "skip-decay-threshold"
    71  )
    72  
    73  type SpamRecordCacheDecay struct {
    74  	// PenaltyDecaySlowdownThreshold defines the penalty level which the decay rate is reduced by `DecayRateReductionFactor` every time the penalty of a node falls below the threshold, thereby slowing down the decay process.
    75  	// This mechanism ensures that malicious nodes experience longer decay periods, while honest nodes benefit from quicker decay.
    76  	PenaltyDecaySlowdownThreshold float64 `validate:"lt=0" mapstructure:"penalty-decay-slowdown-threshold"`
    77  
    78  	// DecayRateReductionFactor defines the value by which the decay rate is decreased every time the penalty is below the PenaltyDecaySlowdownThreshold. A reduced decay rate extends the time it takes for penalties to diminish.
    79  	DecayRateReductionFactor float64 `validate:"gt=0,lt=1" mapstructure:"penalty-decay-rate-reduction-factor"`
    80  
    81  	// PenaltyDecayEvaluationPeriod defines the interval at which the decay for a spam record is okay to be adjusted.
    82  	PenaltyDecayEvaluationPeriod time.Duration `validate:"gt=0" mapstructure:"penalty-decay-evaluation-period"`
    83  
    84  	SkipDecayThreshold float64 `validate:"gt=-1,lt=0" mapstructure:"skip-decay-threshold"`
    85  
    86  	MinimumSpamPenaltyDecayFactor float64 `validate:"gt=0,lte=1" mapstructure:"minimum-spam-penalty-decay-factor"`
    87  	MaximumSpamPenaltyDecayFactor float64 `validate:"gt=0,lte=1" mapstructure:"maximum-spam-penalty-decay-factor"`
    88  }
    89  
    90  const (
    91  	MisbehaviourPenaltiesKey          = "misbehaviour-penalties"
    92  	GraftKey                          = "graft"
    93  	PruneKey                          = "prune"
    94  	IWantKey                          = "iwant"
    95  	IHaveKey                          = "ihave"
    96  	PublishKey                        = "publish"
    97  	ClusterPrefixedReductionFactorKey = "cluster-prefixed-reduction-factor"
    98  )
    99  
   100  type MisbehaviourPenalties struct {
   101  	GraftMisbehaviour              float64 `validate:"lt=0" mapstructure:"graft"`
   102  	PruneMisbehaviour              float64 `validate:"lt=0" mapstructure:"prune"`
   103  	IHaveMisbehaviour              float64 `validate:"lt=0" mapstructure:"ihave"`
   104  	IWantMisbehaviour              float64 `validate:"lt=0" mapstructure:"iwant"`
   105  	PublishMisbehaviour            float64 `validate:"lt=0" mapstructure:"publish"`
   106  	ClusterPrefixedReductionFactor float64 `validate:"gt=0,lte=1" mapstructure:"cluster-prefixed-reduction-factor"`
   107  }