github.com/ethereum/go-ethereum@v1.16.1/p2p/config.go (about)

     1  // Copyright 2025 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package p2p
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"encoding"
    22  	"fmt"
    23  
    24  	"github.com/ethereum/go-ethereum/common/mclock"
    25  	"github.com/ethereum/go-ethereum/log"
    26  	"github.com/ethereum/go-ethereum/p2p/enode"
    27  	"github.com/ethereum/go-ethereum/p2p/nat"
    28  	"github.com/ethereum/go-ethereum/p2p/netutil"
    29  )
    30  
    31  //go:generate go run github.com/fjl/gencodec -type Config -field-override configMarshaling -formats toml -out config_toml.go
    32  
    33  // Config holds Server options.
    34  type Config struct {
    35  	// This field must be set to a valid secp256k1 private key.
    36  	PrivateKey *ecdsa.PrivateKey `toml:"-"`
    37  
    38  	// MaxPeers is the maximum number of peers that can be
    39  	// connected. It must be greater than zero.
    40  	MaxPeers int
    41  
    42  	// MaxPendingPeers is the maximum number of peers that can be pending in the
    43  	// handshake phase, counted separately for inbound and outbound connections.
    44  	// Zero defaults to preset values.
    45  	MaxPendingPeers int `toml:",omitempty"`
    46  
    47  	// DialRatio controls the ratio of inbound to dialed connections.
    48  	// Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
    49  	// Setting DialRatio to zero defaults it to 3.
    50  	DialRatio int `toml:",omitempty"`
    51  
    52  	// NoDiscovery can be used to disable the peer discovery mechanism.
    53  	// Disabling is useful for protocol debugging (manual topology).
    54  	NoDiscovery bool
    55  
    56  	// DiscoveryV4 specifies whether V4 discovery should be started.
    57  	DiscoveryV4 bool `toml:",omitempty"`
    58  
    59  	// DiscoveryV5 specifies whether the new topic-discovery based V5 discovery
    60  	// protocol should be started or not.
    61  	DiscoveryV5 bool `toml:",omitempty"`
    62  
    63  	// Name sets the node name of this server.
    64  	Name string `toml:"-"`
    65  
    66  	// BootstrapNodes are used to establish connectivity
    67  	// with the rest of the network.
    68  	BootstrapNodes []*enode.Node
    69  
    70  	// BootstrapNodesV5 are used to establish connectivity
    71  	// with the rest of the network using the V5 discovery
    72  	// protocol.
    73  	BootstrapNodesV5 []*enode.Node `toml:",omitempty"`
    74  
    75  	// Static nodes are used as pre-configured connections which are always
    76  	// maintained and re-connected on disconnects.
    77  	StaticNodes []*enode.Node
    78  
    79  	// Trusted nodes are used as pre-configured connections which are always
    80  	// allowed to connect, even above the peer limit.
    81  	TrustedNodes []*enode.Node
    82  
    83  	// Connectivity can be restricted to certain IP networks.
    84  	// If this option is set to a non-nil value, only hosts which match one of the
    85  	// IP networks contained in the list are considered.
    86  	NetRestrict *netutil.Netlist `toml:",omitempty"`
    87  
    88  	// NodeDatabase is the path to the database containing the previously seen
    89  	// live nodes in the network.
    90  	NodeDatabase string `toml:",omitempty"`
    91  
    92  	// Protocols should contain the protocols supported
    93  	// by the server. Matching protocols are launched for
    94  	// each peer.
    95  	Protocols []Protocol `toml:"-" json:"-"`
    96  
    97  	// If ListenAddr is set to a non-nil address, the server
    98  	// will listen for incoming connections.
    99  	//
   100  	// If the port is zero, the operating system will pick a port. The
   101  	// ListenAddr field will be updated with the actual address when
   102  	// the server is started.
   103  	ListenAddr string
   104  
   105  	// If DiscAddr is set to a non-nil value, the server will use ListenAddr
   106  	// for TCP and DiscAddr for the UDP discovery protocol.
   107  	DiscAddr string
   108  
   109  	// If set to a non-nil value, the given NAT port mapper
   110  	// is used to make the listening port available to the
   111  	// Internet.
   112  	NAT nat.Interface `toml:",omitempty"`
   113  
   114  	// If Dialer is set to a non-nil value, the given Dialer
   115  	// is used to dial outbound peer connections.
   116  	Dialer NodeDialer `toml:"-"`
   117  
   118  	// If NoDial is true, the server will not dial any peers.
   119  	NoDial bool `toml:",omitempty"`
   120  
   121  	// If EnableMsgEvents is set then the server will emit PeerEvents
   122  	// whenever a message is sent to or received from a peer
   123  	EnableMsgEvents bool
   124  
   125  	// Logger is a custom logger to use with the p2p.Server.
   126  	Logger log.Logger `toml:"-"`
   127  
   128  	clock mclock.Clock
   129  }
   130  
   131  type configMarshaling struct {
   132  	NAT configNAT
   133  }
   134  
   135  type configNAT struct {
   136  	nat.Interface
   137  }
   138  
   139  func (w *configNAT) MarshalText() ([]byte, error) {
   140  	if tm, ok := w.Interface.(encoding.TextMarshaler); ok {
   141  		return tm.MarshalText()
   142  	}
   143  	return nil, fmt.Errorf("NAT specification %#v cannot be marshaled", w.Interface)
   144  }
   145  
   146  func (w *configNAT) UnmarshalText(input []byte) error {
   147  	n, err := nat.Parse(string(input))
   148  	if err != nil {
   149  		return fmt.Errorf("invalid NAT specification: %v", err)
   150  	}
   151  	w.Interface = n
   152  	return nil
   153  }