mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-02 14:02:14 +00:00
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
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
'use strict'
|
|
|
|
const pTimes = require('p-times')
|
|
|
|
const { Multiaddr } = require('multiaddr')
|
|
const PeerId = require('peer-id')
|
|
|
|
const Libp2p = require('../../../src')
|
|
const Peers = require('../../fixtures/peers')
|
|
const defaultOptions = require('../base-options.browser')
|
|
|
|
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
|
|
|
/**
|
|
* Create libp2p nodes.
|
|
*
|
|
* @param {Object} [properties]
|
|
* @param {Object} [properties.config]
|
|
* @param {number} [properties.number] - number of peers (default: 1).
|
|
* @param {boolean} [properties.fixture] - use fixture for peer-id generation (default: true)
|
|
* @param {boolean} [properties.started] - nodes should start (default: true)
|
|
* @param {boolean} [properties.populateAddressBooks] - nodes addressBooks should be populated with other peers (default: true)
|
|
* @returns {Promise<Array<Libp2p>>}
|
|
*/
|
|
async function createPeer ({ number = 1, fixture = true, started = true, populateAddressBooks = true, config = {} } = {}) {
|
|
const peerIds = await createPeerId({ number, fixture })
|
|
|
|
const addresses = started ? { listen: [listenAddr] } : {}
|
|
const peers = await pTimes(number, (i) => Libp2p.create({
|
|
peerId: peerIds[i],
|
|
addresses,
|
|
...defaultOptions,
|
|
...config
|
|
}))
|
|
|
|
if (started) {
|
|
await Promise.all(peers.map((p) => p.start()))
|
|
|
|
populateAddressBooks && _populateAddressBooks(peers)
|
|
}
|
|
|
|
return peers
|
|
}
|
|
|
|
function _populateAddressBooks (peers) {
|
|
for (let i = 0; i < peers.length; i++) {
|
|
for (let j = 0; j < peers.length; j++) {
|
|
if (i !== j) {
|
|
peers[i].peerStore.addressBook.set(peers[j].peerId, peers[j].multiaddrs)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create Peer-ids.
|
|
*
|
|
* @param {Object} [properties]
|
|
* @param {number} [properties.number] - number of peers (default: 1).
|
|
* @param {boolean} [properties.fixture] - use fixture for peer-id generation (default: true)
|
|
* @param {PeerId.CreateOptions} [properties.opts]
|
|
* @returns {Promise<Array<PeerId>>}
|
|
*/
|
|
function createPeerId ({ number = 1, fixture = true, opts = {} } = {}) {
|
|
return pTimes(number, (i) => fixture
|
|
? PeerId.createFromJSON(Peers[i])
|
|
: PeerId.create(opts)
|
|
)
|
|
}
|
|
|
|
module.exports.createPeer = createPeer
|
|
module.exports.createPeerId = createPeerId
|