js-libp2p/test/peer-store/key-book.spec.js
Alex Potsides 978eb3676f
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
2022-01-20 12:03:35 +00:00

115 lines
3.1 KiB
JavaScript

'use strict'
/* eslint-env mocha */
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', () => {
/** @type {PeerId} */
let peerId
/** @type {PeerStore} */
let peerStore
/** @type {KeyBook} */
let kb
/** @type {MemoryDatastore} */
let datastore
beforeEach(async () => {
[peerId] = await peerUtils.createPeerId()
datastore = new MemoryDatastore()
peerStore = new PeerStore({
peerId,
datastore
})
kb = peerStore.keyBook
})
it('throws invalid parameters error if invalid PeerId is provided in set', async () => {
try {
await kb.set('invalid peerId')
} catch (/** @type {any} */ err) {
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
return
}
throw new Error('invalid peerId should throw error')
})
it('throws invalid parameters error if invalid PeerId is provided in get', async () => {
try {
await kb.get('invalid peerId')
} catch (/** @type {any} */ err) {
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
return
}
throw new Error('invalid peerId should throw error')
})
it('stores the peerId in the book and returns the public key', async () => {
// Set PeerId
await kb.set(peerId, peerId.pubKey)
// Get public key
const pubKey = await kb.get(peerId)
expect(peerId.pubKey.bytes).to.equalBytes(pubKey.bytes)
})
it('should not store if already stored', async () => {
const spy = sinon.spy(datastore, 'put')
// Set PeerId
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
})
})