From 7977247b7949d65a110844f9386c8fdc126f8104 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 23 Oct 2014 13:07:40 -0700 Subject: [PATCH] packet events. --- example/index.js | 4 +-- lib/bitcoind.js | 75 +++++++++++++++++++----------------------------- 2 files changed, 31 insertions(+), 48 deletions(-) diff --git a/example/index.js b/example/index.js index cbde2790..fd3f431a 100755 --- a/example/index.js +++ b/example/index.js @@ -113,8 +113,8 @@ bitcoind.on('open', function(status) { }); } - bitcoind.on('packets', function(packets) { - bitcoind.log(packets); + bitcoind.on('packet:parsed', function(packet) { + bitcoind.log(packet); }); return; diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 93a7a149..ebe70254 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -144,7 +144,7 @@ Bitcoin.prototype.start = function(options, callback) { if (!packets) { if (self.debug) { - bitcoind.error('Error polling packet queue.'); + self.error('Error polling packet queue.'); } return; } @@ -153,63 +153,46 @@ Bitcoin.prototype.start = function(options, callback) { return; } - self.emit('packets', packets); + self.emit('_packets', packets); packets.forEach(function(packet) { setImmediate(function() { - self.emit('packet:' + packet.name, packet); - self.emit('packet', packet); - if (packet.name === 'version') { - self.emit(packet.name, packet); - self.emit('peer', packet); - } else if (packet.name === 'verack') { - self.emit(packet.name, packet); - } else if (packet.name === 'addr') { + self.emit('raw:' + packet.name, packet); + self.emit('raw', packet); + + if (packet.name === 'addr') { self.emit(packet.name, packet.addresses); - } else if (packet.name === 'inv') { - self.emit(packet.name, packet); - } else if (packet.name === 'getdata') { - self.emit(packet.name, packet); - } else if (packet.name === 'getblocks') { - self.emit(packet.name, packet); - } else if (packet.name === 'getheaders') { - self.emit(packet.name, packet); - } else if (packet.name === 'tx') { - if (!packet.tx.blockhash) { - packet.name = 'mptx'; - } - self.emit(packet.name, bitcoind.tx(packet.tx)); - } else if (packet.name === 'block') { - var block = bitcoind.block(packet.block); + self.emit('parsed', packet.addresses); + return; + } + + if (packet.name === 'block') { + var block = self.block(packet.block); self.emit(packet.name, block); + self.emit('parsed', block); block.tx.forEach(function(tx) { setImmediate(function() { + tx = self.tx(tx); self.emit('tx', tx); self.emit('parsed', tx); }); }); - } else if (packet.name === 'getaddr') { - self.emit(packet.name, packet); - } else if (packet.name === 'mempool') { - self.emit(packet.name, packet); - } else if (packet.name === 'ping') { - self.emit(packet.name, packet); - } else if (packet.name === 'pong') { - self.emit(packet.name, packet); - } else if (packet.name === 'alert') { - self.emit(packet.name, packet); - } else if (packet.name === 'filterload') { - self.emit(packet.name, packet); - } else if (packet.name === 'filteradd') { - self.emit(packet.name, packet); - } else if (packet.name === 'filterclear') { - self.emit(packet.name, packet); - } else if (packet.name === 'reject') { - self.emit(packet.name, packet); - } else if (packet.name === 'unknown') { - self.emit(packet.name, packet); + return; } - self.emit('parsed', packet.addresses || packet.block || packet.tx); + + if (packet.name === 'tx') { + var tx = self.tx(packet.tx); + var name = packet.name; + if (!packet.tx.blockhash) { + name = 'mptx'; + } + self.emit(name, tx); + self.emit('parsed', tx); + return; + } + + self.emit(packet.name, packet); + self.emit('parsed', packet); }); }); }, 50);