Skip to content

Commit 2f2322a

Browse files
authored
fix: do not expire self multiaddrs (#3053)
Do not apply address/record expiry to the peer store entry for the self peer. Fixes #3051
1 parent 95909c3 commit 2f2322a

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

packages/peer-store/src/store.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ export class PersistentStore {
100100
const buf = await this.datastore.get(key)
101101
const peer = PeerPB.decode(buf)
102102

103-
if (this.#peerIsExpired(peer)) {
103+
if (this.#peerIsExpired(peerId, peer)) {
104104
await this.datastore.delete(key)
105105
throw new NotFoundError()
106106
}
107107

108-
return pbToPeer(peerId, peer, this.maxAddressAge)
108+
return pbToPeer(peerId, peer, this.peerId.equals(peerId) ? Infinity : this.maxAddressAge)
109109
}
110110

111111
async save (peerId: PeerId, data: PeerData): Promise<PeerUpdate> {
@@ -152,12 +152,12 @@ export class PersistentStore {
152152
const peer = PeerPB.decode(value)
153153

154154
// remove expired peer
155-
if (this.#peerIsExpired(peer)) {
155+
if (this.#peerIsExpired(peerId, peer)) {
156156
await this.datastore.delete(key)
157157
continue
158158
}
159159

160-
yield pbToPeer(peerId, peer, this.maxAddressAge)
160+
yield pbToPeer(peerId, peer, this.peerId.equals(peerId) ? Infinity : this.maxAddressAge)
161161
}
162162
}
163163

@@ -168,7 +168,7 @@ export class PersistentStore {
168168
const peerPB = PeerPB.decode(buf)
169169

170170
// remove expired peer
171-
if (this.#peerIsExpired(peerPB)) {
171+
if (this.#peerIsExpired(peerId, peerPB)) {
172172
await this.datastore.delete(key)
173173
throw new NotFoundError()
174174
}
@@ -198,11 +198,15 @@ export class PersistentStore {
198198
}
199199
}
200200

201-
#peerIsExpired (peer: PeerPB): boolean {
201+
#peerIsExpired (peerId: PeerId, peer: PeerPB): boolean {
202202
if (peer.updated == null) {
203203
return true
204204
}
205205

206+
if (this.peerId.equals(peerId)) {
207+
return false
208+
}
209+
206210
const expired = peer.updated < (Date.now() - this.maxPeerAge)
207211
const minAddressObserved = Date.now() - this.maxAddressAge
208212
const addrs = peer.addresses.filter(addr => {

packages/peer-store/test/index.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,26 @@ describe('PersistentPeerStore', () => {
315315
.with.lengthOf(0, 'did not expire multiaddrs')
316316
})
317317

318+
it('should not expire self peer multiaddrs', async () => {
319+
const peerStore = persistentPeerStore(components, {
320+
maxAddressAge: 100
321+
})
322+
323+
await peerStore.patch(components.peerId, {
324+
multiaddrs: [
325+
multiaddr('/ip4/123.123.123.123/tcp/1234')
326+
]
327+
})
328+
329+
await expect(peerStore.get(components.peerId)).to.eventually.have.property('addresses')
330+
.with.lengthOf(1)
331+
332+
await delay(500)
333+
334+
await expect(peerStore.get(components.peerId)).to.eventually.have.property('addresses')
335+
.with.lengthOf(1, 'deleted own multiaddrs')
336+
})
337+
318338
it('should evict expired peers from .has', async () => {
319339
const peerStore = persistentPeerStore(components, {
320340
maxAddressAge: 50,

0 commit comments

Comments
 (0)