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

     1  package p2p
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     9  	"github.com/libp2p/go-libp2p/core/host"
    10  	"github.com/libp2p/go-libp2p/core/metrics"
    11  
    12  	"github.com/ethereum/go-ethereum/log"
    13  	"github.com/ethereum/go-ethereum/p2p/discover"
    14  	"github.com/ethereum/go-ethereum/p2p/enode"
    15  	"github.com/ethereum/go-ethereum/p2p/enr"
    16  
    17  	"github.com/ethereum-optimism/optimism/op-node/rollup"
    18  )
    19  
    20  // Prepared provides a p2p host and discv5 service that is already set up.
    21  // This implements SetupP2P.
    22  type Prepared struct {
    23  	HostP2P   host.Host
    24  	LocalNode *enode.LocalNode
    25  	UDPv5     *discover.UDPv5
    26  
    27  	EnableReqRespSync bool
    28  }
    29  
    30  var _ SetupP2P = (*Prepared)(nil)
    31  
    32  func (p *Prepared) TargetPeers() uint {
    33  	return 20
    34  }
    35  
    36  func (p *Prepared) Check() error {
    37  	if (p.LocalNode == nil) != (p.UDPv5 == nil) {
    38  		return fmt.Errorf("inconsistent discv5 setup: %v <> %v", p.LocalNode, p.UDPv5)
    39  	}
    40  	if p.LocalNode != nil && p.HostP2P == nil {
    41  		return errors.New("cannot provide discovery without p2p host")
    42  	}
    43  	return nil
    44  }
    45  
    46  // Host creates a libp2p host service. Returns nil, nil if p2p is disabled.
    47  func (p *Prepared) Host(log log.Logger, reporter metrics.Reporter, metrics HostMetrics) (host.Host, error) {
    48  	return p.HostP2P, nil
    49  }
    50  
    51  // Discovery creates a disc-v5 service. Returns nil, nil, nil if discovery is disabled.
    52  func (p *Prepared) Discovery(log log.Logger, rollupCfg *rollup.Config, tcpPort uint16) (*enode.LocalNode, *discover.UDPv5, error) {
    53  	if p.LocalNode != nil {
    54  		dat := OpStackENRData{
    55  			chainID: rollupCfg.L2ChainID.Uint64(),
    56  			version: 0,
    57  		}
    58  		p.LocalNode.Set(&dat)
    59  		if tcpPort != 0 {
    60  			p.LocalNode.Set(enr.TCP(tcpPort))
    61  		}
    62  	}
    63  	return p.LocalNode, p.UDPv5, nil
    64  }
    65  
    66  func (p *Prepared) ConfigureGossip(rollupCfg *rollup.Config) []pubsub.Option {
    67  	return []pubsub.Option{
    68  		pubsub.WithGossipSubParams(BuildGlobalGossipParams(rollupCfg)),
    69  	}
    70  }
    71  
    72  func (p *Prepared) PeerScoringParams() *ScoringParams {
    73  	return nil
    74  }
    75  
    76  func (p *Prepared) BanPeers() bool {
    77  	return false
    78  }
    79  
    80  func (p *Prepared) BanThreshold() float64 {
    81  	return -100
    82  }
    83  
    84  func (p *Prepared) BanDuration() time.Duration {
    85  	return 1 * time.Hour
    86  }
    87  
    88  func (p *Prepared) Disabled() bool {
    89  	return false
    90  }
    91  
    92  func (p *Prepared) ReqRespSyncEnabled() bool {
    93  	return p.EnableReqRespSync
    94  }