Skip to content

Commit edb75ab

Browse files
authored
Merge pull request #2249 from CortexFoundation/dev
toml config file supported
2 parents 7556482 + a073c8d commit edb75ab

File tree

11 files changed

+397
-116
lines changed

11 files changed

+397
-116
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/dop251/goja v0.0.0-20250114131315-46d383d606d3
2626
github.com/ethereum/c-kzg-4844 v1.0.3
2727
github.com/ethereum/go-verkle v0.2.2
28-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e
28+
github.com/fjl/gencodec v0.1.0
2929
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
3030
github.com/fsnotify/fsnotify v1.8.0
3131
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ github.com/ethereum/c-kzg-4844 v1.0.3/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1
463463
github.com/ethereum/go-verkle v0.2.2 h1:I2W0WjnrFUIzzVPwm8ykY+7pL2d4VhlsePn4j7cnFk8=
464464
github.com/ethereum/go-verkle v0.2.2/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
465465
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
466-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e h1:bBLctRc7kr01YGvaDfgLbTwjFNW5jdp5y5rj8XXBHfY=
467-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e/go.mod h1:AzA8Lj6YtixmJWL+wkKoBGsLWy9gFrAzi4g+5bCKwpY=
466+
github.com/fjl/gencodec v0.1.0 h1:B3K0xPfc52cw52BBgUbSPxYo+HlLfAgWMVKRWXUXBcs=
467+
github.com/fjl/gencodec v0.1.0/go.mod h1:Um1dFHPONZGTHog1qD1NaWjXJW/SPB38wPv0O8uZ2fI=
468468
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
469469
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
470470
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=

p2p/config.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

p2p/config_toml.go

Lines changed: 165 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p2p/nat/nat.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int,
133133
// Mapping operations will not return an error but won't actually do anything.
134134
type ExtIP net.IP
135135

136-
func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
137-
func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
136+
func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
137+
func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
138+
func (n ExtIP) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("extip:%v", net.IP(n))), nil }
138139

139140
// These do nothing.
140141

@@ -148,7 +149,7 @@ func (ExtIP) DeleteMapping(string, int, int) error { return nil }
148149
func Any() Interface {
149150
// TODO: attempt to discover whether the local machine has an
150151
// Internet-class address. Return ExtIP in this case.
151-
return startautodisc("UPnP or NAT-PMP", func() Interface {
152+
return startautodisc("any", func() Interface {
152153
found := make(chan Interface, 2)
153154
go func() { found <- discoverUPnP() }()
154155
go func() { found <- discoverPMP() }()
@@ -164,7 +165,7 @@ func Any() Interface {
164165
// UPnP returns a port mapper that uses UPnP. It will attempt to
165166
// discover the address of your router using UDP broadcasts.
166167
func UPnP() Interface {
167-
return startautodisc("UPnP", discoverUPnP)
168+
return startautodisc("upnp", discoverUPnP)
168169
}
169170

170171
// PMP returns a port mapper that uses NAT-PMP. The provided gateway
@@ -174,7 +175,7 @@ func PMP(gateway net.IP) Interface {
174175
if gateway != nil {
175176
return &pmp{gw: gateway, c: natpmp.NewClient(gateway)}
176177
}
177-
return startautodisc("NAT-PMP", discoverPMP)
178+
return startautodisc("natpmp", discoverPMP)
178179
}
179180

180181
// autodisc represents a port mapping mechanism that is still being
@@ -228,6 +229,10 @@ func (n *autodisc) String() string {
228229
return n.found.String()
229230
}
230231

232+
func (n *autodisc) MarshalText() ([]byte, error) {
233+
return []byte(n.what), nil
234+
}
235+
231236
// wait blocks until auto-discovery has been performed.
232237
func (n *autodisc) wait() error {
233238
n.once.Do(func() {

p2p/nat/natpmp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
6969
return err
7070
}
7171

72+
func (n *pmp) MarshalText() ([]byte, error) {
73+
return []byte(fmt.Sprintf("natpmp:%v", n.gw)), nil
74+
}
75+
7276
func discoverPMP() Interface {
7377
// run external address lookups on all potential gateways
7478
gws := potentialGateways()

0 commit comments

Comments
 (0)