From b88d825b545ffca0e20a75ab07e335e0fc2c18d9 Mon Sep 17 00:00:00 2001
From: achingbrain <alex@achingbrain.net>
Date: Tue, 18 Mar 2025 15:02:07 +0100
Subject: [PATCH] fix: do not expire self multiaddrs

Do not apply address/record expiry to the peer store entry for the
self peer.

Fixes #3051
---
 packages/peer-store/src/store.ts       | 16 ++++++++++------
 packages/peer-store/test/index.spec.ts | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/packages/peer-store/src/store.ts b/packages/peer-store/src/store.ts
index 2b6ba86ba9..e468951425 100644
--- a/packages/peer-store/src/store.ts
+++ b/packages/peer-store/src/store.ts
@@ -100,12 +100,12 @@ export class PersistentStore {
     const buf = await this.datastore.get(key)
     const peer = PeerPB.decode(buf)
 
-    if (this.#peerIsExpired(peer)) {
+    if (this.#peerIsExpired(peerId, peer)) {
       await this.datastore.delete(key)
       throw new NotFoundError()
     }
 
-    return pbToPeer(peerId, peer, this.maxAddressAge)
+    return pbToPeer(peerId, peer, this.peerId.equals(peerId) ? Infinity : this.maxAddressAge)
   }
 
   async save (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
@@ -152,12 +152,12 @@ export class PersistentStore {
       const peer = PeerPB.decode(value)
 
       // remove expired peer
-      if (this.#peerIsExpired(peer)) {
+      if (this.#peerIsExpired(peerId, peer)) {
         await this.datastore.delete(key)
         continue
       }
 
-      yield pbToPeer(peerId, peer, this.maxAddressAge)
+      yield pbToPeer(peerId, peer, this.peerId.equals(peerId) ? Infinity : this.maxAddressAge)
     }
   }
 
@@ -168,7 +168,7 @@ export class PersistentStore {
       const peerPB = PeerPB.decode(buf)
 
       // remove expired peer
-      if (this.#peerIsExpired(peerPB)) {
+      if (this.#peerIsExpired(peerId, peerPB)) {
         await this.datastore.delete(key)
         throw new NotFoundError()
       }
@@ -198,11 +198,15 @@ export class PersistentStore {
     }
   }
 
-  #peerIsExpired (peer: PeerPB): boolean {
+  #peerIsExpired (peerId: PeerId, peer: PeerPB): boolean {
     if (peer.updated == null) {
       return true
     }
 
+    if (this.peerId.equals(peerId)) {
+      return false
+    }
+
     const expired = peer.updated < (Date.now() - this.maxPeerAge)
     const minAddressObserved = Date.now() - this.maxAddressAge
     const addrs = peer.addresses.filter(addr => {
diff --git a/packages/peer-store/test/index.spec.ts b/packages/peer-store/test/index.spec.ts
index e1c33ff6a5..5dc8fd20f7 100644
--- a/packages/peer-store/test/index.spec.ts
+++ b/packages/peer-store/test/index.spec.ts
@@ -315,6 +315,26 @@ describe('PersistentPeerStore', () => {
         .with.lengthOf(0, 'did not expire multiaddrs')
     })
 
+    it('should not expire self peer multiaddrs', async () => {
+      const peerStore = persistentPeerStore(components, {
+        maxAddressAge: 100
+      })
+
+      await peerStore.patch(components.peerId, {
+        multiaddrs: [
+          multiaddr('/ip4/123.123.123.123/tcp/1234')
+        ]
+      })
+
+      await expect(peerStore.get(components.peerId)).to.eventually.have.property('addresses')
+        .with.lengthOf(1)
+
+      await delay(500)
+
+      await expect(peerStore.get(components.peerId)).to.eventually.have.property('addresses')
+        .with.lengthOf(1, 'deleted own multiaddrs')
+    })
+
     it('should evict expired peers from .has', async () => {
       const peerStore = persistentPeerStore(components, {
         maxAddressAge: 50,