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