diff --git a/integration/regtest.js b/integration/regtest.js index 71dde00a..a4f3f775 100644 --- a/integration/regtest.js +++ b/integration/regtest.js @@ -235,7 +235,7 @@ describe('Daemon Binding Functionality', function() { describe('tip updates', function() { it('will get an event when the tip is new', function(done) { this.timeout(4000); - bitcoind.once('tip', function(height) { + bitcoind.on('tip', function(height) { height.should.equal(152); done(); }); diff --git a/lib/daemon.js b/lib/daemon.js index b4eee846..87a3bd07 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -10,8 +10,6 @@ var $ = bitcore.util.preconditions; var daemon = Daemon; - - function Daemon(options) { var self = this; @@ -202,22 +200,20 @@ Daemon.prototype.start = function(options, callback) { }); bitcoindjs.onBlocksReady(function(err, result) { - self.emit('ready', result); - }); - function onTipUpdateListener(err, result) { - if (err) { - self.emit('error', err); - } else { - // Emit and event that the tip was updated - self.emit('tip', result); + function onTipUpdateListener(result) { + if (result) { + // Emit and event that the tip was updated + self.emit('tip', result); + // Recursively wait until the next update + bitcoindjs.onTipUpdate(onTipUpdateListener); + } } - // Recursively wait until the next update bitcoindjs.onTipUpdate(onTipUpdateListener); - } - bitcoindjs.onTipUpdate(onTipUpdateListener); + self.emit('ready', result); + }); setTimeout(function callee() { // Wait until wallet is loaded: diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 747e9a25..91ce4bf3 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -229,9 +229,9 @@ static void async_tip_update(uv_work_t *req) { async_tip_update_data *data = static_cast(req->data); - int64_t nLastBest = nTimeBestReceived; + size_t lastHeight = chainActive.Height(); - while(nLastBest == nTimeBestReceived && !shutdown_complete) { + while(lastHeight == (size_t)chainActive.Height() && !shutdown_complete) { usleep(1E6); } @@ -246,10 +246,13 @@ async_tip_update_after(uv_work_t *req) { async_tip_update_data *data = static_cast(req->data); Local cb = data->callback.Get(isolate); - const unsigned argc = 2; + const unsigned argc = 1; + Local result = Undefined(isolate); + if (!shutdown_complete) { + result = NanNew(data->result); + } Local argv[argc] = { - v8::Null(isolate), - Local::New(isolate, NanNew(data->result)) + Local::New(isolate, result) }; TryCatch try_catch; cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); @@ -259,7 +262,6 @@ async_tip_update_after(uv_work_t *req) { delete data; delete req; - } NAN_METHOD(OnBlocksReady) {