diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 2ab5c9b6..6acc7219 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -1583,10 +1583,11 @@ Peer.prototype._handleAlert = function _handleAlert(details) { /** * Send an `alert` to peer. * @param {AlertPacket} details + * @param {Buffer|KeyPair} [key=network.alertPrivateKey] */ -Peer.prototype.sendAlert = function sendAlert(details) { - var data = bcoin.protocol.framer.alert(details); +Peer.prototype.sendAlert = function sendAlert(details, key) { + var data = bcoin.protocol.framer.alert(details, key); if (!this.invFilter.added(details.hash, 'hex')) return; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b4842809..d09ae8bd 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -794,7 +794,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { * Send `mempool` to all peers. */ -Pool.prototype.getMempool = function getMempool() { +Pool.prototype.sendMempool = function sendMempool() { var i; if (this.peers.load) @@ -804,6 +804,25 @@ Pool.prototype.getMempool = function getMempool() { 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) { var self = this; @@ -1015,7 +1034,6 @@ Pool.prototype._handleAlert = function _handleAlert(details, peer) { var hash = new Buffer(details.hash, 'hex'); var signature = details.signature; var now = bcoin.now(); - var i; if (!this.rejects.added(hash)) return; @@ -1037,14 +1055,7 @@ Pool.prototype._handleAlert = function _handleAlert(details, peer) { bcoin.debug('Received alert from peer (%s).', peer.hostname); bcoin.debug(details); - if (this.peers.load) - 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.sendAlert(details); this.emit('alert', details, peer); }; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 220ba8f5..14e79550 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -1186,52 +1186,93 @@ Framer.addr = function addr(hosts, writer) { /** * Create an alert packet (without a header). * @param {AlertPacket} data + * @param {Buffer|KeyPair} key * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @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 key = data.key; - var p, i, payload, hash; + var p, i, payload, hash, time; + var version, relayUntil, expiration, id, cancel; + var cancels, count, i, minVer, maxVer, subVers; + var priority, comment, statusBar, reserved; if (!key && network.alertPrivateKey) key = network.alertPrivateKey; if (!data.payload) { p = new BufferWriter(); - p.write32(data.version); - p.write64(data.relayUntil); - p.write64(data.expiration); - p.write32(data.id); - p.write32(data.cancel); - p.writeVarint(data.cancels.length); - for (i = 0; i < data.cancels.length; i++) - p.write32(data.cancels[i]); - p.write32(data.minVer); - p.write32(data.maxVer); - p.writeVarint(data.subVers.length); - for (i = 0; i < data.subVers.length; i++) - p.writeVarString(data.subVers[i], 'ascii'); - p.write32(data.priority); - p.writeVarString(data.comment, 'ascii'); - p.writeVarString(data.statusBar, 'ascii'); - p.writeVarString(data.reserved || '', 'ascii'); + time = bcoin.now() + 7 * 86400; + + version = data.version; + relayUntil = data.relayUntil; + expiration = data.expiration; + id = data.id; + cancels = data.cancels || []; + minVer = data.minVer; + maxVer = data.maxVer; + subVers = data.subVers || []; + priority = data.priority; + comment = data.comment || ''; + statusBar = data.statusBar || ''; + reserved = data.reserved || ''; + + if (version == null) + 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(); } else { payload = data.payload; } - p = new BufferWriter(writer); - p.writeVarBytes(payload); - - if (data.signature) { - p.writeVarBytes(data.signature); - } else if (key) { + if (!data.signature) { + assert(key, 'No key or signature.'); hash = utils.dsha256(payload); - p.writeVarBytes(bcoin.ec.sign(hash, key)); - } else { - assert(false, 'No key or signature.'); + data.signature = bcoin.ec.sign(hash, key); } if (!data.hash) { @@ -1240,6 +1281,10 @@ Framer.alert = function alert(data, writer) { data.hash = hash.toString('hex'); } + p = new BufferWriter(writer); + p.writeVarBytes(payload); + p.writeVarBytes(data.signature); + if (!writer) p = p.render();