Skip to content

Commit

Permalink
fix: don't try to marshal a nil record
Browse files Browse the repository at this point in the history
fixes #939
  • Loading branch information
Stebalien committed May 20, 2020
1 parent aa60461 commit 01da330
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 1 addition & 4 deletions p2p/protocol/identify/id.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions p2p/protocol/identify/id_test.go
Expand Up @@ -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()
Expand Down

0 comments on commit 01da330

Please sign in to comment.