github.com/onflow/flow-go@v0.33.17/engine/common/synchronization/config.go (about)

     1  package synchronization
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/onflow/flow-go/config"
     8  	core "github.com/onflow/flow-go/module/chainsync"
     9  )
    10  
    11  type Config struct {
    12  	PollInterval time.Duration
    13  	ScanInterval time.Duration
    14  }
    15  
    16  func DefaultConfig() *Config {
    17  	scanInterval := 2 * time.Second
    18  	pollInterval := time.Duration(core.DefaultQueuedHeightMultiplicity) * scanInterval
    19  	return &Config{
    20  		PollInterval: pollInterval,
    21  		ScanInterval: scanInterval,
    22  	}
    23  }
    24  
    25  type OptionFunc func(*Config)
    26  
    27  // WithPollInterval sets a custom interval at which we scan for poll items
    28  func WithPollInterval(interval time.Duration) OptionFunc {
    29  	return func(cfg *Config) {
    30  		cfg.PollInterval = interval
    31  	}
    32  }
    33  
    34  // WithScanInterval sets a custom interval at which we scan for pending items
    35  // and batch them for requesting.
    36  func WithScanInterval(interval time.Duration) OptionFunc {
    37  	return func(cfg *Config) {
    38  		cfg.ScanInterval = interval
    39  	}
    40  }
    41  
    42  // spamProbabilityMultiplier is used to convert probability factor to an integer as well as a maximum value for the
    43  // random number that can be generated by the random number generator.
    44  const spamProbabilityMultiplier = 1000
    45  
    46  // SpamDetectionConfig contains configuration parameters for spam detection for different message types.
    47  // The probability of creating a misbehavior report for a message of a given type is calculated differently for different
    48  // message types.
    49  // MisbehaviourReports are generated for two reasons:
    50  //  1. A malformed message will always produce a MisbehaviourReport, to notify ALSP of *unambiguous* spam.
    51  //  2. A correctly formed message may produce a MisbehaviourReport probabilistically, to notify ALSP of *ambiguous* spam.
    52  //     This effectively tracks the load associated with a particular sender, for this engine, and, on average,
    53  //     reports message load proportionally as misbehaviour to ALSP.
    54  type SpamDetectionConfig struct {
    55  
    56  	// batchRequestBaseProb is the base probability in [0,1] that's used in creating the final probability of creating a
    57  	// misbehavior report for a BatchRequest message. This is why the word "base" is used in the name of this field,
    58  	// since it's not the final probability and there are other factors that determine the final probability.
    59  	// The reason for this is that we want to increase the probability of creating a misbehavior report for a large batch.
    60  	batchRequestBaseProb float32
    61  
    62  	// syncRequestProb is the probability in [0,1] of creating a misbehavior report for a SyncRequest message.
    63  	syncRequestProb float32
    64  
    65  	// rangeRequestBaseProb is the base probability in [0,1] that's used in creating the final probability of creating a
    66  	// misbehavior report for a RangeRequest message. This is why the word "base" is used in the name of this field,
    67  	// since it's not the final probability and there are other factors that determine the final probability.
    68  	// The reason for this is that we want to increase the probability of creating a misbehavior report for a large range.
    69  	rangeRequestBaseProb float32
    70  }
    71  
    72  func NewSpamDetectionConfig() (*SpamDetectionConfig, error) {
    73  	flowConfig, err := config.DefaultConfig()
    74  	if err != nil {
    75  		return nil, fmt.Errorf("failed to read default config: %w", err)
    76  	}
    77  
    78  	return &SpamDetectionConfig{
    79  		// see config/default-config.yml for more information on the following fields
    80  		batchRequestBaseProb: flowConfig.NetworkConfig.SyncEngine.BatchRequestBaseProb,
    81  		syncRequestProb:      flowConfig.NetworkConfig.SyncEngine.SyncRequestProb,
    82  		rangeRequestBaseProb: flowConfig.NetworkConfig.SyncEngine.RangeRequestBaseProb,
    83  	}, nil
    84  }