Skip to content

Commit

Permalink
fix: ignore corrupt peerstore data (#2859)
Browse files Browse the repository at this point in the history
When we can't load or parse peerstore data, ignore it
  • Loading branch information
achingbrain authored Nov 28, 2024
1 parent 0e176b9 commit f2f9008
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/peer-store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { bytesToPeer } from './utils/bytes-to-peer.js'
import { NAMESPACE_COMMON, peerIdToDatastoreKey } from './utils/peer-id-to-datastore-key.js'
import { toPeerPB } from './utils/to-peer-pb.js'
import type { AddressFilter, PersistentPeerStoreComponents, PersistentPeerStoreInit } from './index.js'
import type { PeerUpdate as PeerUpdateExternal, PeerId, Peer, PeerData, PeerQuery } from '@libp2p/interface'
import type { PeerUpdate as PeerUpdateExternal, PeerId, Peer, PeerData, PeerQuery, Logger } from '@libp2p/interface'
import type { Datastore, Key, Query } from 'interface-datastore'

/**
Expand Down Expand Up @@ -49,8 +49,10 @@ export class PersistentStore {
private readonly datastore: Datastore
public readonly lock: Mortice
private readonly addressFilter?: AddressFilter
private readonly log: Logger

constructor (components: PersistentPeerStoreComponents, init: PersistentPeerStoreInit = {}) {
this.log = components.logger.forComponent('libp2p:peer-store')
this.peerId = components.peerId
this.datastore = components.datastore
this.addressFilter = init.addressFilter
Expand Down Expand Up @@ -141,10 +143,8 @@ export class PersistentStore {
existingBuf,
existingPeer
}
} catch (err: any) {
if (err.name !== 'NotFoundError') {
throw err
}
} catch (err) {
this.log.error('invalid peer data found in peer store - %e', err)
}

return {}
Expand Down
32 changes: 31 additions & 1 deletion packages/peer-store/test/merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { expect } from 'aegir/chai'
import { MemoryDatastore } from 'datastore-core/memory'
import { pEvent } from 'p-event'
import { persistentPeerStore } from '../src/index.js'
import { peerIdToDatastoreKey } from '../src/utils/peer-id-to-datastore-key.js'
import type { TypedEventTarget, Libp2pEvents, PeerId, PeerStore, PeerData } from '@libp2p/interface'
import type { Datastore } from 'interface-datastore'

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

beforeEach(async () => {
peerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
otherPeerId = peerIdFromPrivateKey(await generateKeyPair('Ed25519'))
events = new TypedEventEmitter()
datastore = new MemoryDatastore()
peerStore = persistentPeerStore({
peerId,
events,
datastore: new MemoryDatastore(),
datastore,
logger: defaultLogger()
})
})
Expand Down Expand Up @@ -249,4 +253,30 @@ describe('merge', () => {
expect(updated).to.have.property('tags').that.deep.equals(original.tags)
expect(updated).to.have.property('protocols').that.deep.equals(original.protocols)
})

it('should ignore corrupt peer store data', async () => {
const badPeer: PeerData = {
multiaddrs: [
addr1
]
}
await peerStore.save(otherPeerId, badPeer)
const key = peerIdToDatastoreKey(otherPeerId)

// store unparsable data
await datastore.put(key, Uint8Array.from([0, 1, 2, 3, 4, 5]))

// update the peer
const peer: PeerData = {
multiaddrs: [
addr2
]
}
const updated = await peerStore.merge(otherPeerId, peer)

expect(updated).to.have.property('addresses').that.deep.equals([{
multiaddr: addr2,
isCertified: false
}])
})
})
12 changes: 11 additions & 1 deletion packages/peer-store/test/save.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type { TypedEventTarget, Libp2pEvents, PeerId, PeerStore, PeerData, PeerU

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

describe('save', () => {
let peerId: PeerId
Expand Down Expand Up @@ -218,7 +220,9 @@ describe('save', () => {
const peer: PeerData = {
multiaddrs: [
addr1,
addr2
addr2,
addr3,
addr4
],
metadata: {
foo: Uint8Array.from([0, 1, 2])
Expand All @@ -240,6 +244,12 @@ describe('save', () => {
}, {
multiaddr: addr2,
isCertified: false
}, {
multiaddr: addr3,
isCertified: false
}, {
multiaddr: addr4,
isCertified: false
}])
expect(saved).to.have.property('metadata').that.deep.equals(
new Map([
Expand Down

0 comments on commit f2f9008

Please sign in to comment.