github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/config.go (about)

     1  package p2p
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/ethereum-optimism/optimism/op-node/p2p/gating"
    10  
    11  	"github.com/ethereum/go-ethereum/log"
    12  	"github.com/ethereum/go-ethereum/p2p/discover"
    13  	"github.com/ethereum/go-ethereum/p2p/enode"
    14  	"github.com/ethereum/go-ethereum/p2p/netutil"
    15  	ds "github.com/ipfs/go-datastore"
    16  	"github.com/libp2p/go-libp2p"
    17  	pubsub "github.com/libp2p/go-libp2p-pubsub"
    18  	"github.com/libp2p/go-libp2p/core"
    19  	"github.com/libp2p/go-libp2p/core/connmgr"
    20  	"github.com/libp2p/go-libp2p/core/crypto"
    21  	"github.com/libp2p/go-libp2p/core/host"
    22  	"github.com/libp2p/go-libp2p/core/metrics"
    23  	cmgr "github.com/libp2p/go-libp2p/p2p/net/connmgr"
    24  
    25  	"github.com/ethereum-optimism/optimism/op-node/rollup"
    26  )
    27  
    28  var DefaultBootnodes = []*enode.Node{
    29  	// OP Labs
    30  	enode.MustParse("enode://869d07b5932f17e8490990f75a3f94195e9504ddb6b85f7189e5a9c0a8fff8b00aecf6f3ac450ecba6cdabdb5858788a94bde2b613e0f2d82e9b395355f76d1a@34.65.67.101:30305"),
    31  	enode.MustParse("enode://2d4e7e9d48f4dd4efe9342706dd1b0024681bd4c3300d021f86fc75eab7865d4e0cbec6fbc883f011cfd6a57423e7e2f6e104baad2b744c3cafaec6bc7dc92c1@34.65.43.171:30305"),
    32  	enode.MustParse("enode://9d7a3efefe442351217e73b3a593bcb8efffb55b4807699972145324eab5e6b382152f8d24f6301baebbfb5ecd4127bd3faab2842c04cd432bdf50ba092f6645@34.65.109.126:30305"),
    33  	// Base
    34  	enode.MustParse("enr:-J24QNz9lbrKbN4iSmmjtnr7SjUMk4zB7f1krHZcTZx-JRKZd0kA2gjufUROD6T3sOWDVDnFJRvqBBo62zuF-hYCohOGAYiOoEyEgmlkgnY0gmlwhAPniryHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQKNVFlCxh_B-716tTs-h1vMzZkSs1FTu_OYTNjgufplG4N0Y3CCJAaDdWRwgiQG"),
    35  	enode.MustParse("enr:-J24QH-f1wt99sfpHy4c0QJM-NfmsIfmlLAMMcgZCUEgKG_BBYFc6FwYgaMJMQN5dsRBJApIok0jFn-9CS842lGpLmqGAYiOoDRAgmlkgnY0gmlwhLhIgb2Hb3BzdGFja4OFQgCJc2VjcDI1NmsxoQJ9FTIv8B9myn1MWaC_2lJ-sMoeCDkusCsk4BYHjjCq04N0Y3CCJAaDdWRwgiQG"),
    36  	enode.MustParse("enr:-J24QDXyyxvQYsd0yfsN0cRr1lZ1N11zGTplMNlW4xNEc7LkPXh0NAJ9iSOVdRO95GPYAIc6xmyoCCG6_0JxdL3a0zaGAYiOoAjFgmlkgnY0gmlwhAPckbGHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQJwoS7tzwxqXSyFL7g0JM-KWVbgvjfB8JA__T7yY_cYboN0Y3CCJAaDdWRwgiQG"),
    37  	enode.MustParse("enr:-J24QHmGyBwUZXIcsGYMaUqGGSl4CFdx9Tozu-vQCn5bHIQbR7On7dZbU61vYvfrJr30t0iahSqhc64J46MnUO2JvQaGAYiOoCKKgmlkgnY0gmlwhAPnCzSHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQINc4fSijfbNIiGhcgvwjsjxVFJHUstK9L1T8OTKUjgloN0Y3CCJAaDdWRwgiQG"),
    38  	enode.MustParse("enr:-J24QG3ypT4xSu0gjb5PABCmVxZqBjVw9ca7pvsI8jl4KATYAnxBmfkaIuEqy9sKvDHKuNCsy57WwK9wTt2aQgcaDDyGAYiOoGAXgmlkgnY0gmlwhDbGmZaHb3BzdGFja4OFQgCJc2VjcDI1NmsxoQIeAK_--tcLEiu7HvoUlbV52MspE0uCocsx1f_rYvRenIN0Y3CCJAaDdWRwgiQG"),
    39  	// Conduit
    40  	enode.MustParse("enode://9d7a3efefe442351217e73b3a593bcb8efffb55b4807699972145324eab5e6b382152f8d24f6301baebbfb5ecd4127bd3faab2842c04cd432bdf50ba092f6645@34.65.109.126:30305"),
    41  }
    42  
    43  type HostMetrics interface {
    44  	gating.UnbanMetrics
    45  	gating.ConnectionGaterMetrics
    46  }
    47  
    48  // SetupP2P provides a host and discovery service for usage in the rollup node.
    49  type SetupP2P interface {
    50  	Check() error
    51  	Disabled() bool
    52  	// Host creates a libp2p host service. Returns nil, nil if p2p is disabled.
    53  	Host(log log.Logger, reporter metrics.Reporter, metrics HostMetrics) (host.Host, error)
    54  	// Discovery creates a disc-v5 service. Returns nil, nil, nil if discovery is disabled.
    55  	Discovery(log log.Logger, rollupCfg *rollup.Config, tcpPort uint16) (*enode.LocalNode, *discover.UDPv5, error)
    56  	TargetPeers() uint
    57  	BanPeers() bool
    58  	BanThreshold() float64
    59  	BanDuration() time.Duration
    60  	GossipSetupConfigurables
    61  	ReqRespSyncEnabled() bool
    62  }
    63  
    64  // ScoringParams defines the various types of peer scoring parameters.
    65  type ScoringParams struct {
    66  	PeerScoring        pubsub.PeerScoreParams
    67  	ApplicationScoring ApplicationScoreParams
    68  }
    69  
    70  // Config sets up a p2p host and discv5 service from configuration.
    71  // This implements SetupP2P.
    72  type Config struct {
    73  	Priv *crypto.Secp256k1PrivateKey
    74  
    75  	DisableP2P  bool
    76  	NoDiscovery bool
    77  
    78  	ScoringParams *ScoringParams
    79  
    80  	// Whether to ban peers based on their [PeerScoring] score. Should be negative.
    81  	BanningEnabled bool
    82  	// Minimum score before peers are disconnected and banned
    83  	BanningThreshold float64
    84  	BanningDuration  time.Duration
    85  
    86  	ListenIP      net.IP
    87  	ListenTCPPort uint16
    88  
    89  	// Port to bind discv5 to
    90  	ListenUDPPort uint16
    91  
    92  	AdvertiseIP      net.IP
    93  	AdvertiseTCPPort uint16
    94  	AdvertiseUDPPort uint16
    95  	Bootnodes        []*enode.Node
    96  	DiscoveryDB      *enode.DB
    97  	NetRestrict      *netutil.Netlist
    98  
    99  	StaticPeers []core.Multiaddr
   100  
   101  	HostMux             []libp2p.Option
   102  	HostSecurity        []libp2p.Option
   103  	NoTransportSecurity bool
   104  
   105  	PeersLo    uint
   106  	PeersHi    uint
   107  	PeersGrace time.Duration
   108  
   109  	MeshD     int // topic stable mesh target count
   110  	MeshDLo   int // topic stable mesh low watermark
   111  	MeshDHi   int // topic stable mesh high watermark
   112  	MeshDLazy int // gossip target
   113  
   114  	// FloodPublish publishes messages from ourselves to peers outside of the gossip topic mesh but supporting the same topic.
   115  	FloodPublish bool
   116  
   117  	// If true a NAT manager will host a NAT port mapping that is updated with PMP and UPNP by libp2p/go-nat
   118  	NAT bool
   119  
   120  	UserAgent string
   121  
   122  	TimeoutNegotiation time.Duration
   123  	TimeoutAccept      time.Duration
   124  	TimeoutDial        time.Duration
   125  
   126  	// Underlying store that hosts connection-gater and peerstore data.
   127  	Store ds.Batching
   128  
   129  	EnableReqRespSync bool
   130  
   131  	EnablePingService bool
   132  }
   133  
   134  func DefaultConnManager(conf *Config) (connmgr.ConnManager, error) {
   135  	return cmgr.NewConnManager(
   136  		int(conf.PeersLo),
   137  		int(conf.PeersHi),
   138  		cmgr.WithGracePeriod(conf.PeersGrace),
   139  		cmgr.WithSilencePeriod(time.Minute),
   140  		cmgr.WithEmergencyTrim(true))
   141  }
   142  
   143  func (conf *Config) TargetPeers() uint {
   144  	return conf.PeersLo
   145  }
   146  
   147  func (conf *Config) Disabled() bool {
   148  	return conf.DisableP2P
   149  }
   150  
   151  func (conf *Config) PeerScoringParams() *ScoringParams {
   152  	if conf.ScoringParams == nil {
   153  		return nil
   154  	}
   155  	return conf.ScoringParams
   156  }
   157  
   158  func (conf *Config) BanPeers() bool {
   159  	return conf.BanningEnabled
   160  }
   161  
   162  func (conf *Config) BanThreshold() float64 {
   163  	return conf.BanningThreshold
   164  }
   165  
   166  func (conf *Config) BanDuration() time.Duration {
   167  	return conf.BanningDuration
   168  }
   169  
   170  func (conf *Config) ReqRespSyncEnabled() bool {
   171  	return conf.EnableReqRespSync
   172  }
   173  
   174  const maxMeshParam = 1000
   175  
   176  func (conf *Config) Check() error {
   177  	if conf.DisableP2P {
   178  		return nil
   179  	}
   180  	if conf.Store == nil {
   181  		return errors.New("p2p requires a persistent or in-memory peerstore, but found none")
   182  	}
   183  	if !conf.NoDiscovery {
   184  		if conf.DiscoveryDB == nil {
   185  			return errors.New("discovery requires a persistent or in-memory discv5 db, but found none")
   186  		}
   187  	}
   188  	if conf.PeersLo == 0 || conf.PeersHi == 0 || conf.PeersLo > conf.PeersHi {
   189  		return fmt.Errorf("peers lo/hi tides are invalid: %d, %d", conf.PeersLo, conf.PeersHi)
   190  	}
   191  	if conf.MeshD <= 0 || conf.MeshD > maxMeshParam {
   192  		return fmt.Errorf("mesh D param must not be 0 or exceed %d, but got %d", maxMeshParam, conf.MeshD)
   193  	}
   194  	if conf.MeshDLo <= 0 || conf.MeshDLo > maxMeshParam {
   195  		return fmt.Errorf("mesh Dlo param must not be 0 or exceed %d, but got %d", maxMeshParam, conf.MeshDLo)
   196  	}
   197  	if conf.MeshDHi <= 0 || conf.MeshDHi > maxMeshParam {
   198  		return fmt.Errorf("mesh Dhi param must not be 0 or exceed %d, but got %d", maxMeshParam, conf.MeshDHi)
   199  	}
   200  	if conf.MeshDLazy <= 0 || conf.MeshDLazy > maxMeshParam {
   201  		return fmt.Errorf("mesh Dlazy param must not be 0 or exceed %d, but got %d", maxMeshParam, conf.MeshDLazy)
   202  	}
   203  	return nil
   204  }