From 542b839ec74ff0ce94ab0d34e25531f5819d95d1 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 18 May 2016 11:31:00 +0200 Subject: [PATCH] node, p2p: move network config out of Server This silences a go vet message about copying p2p.Server in package node. --- node/node.go | 8 +++----- p2p/dial_test.go | 3 ++- p2p/server.go | 14 ++++++++------ p2p/server_test.go | 39 +++++++++++++++++++++++---------------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/node/node.go b/node/node.go index 06a1b7aed..1f517a027 100644 --- a/node/node.go +++ b/node/node.go @@ -49,7 +49,7 @@ type Node struct { datadir string // Path to the currently used data directory eventmux *event.TypeMux // Event multiplexer used between the services of a stack - serverConfig *p2p.Server // Configuration of the underlying P2P networking layer + serverConfig p2p.Config server *p2p.Server // Currently running P2P networking layer serviceFuncs []ServiceConstructor // Service constructors (in dependency order) @@ -97,7 +97,7 @@ func New(conf *Config) (*Node, error) { } return &Node{ datadir: conf.DataDir, - serverConfig: &p2p.Server{ + serverConfig: p2p.Config{ PrivateKey: conf.NodeKey(), Name: conf.Name, Discovery: !conf.NoDiscovery, @@ -151,9 +151,7 @@ func (n *Node) Start() error { return ErrNodeRunning } // Otherwise copy and specialize the P2P configuration - running := new(p2p.Server) - *running = *n.serverConfig - + running := &p2p.Server{Config: n.serverConfig} services := make(map[reflect.Type]Service) for _, constructor := range n.serviceFuncs { // Create a new context for the particular service diff --git a/p2p/dial_test.go b/p2p/dial_test.go index 3447660a3..05d9b7562 100644 --- a/p2p/dial_test.go +++ b/p2p/dial_test.go @@ -478,7 +478,8 @@ func TestDialResolve(t *testing.T) { } // Now run the task, it should resolve the ID once. - srv := &Server{ntab: table, Dialer: &net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}} + config := Config{Dialer: &net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}} + srv := &Server{ntab: table, Config: config} tasks[0].Do(srv) if !reflect.DeepEqual(table.resolveCalls, []discover.NodeID{dest.ID}) { t.Fatalf("wrong resolve calls, got %v", table.resolveCalls) diff --git a/p2p/server.go b/p2p/server.go index 3b2f2b078..880aa7cf1 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -54,12 +54,8 @@ var errServerStopped = errors.New("server stopped") var srvjslog = logger.NewJsonLogger() -// Server manages all peer connections. -// -// The fields of Server are used as configuration parameters. -// You should set them before starting the Server. Fields may not be -// modified while the server is running. -type Server struct { +// Config holds Server options. +type Config struct { // This field must be set to a valid secp256k1 private key. PrivateKey *ecdsa.PrivateKey @@ -120,6 +116,12 @@ type Server struct { // If NoDial is true, the server will not dial any peers. NoDial bool +} + +// Server manages all peer connections. +type Server struct { + // Config fields may not be modified while the server is running. + Config // Hooks for testing. These are useful because we can inhibit // the whole protocol stack. diff --git a/p2p/server_test.go b/p2p/server_test.go index b437ac367..deb34f5bb 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -67,11 +67,14 @@ func (c *testTransport) close(err error) { } func startTestServer(t *testing.T, id discover.NodeID, pf func(*Peer)) *Server { + config := Config{ + Name: "test", + MaxPeers: 10, + ListenAddr: "127.0.0.1:0", + PrivateKey: newkey(), + } server := &Server{ - Name: "test", - MaxPeers: 10, - ListenAddr: "127.0.0.1:0", - PrivateKey: newkey(), + Config: config, newPeerHook: pf, newTransport: func(fd net.Conn) transport { return newTestTransport(id, fd) }, } @@ -200,10 +203,10 @@ func TestServerTaskScheduling(t *testing.T) { // The Server in this test isn't actually running // because we're only interested in what run does. srv := &Server{ - MaxPeers: 10, - quit: make(chan struct{}), - ntab: fakeTable{}, - running: true, + Config: Config{MaxPeers: 10}, + quit: make(chan struct{}), + ntab: fakeTable{}, + running: true, } srv.loopWG.Add(1) go func() { @@ -314,10 +317,12 @@ func (t *testTask) Do(srv *Server) { func TestServerAtCap(t *testing.T) { trustedID := randomID() srv := &Server{ - PrivateKey: newkey(), - MaxPeers: 10, - NoDial: true, - TrustedNodes: []*discover.Node{{ID: trustedID}}, + Config: Config{ + PrivateKey: newkey(), + MaxPeers: 10, + NoDial: true, + TrustedNodes: []*discover.Node{{ID: trustedID}}, + }, } if err := srv.Start(); err != nil { t.Fatalf("could not start: %v", err) @@ -415,10 +420,12 @@ func TestServerSetupConn(t *testing.T) { for i, test := range tests { srv := &Server{ - PrivateKey: srvkey, - MaxPeers: 10, - NoDial: true, - Protocols: []Protocol{discard}, + Config: Config{ + PrivateKey: srvkey, + MaxPeers: 10, + NoDial: true, + Protocols: []Protocol{discard}, + }, newTransport: func(fd net.Conn) transport { return test.tt }, } if !test.dontstart {