From f2e0abf1dc23544c7ce5cfb0bdd0fd46dc96ce1e Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 1 Jan 2018 20:21:11 -0500 Subject: [PATCH] p2p: reorder some checks in addPeer; add comments to NodeInfo --- p2p/peer.go | 1 + p2p/switch.go | 20 ++++++++++---------- p2p/types.go | 14 +++++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/p2p/peer.go b/p2p/peer.go index 91824dc8f..e2e73f0f3 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -304,6 +304,7 @@ func (p *peer) Set(key string, data interface{}) { } // Key returns the peer's id key. +// TODO: call this ID func (p *peer) Key() string { return p.nodeInfo.ListenAddr // XXX: should probably be PubKey.KeyString() } diff --git a/p2p/switch.go b/p2p/switch.go index fde216429..d4e3b3484 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -232,10 +232,15 @@ func (sw *Switch) OnStop() { // NOTE: If error is returned, caller is responsible for calling peer.CloseConn() func (sw *Switch) addPeer(peer *peer) error { + // Avoid self + if sw.nodeInfo.PubKey.Equals(peer.PubKey().Wrap()) { + return errors.New("Ignoring connection from self") + } + + // Filter peer against white list if err := sw.FilterConnByAddr(peer.Addr()); err != nil { return err } - if err := sw.FilterConnByPubKey(peer.PubKey()); err != nil { return err } @@ -244,9 +249,10 @@ func (sw *Switch) addPeer(peer *peer) error { return err } - // Avoid self - if sw.nodeInfo.PubKey.Equals(peer.PubKey().Wrap()) { - return errors.New("Ignoring connection from self") + // Avoid duplicate + if sw.peers.Has(peer.Key()) { + return ErrSwitchDuplicatePeer + } // Check version, chain id @@ -254,12 +260,6 @@ func (sw *Switch) addPeer(peer *peer) error { return err } - // Check for duplicate peer - if sw.peers.Has(peer.Key()) { - return ErrSwitchDuplicatePeer - - } - // Start peer if sw.IsRunning() { sw.startInitPeer(peer) diff --git a/p2p/types.go b/p2p/types.go index 63494d9cd..860132902 100644 --- a/p2p/types.go +++ b/p2p/types.go @@ -12,13 +12,13 @@ import ( const maxNodeInfoSize = 10240 // 10Kb type NodeInfo struct { - PubKey crypto.PubKey `json:"pub_key"` - Moniker string `json:"moniker"` - Network string `json:"network"` - RemoteAddr string `json:"remote_addr"` - ListenAddr string `json:"listen_addr"` - Version string `json:"version"` // major.minor.revision - Other []string `json:"other"` // other application specific data + PubKey crypto.PubKey `json:"pub_key"` // authenticated pubkey + Moniker string `json:"moniker"` // arbitrary moniker + Network string `json:"network"` // network/chain ID + RemoteAddr string `json:"remote_addr"` // address for the connection + ListenAddr string `json:"listen_addr"` // accepting incoming + Version string `json:"version"` // major.minor.revision + Other []string `json:"other"` // other application specific data } // CONTRACT: two nodes are compatible if the major/minor versions match and network match