Skip to content

Commit

Permalink
rename Expires -> Expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 16, 2024
1 parent 253dfed commit 0a6a579
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
30 changes: 14 additions & 16 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand All @@ -81,7 +81,6 @@ func (pa *peerAddrs) Pop() any {
delete(pa.Addrs, a.Peer)
}
}

return a
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -411,7 +410,6 @@ func validAddrs(now time.Time, amap map[string]*expiringAddr) []ma.Multiaddr {
good = append(good, m.Addr)
}
}

return good
}

Expand Down
26 changes: 13 additions & 13 deletions p2p/host/peerstore/pstoremem/addr_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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])
Expand All @@ -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)
}
}
Expand All @@ -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())
}
Expand Down

0 comments on commit 0a6a579

Please sign in to comment.