Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
fix: delete addrs when "updating" them to zero
Browse files Browse the repository at this point in the history
1. Fix expiration check to check "not before" instead of after.
Otherwise, something that is expiring "now" won't count as expired. The
datastore-backed peerstore already had the correct logic.
2. Short-circuit updating the TTL to 0 and just delete the records.

Note: this wasn't causing problems on Linux (likely due to monotonic
clocks?) but was causing go-libp2p tests to fail reliably on Windows.
  • Loading branch information
Stebalien committed Apr 30, 2021
1 parent 74ed1a9 commit cce0adb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type expiringAddr struct {
}

func (e *expiringAddr) ExpiredBy(t time.Time) bool {
return t.After(e.Expires)
return !t.Before(e.Expires)
}

type peerRecordState struct {
Expand Down Expand Up @@ -315,9 +315,15 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
defer s.Unlock()
exp := time.Now().Add(newTTL)
amap, found := s.addrs[p]
if found {
for k, a := range amap {
if oldTTL == a.TTL {
if !found {
return
}

for k, a := range amap {
if oldTTL == a.TTL {
if newTTL == 0 {
delete(amap, k)
} else {
a.TTL = newTTL
a.Expires = exp
amap[k] = a
Expand Down
12 changes: 12 additions & 0 deletions test/addr_book_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
m.UpdateAddrs(id, time.Hour, time.Minute)
})

t.Run("update to 0 clears addrs", func(t *testing.T) {
id := GeneratePeerIDs(1)[0]
addrs := GenerateAddrs(1)

// Shouldn't panic.
m.SetAddrs(id, addrs, time.Hour)
m.UpdateAddrs(id, time.Hour, 0)
if len(m.Addrs(id)) != 0 {
t.Error("expected no addresses")
}
})

t.Run("update ttls successfully", func(t *testing.T) {
ids := GeneratePeerIDs(2)
addrs1, addrs2 := GenerateAddrs(2), GenerateAddrs(2)
Expand Down

0 comments on commit cce0adb

Please sign in to comment.