mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-26 07:21:36 +00:00
feat: async peerstore backed by datastores (#1058)
We have a peerstore that keeps all data for all observed peers in memory with no eviction. This is fine when you don't discover many peers but when using the DHT you encounter a significant number of peers so our peer storage grows and grows over time. We have a persistent peer store, but it just periodically writes peers into the datastore to be read at startup, still keeping them in memory. It also means a restart doesn't give you any temporary reprieve from the memory leak as the previously observed peer data is read into memory at startup. This change refactors the peerstore to use a datastore by default, reading and writing peer info as it arrives. It can be configured with a MemoryDatastore if desired. It was necessary to change the peerstore and *book interfaces to be asynchronous since the datastore api is asynchronous. BREAKING CHANGE: `libp2p.handle`, `libp2p.registrar.register` and the peerstore methods have become async
This commit is contained in:
@ -3,26 +3,43 @@
|
||||
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const { MemoryDatastore } = require('datastore-core/memory')
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
|
||||
const pDefer = require('p-defer')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const {
|
||||
codes: { ERR_INVALID_PARAMETERS }
|
||||
} = require('../../src/errors')
|
||||
|
||||
/**
|
||||
* @typedef {import('../../src/peer-store/types').PeerStore} PeerStore
|
||||
* @typedef {import('../../src/peer-store/types').KeyBook} KeyBook
|
||||
* @typedef {import('peer-id')} PeerId
|
||||
*/
|
||||
|
||||
describe('keyBook', () => {
|
||||
let peerId, peerStore, kb
|
||||
/** @type {PeerId} */
|
||||
let peerId
|
||||
/** @type {PeerStore} */
|
||||
let peerStore
|
||||
/** @type {KeyBook} */
|
||||
let kb
|
||||
/** @type {MemoryDatastore} */
|
||||
let datastore
|
||||
|
||||
beforeEach(async () => {
|
||||
[peerId] = await peerUtils.createPeerId()
|
||||
peerStore = new PeerStore({ peerId })
|
||||
datastore = new MemoryDatastore()
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore
|
||||
})
|
||||
kb = peerStore.keyBook
|
||||
})
|
||||
|
||||
it('throws invalid parameters error if invalid PeerId is provided in set', () => {
|
||||
it('throws invalid parameters error if invalid PeerId is provided in set', async () => {
|
||||
try {
|
||||
kb.set('invalid peerId')
|
||||
await kb.set('invalid peerId')
|
||||
} catch (/** @type {any} */ err) {
|
||||
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
|
||||
return
|
||||
@ -30,9 +47,9 @@ describe('keyBook', () => {
|
||||
throw new Error('invalid peerId should throw error')
|
||||
})
|
||||
|
||||
it('throws invalid parameters error if invalid PeerId is provided in get', () => {
|
||||
it('throws invalid parameters error if invalid PeerId is provided in get', async () => {
|
||||
try {
|
||||
kb.get('invalid peerId')
|
||||
await kb.get('invalid peerId')
|
||||
} catch (/** @type {any} */ err) {
|
||||
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
|
||||
return
|
||||
@ -40,22 +57,58 @@ describe('keyBook', () => {
|
||||
throw new Error('invalid peerId should throw error')
|
||||
})
|
||||
|
||||
it('stores the peerId in the book and returns the public key', () => {
|
||||
it('stores the peerId in the book and returns the public key', async () => {
|
||||
// Set PeerId
|
||||
kb.set(peerId, peerId.pubKey)
|
||||
await kb.set(peerId, peerId.pubKey)
|
||||
|
||||
// Get public key
|
||||
const pubKey = kb.get(peerId)
|
||||
const pubKey = await kb.get(peerId)
|
||||
expect(peerId.pubKey.bytes).to.equalBytes(pubKey.bytes)
|
||||
})
|
||||
|
||||
it('should not store if already stored', () => {
|
||||
const spy = sinon.spy(kb, '_setData')
|
||||
it('should not store if already stored', async () => {
|
||||
const spy = sinon.spy(datastore, 'put')
|
||||
|
||||
// Set PeerId
|
||||
kb.set(peerId, peerId.pubKey)
|
||||
kb.set(peerId, peerId.pubKey)
|
||||
await kb.set(peerId, peerId.pubKey)
|
||||
await kb.set(peerId, peerId.pubKey)
|
||||
|
||||
expect(spy).to.have.property('callCount', 1)
|
||||
})
|
||||
|
||||
it('should emit an event when setting a key', async () => {
|
||||
const defer = pDefer()
|
||||
|
||||
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
|
||||
expect(id.toB58String()).to.equal(peerId.toB58String())
|
||||
expect(pubKey.bytes).to.equalBytes(peerId.pubKey.bytes)
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
// Set PeerId
|
||||
await kb.set(peerId, peerId.pubKey)
|
||||
await defer.promise
|
||||
})
|
||||
|
||||
it('should not set when key does not match', async () => {
|
||||
const [edKey] = await peerUtils.createPeerId({ fixture: false, opts: { keyType: 'Ed25519' } })
|
||||
|
||||
// Set PeerId
|
||||
await expect(kb.set(edKey, peerId.pubKey)).to.eventually.be.rejectedWith(/bytes do not match/)
|
||||
})
|
||||
|
||||
it('should emit an event when deleting a key', async () => {
|
||||
const defer = pDefer()
|
||||
|
||||
await kb.set(peerId, peerId.pubKey)
|
||||
|
||||
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
|
||||
expect(id.toB58String()).to.equal(peerId.toB58String())
|
||||
expect(pubKey).to.be.undefined()
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
await kb.delete(peerId)
|
||||
await defer.promise
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user