github.com/onflow/flow-go@v0.33.17/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  )
    32  
    33  // AppSpecificScoreParameters is the parameters for the GossipSubAppSpecificScoreRegistry.
    34  // Parameters are "numerical values" that are used to compute or build components that compute or maintain the application specific score of peers.
    35  type AppSpecificScoreParameters struct {
    36  	// ScoreUpdateWorkerNum is the number of workers in the worker pool for handling the application specific score update of peers in a non-blocking way.
    37  	ScoreUpdateWorkerNum int `validate:"gt=0" mapstructure:"score-update-worker-num"`
    38  
    39  	// ScoreUpdateRequestQueueSize is the size of the worker pool for handling the application specific score update of peers in a non-blocking way.
    40  	ScoreUpdateRequestQueueSize uint32 `validate:"gt=0" mapstructure:"score-update-request-queue-size"`
    41  
    42  	// ScoreTTL is the time to live of the application specific score of a peer; the registry keeps a cached copy of the
    43  	// application specific score of a peer for this duration. When the duration expires, the application specific score
    44  	// of the peer is updated asynchronously. As long as the update is in progress, the cached copy of the application
    45  	// specific score of the peer is used even if it is expired.
    46  	ScoreTTL time.Duration `validate:"required" mapstructure:"score-ttl"`
    47  }
    48  
    49  const (
    50  	DecayKey = "decay"
    51  )
    52  
    53  type SpamRecordCacheParameters struct {
    54  	// CacheSize is size of the cache used to store the spam records of peers.
    55  	// The spam records are used to penalize peers that send invalid messages.
    56  	CacheSize uint32               `validate:"gt=0" mapstructure:"cache-size"`
    57  	Decay     SpamRecordCacheDecay `validate:"required" mapstructure:"decay"`
    58  }
    59  
    60  const (
    61  	PenaltyDecaySlowdownThresholdKey = "penalty-decay-slowdown-threshold"
    62  	DecayRateReductionFactorKey      = "penalty-decay-rate-reduction-factor"
    63  	PenaltyDecayEvaluationPeriodKey  = "penalty-decay-evaluation-period"
    64  	MinimumSpamPenaltyDecayFactorKey = "minimum-spam-penalty-decay-factor"
    65  	MaximumSpamPenaltyDecayFactorKey = "maximum-spam-penalty-decay-factor"
    66  	SkipDecayThresholdKey            = "skip-decay-threshold"
    67  )
    68  
    69  type SpamRecordCacheDecay struct {
    70  	// 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.
    71  	// This mechanism ensures that malicious nodes experience longer decay periods, while honest nodes benefit from quicker decay.
    72  	PenaltyDecaySlowdownThreshold float64 `validate:"lt=0" mapstructure:"penalty-decay-slowdown-threshold"`
    73  
    74  	// 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.
    75  	DecayRateReductionFactor float64 `validate:"gt=0,lt=1" mapstructure:"penalty-decay-rate-reduction-factor"`
    76  
    77  	// PenaltyDecayEvaluationPeriod defines the interval at which the decay for a spam record is okay to be adjusted.
    78  	PenaltyDecayEvaluationPeriod time.Duration `validate:"gt=0" mapstructure:"penalty-decay-evaluation-period"`
    79  
    80  	SkipDecayThreshold float64 `validate:"gt=-1,lt=0" mapstructure:"skip-decay-threshold"`
    81  
    82  	MinimumSpamPenaltyDecayFactor float64 `validate:"gt=0,lte=1" mapstructure:"minimum-spam-penalty-decay-factor"`
    83  	MaximumSpamPenaltyDecayFactor float64 `validate:"gt=0,lte=1" mapstructure:"maximum-spam-penalty-decay-factor"`
    84  }
    85  
    86  const (
    87  	MisbehaviourPenaltiesKey          = "misbehaviour-penalties"
    88  	GraftMisbehaviourKey              = "graft"
    89  	PruneMisbehaviourKey              = "prune"
    90  	IHaveMisbehaviourKey              = "ihave"
    91  	IWantMisbehaviourKey              = "iwant"
    92  	PublishMisbehaviourKey            = "publish"
    93  	ClusterPrefixedReductionFactorKey = "cluster-prefixed-reduction-factor"
    94  )
    95  
    96  type MisbehaviourPenalties struct {
    97  	GraftMisbehaviour              float64 `validate:"lt=0" mapstructure:"graft"`
    98  	PruneMisbehaviour              float64 `validate:"lt=0" mapstructure:"prune"`
    99  	IHaveMisbehaviour              float64 `validate:"lt=0" mapstructure:"ihave"`
   100  	IWantMisbehaviour              float64 `validate:"lt=0" mapstructure:"iwant"`
   101  	PublishMisbehaviour            float64 `validate:"lt=0" mapstructure:"publish"`
   102  	ClusterPrefixedReductionFactor float64 `validate:"gt=0,lte=1" mapstructure:"cluster-prefixed-reduction-factor"`
   103  }