net: move netaddress.
This commit is contained in:
parent
5ea218b373
commit
f99064750a
@ -75,6 +75,7 @@ bcoin.net = require('./net');
|
||||
bcoin.bip150 = require('./net/bip150');
|
||||
bcoin.bip151 = require('./net/bip151');
|
||||
bcoin.bip152 = require('./net/bip152');
|
||||
bcoin.netaddress = require('./net/netaddress');
|
||||
bcoin.packets = require('./net/packets');
|
||||
bcoin.peer = require('./net/peer');
|
||||
bcoin.pool = require('./net/pool');
|
||||
@ -95,7 +96,6 @@ bcoin.invitem = require('./primitives/invitem');
|
||||
bcoin.keyring = require('./primitives/keyring');
|
||||
bcoin.merkleblock = require('./primitives/merkleblock');
|
||||
bcoin.mtx = require('./primitives/mtx');
|
||||
bcoin.netaddress = require('./primitives/netaddress');
|
||||
bcoin.outpoint = require('./primitives/outpoint');
|
||||
bcoin.output = require('./primitives/output');
|
||||
bcoin.tx = require('./primitives/tx');
|
||||
|
||||
@ -110,6 +110,7 @@ bcoin.define('net', './net');
|
||||
bcoin.define('bip150', './net/bip150');
|
||||
bcoin.define('bip151', './net/bip151');
|
||||
bcoin.define('bip152', './net/bip152');
|
||||
bcoin.define('netaddress', './net/netaddress');
|
||||
bcoin.define('packets', './net/packets');
|
||||
bcoin.define('peer', './net/peer');
|
||||
bcoin.define('pool', './net/pool');
|
||||
@ -130,7 +131,6 @@ bcoin.define('invitem', './primitives/invitem');
|
||||
bcoin.define('keyring', './primitives/keyring');
|
||||
bcoin.define('merkleblock', './primitives/merkleblock');
|
||||
bcoin.define('mtx', './primitives/mtx');
|
||||
bcoin.define('netaddress', './primitives/netaddress');
|
||||
bcoin.define('outpoint', './primitives/outpoint');
|
||||
bcoin.define('output', './primitives/output');
|
||||
bcoin.define('tx', './primitives/tx');
|
||||
|
||||
@ -15,10 +15,15 @@ const Logger = require('blgr');
|
||||
const {murmur3} = require('bfilter');
|
||||
const util = require('../utils/util');
|
||||
const Network = require('../protocol/network');
|
||||
const NetAddress = require('../primitives/netaddress');
|
||||
const NetAddress = require('./netaddress');
|
||||
const List = require('../utils/list');
|
||||
const common = require('./common');
|
||||
const seeds = require('./seeds');
|
||||
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
|
||||
const POOL32 = Buffer.allocUnsafe(32);
|
||||
|
||||
/**
|
||||
@ -67,13 +72,11 @@ class HostList {
|
||||
init() {
|
||||
const options = this.options;
|
||||
const scores = HostList.scores;
|
||||
const hosts = IP.getPublic();
|
||||
const port = this.address.port;
|
||||
|
||||
for (let i = 0; i < this.options.maxBuckets; i++)
|
||||
for (let i = 0; i < options.maxBuckets; i++)
|
||||
this.fresh.push(new Map());
|
||||
|
||||
for (let i = 0; i < this.options.maxBuckets; i++)
|
||||
for (let i = 0; i < options.maxBuckets; i++)
|
||||
this.used.push(new List());
|
||||
|
||||
this.setSeeds(options.seeds);
|
||||
@ -82,6 +85,9 @@ class HostList {
|
||||
this.pushLocal(this.address, scores.MANUAL);
|
||||
this.addLocal(options.host, options.port, scores.BIND);
|
||||
|
||||
const hosts = IP.getPublic();
|
||||
const port = this.address.port;
|
||||
|
||||
for (const host of hosts)
|
||||
this.addLocal(host, port, scores.IF);
|
||||
}
|
||||
@ -355,18 +361,19 @@ class HostList {
|
||||
return null;
|
||||
|
||||
const now = this.network.now();
|
||||
|
||||
let factor = 1;
|
||||
|
||||
for (;;) {
|
||||
let index = random(buckets.length);
|
||||
const bucket = buckets[index];
|
||||
const i = random(buckets.length);
|
||||
const bucket = buckets[i];
|
||||
|
||||
if (bucket.size === 0)
|
||||
continue;
|
||||
|
||||
index = random(bucket.size);
|
||||
|
||||
let index = random(bucket.size);
|
||||
let entry;
|
||||
|
||||
if (buckets === this.used) {
|
||||
entry = bucket.head;
|
||||
while (index--)
|
||||
@ -375,7 +382,7 @@ class HostList {
|
||||
for (entry of bucket.values()) {
|
||||
if (index === 0)
|
||||
break;
|
||||
index--;
|
||||
index -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -431,7 +438,6 @@ class HostList {
|
||||
let entry = this.map.get(addr.hostname);
|
||||
|
||||
if (entry) {
|
||||
const now = this.network.now();
|
||||
let penalty = 2 * 60 * 60;
|
||||
let interval = 24 * 60 * 60;
|
||||
|
||||
@ -445,6 +451,7 @@ class HostList {
|
||||
entry.addr.services >>>= 0;
|
||||
|
||||
// Online?
|
||||
const now = this.network.now();
|
||||
if (now - addr.time < 24 * 60 * 60)
|
||||
interval = 60 * 60;
|
||||
|
||||
@ -490,7 +497,7 @@ class HostList {
|
||||
|
||||
entry = new HostEntry(addr, src);
|
||||
|
||||
this.totalFresh++;
|
||||
this.totalFresh += 1;
|
||||
}
|
||||
|
||||
const bucket = this.freshBucket(entry);
|
||||
@ -502,7 +509,7 @@ class HostList {
|
||||
this.evictFresh(bucket);
|
||||
|
||||
bucket.set(entry.key(), entry);
|
||||
entry.refCount++;
|
||||
entry.refCount += 1;
|
||||
|
||||
this.map.set(entry.key(), entry);
|
||||
this.needsFlush = true;
|
||||
@ -524,7 +531,7 @@ class HostList {
|
||||
|
||||
if (--entry.refCount === 0) {
|
||||
this.map.delete(entry.key());
|
||||
this.totalFresh--;
|
||||
this.totalFresh -= 1;
|
||||
}
|
||||
|
||||
continue;
|
||||
@ -546,7 +553,7 @@ class HostList {
|
||||
|
||||
if (--old.refCount === 0) {
|
||||
this.map.delete(old.key());
|
||||
this.totalFresh--;
|
||||
this.totalFresh -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -605,7 +612,7 @@ class HostList {
|
||||
for (const bucket of this.used) {
|
||||
if (bucket.head === head) {
|
||||
bucket.remove(entry);
|
||||
this.totalUsed--;
|
||||
this.totalUsed -= 1;
|
||||
head = null;
|
||||
break;
|
||||
}
|
||||
@ -615,10 +622,10 @@ class HostList {
|
||||
} else {
|
||||
for (const bucket of this.fresh) {
|
||||
if (bucket.delete(entry.key()))
|
||||
entry.refCount--;
|
||||
entry.refCount -= 1;
|
||||
}
|
||||
|
||||
this.totalFresh--;
|
||||
this.totalFresh -= 1;
|
||||
assert(entry.refCount === 0);
|
||||
}
|
||||
|
||||
@ -639,7 +646,7 @@ class HostList {
|
||||
if (!entry)
|
||||
return;
|
||||
|
||||
entry.attempts++;
|
||||
entry.attempts += 1;
|
||||
entry.lastAttempt = now;
|
||||
}
|
||||
|
||||
@ -686,17 +693,17 @@ class HostList {
|
||||
assert(entry.refCount > 0);
|
||||
|
||||
// Remove from fresh.
|
||||
let old;
|
||||
let old = null;
|
||||
for (const bucket of this.fresh) {
|
||||
if (bucket.delete(entry.key())) {
|
||||
entry.refCount--;
|
||||
entry.refCount -= 1;
|
||||
old = bucket;
|
||||
}
|
||||
}
|
||||
|
||||
assert(old);
|
||||
assert(entry.refCount === 0);
|
||||
this.totalFresh--;
|
||||
this.totalFresh -= 1;
|
||||
|
||||
// Find room in used bucket.
|
||||
const bucket = this.usedBucket(entry);
|
||||
@ -704,12 +711,13 @@ class HostList {
|
||||
if (bucket.size < this.options.maxEntries) {
|
||||
entry.used = true;
|
||||
bucket.push(entry);
|
||||
this.totalUsed++;
|
||||
this.totalUsed += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// No room. Evict.
|
||||
const evicted = this.evictUsed(bucket);
|
||||
|
||||
let fresh = this.freshBucket(evicted);
|
||||
|
||||
// Move to entry's old bucket if no room.
|
||||
@ -724,8 +732,8 @@ class HostList {
|
||||
evicted.used = false;
|
||||
fresh.set(evicted.key(), evicted);
|
||||
assert(evicted.refCount === 0);
|
||||
evicted.refCount++;
|
||||
this.totalFresh++;
|
||||
evicted.refCount += 1;
|
||||
this.totalFresh += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -937,7 +945,7 @@ class HostList {
|
||||
if (!local)
|
||||
return false;
|
||||
|
||||
local.score++;
|
||||
local.score += 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -954,7 +962,7 @@ class HostList {
|
||||
for (const seed of this.dnsSeeds)
|
||||
jobs.push(this.populateSeed(seed));
|
||||
|
||||
await Promise.all(jobs);
|
||||
return Promise.all(jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -969,7 +977,7 @@ class HostList {
|
||||
for (const node of this.dnsNodes)
|
||||
jobs.push(this.populateNode(node));
|
||||
|
||||
await Promise.all(jobs);
|
||||
return Promise.all(jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1078,11 +1086,12 @@ class HostList {
|
||||
fromJSON(json) {
|
||||
const sources = new Map();
|
||||
const map = new Map();
|
||||
let totalFresh = 0;
|
||||
let totalUsed = 0;
|
||||
const fresh = [];
|
||||
const used = [];
|
||||
|
||||
let totalFresh = 0;
|
||||
let totalUsed = 0;
|
||||
|
||||
assert(json && typeof json === 'object');
|
||||
|
||||
assert(json.version === HostList.VERSION,
|
||||
@ -1092,6 +1101,7 @@ class HostList {
|
||||
|
||||
for (const addr of json.addrs) {
|
||||
const entry = HostEntry.fromJSON(addr, this.network);
|
||||
|
||||
let src = sources.get(entry.src.hostname);
|
||||
|
||||
// Save some memory.
|
||||
@ -1116,8 +1126,8 @@ class HostList {
|
||||
const entry = map.get(key);
|
||||
assert(entry);
|
||||
if (entry.refCount === 0)
|
||||
totalFresh++;
|
||||
entry.refCount++;
|
||||
totalFresh += 1;
|
||||
entry.refCount += 1;
|
||||
bucket.set(key, entry);
|
||||
}
|
||||
|
||||
@ -1143,7 +1153,7 @@ class HostList {
|
||||
assert(entry.refCount === 0);
|
||||
assert(!entry.used);
|
||||
entry.used = true;
|
||||
totalUsed++;
|
||||
totalUsed += 1;
|
||||
bucket.push(entry);
|
||||
}
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ exports.common = require('./common');
|
||||
exports.external = require('./external');
|
||||
exports.Framer = require('./framer');
|
||||
exports.HostList = require('./hostlist');
|
||||
exports.NetAddress = require('./netaddress');
|
||||
exports.packets = require('./packets');
|
||||
exports.Parser = require('./parser');
|
||||
exports.Peer = require('./peer');
|
||||
|
||||
@ -9,14 +9,14 @@
|
||||
const assert = require('assert');
|
||||
const bio = require('bufio');
|
||||
const IP = require('binet');
|
||||
const common = require('../net/common');
|
||||
const Network = require('../protocol/network');
|
||||
const util = require('../utils/util');
|
||||
const common = require('./common');
|
||||
|
||||
/**
|
||||
* Net Address
|
||||
* Represents a network address.
|
||||
* @alias module:primitives.NetAddress
|
||||
* @alias module:net.NetAddress
|
||||
* @property {Host} host
|
||||
* @property {Number} port
|
||||
* @property {Number} services
|
||||
@ -17,7 +17,7 @@ const {BloomFilter} = require('bfilter');
|
||||
const common = require('./common');
|
||||
const util = require('../utils/util');
|
||||
const bip152 = require('./bip152');
|
||||
const NetAddress = require('../primitives/netaddress');
|
||||
const NetAddress = require('./netaddress');
|
||||
const Headers = require('../primitives/headers');
|
||||
const InvItem = require('../primitives/invitem');
|
||||
const MemBlock = require('../primitives/memblock');
|
||||
|
||||
@ -26,7 +26,7 @@ const BIP150 = require('./bip150');
|
||||
const BIP152 = require('./bip152');
|
||||
const Block = require('../primitives/block');
|
||||
const TX = require('../primitives/tx');
|
||||
const NetAddress = require('../primitives/netaddress');
|
||||
const NetAddress = require('./netaddress');
|
||||
const Network = require('../protocol/network');
|
||||
const services = common.services;
|
||||
const invTypes = InvItem.types;
|
||||
|
||||
@ -3329,6 +3329,7 @@ class Pool extends EventEmitter {
|
||||
throw new Error('Peer is destroyed (getdata).');
|
||||
|
||||
let now = Date.now();
|
||||
|
||||
const items = [];
|
||||
|
||||
for (const hash of hashes) {
|
||||
|
||||
@ -19,7 +19,7 @@ const secp256k1 = require('bcrypto/lib/secp256k1');
|
||||
const util = require('../utils/util');
|
||||
const common = require('../blockchain/common');
|
||||
const Amount = require('../btc/amount');
|
||||
const NetAddress = require('../primitives/netaddress');
|
||||
const NetAddress = require('../net/netaddress');
|
||||
const Script = require('../script/script');
|
||||
const Address = require('../primitives/address');
|
||||
const Block = require('../primitives/block');
|
||||
|
||||
@ -21,7 +21,6 @@ exports.KeyRing = require('./keyring');
|
||||
exports.MemBlock = require('./memblock');
|
||||
exports.MerkleBlock = require('./merkleblock');
|
||||
exports.MTX = require('./mtx');
|
||||
exports.NetAddress = require('./netaddress');
|
||||
exports.Outpoint = require('./outpoint');
|
||||
exports.Output = require('./output');
|
||||
exports.TX = require('./tx');
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
const assert = require('./util/assert');
|
||||
const Network = require('../lib/protocol/network');
|
||||
const util = require('../lib/utils/util');
|
||||
const NetAddress = require('../lib/primitives/netaddress');
|
||||
const NetAddress = require('../lib/net/netaddress');
|
||||
const TX = require('../lib/primitives/tx');
|
||||
const Framer = require('../lib/net/framer');
|
||||
const Parser = require('../lib/net/parser');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user