github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/p2p/peers/options.go (about)

     1  package peers
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	libhead "github.com/celestiaorg/go-header"
     8  
     9  	"github.com/celestiaorg/celestia-node/header"
    10  	"github.com/celestiaorg/celestia-node/share/p2p/shrexsub"
    11  )
    12  
    13  type Parameters struct {
    14  	// PoolValidationTimeout is the timeout used for validating incoming datahashes. Pools that have
    15  	// been created for datahashes from shrexsub that do not see this hash from headersub after this
    16  	// timeout will be garbage collected.
    17  	PoolValidationTimeout time.Duration
    18  
    19  	// PeerCooldown is the time a peer is put on cooldown after a ResultCooldownPeer.
    20  	PeerCooldown time.Duration
    21  
    22  	// GcInterval is the interval at which the manager will garbage collect unvalidated pools.
    23  	GcInterval time.Duration
    24  
    25  	// EnableBlackListing turns on blacklisting for misbehaved peers
    26  	EnableBlackListing bool
    27  }
    28  
    29  type Option func(*Manager) error
    30  
    31  // Validate validates the values in Parameters
    32  func (p *Parameters) Validate() error {
    33  	if p.PoolValidationTimeout <= 0 {
    34  		return fmt.Errorf("peer-manager: validation timeout must be positive")
    35  	}
    36  
    37  	if p.PeerCooldown <= 0 {
    38  		return fmt.Errorf("peer-manager: peer cooldown must be positive")
    39  	}
    40  
    41  	if p.GcInterval <= 0 {
    42  		return fmt.Errorf("peer-manager: garbage collection interval must be positive")
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  // DefaultParameters returns the default configuration values for the peer manager parameters
    49  func DefaultParameters() Parameters {
    50  	return Parameters{
    51  		// PoolValidationTimeout's default value is based on the default daser sampling timeout of 1 minute.
    52  		// If a received datahash has not tried to be sampled within these two minutes, the pool will be
    53  		// removed.
    54  		PoolValidationTimeout: 2 * time.Minute,
    55  		// PeerCooldown's default value is based on initial network tests that showed a ~3.5 second
    56  		// sync time for large blocks. This value gives our (discovery) peers enough time to sync
    57  		// the new block before we ask them again.
    58  		PeerCooldown: 3 * time.Second,
    59  		GcInterval:   time.Second * 30,
    60  		// blacklisting is off by default //TODO(@walldiss): enable blacklisting once all related issues
    61  		// are resolved
    62  		EnableBlackListing: false,
    63  	}
    64  }
    65  
    66  // WithShrexSubPools passes a shrexsub and headersub instance to be used to populate and validate
    67  // pools from shrexsub notifications.
    68  func WithShrexSubPools(shrexSub *shrexsub.PubSub, headerSub libhead.Subscriber[*header.ExtendedHeader]) Option {
    69  	return func(m *Manager) error {
    70  		m.shrexSub = shrexSub
    71  		m.headerSub = headerSub
    72  		return nil
    73  	}
    74  }
    75  
    76  // WithMetrics turns on metric collection in peer manager.
    77  func (m *Manager) WithMetrics() error {
    78  	metrics, err := initMetrics(m)
    79  	if err != nil {
    80  		return fmt.Errorf("peer-manager: init metrics: %w", err)
    81  	}
    82  	m.metrics = metrics
    83  	return nil
    84  }