peer: refactor ack handling.

This commit is contained in:
Christopher Jeffrey 2016-12-17 02:07:44 -08:00
parent 7606667ea5
commit 6a87a9588b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 42 additions and 34 deletions

View File

@ -566,6 +566,9 @@ Peer.prototype.announceBlock = function announceBlock(blocks) {
var inv = [];
var i, block;
if (!this.ack)
return;
if (this.destroyed)
return;
@ -614,7 +617,10 @@ Peer.prototype.announceBlock = function announceBlock(blocks) {
Peer.prototype.announceTX = function announceTX(txs) {
var inv = [];
var i, tx, entry;
var i, tx, hash, entry;
if (!this.ack)
return;
if (this.destroyed)
return;
@ -640,7 +646,8 @@ Peer.prototype.announceTX = function announceTX(txs) {
}
if (this.feeRate !== -1 && this.mempool) {
entry = this.mempool.getEntry(tx.hash('hex'));
hash = tx.hash('hex');
entry = this.mempool.getEntry(hash);
if (entry && entry.getRate() < this.feeRate)
continue;
}
@ -693,6 +700,9 @@ Peer.prototype.announceList = function announceList() {
Peer.prototype.sendInv = function sendInv(items) {
var i, item, chunk;
if (!this.ack)
return;
if (this.destroyed)
return;
@ -724,6 +734,9 @@ Peer.prototype.sendInv = function sendInv(items) {
Peer.prototype.sendHeaders = function sendHeaders(items) {
var i, item, chunk;
if (!this.ack)
return;
if (this.destroyed)
return;
@ -794,6 +807,9 @@ Peer.prototype.sendPing = function sendPing() {
*/
Peer.prototype.updateWatch = function updateWatch() {
if (!this.ack)
return;
if (!this.options.spv)
return;
@ -806,6 +822,9 @@ Peer.prototype.updateWatch = function updateWatch() {
*/
Peer.prototype.sendFeeRate = function sendFeeRate(rate) {
if (!this.ack)
return;
this.send(new packets.FeeFilterPacket(rate));
};
@ -866,6 +885,8 @@ Peer.prototype.destroy = function destroy() {
entry.destroy();
}
this.locker.destroy();
this.emit('close');
};
@ -2451,6 +2472,9 @@ Peer.prototype.handleBlockTxn = co(function* handleBlockTxn(packet) {
*/
Peer.prototype.sendAlert = function sendAlert(alert) {
if (!this.ack)
return;
if (!this.invFilter.added(alert.hash()))
return;
@ -2523,6 +2547,9 @@ Peer.prototype.sendGetBlocks = function getBlocks(locator, stop) {
*/
Peer.prototype.sendMempool = function sendMempool() {
if (!this.ack)
return;
if (!this.version)
return;

View File

@ -87,7 +87,6 @@ var VerifyResult = errors.VerifyResult;
* @emits Pool#version
* @emits Pool#ack
* @emits Pool#watched
* @emits Pool#leech
*/
function Pool(options) {
@ -439,7 +438,7 @@ Pool.prototype.listen = function listen() {
}
this.server.on('connection', function(socket) {
self.handleLeech(socket);
self.handleInbound(socket);
});
this.server.on('listening', function() {
@ -480,7 +479,7 @@ Pool.prototype.unlisten = function unlisten() {
* @param {net.Socket} socket
*/
Pool.prototype.handleLeech = function handleLeech(socket) {
Pool.prototype.handleInbound = function handleInbound(socket) {
var addr;
if (!socket.remoteAddress) {
@ -706,7 +705,7 @@ Pool.prototype.sync = function sync() {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.outbound || !peer.ack)
if (!peer.outbound)
continue;
peer.sync();
}
@ -721,7 +720,7 @@ Pool.prototype.forceSync = function forceSync() {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.outbound || !peer.ack)
if (!peer.outbound)
continue;
peer.syncSent = false;
peer.sync();
@ -747,7 +746,7 @@ Pool.prototype.stopSync = co(function* stopSync() {
this.stopTimeout();
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.outbound || !peer.ack)
if (!peer.outbound)
continue;
peer.syncSent = false;
}
@ -760,11 +759,8 @@ Pool.prototype.stopSync = co(function* stopSync() {
Pool.prototype.sendMempool = function sendMempool() {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = this.peers.head(); peer; peer = peer.next)
peer.sendMempool();
}
};
/**
@ -775,11 +771,8 @@ Pool.prototype.sendMempool = function sendMempool() {
Pool.prototype.sendAlert = function sendAlert(alert) {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = this.peers.head(); peer; peer = peer.next)
peer.sendAlert(alert);
}
};
/**
@ -893,7 +886,7 @@ Pool.prototype.bindPeer = function bindPeer(peer) {
}
}));
peer.on('txs', function (txs) {
peer.on('txs', function(txs) {
self.handleTXInv(txs, peer);
});
@ -1524,7 +1517,7 @@ Pool.prototype.addInbound = function addInbound(socket) {
this.peers.add(peer);
util.nextTick(function() {
self.emit('leech', peer);
self.emit('peer', peer);
});
};
@ -1657,11 +1650,8 @@ Pool.prototype.updateWatch = function updateWatch() {
this.pendingWatch = setTimeout(function() {
self.pendingWatch = null;
for (peer = self.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = self.peers.head(); peer; peer = peer.next)
peer.updateWatch();
}
}, 50);
};
@ -1899,11 +1889,8 @@ Pool.prototype.broadcast = function broadcast(msg) {
Pool.prototype.announceBlock = function announceBlock(msg) {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = this.peers.head(); peer; peer = peer.next)
peer.announceBlock(msg);
}
};
/**
@ -1914,11 +1901,8 @@ Pool.prototype.announceBlock = function announceBlock(msg) {
Pool.prototype.announceTX = function announceTX(msg) {
var peer;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = this.peers.head(); peer; peer = peer.next)
peer.announceTX(msg);
}
};
/**
@ -1931,11 +1915,8 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) {
this.feeRate = rate;
for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.ack)
continue;
for (peer = this.peers.head(); peer; peer = peer.next)
peer.sendFeeRate(rate);
}
};
/**