From 21ce5856b3441dd6d5caca16c7a22c0ab3b27d86 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 23 Jan 2018 21:26:19 -0500 Subject: [PATCH] p2p: notes about ListenAddr --- p2p/node_info.go | 23 ++++++----------------- p2p/peer.go | 2 -- p2p/switch.go | 2 ++ 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/p2p/node_info.go b/p2p/node_info.go index 552c464d9..72873add8 100644 --- a/p2p/node_info.go +++ b/p2p/node_info.go @@ -2,8 +2,6 @@ package p2p import ( "fmt" - "net" - "strconv" "strings" crypto "github.com/tendermint/go-crypto" @@ -42,7 +40,8 @@ func (info NodeInfo) Validate(pubKey crypto.PubKey) error { return nil } -// CONTRACT: two nodes are compatible if the major/minor versions match and network match +// CompatibleWith checks if two NodeInfo are compatible with eachother. +// CONTRACT: two nodes are compatible if the major/minor versions match and network match. func (info NodeInfo) CompatibleWith(other NodeInfo) error { iMajor, iMinor, _, iErr := splitVersion(info.Version) oMajor, oMinor, _, oErr := splitVersion(other.Version) @@ -79,6 +78,10 @@ func (info NodeInfo) ID() ID { return PubKeyToID(info.PubKey) } +// NetAddress returns a NetAddress derived from the NodeInfo - +// it includes the authenticated peer ID and the self-reported +// ListenAddr. Note that the ListenAddr is not authenticated and +// may not match that address actually dialed if its an outbound peer. func (info NodeInfo) NetAddress() *NetAddress { id := PubKeyToID(info.PubKey) addr := info.ListenAddr @@ -89,20 +92,6 @@ func (info NodeInfo) NetAddress() *NetAddress { return netAddr } -func (info NodeInfo) ListenHost() string { - host, _, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas - return host -} - -func (info NodeInfo) ListenPort() int { - _, port, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas - port_i, err := strconv.Atoi(port) - if err != nil { - return -1 - } - return port_i -} - func (info NodeInfo) String() string { return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other) } diff --git a/p2p/peer.go b/p2p/peer.go index 60f9dceba..e427b0d95 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -269,8 +269,6 @@ func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) err return errors.Wrap(err, "Error removing deadline") } - // TODO: fix the peerNodeInfo.ListenAddr - p.nodeInfo = peerNodeInfo return nil } diff --git a/p2p/switch.go b/p2p/switch.go index f29d1b273..fb9a6ac25 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -267,6 +267,8 @@ func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) { // If no success after all that, it stops trying, and leaves it // to the PEX/Addrbook to find the peer again func (sw *Switch) reconnectToPeer(peer Peer) { + // NOTE this will connect to the self reported address, + // not necessarily the original we dialed netAddr := peer.NodeInfo().NetAddress() start := time.Now() sw.Logger.Info("Reconnecting to peer", "peer", peer)