Skip to content

Commit 542b839

Browse files
committed
node, p2p: move network config out of Server
This silences a go vet message about copying p2p.Server in package node.
1 parent c8a8ad9 commit 542b839

4 files changed

Lines changed: 36 additions & 28 deletions

File tree

‎node/node.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Node struct {
4949
datadir string // Path to the currently used data directory
5050
eventmux *event.TypeMux // Event multiplexer used between the services of a stack
5151

52-
serverConfig *p2p.Server // Configuration of the underlying P2P networking layer
52+
serverConfig p2p.Config
5353
server *p2p.Server // Currently running P2P networking layer
5454

5555
serviceFuncs []ServiceConstructor // Service constructors (in dependency order)
@@ -97,7 +97,7 @@ func New(conf *Config) (*Node, error) {
9797
}
9898
return &Node{
9999
datadir: conf.DataDir,
100-
serverConfig: &p2p.Server{
100+
serverConfig: p2p.Config{
101101
PrivateKey: conf.NodeKey(),
102102
Name: conf.Name,
103103
Discovery: !conf.NoDiscovery,
@@ -151,9 +151,7 @@ func (n *Node) Start() error {
151151
return ErrNodeRunning
152152
}
153153
// Otherwise copy and specialize the P2P configuration
154-
running := new(p2p.Server)
155-
*running = *n.serverConfig
156-
154+
running := &p2p.Server{Config: n.serverConfig}
157155
services := make(map[reflect.Type]Service)
158156
for _, constructor := range n.serviceFuncs {
159157
// Create a new context for the particular service

‎p2p/dial_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ func TestDialResolve(t *testing.T) {
478478
}
479479

480480
// Now run the task, it should resolve the ID once.
481-
srv := &Server{ntab: table, Dialer: &net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}}
481+
config := Config{Dialer: &net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}}
482+
srv := &Server{ntab: table, Config: config}
482483
tasks[0].Do(srv)
483484
if !reflect.DeepEqual(table.resolveCalls, []discover.NodeID{dest.ID}) {
484485
t.Fatalf("wrong resolve calls, got %v", table.resolveCalls)

‎p2p/server.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ var errServerStopped = errors.New("server stopped")
5454

5555
var srvjslog = logger.NewJsonLogger()
5656

57-
// Server manages all peer connections.
58-
//
59-
// The fields of Server are used as configuration parameters.
60-
// You should set them before starting the Server. Fields may not be
61-
// modified while the server is running.
62-
type Server struct {
57+
// Config holds Server options.
58+
type Config struct {
6359
// This field must be set to a valid secp256k1 private key.
6460
PrivateKey *ecdsa.PrivateKey
6561

@@ -120,6 +116,12 @@ type Server struct {
120116

121117
// If NoDial is true, the server will not dial any peers.
122118
NoDial bool
119+
}
120+
121+
// Server manages all peer connections.
122+
type Server struct {
123+
// Config fields may not be modified while the server is running.
124+
Config
123125

124126
// Hooks for testing. These are useful because we can inhibit
125127
// the whole protocol stack.

‎p2p/server_test.go‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ func (c *testTransport) close(err error) {
6767
}
6868

6969
func startTestServer(t *testing.T, id discover.NodeID, pf func(*Peer)) *Server {
70+
config := Config{
71+
Name: "test",
72+
MaxPeers: 10,
73+
ListenAddr: "127.0.0.1:0",
74+
PrivateKey: newkey(),
75+
}
7076
server := &Server{
71-
Name: "test",
72-
MaxPeers: 10,
73-
ListenAddr: "127.0.0.1:0",
74-
PrivateKey: newkey(),
77+
Config: config,
7578
newPeerHook: pf,
7679
newTransport: func(fd net.Conn) transport { return newTestTransport(id, fd) },
7780
}
@@ -200,10 +203,10 @@ func TestServerTaskScheduling(t *testing.T) {
200203
// The Server in this test isn't actually running
201204
// because we're only interested in what run does.
202205
srv := &Server{
203-
MaxPeers: 10,
204-
quit: make(chan struct{}),
205-
ntab: fakeTable{},
206-
running: true,
206+
Config: Config{MaxPeers: 10},
207+
quit: make(chan struct{}),
208+
ntab: fakeTable{},
209+
running: true,
207210
}
208211
srv.loopWG.Add(1)
209212
go func() {
@@ -314,10 +317,12 @@ func (t *testTask) Do(srv *Server) {
314317
func TestServerAtCap(t *testing.T) {
315318
trustedID := randomID()
316319
srv := &Server{
317-
PrivateKey: newkey(),
318-
MaxPeers: 10,
319-
NoDial: true,
320-
TrustedNodes: []*discover.Node{{ID: trustedID}},
320+
Config: Config{
321+
PrivateKey: newkey(),
322+
MaxPeers: 10,
323+
NoDial: true,
324+
TrustedNodes: []*discover.Node{{ID: trustedID}},
325+
},
321326
}
322327
if err := srv.Start(); err != nil {
323328
t.Fatalf("could not start: %v", err)
@@ -415,10 +420,12 @@ func TestServerSetupConn(t *testing.T) {
415420

416421
for i, test := range tests {
417422
srv := &Server{
418-
PrivateKey: srvkey,
419-
MaxPeers: 10,
420-
NoDial: true,
421-
Protocols: []Protocol{discard},
423+
Config: Config{
424+
PrivateKey: srvkey,
425+
MaxPeers: 10,
426+
NoDial: true,
427+
Protocols: []Protocol{discard},
428+
},
422429
newTransport: func(fd net.Conn) transport { return test.tt },
423430
}
424431
if !test.dontstart {

0 commit comments

Comments
 (0)