net: refactor peer.pending.
This commit is contained in:
parent
edf47d67c8
commit
1365d0ff1d
@ -81,75 +81,67 @@ function Peer(pool) {
|
|||||||
this.pool = pool;
|
this.pool = pool;
|
||||||
this.options = pool.options;
|
this.options = pool.options;
|
||||||
this.logger = pool.logger;
|
this.logger = pool.logger;
|
||||||
this.socket = null;
|
|
||||||
this.outbound = false;
|
|
||||||
this.host = null;
|
|
||||||
this.port = 0;
|
|
||||||
this.hostname = null;
|
|
||||||
this.createSocket = this.options.createSocket;
|
|
||||||
this.chain = this.pool.chain;
|
this.chain = this.pool.chain;
|
||||||
this.mempool = this.pool.mempool;
|
this.mempool = this.pool.mempool;
|
||||||
this.network = this.chain.network;
|
this.network = this.chain.network;
|
||||||
this.locker = new Locker();
|
this.locker = new Locker();
|
||||||
this.version = null;
|
this.createSocket = this.options.createSocket;
|
||||||
|
this.next = null;
|
||||||
|
this.prev = null;
|
||||||
|
|
||||||
|
this.id = Peer.uid++;
|
||||||
|
this.socket = null;
|
||||||
|
this.outbound = false;
|
||||||
|
this.host = '0.0.0.0';
|
||||||
|
this.port = 0;
|
||||||
|
this.hostname = '0.0.0.0:0';
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
this.ack = false;
|
this.ack = false;
|
||||||
this.pending = true;
|
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.ts = 0;
|
this.ts = 0;
|
||||||
|
this.lastSend = 0;
|
||||||
|
this.lastRecv = 0;
|
||||||
|
this.drainStart = 0;
|
||||||
|
this.drainSize = 0;
|
||||||
|
this.drainQueue = [];
|
||||||
|
this.banScore = 0;
|
||||||
|
|
||||||
|
this.version = null;
|
||||||
this.preferHeaders = false;
|
this.preferHeaders = false;
|
||||||
this.haveWitness = false;
|
this.haveWitness = false;
|
||||||
this.hashContinue = null;
|
this.hashContinue = null;
|
||||||
this.spvFilter = null;
|
this.spvFilter = null;
|
||||||
this.relay = true;
|
this.relay = true;
|
||||||
this.feeRate = -1;
|
this.feeRate = -1;
|
||||||
this.addrFilter = new Bloom.Rolling(5000, 0.001);
|
this.bip151 = null;
|
||||||
this.invFilter = new Bloom.Rolling(50000, 0.000001);
|
this.bip150 = null;
|
||||||
this.lastBlock = null;
|
|
||||||
this.waiting = 0;
|
|
||||||
this.syncSent = false;
|
|
||||||
this.connectTimeout = null;
|
|
||||||
this.compactMode = null;
|
this.compactMode = null;
|
||||||
this.compactWitness = false;
|
this.compactWitness = false;
|
||||||
this.compactBlocks = {};
|
this.compactBlocks = {};
|
||||||
|
this.lastMerkle = null;
|
||||||
|
this.waitingTX = 0;
|
||||||
|
this.syncSent = false;
|
||||||
this.sentAddr = false;
|
this.sentAddr = false;
|
||||||
this.bip151 = null;
|
|
||||||
this.bip150 = null;
|
|
||||||
this.lastSend = 0;
|
|
||||||
this.lastRecv = 0;
|
|
||||||
this.drainStart = 0;
|
|
||||||
this.drainSize = 0;
|
|
||||||
this.drainQueue = [];
|
|
||||||
|
|
||||||
this.next = null;
|
|
||||||
this.prev = null;
|
|
||||||
|
|
||||||
this.challenge = null;
|
this.challenge = null;
|
||||||
this.lastPong = -1;
|
this.lastPong = -1;
|
||||||
this.lastPing = -1;
|
this.lastPing = -1;
|
||||||
this.minPing = -1;
|
this.minPing = -1;
|
||||||
|
|
||||||
this.banScore = 0;
|
this.connectTimeout = null;
|
||||||
|
|
||||||
this.pingTimer = null;
|
this.pingTimer = null;
|
||||||
this.pingInterval = 30000;
|
this.pingInterval = 30000;
|
||||||
this.stallTimer = null;
|
this.stallTimer = null;
|
||||||
this.stallInterval = 5000;
|
this.stallInterval = 5000;
|
||||||
|
|
||||||
|
this.addrFilter = new Bloom.Rolling(5000, 0.001);
|
||||||
|
this.invFilter = new Bloom.Rolling(50000, 0.000001);
|
||||||
|
|
||||||
this.requestTimeout = 10000;
|
this.requestTimeout = 10000;
|
||||||
this.requestMap = {};
|
this.requestMap = {};
|
||||||
|
|
||||||
this.queueBlock = new List();
|
this.queueBlock = new List();
|
||||||
this.queueTX = new List();
|
this.queueTX = new List();
|
||||||
|
|
||||||
this.id = Peer.uid++;
|
|
||||||
|
|
||||||
this.setMaxListeners(10000);
|
|
||||||
|
|
||||||
this.host = '0.0.0.0';
|
|
||||||
this.port = 0;
|
|
||||||
this.hostname = '0.0.0.0:0';
|
|
||||||
|
|
||||||
if (this.options.bip151) {
|
if (this.options.bip151) {
|
||||||
this.bip151 = new BIP151();
|
this.bip151 = new BIP151();
|
||||||
if (this.options.bip150) {
|
if (this.options.bip150) {
|
||||||
@ -290,7 +282,6 @@ Peer.prototype.accept = function accept(socket) {
|
|||||||
this.ts = util.now();
|
this.ts = util.now();
|
||||||
this.outbound = false;
|
this.outbound = false;
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
this.pending = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -316,6 +307,7 @@ Peer.prototype.connect = function connect(port, host) {
|
|||||||
this.bind(socket);
|
this.bind(socket);
|
||||||
this.setHost(host, port);
|
this.setHost(host, port);
|
||||||
this.outbound = true;
|
this.outbound = true;
|
||||||
|
this.connected = false;
|
||||||
|
|
||||||
this.logger.debug('Connecting to %s.', this.hostname);
|
this.logger.debug('Connecting to %s.', this.hostname);
|
||||||
|
|
||||||
@ -1133,9 +1125,9 @@ Peer.prototype.onPacket = co(function* onPacket(packet) {
|
|||||||
this.bip150.complete(new Error('Message before auth.'));
|
this.bip150.complete(new Error('Message before auth.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.lastBlock) {
|
if (this.lastMerkle) {
|
||||||
if (packet.type !== packetTypes.TX)
|
if (packet.type !== packetTypes.TX)
|
||||||
this._flushMerkle();
|
this.flushMerkle();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lastRecv = util.ms();
|
this.lastRecv = util.ms();
|
||||||
@ -1225,11 +1217,11 @@ Peer.prototype.onPacket = co(function* onPacket(packet) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Peer.prototype._flushMerkle = function _flushMerkle() {
|
Peer.prototype.flushMerkle = function flushMerkle() {
|
||||||
if (this.lastBlock)
|
assert(this.lastMerkle);
|
||||||
this.fire('merkleblock', this.lastBlock);
|
this.fire('merkleblock', this.lastMerkle);
|
||||||
this.lastBlock = null;
|
this.lastMerkle = null;
|
||||||
this.waiting = 0;
|
this.waitingTX = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1303,11 +1295,11 @@ Peer.prototype.handleMerkleBlock = co(function* handleMerkleBlock(packet) {
|
|||||||
|
|
||||||
block.verifyPartial();
|
block.verifyPartial();
|
||||||
|
|
||||||
this.lastBlock = block;
|
this.lastMerkle = block;
|
||||||
this.waiting = block.matches.length;
|
this.waitingTX = block.matches.length;
|
||||||
|
|
||||||
if (this.waiting === 0)
|
if (this.waitingTX === 0)
|
||||||
this._flushMerkle();
|
this.flushMerkle();
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2125,11 +2117,11 @@ Peer.prototype.handleBlock = co(function* handleBlock(packet) {
|
|||||||
Peer.prototype.handleTX = co(function* handleTX(packet) {
|
Peer.prototype.handleTX = co(function* handleTX(packet) {
|
||||||
var tx = packet.tx;
|
var tx = packet.tx;
|
||||||
|
|
||||||
if (this.lastBlock) {
|
if (this.lastMerkle) {
|
||||||
if (this.lastBlock.hasTX(tx)) {
|
if (this.lastMerkle.hasTX(tx)) {
|
||||||
this.lastBlock.addTX(tx);
|
this.lastMerkle.addTX(tx);
|
||||||
if (--this.waiting === 0)
|
if (--this.waitingTX === 0)
|
||||||
this._flushMerkle();
|
this.flushMerkle();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -706,7 +706,7 @@ Pool.prototype.sync = function sync() {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.outbound || !peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.sync();
|
peer.sync();
|
||||||
}
|
}
|
||||||
@ -721,7 +721,7 @@ Pool.prototype.forceSync = function forceSync() {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.outbound || !peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.syncSent = false;
|
peer.syncSent = false;
|
||||||
peer.sync();
|
peer.sync();
|
||||||
@ -747,7 +747,7 @@ Pool.prototype.stopSync = co(function* stopSync() {
|
|||||||
this.stopTimeout();
|
this.stopTimeout();
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.outbound || !peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.syncSent = false;
|
peer.syncSent = false;
|
||||||
}
|
}
|
||||||
@ -761,7 +761,7 @@ Pool.prototype.sendMempool = function sendMempool() {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.sendMempool();
|
peer.sendMempool();
|
||||||
}
|
}
|
||||||
@ -776,7 +776,7 @@ Pool.prototype.sendAlert = function sendAlert(alert) {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.sendAlert(alert);
|
peer.sendAlert(alert);
|
||||||
}
|
}
|
||||||
@ -1658,7 +1658,7 @@ Pool.prototype.updateWatch = function updateWatch() {
|
|||||||
this.pendingWatch = setTimeout(function() {
|
this.pendingWatch = setTimeout(function() {
|
||||||
self.pendingWatch = null;
|
self.pendingWatch = null;
|
||||||
for (peer = self.peers.head(); peer; peer = peer.next) {
|
for (peer = self.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.updateWatch();
|
peer.updateWatch();
|
||||||
}
|
}
|
||||||
@ -1900,7 +1900,7 @@ Pool.prototype.announceBlock = function announceBlock(msg) {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.announceBlock(msg);
|
peer.announceBlock(msg);
|
||||||
}
|
}
|
||||||
@ -1915,7 +1915,7 @@ Pool.prototype.announceTX = function announceTX(msg) {
|
|||||||
var peer;
|
var peer;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.announceTX(msg);
|
peer.announceTX(msg);
|
||||||
}
|
}
|
||||||
@ -1932,7 +1932,7 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) {
|
|||||||
this.feeRate = rate;
|
this.feeRate = rate;
|
||||||
|
|
||||||
for (peer = this.peers.head(); peer; peer = peer.next) {
|
for (peer = this.peers.head(); peer; peer = peer.next) {
|
||||||
if (!peer.outbound || peer.pending)
|
if (!peer.ack)
|
||||||
continue;
|
continue;
|
||||||
peer.sendFeeRate(rate);
|
peer.sendFeeRate(rate);
|
||||||
}
|
}
|
||||||
@ -2096,8 +2096,7 @@ PeerList.prototype.size = function size() {
|
|||||||
|
|
||||||
PeerList.prototype.promote = function promote(peer) {
|
PeerList.prototype.promote = function promote(peer) {
|
||||||
assert(peer.outbound);
|
assert(peer.outbound);
|
||||||
assert(peer.pending);
|
assert(peer.ack);
|
||||||
peer.pending = false;
|
|
||||||
this.pending--;
|
this.pending--;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2107,7 +2106,7 @@ PeerList.prototype.add = function add(peer) {
|
|||||||
this.map[peer.hostname] = peer;
|
this.map[peer.hostname] = peer;
|
||||||
if (peer.outbound) {
|
if (peer.outbound) {
|
||||||
this.outbound++;
|
this.outbound++;
|
||||||
if (peer.pending)
|
if (!peer.ack)
|
||||||
this.pending++;
|
this.pending++;
|
||||||
} else {
|
} else {
|
||||||
this.inbound++;
|
this.inbound++;
|
||||||
@ -2126,7 +2125,7 @@ PeerList.prototype.remove = function remove(peer) {
|
|||||||
|
|
||||||
if (peer.outbound) {
|
if (peer.outbound) {
|
||||||
this.outbound--;
|
this.outbound--;
|
||||||
if (peer.pending)
|
if (!peer.ack)
|
||||||
this.pending--;
|
this.pending--;
|
||||||
} else {
|
} else {
|
||||||
this.inbound--;
|
this.inbound--;
|
||||||
|
|||||||
@ -194,7 +194,7 @@ describe('Block', function() {
|
|||||||
var ret = {};
|
var ret = {};
|
||||||
block2.hash();
|
block2.hash();
|
||||||
block2.merkleRoot = constants.NULL_HASH;
|
block2.merkleRoot = constants.NULL_HASH;
|
||||||
block2.refresh();
|
block2._validHeaders = null;
|
||||||
assert(!block2.verify(ret));
|
assert(!block2.verify(ret));
|
||||||
assert.equal(ret.reason, 'bad-txnmrklroot');
|
assert.equal(ret.reason, 'bad-txnmrklroot');
|
||||||
block2.merkleRoot = block.merkleRoot;
|
block2.merkleRoot = block.merkleRoot;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user