From 0a6a57997b5d0c39f6ccfdbe6d6f6e2fb3158173 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 16 Sep 2024 12:51:12 +0530 Subject: [PATCH] rename Expires -> Expiry --- p2p/host/peerstore/pstoremem/addr_book.go | 30 +++++++++---------- .../peerstore/pstoremem/addr_book_test.go | 26 ++++++++-------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/p2p/host/peerstore/pstoremem/addr_book.go b/p2p/host/peerstore/pstoremem/addr_book.go index 34d91a2043..b37aee83c5 100644 --- a/p2p/host/peerstore/pstoremem/addr_book.go +++ b/p2p/host/peerstore/pstoremem/addr_book.go @@ -21,16 +21,16 @@ var SignedPeerRecordBound = 100_000 var log = logging.Logger("peerstore") type expiringAddr struct { - Addr ma.Multiaddr - TTL time.Duration - Expires time.Time - Peer peer.ID + Addr ma.Multiaddr + TTL time.Duration + Expiry time.Time + Peer peer.ID // to sort by expiry time heapIndex int } func (e *expiringAddr) ExpiredBy(t time.Time) bool { - return !t.Before(e.Expires) + return !t.Before(e.Expiry) } type peerRecordState struct { @@ -54,7 +54,7 @@ func newPeerAddrs() peerAddrs { func (pa *peerAddrs) Len() int { return len(pa.expiringHeap) } func (pa *peerAddrs) Less(i, j int) bool { - return pa.expiringHeap[i].Expires.Before(pa.expiringHeap[j].Expires) + return pa.expiringHeap[i].Expiry.Before(pa.expiringHeap[j].Expiry) } func (pa *peerAddrs) Swap(i, j int) { pa.expiringHeap[i], pa.expiringHeap[j] = pa.expiringHeap[j], pa.expiringHeap[i] @@ -81,7 +81,6 @@ func (pa *peerAddrs) Pop() any { delete(pa.Addrs, a.Peer) } } - return a } @@ -112,11 +111,11 @@ func (pa *peerAddrs) NextExpiry() time.Time { if len(pa.expiringHeap) == 0 { return time.Time{} } - return pa.expiringHeap[0].Expires + return pa.expiringHeap[0].Expiry } func (pa *peerAddrs) PopIfExpired(now time.Time) (*expiringAddr, bool) { - // Use !Before and not After to ensure that we expire *at* now and not just after now. + // Use `!Before` instead of `After` to ensure that we expire *at* now, and not *just after now*. if len(pa.expiringHeap) > 0 && !now.Before(pa.NextExpiry()) { a := heap.Pop(pa) return a.(*expiringAddr), true @@ -304,7 +303,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl a, found := mab.addrs.FindAddr(p, addr) if !found { // not found, announce it. - entry := &expiringAddr{Addr: addr, Expires: exp, TTL: ttl, Peer: p} + entry := &expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p} heap.Push(&mab.addrs, entry) mab.subManager.BroadcastAddr(p, addr) } else { @@ -314,9 +313,9 @@ func (mab *memoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl changed = true a.TTL = ttl } - if exp.After(a.Expires) { + if exp.After(a.Expiry) { changed = true - a.Expires = exp + a.Expiry = exp } if changed { mab.addrs.Fix(a) @@ -353,7 +352,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du if a, found := mab.addrs.FindAddr(p, addr); found { if ttl > 0 { a.Addr = addr - a.Expires = exp + a.Expiry = exp a.TTL = ttl mab.addrs.Fix(a) mab.subManager.BroadcastAddr(p, addr) @@ -362,7 +361,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du } } else { if ttl > 0 { - heap.Push(&mab.addrs, &expiringAddr{Addr: addr, Expires: exp, TTL: ttl, Peer: p}) + heap.Push(&mab.addrs, &expiringAddr{Addr: addr, Expiry: exp, TTL: ttl, Peer: p}) mab.subManager.BroadcastAddr(p, addr) } } @@ -384,7 +383,7 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t mab.addrs.Delete(a) } else { a.TTL = newTTL - a.Expires = exp + a.Expiry = exp mab.addrs.Fix(a) } } @@ -411,7 +410,6 @@ func validAddrs(now time.Time, amap map[string]*expiringAddr) []ma.Multiaddr { good = append(good, m.Addr) } } - return good } diff --git a/p2p/host/peerstore/pstoremem/addr_book_test.go b/p2p/host/peerstore/pstoremem/addr_book_test.go index caa1bb293b..a6da9d7bdd 100644 --- a/p2p/host/peerstore/pstoremem/addr_book_test.go +++ b/p2p/host/peerstore/pstoremem/addr_book_test.go @@ -22,8 +22,8 @@ func TestPeerAddrsNextExpiry(t *testing.T) { // t1 is before t2 t1 := time.Time{}.Add(1 * time.Second) t2 := time.Time{}.Add(2 * time.Second) - heap.Push(pa, &expiringAddr{Addr: a1, Expires: t1, TTL: 10 * time.Second, Peer: "p1"}) - heap.Push(pa, &expiringAddr{Addr: a2, Expires: t2, TTL: 10 * time.Second, Peer: "p2"}) + heap.Push(pa, &expiringAddr{Addr: a1, Expiry: t1, TTL: 10 * time.Second, Peer: "p1"}) + heap.Push(pa, &expiringAddr{Addr: a2, Expiry: t2, TTL: 10 * time.Second, Peer: "p2"}) if pa.NextExpiry() != t1 { t.Fatal("expiry should be set to t1, got", pa.NextExpiry()) @@ -36,7 +36,7 @@ func peerAddrsInput(n int) []*expiringAddr { a := ma.StringCast(fmt.Sprintf("/ip4/1.2.3.4/udp/%d/quic-v1", i)) e := time.Time{}.Add(time.Duration(i) * time.Second) // expiries are in reverse order p := peer.ID(fmt.Sprintf("p%d", i)) - expiringAddrs[i] = &expiringAddr{Addr: a, Expires: e, TTL: 10 * time.Second, Peer: p} + expiringAddrs[i] = &expiringAddr{Addr: a, Expiry: e, TTL: 10 * time.Second, Peer: p} } return expiringAddrs } @@ -52,11 +52,11 @@ func TestPeerAddrsHeapProperty(t *testing.T) { } for i := 0; i < N; i++ { - ea, ok := pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok := pa.PopIfExpired(expiringAddrs[i].Expiry) require.True(t, ok, "pos: %d", i) require.Equal(t, ea.Addr, expiringAddrs[i].Addr) - ea, ok = pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok = pa.PopIfExpired(expiringAddrs[i].Expiry) require.False(t, ok) require.Nil(t, ea) } @@ -78,7 +78,7 @@ func TestPeerAddrsHeapPropertyDeletions(t *testing.T) { } for i := 0; i < N; i++ { - ea, ok := pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok := pa.PopIfExpired(expiringAddrs[i].Expiry) if i%3 == 0 { require.False(t, ok) require.Nil(t, ea) @@ -87,7 +87,7 @@ func TestPeerAddrsHeapPropertyDeletions(t *testing.T) { require.Equal(t, ea.Addr, expiringAddrs[i].Addr) } - ea, ok = pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok = pa.PopIfExpired(expiringAddrs[i].Expiry) require.False(t, ok) require.Nil(t, ea) } @@ -106,7 +106,7 @@ func TestPeerAddrsHeapPropertyUpdates(t *testing.T) { // update every 3rd element to expire at the end var endElements []ma.Multiaddr for i := 0; i < N; i += 3 { - expiringAddrs[i].Expires = time.Time{}.Add(1000_000 * time.Second) + expiringAddrs[i].Expiry = time.Time{}.Add(1000_000 * time.Second) pa.Fix(expiringAddrs[i]) endElements = append(endElements, expiringAddrs[i].Addr) } @@ -115,11 +115,11 @@ func TestPeerAddrsHeapPropertyUpdates(t *testing.T) { if i%3 == 0 { continue // skip the elements at the end } - ea, ok := pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok := pa.PopIfExpired(expiringAddrs[i].Expiry) require.True(t, ok, "pos: %d", i) require.Equal(t, ea.Addr, expiringAddrs[i].Addr) - ea, ok = pa.PopIfExpired(expiringAddrs[i].Expires) + ea, ok = pa.PopIfExpired(expiringAddrs[i].Expiry) require.False(t, ok) require.Nil(t, ea) } @@ -144,7 +144,7 @@ func TestPeerAddrsExpiry(t *testing.T) { const N = 5 expiringAddrs := peerAddrsInput(N) for i := 0; i < N; i++ { - expiringAddrs[i].Expires = time.Time{}.Add(time.Duration(1+rand.Intn(N)) * time.Second) + expiringAddrs[i].Expiry = time.Time{}.Add(time.Duration(1+rand.Intn(N)) * time.Second) } for i := 0; i < N; i++ { heap.Push(pa, expiringAddrs[i]) @@ -153,7 +153,7 @@ func TestPeerAddrsExpiry(t *testing.T) { expiry := time.Time{}.Add(time.Duration(1+rand.Intn(N)) * time.Second) expected := []ma.Multiaddr{} for i := 0; i < N; i++ { - if !expiry.Before(expiringAddrs[i].Expires) { + if !expiry.Before(expiringAddrs[i].Expiry) { expected = append(expected, expiringAddrs[i].Addr) } } @@ -167,7 +167,7 @@ func TestPeerAddrsExpiry(t *testing.T) { } expiries := []int{} for i := 0; i < N; i++ { - expiries = append(expiries, expiringAddrs[i].Expires.Second()) + expiries = append(expiries, expiringAddrs[i].Expiry.Second()) } require.ElementsMatch(t, expected, got, "failed for input: element expiries: %v, expiry: %v", expiries, expiry.Second()) }