pool: noncelist.

This commit is contained in:
Christopher Jeffrey 2017-01-21 05:59:44 -08:00
parent f67eeee936
commit 58f8035add
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 37 additions and 17 deletions

View File

@ -1611,7 +1611,7 @@ Peer.prototype.handleVersion = co(function* handleVersion(packet) {
this.noRelay = packet.noRelay; this.noRelay = packet.noRelay;
if (!this.network.selfConnect) { if (!this.network.selfConnect) {
if (this.options.hasNonce(packet.nonce, this.hostname())) if (this.options.hasNonce(packet.nonce))
throw new Error('We connected to ourself. Oops.'); throw new Error('We connected to ourself. Oops.');
} }
@ -2301,11 +2301,10 @@ PeerOptions.createNonce = function createNonce(hostname) {
* Test whether version nonce is ours. * Test whether version nonce is ours.
* @private * @private
* @param {Buffer} nonce * @param {Buffer} nonce
* @param {String} hostname
* @returns {Boolean} * @returns {Boolean}
*/ */
PeerOptions.hasNonce = function hasNonce(nonce, hostname) { PeerOptions.hasNonce = function hasNonce(nonce) {
return false; return false;
}; };

View File

@ -1198,6 +1198,7 @@ Pool.prototype.handleVersion = co(function* handleVersion(peer, packet) {
packet.agent); packet.agent);
this.network.time.add(peer.hostname(), packet.ts); this.network.time.add(peer.hostname(), packet.ts);
this.nonces.remove(peer.hostname());
}); });
/** /**
@ -3454,12 +3455,11 @@ PoolOptions.prototype.createNonce = function createNonce(hostname) {
* Test whether version nonce is ours. * Test whether version nonce is ours.
* @private * @private
* @param {Buffer} nonce * @param {Buffer} nonce
* @param {String} hostname
* @returns {Boolean} * @returns {Boolean}
*/ */
PoolOptions.prototype.hasNonce = function hasNonce(nonce, hostname) { PoolOptions.prototype.hasNonce = function hasNonce(nonce) {
return this.nonces.has(nonce, hostname); return this.nonces.has(nonce);
}; };
/** /**
@ -3799,23 +3799,44 @@ BroadcastItem.prototype.inspect = function inspect() {
*/ */
function NonceList() { function NonceList() {
this.seed1 = util.random(0, 0x100000000); this.map = {};
this.seed2 = util.random(0, 0x100000000); this.hosts = {};
} }
NonceList.prototype.alloc = function alloc(hostname) { NonceList.prototype.alloc = function alloc(hostname) {
var nonce = new Buffer(8); var nonce, key;
var data = new Buffer(hostname, 'ascii');
var lo = murmur3(data, this.seed1); for (;;) {
var hi = murmur3(data, this.seed2); nonce = util.nonce();
nonce.writeUInt32LE(lo, 0, true); key = nonce.toString('hex');
nonce.writeUInt32LE(hi, 4, true); if (!this.map[key]) {
this.map[key] = hostname;
assert(!this.hosts[hostname]);
this.hosts[hostname] = key;
break;
}
}
return nonce; return nonce;
}; };
NonceList.prototype.has = function has(nonce, hostname) { NonceList.prototype.has = function has(nonce) {
var item = this.alloc(hostname); var key = nonce.toString('hex');
return crypto.ccmp(nonce, item); return this.map[key] != null;
};
NonceList.prototype.remove = function remove(hostname) {
var key = this.hosts[hostname];
if (!key)
return false;
delete this.hosts[hostname];
assert(this.map[key]);
delete this.map[key];
return true;
}; };
/** /**