From 01da330ef2571da1d4b33c8e0ecbfee65bb82e69 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 19 May 2020 18:08:21 -0700 Subject: [PATCH] fix: don't try to marshal a nil record fixes #939 --- p2p/protocol/identify/id.go | 5 +---- p2p/protocol/identify/id_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index afcbf95661..1eabf9eacf 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -428,9 +428,6 @@ func (ids *IDService) getSnapshot() *identifySnapshot { if !ids.disableSignedPeerRecord { if cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()); ok { snapshot.record = cab.GetPeerRecord(ids.Host.ID()) - if snapshot.record == nil { - log.Errorf("latest peer record does not exist. identify message incomplete!") - } } } snapshot.addrs = ids.Host.Addrs() @@ -465,7 +462,7 @@ func (ids *IDService) populateMessage( mes.ListenAddrs = append(mes.ListenAddrs, addr.Bytes()) } - if !ids.disableSignedPeerRecord { + if !ids.disableSignedPeerRecord && snapshot.record != nil { recBytes, err := snapshot.record.Marshal() if err != nil { log.Errorf("error marshaling peer record: %v", err) diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index e6092d6c07..a2a533c6b9 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -686,6 +686,37 @@ func TestUserAgent(t *testing.T) { } } +func TestNotListening(t *testing.T) { + // Make sure we don't panic if we're not listening on any addresses. + // + // https://github.com/libp2p/go-libp2p/issues/939 + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + h1, err := libp2p.New( + ctx, + libp2p.NoListenAddrs, + ) + if err != nil { + t.Fatal(err) + } + defer h1.Close() + + h2, err := libp2p.New( + ctx, + libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), + ) + if err != nil { + t.Fatal(err) + } + defer h2.Close() + + err = h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}) + if err != nil { + t.Fatal(err) + } +} + func TestSendPushIfDeltaNotSupported(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()