pool: noncelist.

This commit is contained in:
Christopher Jeffrey 2017-01-21 05:17:12 -08:00
parent 3abfdb3770
commit 44201193e8
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 20 additions and 35 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)) if (this.options.hasNonce(packet.nonce, this.hostname()))
throw new Error('We connected to ourself. Oops.'); throw new Error('We connected to ourself. Oops.');
} }
@ -2293,7 +2293,7 @@ PeerOptions.isFull = function isFull() {
* @returns {Buffer} * @returns {Buffer}
*/ */
PeerOptions.createNonce = function createNonce() { PeerOptions.createNonce = function createNonce(hostname) {
return util.nonce(); return util.nonce();
}; };
@ -2301,10 +2301,11 @@ PeerOptions.createNonce = function createNonce() {
* 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() { PeerOptions.hasNonce = function hasNonce(nonce, hostname) {
return false; return false;
}; };

View File

@ -21,6 +21,7 @@ var BIP151 = require('./bip151');
var BIP152 = require('./bip152'); var BIP152 = require('./bip152');
var Bloom = require('../utils/bloom'); var Bloom = require('../utils/bloom');
var ec = require('../crypto/ec'); var ec = require('../crypto/ec');
var crypto = require('../crypto/crypto');
var Lock = require('../utils/lock'); var Lock = require('../utils/lock');
var Network = require('../protocol/network'); var Network = require('../protocol/network');
var Peer = require('./peer'); var Peer = require('./peer');
@ -31,6 +32,7 @@ var dns = require('./dns');
var HostList = require('./hostlist'); var HostList = require('./hostlist');
var InvItem = require('../primitives/invitem'); var InvItem = require('../primitives/invitem');
var Map = require('../utils/map'); var Map = require('../utils/map');
var murmur3 = require('../utils/murmur3');
var packets = require('./packets'); var packets = require('./packets');
var services = common.services; var services = common.services;
var invTypes = InvItem.types; var invTypes = InvItem.types;
@ -2692,7 +2694,6 @@ Pool.prototype.removePeer = function removePeer(peer) {
var i, hashes, hash; var i, hashes, hash;
this.peers.remove(peer); this.peers.remove(peer);
this.nonces.remove(peer.hostname());
hashes = peer.requestMap.keys(); hashes = peer.requestMap.keys();
@ -3453,11 +3454,12 @@ 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) { PoolOptions.prototype.hasNonce = function hasNonce(nonce, hostname) {
return this.nonces.has(nonce); return this.nonces.has(nonce, hostname);
}; };
/** /**
@ -3797,41 +3799,23 @@ BroadcastItem.prototype.inspect = function inspect() {
*/ */
function NonceList() { function NonceList() {
this.nonces = {}; this.seed1 = util.random(0, 0x100000000);
this.hosts = {}; this.seed2 = util.random(0, 0x100000000);
} }
NonceList.prototype.alloc = function alloc(hostname) { NonceList.prototype.alloc = function alloc(hostname) {
var nonce = util.nonce(); var nonce = new Buffer(8);
var key = nonce.toString('hex'); var data = new Buffer(hostname, 'ascii');
this.nonces[key] = hostname; var lo = murmur3(data, this.seed1);
this.hosts[hostname] = key; var hi = murmur3(data, this.seed2);
nonce.writeUInt32LE(lo, 0, true);
nonce.writeUInt32LE(hi, 4, true);
return nonce; return nonce;
}; };
NonceList.prototype.has = function has(nonce) { NonceList.prototype.has = function has(nonce, hostname) {
var key = nonce.toString('hex'); var item = this.alloc(hostname);
var hostname = this.nonces[key]; return crypto.ccmp(nonce, item);
if (!hostname)
return false;
delete this.nonces[key];
delete this.hosts[hostname];
return true;
};
NonceList.prototype.remove = function remove(hostname) {
var key = this.hosts[hostname];
if (!key)
return false;
delete this.nonces[key];
delete this.hosts[hostname];
return true;
}; };
/** /**