alert handling.

This commit is contained in:
Christopher Jeffrey 2016-06-12 02:25:58 -07:00
parent 08f5865e78
commit f313b43414
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 98 additions and 41 deletions

View File

@ -1583,10 +1583,11 @@ Peer.prototype._handleAlert = function _handleAlert(details) {
/** /**
* Send an `alert` to peer. * Send an `alert` to peer.
* @param {AlertPacket} details * @param {AlertPacket} details
* @param {Buffer|KeyPair} [key=network.alertPrivateKey]
*/ */
Peer.prototype.sendAlert = function sendAlert(details) { Peer.prototype.sendAlert = function sendAlert(details, key) {
var data = bcoin.protocol.framer.alert(details); var data = bcoin.protocol.framer.alert(details, key);
if (!this.invFilter.added(details.hash, 'hex')) if (!this.invFilter.added(details.hash, 'hex'))
return; return;

View File

@ -794,7 +794,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) {
* Send `mempool` to all peers. * Send `mempool` to all peers.
*/ */
Pool.prototype.getMempool = function getMempool() { Pool.prototype.sendMempool = function sendMempool() {
var i; var i;
if (this.peers.load) if (this.peers.load)
@ -804,6 +804,25 @@ Pool.prototype.getMempool = function getMempool() {
this.peers.regular[i].sendMempool(); this.peers.regular[i].sendMempool();
}; };
/**
* Send `alert` to all peers.
* @param {AlertPacket} details
* @param {Buffer|KeyPair} [key=network.alertPrivateKey]
*/
Pool.prototype.sendAlert = function sendAlert(details, key) {
var i;
if (this.peers.load)
this.peers.load.sendAlert(details, key);
for (i = 0; i < this.peers.regular.length; i++)
this.peers.regular[i].sendAlert(details, key);
for (i = 0; i < this.peers.leeches.length; i++)
this.peers.leeches[i].sendAlert(details, key);
};
Pool.prototype._createPeer = function _createPeer(options) { Pool.prototype._createPeer = function _createPeer(options) {
var self = this; var self = this;
@ -1015,7 +1034,6 @@ Pool.prototype._handleAlert = function _handleAlert(details, peer) {
var hash = new Buffer(details.hash, 'hex'); var hash = new Buffer(details.hash, 'hex');
var signature = details.signature; var signature = details.signature;
var now = bcoin.now(); var now = bcoin.now();
var i;
if (!this.rejects.added(hash)) if (!this.rejects.added(hash))
return; return;
@ -1037,14 +1055,7 @@ Pool.prototype._handleAlert = function _handleAlert(details, peer) {
bcoin.debug('Received alert from peer (%s).', peer.hostname); bcoin.debug('Received alert from peer (%s).', peer.hostname);
bcoin.debug(details); bcoin.debug(details);
if (this.peers.load) this.sendAlert(details);
this.peers.load.sendAlert(details);
for (i = 0; i < this.peers.regular.length; i++)
this.peers.regular[i].sendAlert(details);
for (i = 0; i < this.peers.leeches.length; i++)
this.peers.leeches[i].sendAlert(details);
this.emit('alert', details, peer); this.emit('alert', details, peer);
}; };

View File

@ -1186,52 +1186,93 @@ Framer.addr = function addr(hosts, writer) {
/** /**
* Create an alert packet (without a header). * Create an alert packet (without a header).
* @param {AlertPacket} data * @param {AlertPacket} data
* @param {Buffer|KeyPair} key
* @param {BufferWriter?} writer - A buffer writer to continue writing from. * @param {BufferWriter?} writer - A buffer writer to continue writing from.
* @returns {Buffer} Returns a BufferWriter if `writer` was passed in. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in.
*/ */
Framer.alert = function alert(data, writer) { Framer.alert = function alert(data, key, writer) {
var network = bcoin.network.get(data.network); var network = bcoin.network.get(data.network);
var key = data.key; var p, i, payload, hash, time;
var p, i, payload, hash; var version, relayUntil, expiration, id, cancel;
var cancels, count, i, minVer, maxVer, subVers;
var priority, comment, statusBar, reserved;
if (!key && network.alertPrivateKey) if (!key && network.alertPrivateKey)
key = network.alertPrivateKey; key = network.alertPrivateKey;
if (!data.payload) { if (!data.payload) {
p = new BufferWriter(); p = new BufferWriter();
p.write32(data.version); time = bcoin.now() + 7 * 86400;
p.write64(data.relayUntil);
p.write64(data.expiration); version = data.version;
p.write32(data.id); relayUntil = data.relayUntil;
p.write32(data.cancel); expiration = data.expiration;
p.writeVarint(data.cancels.length); id = data.id;
for (i = 0; i < data.cancels.length; i++) cancels = data.cancels || [];
p.write32(data.cancels[i]); minVer = data.minVer;
p.write32(data.minVer); maxVer = data.maxVer;
p.write32(data.maxVer); subVers = data.subVers || [];
p.writeVarint(data.subVers.length); priority = data.priority;
for (i = 0; i < data.subVers.length; i++) comment = data.comment || '';
p.writeVarString(data.subVers[i], 'ascii'); statusBar = data.statusBar || '';
p.write32(data.priority); reserved = data.reserved || '';
p.writeVarString(data.comment, 'ascii');
p.writeVarString(data.statusBar, 'ascii'); if (version == null)
p.writeVarString(data.reserved || '', 'ascii'); version = 1;
if (relayUntil == null)
relayUntil = time;
if (expiration == null)
expiration = time;
if (id == null)
id = 1;
if (cancel == null)
cancel = 0;
if (minVer == null)
minVer = 10000;
if (maxVer == null)
maxVer = constants.VERSION;
if (priority == null)
priority = 100;
p.write32(version);
p.write64(relayUntil);
p.write64(expiration);
p.write32(id);
p.write32(cancel);
p.writeVarint(cancels.length);
for (i = 0; i < cancels.length; i++)
p.write32(cancels[i]);
p.write32(minVer);
p.write32(maxVer);
p.writeVarint(subVers.length);
for (i = 0; i < subVers.length; i++)
p.writeVarString(subVers[i], 'ascii');
p.write32(priority);
p.writeVarString(comment, 'ascii');
p.writeVarString(statusBar, 'ascii');
p.writeVarString(reserved, 'ascii');
payload = p.render(); payload = p.render();
} else { } else {
payload = data.payload; payload = data.payload;
} }
p = new BufferWriter(writer); if (!data.signature) {
p.writeVarBytes(payload); assert(key, 'No key or signature.');
if (data.signature) {
p.writeVarBytes(data.signature);
} else if (key) {
hash = utils.dsha256(payload); hash = utils.dsha256(payload);
p.writeVarBytes(bcoin.ec.sign(hash, key)); data.signature = bcoin.ec.sign(hash, key);
} else {
assert(false, 'No key or signature.');
} }
if (!data.hash) { if (!data.hash) {
@ -1240,6 +1281,10 @@ Framer.alert = function alert(data, writer) {
data.hash = hash.toString('hex'); data.hash = hash.toString('hex');
} }
p = new BufferWriter(writer);
p.writeVarBytes(payload);
p.writeVarBytes(data.signature);
if (!writer) if (!writer)
p = p.render(); p = p.render();