Compare commits

...

8 Commits

Author SHA1 Message Date
bbdd559a02 chore: release version v0.35.7 2021-12-24 14:51:51 +00:00
4070dcdf55 chore: update contributors 2021-12-24 14:51:51 +00:00
cb0d7d6c99 fix: type definitions for big dialrequest and persistent peerstore (#1078)
Signed-off-by: Tuyen Nguyen <vutuyen2636@gmail.com>
2021-12-24 15:46:00 +01:00
d1c48dcbed fix: main ci (#1079) 2021-12-24 11:18:09 +01:00
c4a442788b docs: update example config ipfs links (#1077) 2021-12-22 17:03:03 +01:00
70a4bb9451 docs: peerstore configuration datastore fixed 2021-12-22 14:15:18 +01:00
a0516ebc85 docs: update node and npm version badge according to package.json (#1074) 2021-12-22 14:01:54 +01:00
b425fa1230 fix: add tracked map (#1069)
Small refactor of the component stats - adds a `TrackedMap` which encapsulates updating the metrics and means we don't need to null guard on `this._metrics` everywhere.

If metrics are not enabled a regular `Map` is used.
2021-12-21 15:51:06 +01:00
10 changed files with 126 additions and 50 deletions

View File

@ -19,7 +19,7 @@ jobs:
- run: npx aegir lint
- run: npx aegir build
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size
- uses: ipfs/aegir/actions/bundle-size@master
name: size
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,3 +1,23 @@
## [0.35.7](https://github.com/libp2p/js-libp2p/compare/v0.35.2...v0.35.7) (2021-12-24)
### Bug Fixes
* add tracked map ([#1069](https://github.com/libp2p/js-libp2p/issues/1069)) ([b425fa1](https://github.com/libp2p/js-libp2p/commit/b425fa12304def2a007d43a0aa445c28b766ed02))
* clean up pending dial targets ([#1059](https://github.com/libp2p/js-libp2p/issues/1059)) ([bdc9f16](https://github.com/libp2p/js-libp2p/commit/bdc9f16d0cbe56ccf26822f11068e7795bcef046))
* fix uncaught promise rejection when finding peers ([#1044](https://github.com/libp2p/js-libp2p/issues/1044)) ([3b683e7](https://github.com/libp2p/js-libp2p/commit/3b683e715686163e229b7b5c3a892327dfd4fc63))
* increase the maxlisteners for timeout controllers ([#1065](https://github.com/libp2p/js-libp2p/issues/1065)) ([09a0f94](https://github.com/libp2p/js-libp2p/commit/09a0f940df7fdb4ece34604e85693709df5c213e))
* main ci ([#1079](https://github.com/libp2p/js-libp2p/issues/1079)) ([d1c48dc](https://github.com/libp2p/js-libp2p/commit/d1c48dcbeded828f2dd3044cc9aed3f17f02846d))
* make error codes consistent ([#1054](https://github.com/libp2p/js-libp2p/issues/1054)) ([b25e0fe](https://github.com/libp2p/js-libp2p/commit/b25e0fe5312db58a06c39500ae84c50fed3a93bd))
* type definitions for big dialrequest and persistent peerstore ([#1078](https://github.com/libp2p/js-libp2p/issues/1078)) ([cb0d7d6](https://github.com/libp2p/js-libp2p/commit/cb0d7d6c99d179498f04e76df76e70e4f7d41c4c))
### Features
* allow per-component metrics to be collected ([#1061](https://github.com/libp2p/js-libp2p/issues/1061)) ([2f0b311](https://github.com/libp2p/js-libp2p/commit/2f0b311df7127aa44512c2008142d4ca30268986)), closes [#1060](https://github.com/libp2p/js-libp2p/issues/1060)
## [0.35.6](https://github.com/libp2p/js-libp2p/compare/v0.35.5...v0.35.6) (2021-12-18)

View File

@ -23,8 +23,8 @@
<a href="https://david-dm.org/libp2p/js-libp2p"><img src="https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" /></a>
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D6.0.0-orange.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D12.0.0-orange.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D7.0.0-orange.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D15.0.0-orange.svg?style=flat-square" /></a>
<br>
</p>

View File

@ -675,15 +675,15 @@ const { NOISE } = require('libp2p-noise')
const LevelDatastore = require('datastore-level')
const datastore = new LevelDatastore('path/to/store')
const dsInstant = await datastore.open()
await datastore.open() // level database must be ready before node boot
const node = await Libp2p.create({
datastore, // pass the opened datastore
modules: {
transport: [TCP],
streamMuxer: [MPLEX],
connEncryption: [NOISE]
},
datastore: dsInstant,
peerStore: {
persistence: true,
threshold: 5
@ -806,8 +806,8 @@ protocols: [
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-nodejs.js) - libp2p configuration used by js-ipfs when running in Node.js
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/src/core/runtime/libp2p-browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core-config/src/libp2p.js) - libp2p configuration used by js-ipfs when running in Node.js
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-core-config/src/libp2p.browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
If you have developed a project using `js-libp2p`, please consider submitting your configuration to this list so that it can be found easily by other users.

View File

@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.35.6",
"version": "0.35.7",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@ -181,9 +181,9 @@
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Hugo Dias <mail@hugodias.me>",
"dirkmc <dirkmdev@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>",
"Volker Mische <volker.mische@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>",
"dirkmc <dirkmdev@gmail.com>",
"Robert Kiel <robert.kiel@hoprnet.org>",
"Richard Littauer <richard.littauer@gmail.com>",
"zeim839 <50573884+zeim839@users.noreply.github.com>",
@ -193,22 +193,23 @@
"Andrew Nesbitt <andrewnez@gmail.com>",
"Franck Royer <franck@royer.one>",
"Thomas Eizinger <thomas@eizinger.io>",
"Vít Habada <vithabada93@gmail.com>",
"Giovanni T. Parra <fiatjaf@gmail.com>",
"acolytec3 <17355484+acolytec3@users.noreply.github.com>",
"Alan Smithee <ggnore.alan.smithee@gmail.com>",
"Elven <mon.samuel@qq.com>",
"Samlior <samlior@foxmail.com>",
"Didrik Nordström <didrik.nordstrom@gmail.com>",
"Soeren <nikorpoulsen@gmail.com>",
"Sönke Hahn <soenkehahn@gmail.com>",
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
"TJKoury <TJKoury@gmail.com>",
"TheStarBoys <41286328+TheStarBoys@users.noreply.github.com>",
"Tiago Alves <alvesjtiago@gmail.com>",
"Tim Daubenschütz <tim@daubenschuetz.de>",
"XiaoZhang <zxinmyth@gmail.com>",
"Yusef Napora <yusef@napora.org>",
"Zane Starr <zcstarr@gmail.com>",
"ebinks <elizabethjbinks@gmail.com>",
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
"greenSnot <greenSnot@users.noreply.github.com>",
"isan_rivkin <isanrivkin@gmail.com>",
"mayerwin <mayerwin@users.noreply.github.com>",
"mcclure <andi.m.mcclure@gmail.com>",
@ -217,7 +218,8 @@
"robertkiel <robert.kiel@validitylabs.org>",
"shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
"swedneck <40505480+swedneck@users.noreply.github.com>",
"greenSnot <greenSnot@users.noreply.github.com>",
"tuyennhv <vutuyen2636@gmail.com>",
"Sönke Hahn <soenkehahn@gmail.com>",
"Aleksei <vozhdb@gmail.com>",
"Bernd Strehl <bernd.strehl@gmail.com>",
"Chris Bratlien <chrisbratlien@gmail.com>",
@ -248,6 +250,7 @@
"Nuno Nogueira <nunofmn@gmail.com>",
"Philipp Muens <raute1337@gmx.de>",
"RasmusErik Voel Jensen <github@solsort.com>",
"Smite Chow <xiaopengyou@live.com>"
"Smite Chow <xiaopengyou@live.com>",
"Soeren <nikorpoulsen@gmail.com>"
]
}

View File

@ -12,7 +12,7 @@ const LatencyMonitor = require('./latency-monitor')
const retimer = require('retimer')
const { EventEmitter } = require('events')
const trackedMap = require('../metrics/tracked-map')
const PeerId = require('peer-id')
const {
@ -34,7 +34,7 @@ const defaultOptions = {
const METRICS_COMPONENT = 'connection-manager'
const METRICS_PEER_CONNECTIONS = 'peer-connections'
const METRICS_ALL_CONNECTIONS = 'all-connections'
const METRICS_PEER_VALUES = 'peer-values'
/**
* @typedef {import('../')} Libp2p
@ -87,14 +87,14 @@ class ConnectionManager extends EventEmitter {
*
* @type {Map<string, number>}
*/
this._peerValues = new Map()
this._peerValues = trackedMap(METRICS_COMPONENT, METRICS_PEER_VALUES, this._libp2p.metrics)
/**
* Map of connections per peer
*
* @type {Map<string, Connection[]>}
*/
this.connections = new Map()
this.connections = trackedMap(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this._libp2p.metrics)
this._started = false
this._timer = null
@ -164,8 +164,6 @@ class ConnectionManager extends EventEmitter {
await Promise.all(tasks)
this.connections.clear()
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, 0)
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, 0)
}
/**
@ -222,8 +220,6 @@ class ConnectionManager extends EventEmitter {
storedConn.push(connection)
} else {
this.connections.set(peerIdStr, [connection])
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
}
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
@ -255,9 +251,6 @@ class ConnectionManager extends EventEmitter {
this._libp2p.metrics && this._libp2p.metrics.onPeerDisconnected(connection.remotePeer)
}
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
}
/**

View File

@ -14,7 +14,7 @@ const { setMaxListeners } = require('events')
const DialRequest = require('./dial-request')
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
const getPeer = require('../get-peer')
const trackedMap = require('../metrics/tracked-map')
const { codes } = require('../errors')
const {
DIAL_TIMEOUT,
@ -56,8 +56,8 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
* @property {Multiaddr[]} addrs
*
* @typedef PendingDial
* @property {DialRequest} dialRequest
* @property {TimeoutController} controller
* @property {import('./dial-request')} dialRequest
* @property {import('timeout-abort-controller').TimeoutController} controller
* @property {Promise<Connection>} promise
* @property {function():void} destroy
*/
@ -86,9 +86,12 @@ class Dialer {
this.timeout = dialTimeout
this.maxDialsPerPeer = maxDialsPerPeer
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
this._pendingDials = new Map()
this._pendingDialTargets = new Map()
this._metrics = metrics
/** @type {Map<string, PendingDial>} */
this._pendingDials = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIALS, metrics)
/** @type {Map<string, { resolve: (value: any) => void, reject: (err: Error) => void}>} */
this._pendingDialTargets = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, metrics)
for (const [key, value] of Object.entries(resolvers)) {
Multiaddr.resolvers.set(key, value)
@ -112,9 +115,6 @@ class Dialer {
pendingTarget.reject(new AbortError('Dialer was destroyed'))
}
this._pendingDialTargets.clear()
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, 0)
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, 0)
}
/**
@ -164,7 +164,6 @@ class Dialer {
const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString() + Date.now()}`
const cancellablePromise = new Promise((resolve, reject) => {
this._pendingDialTargets.set(id, { resolve, reject })
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
})
try {
@ -176,7 +175,6 @@ class Dialer {
return dialTarget
} finally {
this._pendingDialTargets.delete(id)
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
}
}
@ -269,13 +267,10 @@ class Dialer {
destroy: () => {
timeoutController.clear()
this._pendingDials.delete(dialTarget.id)
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
}
}
this._pendingDials.set(dialTarget.id, pendingDial)
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
return pendingDial
}

View File

@ -48,6 +48,8 @@ const { updateSelfPeerRecord } = require('./record/utils')
* @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('./pnet')} Protector
* @typedef {Object} PersistentPeerStoreOptions
* @property {number} [threshold]
*/
/**
@ -110,7 +112,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
* @property {PeerStoreOptions & import('./peer-store/persistent').PersistentPeerStoreOptions} [peerStore]
* @property {PeerStoreOptions & PersistentPeerStoreOptions} [peerStore]
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
* @property {Libp2pConfig} [config]
*
@ -161,6 +163,15 @@ class Libp2p extends EventEmitter {
this.peerId = this._options.peerId
this.datastore = this._options.datastore
// Create Metrics
if (this._options.metrics.enabled) {
const metrics = new Metrics({
...this._options.metrics
})
this.metrics = metrics
}
this.peerStore = (this.datastore && this._options.peerStore.persistence)
? new PersistentPeerStore({
peerId: this.peerId,
@ -195,15 +206,6 @@ class Libp2p extends EventEmitter {
autoDialInterval: this._options.connectionManager.autoDialInterval
})
// Create Metrics
if (this._options.metrics.enabled) {
const metrics = new Metrics({
...this._options.metrics
})
this.metrics = metrics
}
// Create keychain
if (this._options.keychain && this._options.keychain.datastore) {
log('creating keychain')

View File

@ -8,6 +8,7 @@ const retimer = require('retimer')
/**
* @typedef {import('@vascosantos/moving-average').IMovingAverage} IMovingAverage
* @typedef {import('bignumber.js').BigNumber} Big
*/
class Stats extends EventEmitter {

View File

@ -0,0 +1,62 @@
'use strict'
/**
* @template K
* @template V
*/
class TrackedMap extends Map {
/**
* @param {string} component
* @param {string} name
* @param {import('.')} metrics
*/
constructor (component, name, metrics) {
super()
this._component = component
this._name = name
this._metrics = metrics
this._metrics.updateComponentMetric(this._component, this._name, this.size)
}
/**
* @param {K} key
* @param {V} value
*/
set (key, value) {
super.set(key, value)
this._metrics.updateComponentMetric(this._component, this._name, this.size)
return this
}
/**
* @param {K} key
*/
delete (key) {
const deleted = super.delete(key)
this._metrics.updateComponentMetric(this._component, this._name, this.size)
return deleted
}
}
/**
* @template K
* @template V
* @param {string} component
* @param {string} name
* @param {import('.')} [metrics]
* @returns {Map<K, V>}
*/
module.exports = (component, name, metrics) => {
/** @type {Map<K, V>} */
let map
if (metrics) {
map = new TrackedMap(component, name, metrics)
} else {
map = new Map()
}
return map
}