Skip to content

Commit f2f9008

Browse files
authored
fix: ignore corrupt peerstore data (#2859)
When we can't load or parse peerstore data, ignore it
1 parent 0e176b9 commit f2f9008

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

packages/peer-store/src/store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { bytesToPeer } from './utils/bytes-to-peer.js'
99
import { NAMESPACE_COMMON, peerIdToDatastoreKey } from './utils/peer-id-to-datastore-key.js'
1010
import { toPeerPB } from './utils/to-peer-pb.js'
1111
import type { AddressFilter, PersistentPeerStoreComponents, PersistentPeerStoreInit } from './index.js'
12-
import type { PeerUpdate as PeerUpdateExternal, PeerId, Peer, PeerData, PeerQuery } from '@libp2p/interface'
12+
import type { PeerUpdate as PeerUpdateExternal, PeerId, Peer, PeerData, PeerQuery, Logger } from '@libp2p/interface'
1313
import type { Datastore, Key, Query } from 'interface-datastore'
1414

1515
/**
@@ -49,8 +49,10 @@ export class PersistentStore {
4949
private readonly datastore: Datastore
5050
public readonly lock: Mortice
5151
private readonly addressFilter?: AddressFilter
52+
private readonly log: Logger
5253

5354
constructor (components: PersistentPeerStoreComponents, init: PersistentPeerStoreInit = {}) {
55+
this.log = components.logger.forComponent('libp2p:peer-store')
5456
this.peerId = components.peerId
5557
this.datastore = components.datastore
5658
this.addressFilter = init.addressFilter
@@ -141,10 +143,8 @@ export class PersistentStore {
141143
existingBuf,
142144
existingPeer
143145
}
144-
} catch (err: any) {
145-
if (err.name !== 'NotFoundError') {
146-
throw err
147-
}
146+
} catch (err) {
147+
this.log.error('invalid peer data found in peer store - %e', err)
148148
}
149149

150150
return {}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { expect } from 'aegir/chai'
1010
import { MemoryDatastore } from 'datastore-core/memory'
1111
import { pEvent } from 'p-event'
1212
import { persistentPeerStore } from '../src/index.js'
13+
import { peerIdToDatastoreKey } from '../src/utils/peer-id-to-datastore-key.js'
1314
import type { TypedEventTarget, Libp2pEvents, PeerId, PeerStore, PeerData } from '@libp2p/interface'
15+
import type { Datastore } from 'interface-datastore'
1416

1517
const addr1 = multiaddr('/ip4/127.0.0.1/tcp/8000')
1618
const addr2 = multiaddr('/ip4/20.0.0.1/tcp/8001')
@@ -21,15 +23,17 @@ describe('merge', () => {
2123
let otherPeerId: PeerId
2224
let peerStore: PeerStore
2325
let events: TypedEventTarget<Libp2pEvents>
26+
let datastore: Datastore
2427

2528
beforeEach(async () => {
2629
peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
2730
otherPeerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
2831
events = new TypedEventEmitter()
32+
datastore = new MemoryDatastore()
2933
peerStore = persistentPeerStore({
3034
peerId,
3135
events,
32-
datastore: new MemoryDatastore(),
36+
datastore,
3337
logger: defaultLogger()
3438
})
3539
})
@@ -249,4 +253,30 @@ describe('merge', () => {
249253
expect(updated).to.have.property('tags').that.deep.equals(original.tags)
250254
expect(updated).to.have.property('protocols').that.deep.equals(original.protocols)
251255
})
256+
257+
it('should ignore corrupt peer store data', async () => {
258+
const badPeer: PeerData = {
259+
multiaddrs: [
260+
addr1
261+
]
262+
}
263+
await peerStore.save(otherPeerId, badPeer)
264+
const key = peerIdToDatastoreKey(otherPeerId)
265+
266+
// store unparsable data
267+
await datastore.put(key, Uint8Array.from([0, 1, 2, 3, 4, 5]))
268+
269+
// update the peer
270+
const peer: PeerData = {
271+
multiaddrs: [
272+
addr2
273+
]
274+
}
275+
const updated = await peerStore.merge(otherPeerId, peer)
276+
277+
expect(updated).to.have.property('addresses').that.deep.equals([{
278+
multiaddr: addr2,
279+
isCertified: false
280+
}])
281+
})
252282
})

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import type { TypedEventTarget, Libp2pEvents, PeerId, PeerStore, PeerData, PeerU
1717

1818
const addr1 = multiaddr('/ip4/127.0.0.1/tcp/8000')
1919
const addr2 = multiaddr('/ip4/20.0.0.1/tcp/8001')
20+
const addr3 = multiaddr('/ip6/2a00:23c6:14b1:7e00:440d:76a7:f9f:b53f/tcp/8001')
21+
const addr4 = multiaddr('/ip6/fdad:bda1:c508:6261:1e:bea5:2ae6:fef0/tcp/8001')
2022

2123
describe('save', () => {
2224
let peerId: PeerId
@@ -218,7 +220,9 @@ describe('save', () => {
218220
const peer: PeerData = {
219221
multiaddrs: [
220222
addr1,
221-
addr2
223+
addr2,
224+
addr3,
225+
addr4
222226
],
223227
metadata: {
224228
foo: Uint8Array.from([0, 1, 2])
@@ -240,6 +244,12 @@ describe('save', () => {
240244
}, {
241245
multiaddr: addr2,
242246
isCertified: false
247+
}, {
248+
multiaddr: addr3,
249+
isCertified: false
250+
}, {
251+
multiaddr: addr4,
252+
isCertified: false
243253
}])
244254
expect(saved).to.have.property('metadata').that.deep.equals(
245255
new Map([

0 commit comments

Comments
 (0)