Skip to content

Commit

Permalink
Merge pull request ethereum#40 from hadv/static-node
Browse files Browse the repository at this point in the history
p2p: add private nodes as static node and trusted node as well
  • Loading branch information
uprendis authored Nov 15, 2022
2 parents 133089e + 08e00ba commit 2b28808
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ func (c *Config) TrustedNodes() []*enode.Node {
return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes))
}

func (c *Config) PrivateNodes() []*enode.Node {
return c.P2P.PrivateNodes
}

// parsePersistentNodes parses a list of discovery node URLs loaded from a .json
// file from within the data directory.
func (c *Config) parsePersistentNodes(w *bool, path string) []*enode.Node {
Expand Down
6 changes: 6 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ func (srv *Server) setupDialScheduler() {
config.dialer = tcpDialer{&net.Dialer{Timeout: defaultDialTimeout}}
}
srv.dialsched = newDialScheduler(config, srv.discmix, srv.SetupConn)
for _, n := range srv.PrivateNodes {
srv.dialsched.addStatic(n)
}
for _, n := range srv.StaticNodes {
srv.dialsched.addStatic(n)
}
Expand Down Expand Up @@ -734,6 +737,9 @@ func (srv *Server) run() {
for _, n := range srv.TrustedNodes {
trusted[n.ID()] = true
}
for _, n := range srv.PrivateNodes {
trusted[n.ID()] = true
}

running:
for {
Expand Down
55 changes: 49 additions & 6 deletions p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,17 @@ func TestServerRemovePeerDisconnect(t *testing.T) {
// when the server is at capacity. Trusted connections should still be accepted.
func TestServerAtCap(t *testing.T) {
trustedNode := newkey()
privateNode := newkey()
trustedID := enode.PubkeyToIDV4(&trustedNode.PublicKey)
privateID := enode.PubkeyToIDV4(&privateNode.PublicKey)
srv := &Server{
Config: Config{
PrivateKey: newkey(),
MaxPeers: 10,
NoDial: true,
NoDiscovery: true,
TrustedNodes: []*enode.Node{newNode(trustedID, "")},
PrivateNodes: []*enode.Node{newNode(privateID, "")},
Logger: testlog.Logger(t, log.LvlTrace),
},
}
Expand All @@ -260,6 +263,9 @@ func TestServerAtCap(t *testing.T) {
return &conn{fd: fd, transport: tx, flags: inboundConn, node: node, cont: make(chan error)}
}

// remove private node (it was added a static node at the beginning)
srv.RemovePeer(newNode(privateID, ""))

// Inject a few connections to fill up the peer set.
for i := 0; i < 10; i++ {
c := newconn(randomID())
Expand All @@ -282,6 +288,21 @@ func TestServerAtCap(t *testing.T) {
t.Error("Server did not set trusted flag")
}

// Try inserting a non-trusted connection.
anotherID = randomID()
c = newconn(anotherID)
if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != DiscTooManyPeers {
t.Error("wrong error for insert:", err)
}
// Try inserting the private node as trusted connection as well
c = newconn(privateID)
if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != nil {
t.Error("unexpected error for trusted conn @posthandshake:", err)
}
if !c.is(trustedConn) {
t.Error("Server did not set trusted flag")
}

// Remove from trusted set and try again
srv.RemoveTrustedPeer(newNode(trustedID, ""))
c = newconn(trustedID)
Expand All @@ -304,6 +325,8 @@ func TestServerPeerLimits(t *testing.T) {
srvkey := newkey()
clientkey := newkey()
clientnode := enode.NewV4(&clientkey.PublicKey, nil, 0, 0)
privatekey := newkey()
privatenode := enode.NewV4(&privatekey.PublicKey, nil, 0, 0)

var tp = &setupTransport{
pubkey: &clientkey.PublicKey,
Expand All @@ -316,12 +339,13 @@ func TestServerPeerLimits(t *testing.T) {

srv := &Server{
Config: Config{
PrivateKey: srvkey,
MaxPeers: 0,
NoDial: true,
NoDiscovery: true,
Protocols: []Protocol{discard},
Logger: testlog.Logger(t, log.LvlTrace),
PrivateKey: srvkey,
MaxPeers: 0,
NoDial: true,
NoDiscovery: true,
Protocols: []Protocol{discard},
PrivateNodes: []*enode.Node{privatenode},
Logger: testlog.Logger(t, log.LvlTrace),
},
newTransport: func(fd net.Conn, dialDest *ecdsa.PublicKey) transport { return tp },
}
Expand Down Expand Up @@ -363,6 +387,25 @@ func TestServerPeerLimits(t *testing.T) {
t.Errorf("unexpected close error: %q", tp.closeErr)
}
conn.Close()

srv.RemovePeer(privatenode)

// Check that server is full again.
conn, _ = net.Pipe()
srv.SetupConn(conn, flags, dialDest)
if tp.closeErr != DiscTooManyPeers {
t.Errorf("unexpected close error: %q", tp.closeErr)
}
conn.Close()

// Check that server allows a private peer despite being full.
dialDest = privatenode
conn, _ = net.Pipe()
srv.SetupConn(conn, flags, dialDest)
if tp.closeErr == DiscTooManyPeers {
t.Errorf("failed to bypass MaxPeers with private node: %q", tp.closeErr)
}
conn.Close()
}

func TestServerSetupConn(t *testing.T) {
Expand Down

0 comments on commit 2b28808

Please sign in to comment.