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;
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.');
}
@ -2293,7 +2293,7 @@ PeerOptions.isFull = function isFull() {
* @returns {Buffer}
*/
PeerOptions.createNonce = function createNonce() {
PeerOptions.createNonce = function createNonce(hostname) {
return util.nonce();
};
@ -2301,10 +2301,11 @@ PeerOptions.createNonce = function createNonce() {
* Test whether version nonce is ours.
* @private
* @param {Buffer} nonce
* @param {String} hostname
* @returns {Boolean}
*/
PeerOptions.hasNonce = function hasNonce() {
PeerOptions.hasNonce = function hasNonce(nonce, hostname) {
return false;
};

View File

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