Browse Source

only add one sample per bootstrap node

session-estimator
Mathias Buus 4 years ago
parent
commit
1016f59e0e
  1. 10
      index.js
  2. 12
      lib/nat-analyzer.js

10
index.js

@ -299,9 +299,9 @@ class DHT extends EventEmitter {
async _onpersistent () {
if (this.ephemeral === false) return false
// TODO: do nat check also
const addr = this.remoteAddress(this._forcePersistent ? 1 : 3)
// only require one sample here as we do the nat check anyway after
// makes settting up a dht easier...
const addr = this.remoteAddress(1)
if (addr.type !== DHT.NAT_PORT_CONSISTENT && addr.type !== DHT.NAT_OPEN) {
this._persistentTicks = MORE_PERSISTENT_TICKS
@ -473,7 +473,7 @@ class DHT extends EventEmitter {
}
// add a sample of our address from the remote nodes pov
this._nat.add(m.to)
this._nat.add(m.to, m.from)
this._addNode({
id,
@ -522,7 +522,7 @@ class DHT extends EventEmitter {
_onresponse (res) {
if (res.id !== null) this._addNodeFromMessage(res)
else if (this._nat.length < 3) this._nat.add(res.to)
else if (this._nat.length < 3 && !this._nat.sampled(res.from)) this._nat.add(res.to, res.from)
}
bind (...args) {

12
lib/nat-analyzer.js

@ -9,9 +9,17 @@ class NatAnalyzer {
this.top = 0
}
add (addr) {
sampled (referrer) {
for (let i = 0; i < this.length; i++) {
const r = this.samples[i].referrer
if (r.port === referrer.port && r.host === referrer.host) return true
}
return false
}
add (addr, referrer) {
if (this.length < this.samples.length) this.length++
this.samples[this.top] = { port: addr.port, host: addr.host, dist: 0 }
this.samples[this.top] = { port: addr.port, host: addr.host, dist: 0, referrer }
this.top = (this.top + 1) & (this.samples.length - 1)
}

Loading…
Cancel
Save