Skip to content

Commit 413ace3

Browse files
committed
eth, p2p: rename trusted nodes to static, drop inbound extra slots
1 parent 701591b commit 413ace3

File tree

5 files changed

+59
-57
lines changed

5 files changed

+59
-57
lines changed

eth/backend.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ var (
4141
discover.MustParseNode("enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"),
4242
}
4343

44-
// Path within <datadir> to search for the trusted node list
45-
trustedNodes = "trusted-nodes.json"
44+
// Path within <datadir> to search for the static node list
45+
staticNodes = "static-nodes.json"
4646
)
4747

4848
type Config struct {
@@ -102,23 +102,23 @@ func (cfg *Config) parseBootNodes() []*discover.Node {
102102
return ns
103103
}
104104

105-
// parseTrustedNodes parses a list of discovery node URLs either given literally,
105+
// parseStaticNodes parses a list of discovery node URLs either given literally,
106106
// or loaded from a .json file.
107-
func (cfg *Config) parseTrustedNodes() []*discover.Node {
108-
// Short circuit if no trusted node config is present
109-
path := filepath.Join(cfg.DataDir, trustedNodes)
107+
func (cfg *Config) parseStaticNodes() []*discover.Node {
108+
// Short circuit if no static node config is present
109+
path := filepath.Join(cfg.DataDir, staticNodes)
110110
if _, err := os.Stat(path); err != nil {
111111
return nil
112112
}
113-
// Load the trusted nodes from the config file
113+
// Load the static nodes from the config file
114114
blob, err := ioutil.ReadFile(path)
115115
if err != nil {
116-
glog.V(logger.Error).Infof("Failed to access trusted nodes: %v", err)
116+
glog.V(logger.Error).Infof("Failed to access static nodes: %v", err)
117117
return nil
118118
}
119119
nodelist := []string{}
120120
if err := json.Unmarshal(blob, &nodelist); err != nil {
121-
glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err)
121+
glog.V(logger.Error).Infof("Failed to load static nodes: %v", err)
122122
return nil
123123
}
124124
// Interpret the list as a discovery node array
@@ -129,7 +129,7 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node {
129129
}
130130
node, err := discover.ParseNode(url)
131131
if err != nil {
132-
glog.V(logger.Error).Infof("Trusted node URL %s: %v\n", url, err)
132+
glog.V(logger.Error).Infof("Static node URL %s: %v\n", url, err)
133133
continue
134134
}
135135
nodes = append(nodes, node)
@@ -288,7 +288,7 @@ func New(config *Config) (*Ethereum, error) {
288288
NAT: config.NAT,
289289
NoDial: !config.Dial,
290290
BootstrapNodes: config.parseBootNodes(),
291-
TrustedNodes: config.parseTrustedNodes(),
291+
StaticNodes: config.parseStaticNodes(),
292292
NodeDatabase: nodeDb,
293293
}
294294
if len(config.Port) > 0 {

p2p/handshake.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ type protoHandshake struct {
7070
// If dial is non-nil, the connection the local node is the initiator.
7171
// If atcap is true, the connection will be disconnected with DiscTooManyPeers
7272
// after the key exchange.
73-
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trust map[discover.NodeID]bool) (*conn, error) {
73+
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool) (*conn, error) {
7474
if dial == nil {
75-
return setupInboundConn(fd, prv, our, atcap, trust)
75+
return setupInboundConn(fd, prv, our, atcap)
7676
} else {
77-
return setupOutboundConn(fd, prv, our, dial, atcap, trust)
77+
return setupOutboundConn(fd, prv, our, dial, atcap)
7878
}
7979
}
8080

81-
func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, atcap bool, trust map[discover.NodeID]bool) (*conn, error) {
81+
func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, atcap bool) (*conn, error) {
8282
secrets, err := receiverEncHandshake(fd, prv, nil)
8383
if err != nil {
8484
return nil, fmt.Errorf("encryption handshake failed: %v", err)
8585
}
8686
rw := newRlpxFrameRW(fd, secrets)
87-
if atcap && !trust[secrets.RemoteID] {
87+
if atcap {
8888
SendItems(rw, discMsg, DiscTooManyPeers)
8989
return nil, errors.New("we have too many peers")
9090
}
@@ -99,13 +99,13 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, a
9999
return &conn{rw, rhs}, nil
100100
}
101101

102-
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trust map[discover.NodeID]bool) (*conn, error) {
102+
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool) (*conn, error) {
103103
secrets, err := initiatorEncHandshake(fd, prv, dial.ID, nil)
104104
if err != nil {
105105
return nil, fmt.Errorf("encryption handshake failed: %v", err)
106106
}
107107
rw := newRlpxFrameRW(fd, secrets)
108-
if atcap && !trust[secrets.RemoteID] {
108+
if atcap {
109109
SendItems(rw, discMsg, DiscTooManyPeers)
110110
return nil, errors.New("we have too many peers")
111111
}

p2p/handshake_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestSetupConn(t *testing.T) {
143143
done := make(chan struct{})
144144
go func() {
145145
defer close(done)
146-
conn0, err := setupConn(fd0, prv0, hs0, node1, false, nil)
146+
conn0, err := setupConn(fd0, prv0, hs0, node1, false)
147147
if err != nil {
148148
t.Errorf("outbound side error: %v", err)
149149
return
@@ -156,7 +156,7 @@ func TestSetupConn(t *testing.T) {
156156
}
157157
}()
158158

159-
conn1, err := setupConn(fd1, prv1, hs1, nil, false, nil)
159+
conn1, err := setupConn(fd1, prv1, hs1, nil, false)
160160
if err != nil {
161161
t.Fatalf("inbound side error: %v", err)
162162
}

p2p/server.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ type Server struct {
6060
// with the rest of the network.
6161
BootstrapNodes []*discover.Node
6262

63-
// Trusted nodes are used as privileged connections which are always accepted
64-
// and also always maintained.
65-
TrustedNodes []*discover.Node
63+
// Static nodes are used as pre-configured connections which are always
64+
// maintained and re-connected on disconnects.
65+
StaticNodes []*discover.Node
6666

6767
// NodeDatabase is the path to the database containing the previously seen
6868
// live nodes in the network.
@@ -100,11 +100,11 @@ type Server struct {
100100

101101
ourHandshake *protoHandshake
102102

103-
lock sync.RWMutex // protects running, peers and the trust fields
104-
running bool
105-
peers map[discover.NodeID]*Peer
106-
trusts map[discover.NodeID]*discover.Node // Map of currently trusted remote nodes
107-
trustDial chan *discover.Node // Dial request channel reserved for the trusted nodes
103+
lock sync.RWMutex // protects running, peers and the trust fields
104+
running bool
105+
peers map[discover.NodeID]*Peer
106+
statics map[discover.NodeID]*discover.Node // Map of currently static remote nodes
107+
staticDial chan *discover.Node // Dial request channel reserved for the static nodes
108108

109109
ntab *discover.Table
110110
listener net.Listener
@@ -114,7 +114,7 @@ type Server struct {
114114
peerWG sync.WaitGroup // active peer goroutines
115115
}
116116

117-
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool, map[discover.NodeID]bool) (*conn, error)
117+
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool) (*conn, error)
118118
type newPeerHook func(*Peer)
119119

120120
// Peers returns all connected peers.
@@ -144,7 +144,7 @@ func (srv *Server) AddPeer(node *discover.Node) {
144144
srv.lock.Lock()
145145
defer srv.lock.Unlock()
146146

147-
srv.trusts[node.ID] = node
147+
srv.statics[node.ID] = node
148148
}
149149

150150
// Broadcast sends an RLP-encoded message to all connected peers.
@@ -207,11 +207,11 @@ func (srv *Server) Start() (err error) {
207207
srv.peers = make(map[discover.NodeID]*Peer)
208208

209209
// Create the current trust map, and the associated dialing channel
210-
srv.trusts = make(map[discover.NodeID]*discover.Node)
211-
for _, node := range srv.TrustedNodes {
212-
srv.trusts[node.ID] = node
210+
srv.statics = make(map[discover.NodeID]*discover.Node)
211+
for _, node := range srv.StaticNodes {
212+
srv.statics[node.ID] = node
213213
}
214-
srv.trustDial = make(chan *discover.Node)
214+
srv.staticDial = make(chan *discover.Node)
215215

216216
if srv.setupFunc == nil {
217217
srv.setupFunc = setupConn
@@ -246,8 +246,8 @@ func (srv *Server) Start() (err error) {
246246
if srv.NoDial && srv.ListenAddr == "" {
247247
glog.V(logger.Warn).Infoln("I will be kind-of useless, neither dialing nor listening.")
248248
}
249-
// maintain the trusted peers
250-
go srv.trustedNodesLoop()
249+
// maintain the static peers
250+
go srv.staticNodesLoop()
251251

252252
srv.running = true
253253
return nil
@@ -342,20 +342,20 @@ func (srv *Server) listenLoop() {
342342
}
343343
}
344344

345-
// trustedNodesLoop is responsible for periodically checking that trusted
345+
// staticNodesLoop is responsible for periodically checking that static
346346
// connections are actually live, and requests dialing if not.
347-
func (srv *Server) trustedNodesLoop() {
347+
func (srv *Server) staticNodesLoop() {
348348
tick := time.Tick(trustedPeerCheckInterval)
349349
for {
350350
select {
351351
case <-srv.quit:
352352
return
353353

354354
case <-tick:
355-
// Collect all the non-connected trusted nodes
355+
// Collect all the non-connected static nodes
356356
needed := []*discover.Node{}
357357
srv.lock.RLock()
358-
for id, node := range srv.trusts {
358+
for id, node := range srv.statics {
359359
if _, ok := srv.peers[id]; !ok {
360360
needed = append(needed, node)
361361
}
@@ -364,9 +364,9 @@ func (srv *Server) trustedNodesLoop() {
364364

365365
// Try to dial each of them (don't hang if server terminates)
366366
for _, node := range needed {
367-
glog.V(logger.Debug).Infof("Dialing trusted peer %v", node)
367+
glog.V(logger.Debug).Infof("Dialing static peer %v", node)
368368
select {
369-
case srv.trustDial <- node:
369+
case srv.staticDial <- node:
370370
case <-srv.quit:
371371
return
372372
}
@@ -425,7 +425,7 @@ func (srv *Server) dialLoop() {
425425
// below MaxPeers.
426426
refresh.Reset(refreshPeersInterval)
427427
}
428-
case dest := <-srv.trustDial:
428+
case dest := <-srv.staticDial:
429429
dial(dest)
430430
case dests := <-findresults:
431431
for _, dest := range dests {
@@ -469,17 +469,17 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
469469
// the callers of startPeer added the peer to the wait group already.
470470
fd.SetDeadline(time.Now().Add(handshakeTimeout))
471471

472-
// Check capacity and trust list
472+
// Check capacity, but override for static nodes
473473
srv.lock.RLock()
474474
atcap := len(srv.peers) == srv.MaxPeers
475-
476-
trust := make(map[discover.NodeID]bool)
477-
for id, _ := range srv.trusts {
478-
trust[id] = true
475+
if dest != nil {
476+
if _, ok := srv.statics[dest.ID]; ok {
477+
atcap = false
478+
}
479479
}
480480
srv.lock.RUnlock()
481481

482-
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap, trust)
482+
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap)
483483
if err != nil {
484484
fd.Close()
485485
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)
@@ -535,14 +535,14 @@ func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
535535
// checkPeer verifies whether a peer looks promising and should be allowed/kept
536536
// in the pool, or if it's of no use.
537537
func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
538-
// First up, figure out if the peer is trusted
539-
_, trusted := srv.trusts[id]
538+
// First up, figure out if the peer is static
539+
_, static := srv.statics[id]
540540

541541
// Make sure the peer passes all required checks
542542
switch {
543543
case !srv.running:
544544
return false, DiscQuitting
545-
case !trusted && len(srv.peers) >= srv.MaxPeers:
545+
case !static && len(srv.peers) >= srv.MaxPeers:
546546
return false, DiscTooManyPeers
547547
case srv.peers[id] != nil:
548548
return false, DiscAlreadyConnected

p2p/server_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func startTestServer(t *testing.T, pf newPeerHook) *Server {
2222
ListenAddr: "127.0.0.1:0",
2323
PrivateKey: newkey(),
2424
newPeerHook: pf,
25-
setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trust map[discover.NodeID]bool) (*conn, error) {
25+
setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool) (*conn, error) {
2626
id := randomID()
2727
rw := newRlpxFrameRW(fd, secrets{
2828
MAC: zero16,
@@ -102,7 +102,7 @@ func TestServerDial(t *testing.T) {
102102

103103
// tell the server to connect
104104
tcpAddr := listener.Addr().(*net.TCPAddr)
105-
srv.trustDial <- &discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port}
105+
srv.staticDial <- &discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port}
106106

107107
select {
108108
case conn := <-accepted:
@@ -200,7 +200,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
200200
// Run the handshakes just like a real peer would.
201201
key := newkey()
202202
hs := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
203-
_, err = setupConn(conn, key, hs, srv.Self(), false, nil)
203+
_, err = setupConn(conn, key, hs, srv.Self(), false)
204204
if i == nconns-1 {
205205
// When handling the last connection, the server should
206206
// disconnect immediately instead of running the protocol
@@ -219,6 +219,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
219219
}
220220
}
221221

222+
/*
222223
// Tests that trusted peers and can connect above max peer caps.
223224
func TestServerTrustedPeers(t *testing.T) {
224225
defer testlog(t).detach()
@@ -250,7 +251,7 @@ func TestServerTrustedPeers(t *testing.T) {
250251
// Run the handshakes just like a real peer would, and wait for completion
251252
key := newkey()
252253
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
253-
if _, err = setupConn(conn, key, shake, server.Self(), false, nil); err != nil {
254+
if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
254255
t.Fatalf("conn %d: unexpected error: %v", i, err)
255256
}
256257
<-started
@@ -269,7 +270,7 @@ func TestServerTrustedPeers(t *testing.T) {
269270
defer conn.Close()
270271
271272
shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
272-
if _, err = setupConn(conn, key, shake, server.Self(), false, nil); err != nil {
273+
if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
273274
t.Fatalf("trusted node: unexpected error: %v", err)
274275
}
275276
select {
@@ -280,6 +281,7 @@ func TestServerTrustedPeers(t *testing.T) {
280281
t.Fatalf("trusted node timeout")
281282
}
282283
}
284+
*/
283285

284286
func newkey() *ecdsa.PrivateKey {
285287
key, err := crypto.GenerateKey()

0 commit comments

Comments
 (0)