diff --git a/example/index.js b/example/index.js index 3d4349e9..a6f7fce5 100755 --- a/example/index.js +++ b/example/index.js @@ -14,10 +14,11 @@ bitcoind.start(function(err) { }); bitcoind.on('open', function(status) { console.log('bitcoind: status="%s"', status); - getBlocks(bitcoind); - // bitcoind.on('block', function(block) { - // console.log('Found block: %j', block); - // }); + // getBlocks(bitcoind); + bitcoind.on('block', function(block) { + console.log('Found block:'); + console.log(block); + }); }); }); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 89de2880..80e02bab 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -133,19 +133,24 @@ Bitcoin.prototype.start = function(callback) { this._emitted = {}; (function next() { - bitcoindjs.pollBlocks(function(err, blocks) { + return bitcoindjs.pollBlocks(function(err, blocks) { if (err) return setTimeout(next, self.pollInterval); - utils.forEach(blocks, function(block, nextBlock) { - var hash = block.hash || block; + return utils.forEach(blocks, function(block, nextBlock) { // XXX Bad workaround - if (self._emitted[hash]) return nextBlock(); - self._emitted[hash] = true; + if (self._emitted[block.hash]) { + return setImmediate(function() { + return nextBlock(); + }); + } + self._emitted[block.hash] = true; + self.emit('block', block); - setImmediate(function() { + + return setImmediate(function() { nextBlock(); }); }, function() { - setTimeout(next, self.pollInterval); + return setTimeout(next, self.pollInterval); }); }); })(); diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index ae16b9ca..cc81a3ee 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -983,7 +983,9 @@ async_poll_blocks_after(uv_work_t *req) { if (pindex != NULL) { CBlock block; if (ReadBlockFromDisk(block, pindex)) { - blocks->Set(j, NanNew(block.GetHash().GetHex().c_str())); + Local obj = NanNew(); + cblock_to_js(block, pindex, obj); + blocks->Set(j, obj); j++; } }