From aafaf38683c77ac33b26d01bd1a9b8628b2c2a13 Mon Sep 17 00:00:00 2001 From: jbenet Date: Sun, 11 Sep 2016 19:04:08 -0400 Subject: [PATCH] identify: handle case where local peer.ID is empty --- p2p/protocol/identify/id.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 6b74ba7a..d947901d 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -166,6 +166,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) { } log.Debugf("%s sent listen addrs to %s: %s", c.LocalPeer(), c.RemotePeer(), laddrs) + // set our public key ownKey := ids.Host.Peerstore().PubKey(ids.Host.ID()) if ownKey == nil { log.Errorf("did not have own public key in Peerstore") @@ -258,8 +259,21 @@ func (ids *IDService) consumeReceivedPubKey(c inet.Conn, kb []byte) { log.Debugf("%s cannot get peer.ID from key of remote peer: %s, %s", lp, rp, err) return } + if np != rp { - log.Errorf("%s received key for remote peer %s mismatch: %s", lp, rp, np) + // if the newKey's peer.ID does not match known peer.ID... + + if rp == "" && np != "" { + // if local peerid is empty, then use the new, sent key. + err := ids.Host.Peerstore().AddPubKey(rp, newKey) + if err != nil { + log.Debugf("%s could not add key for %s to peerstore: %s", lp, rp, err) + } + + } else { + // we have a local peer.ID and it does not match the sent key... error. + log.Errorf("%s received key for remote peer %s mismatch: %s", lp, rp, np) + } return }