Compare commits

..

3 Commits

Author SHA1 Message Date
Vasco Santos
aec8e3d3bb chore: release version v0.30.7 2021-02-01 18:40:05 +01:00
Vasco Santos
3abf4aeb35 chore: update contributors 2021-02-01 18:40:05 +01:00
Alex Potsides
a36b2112aa fix: do not add observed address received from peers (#882) 2021-02-01 18:32:57 +01:00
12 changed files with 45 additions and 182 deletions

View File

@@ -48,7 +48,7 @@ const after = async () => {
}
module.exports = {
bundlesize: { maxSize: '216kB' },
bundlesize: { maxSize: '215kB' },
hooks: {
pre: before,
post: after

View File

@@ -1,3 +1,12 @@
## [0.30.7](https://github.com/libp2p/js-libp2p/compare/v0.30.6...v0.30.7) (2021-02-01)
### Bug Fixes
* do not add observed address received from peers ([#882](https://github.com/libp2p/js-libp2p/issues/882)) ([a36b211](https://github.com/libp2p/js-libp2p/commit/a36b2112aafcee309a02de0cff5440cf69cd53a7))
## [0.30.6](https://github.com/libp2p/js-libp2p/compare/v0.30.5...v0.30.6) (2021-01-29)

View File

@@ -23,7 +23,6 @@
- [Setup with Auto Relay](#setup-with-auto-relay)
- [Setup with Keychain](#setup-with-keychain)
- [Configuring Dialing](#configuring-dialing)
- [Configuring Address Manager](#configuring-address-manager)
- [Configuring Connection Manager](#configuring-connection-manager)
- [Configuring Transport Manager](#configuring-transport-manager)
- [Configuring Metrics](#configuring-metrics)
@@ -550,26 +549,6 @@ const node = await Libp2p.create({
}
```
#### Configuring Address Manager
The address manager receives observed addresses from network peers. We accept observed addresses once a certain number of peers have reported the same observed address within a certain window of time.
```js
const node = await Libp2p.create({
addressManager: {
observedAddresses: {
// we must receive the same observed address from this many
// peers before we start believe it
minConfidence: 4,
// an address must reach the minimum level of confidence within
// this timeout otherwise it will be ignored
maxLifetimeBeforeEviction: (60 * 10) * 1000 // ten minutes in ms
}
},
// ...other options
})
```
#### Configuring Connection Manager
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md). The configuration values below show the defaults for Connection Manager. See [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md#options) for a full description of the parameters.

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.30.6",
"version": "0.30.7",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",

View File

@@ -31,18 +31,14 @@ class AddressManager extends EventEmitter {
* @param {object} [options]
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
* @param {object} [options.observedAddresses = { minConfidence: 4, maxLifetimeBeforeEviction: 600000 }] - configuration options for observed addresses
*/
constructor (peerId, { listen = [], announce = [], observedAddresses = { minConfidence: 4, maxLifetimeBeforeEviction: (60 * 10) * 1000 } } = {}) {
constructor (peerId, { listen = [], announce = [] } = {}) {
super()
this.peerId = peerId
this.listen = new Set(listen.map(ma => ma.toString()))
this.announce = new Set(announce.map(ma => ma.toString()))
this.observed = new Map()
this.config = {
observedAddresses
}
this.observed = new Set()
}
/**
@@ -69,25 +65,15 @@ class AddressManager extends EventEmitter {
* @returns {Array<Multiaddr>}
*/
getObservedAddrs () {
const output = []
this.observed.forEach(({ confidence }, addr) => {
if (confidence >= this.config.observedAddresses.minConfidence) {
output.push(multiaddr(addr))
}
})
return output
return Array.from(this.observed).map((a) => multiaddr(a))
}
/**
* Add peer observed addresses
*
* @param {string | Multiaddr} addr
* @param {PeerId} reporter
* @param {number} [confidence=1]
*/
addObservedAddr (addr, reporter, confidence = 1) {
addObservedAddr (addr) {
let ma = multiaddr(addr)
const remotePeer = ma.getPeerId()
@@ -101,41 +87,15 @@ class AddressManager extends EventEmitter {
}
}
const now = Date.now()
const addrString = ma.toString()
const wasNewAddr = !this.observed.has(addrString)
let addrRecord = {
confidence,
reporters: [
reporter.toB58String()
],
expires: now + this.config.observedAddresses.maxLifetimeBeforeEviction
// do not trigger the change:addresses event if we already know about this address
if (this.observed.has(addrString)) {
return
}
// we've seen this address before, increase the confidence we have in it
if (!wasNewAddr) {
addrRecord = this.observed.get(addrString)
if (!addrRecord.reporters.includes(reporter.toB58String())) {
addrRecord.confidence++
addrRecord.reporters.push(reporter.toB58String())
addrRecord.expires = now + this.config.observedAddresses.maxLifetimeBeforeEviction
}
}
this.observed.set(addrString, addrRecord)
// only emit event if we've reached the minimum confidence
if (addrRecord.confidence === this.config.observedAddresses.minConfidence) {
this.emit('change:addresses')
}
// evict addresses older than MAX_LOW_CONFIDENCE_ADDR_LIFETIME_MS we are not confident in
this.observed.forEach(({ confidence, expires }, key, map) => {
if (confidence < this.config.observedAddresses.minConfidence && expires < now) {
map.delete(key)
}
})
this.observed.add(addrString)
this.emit('change:addresses')
}
}

View File

@@ -16,12 +16,6 @@ const DefaultConfig = {
announce: [],
noAnnounce: []
},
addressManager: {
observedAddresses: {
minConfidence: 4,
maxLifetimeBeforeEviction: (60 * 10) * 1000
}
},
connectionManager: {
minConnections: 25
},

View File

@@ -202,8 +202,9 @@ class IdentifyService {
this.peerStore.protoBook.set(id, protocols)
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
// TODO: Add and score our observed addr
log('received observed address of %s', observedAddr)
this.addressManager.addObservedAddr(observedAddr, id)
// this.addressManager.addObservedAddr(observedAddr)
}
/**

View File

@@ -137,10 +137,7 @@ class Libp2p extends EventEmitter {
// Addresses {listen, announce, noAnnounce}
this.addresses = this._options.addresses
this.addressManager = new AddressManager(this.peerId, {
...this._options.addresses,
...this._options.addressManager
})
this.addressManager = new AddressManager(this.peerId, this._options.addresses)
// when addresses change, update our peer record
this.addressManager.on('change:addresses', () => {

View File

@@ -16,11 +16,11 @@ const {
codes: { ERR_INVALID_PARAMETERS }
} = require('./errors')
const isLoopback = require('libp2p-utils/src/multiaddr/is-loopback')
const AddressManager = require('./address-manager')
/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('./transport-manager')} TransportManager
* @typedef {import('./address-manager')} AddressManager
*/
function highPort (min = 1024, max = 65535) {
@@ -118,12 +118,11 @@ class NatManager {
protocol: transport.toUpperCase()
})
// add with high confidence
this._addressManager.addObservedAddr(Multiaddr.fromNodeAddress({
family: 'IPv4',
address: publicIp,
port: `${publicPort}`
}, transport), this._peerId, this._addressManager.config.observedAddresses.minConfidence)
}, transport))
}
}

View File

@@ -15,11 +15,9 @@ const announceAddreses = ['/dns4/peer.io']
describe('Address Manager', () => {
let peerId
let peerIds
before(async () => {
peerId = await PeerId.createFromJSON(Peers[0])
peerIds = await Promise.all(Peers.slice(1).map(peerId => PeerId.createFromJSON(peerId)))
})
it('should not need any addresses', () => {
@@ -62,7 +60,7 @@ describe('Address Manager', () => {
expect(am.observed).to.be.empty()
am.addObservedAddr('/ip4/123.123.123.123/tcp/39201', peerId)
am.addObservedAddr('/ip4/123.123.123.123/tcp/39201')
expect(am.observed).to.have.property('size', 1)
})
@@ -73,12 +71,12 @@ describe('Address Manager', () => {
expect(am.observed).to.be.empty()
am.addObservedAddr(ma, peerId)
am.addObservedAddr(ma, peerId)
am.addObservedAddr(ma, peerId)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
expect(am.observed).to.have.property('size', 1)
expect(Array.from(am.observed.keys())).to.include(ma)
expect(am.observed).to.include(ma)
})
it('should only emit one change:addresses event', () => {
@@ -90,25 +88,11 @@ describe('Address Manager', () => {
eventCount++
})
am.addObservedAddr(ma, peerIds[0])
am.addObservedAddr(ma, peerIds[1])
am.addObservedAddr(ma, peerIds[2])
am.addObservedAddr(`${ma}/p2p/${peerId}`, peerIds[3])
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`, peerIds[4])
expect(eventCount).to.equal(1)
})
it('should emit one change:addresses event when specifying confidence', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
let eventCount = 0
am.on('change:addresses', () => {
eventCount++
})
am.addObservedAddr(ma, peerId, am.config.observedAddresses.minConfidence)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`)
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`)
expect(eventCount).to.equal(1)
})
@@ -119,12 +103,11 @@ describe('Address Manager', () => {
expect(am.observed).to.be.empty()
am.addObservedAddr(ma, peerId)
am.addObservedAddr(`${ma}/p2p/${peerId}`, peerId)
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`)
expect(am.observed).to.have.property('size', 1)
expect(Array.from(am.observed.keys())).to.include(ma)
expect(am.observed).to.include(ma)
})
it('should strip our peer address from added observed addresses in difference formats', () => {
@@ -133,71 +116,12 @@ describe('Address Manager', () => {
expect(am.observed).to.be.empty()
am.addObservedAddr(ma, peerId)
am.addObservedAddr(`${ma}/p2p/${peerId}`, peerId) // base32 CID
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`, peerId) // base58btc
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId}`) // base32 CID
am.addObservedAddr(`${ma}/p2p/${peerId.toB58String()}`) // base58btc
expect(am.observed).to.have.property('size', 1)
expect(Array.from(am.observed.keys())).to.include(ma)
})
it('should require a number of confirmations before believing address', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma, peerId)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.not.include(ma)
for (let i = 0; i < am.config.observedAddresses.minConfidence; i++) {
am.addObservedAddr(ma, peerIds[i])
}
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
})
it('should require a number of confirmations from different peers', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma, peerId)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.not.include(ma)
for (let i = 0; i < am.config.observedAddresses.minConfidence; i++) {
am.addObservedAddr(ma, peerIds[0])
}
expect(am.getObservedAddrs().map(ma => ma.toString())).to.not.include(ma)
})
it('should evict addresses that do not receive enough confirmations within the timeout', () => {
const ma1 = '/ip4/123.123.123.123/tcp/39201'
const ma2 = '/ip4/124.124.124.124/tcp/39202'
const am = new AddressManager(peerId)
expect(am.observed).to.be.empty()
am.addObservedAddr(ma1, peerId)
const observedAddrs = Array.from(am.observed.values())
expect(Array.from(am.observed.keys())).to.include(ma1)
// make expiry date a while ago
observedAddrs[0].expires = Date.now() - 1000
// will evict any old multiaddrs
am.addObservedAddr(ma2, peerId)
// should have been evicted
expect(Array.from(am.observed.keys())).to.not.include(ma1)
expect(Array.from(am.observed.keys())).to.include(ma2)
expect(am.observed).to.include(ma)
})
})

View File

@@ -164,7 +164,7 @@ describe('libp2p.multiaddrs', () => {
expect(libp2p.multiaddrs).to.have.lengthOf(listenAddresses.length)
libp2p.addressManager.addObservedAddr(ma, libp2p.peerId, libp2p.addressManager.config.observedAddresses.minConfidence)
libp2p.addressManager.addObservedAddr(ma)
expect(libp2p.multiaddrs).to.have.lengthOf(listenAddresses.length + 1)
expect(libp2p.multiaddrs.map(ma => ma.toString())).to.include(ma)

View File

@@ -37,7 +37,7 @@ describe('Consume peer record', () => {
done = resolve
})
libp2p.addressManager.addObservedAddr('/ip4/123.123.123.123/tcp/3983', libp2p.peerId, libp2p.addressManager.config.observedAddresses.minConfidence)
libp2p.addressManager.addObservedAddr('/ip4/123.123.123.123/tcp/3983')
await p