Fix bug with feedback loop during shutdown

- Use height to indicate tip change by comparing heights
- Give undefined for callback when shutting down
This commit is contained in:
Braydon Fuller 2015-07-24 12:32:28 -04:00
parent 4189e3548a
commit 93ed8bfd8d
3 changed files with 18 additions and 20 deletions

View File

@ -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();
});

View File

@ -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:

View File

@ -229,9 +229,9 @@ static void
async_tip_update(uv_work_t *req) {
async_tip_update_data *data = static_cast<async_tip_update_data*>(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<async_tip_update_data*>(req->data);
Local<Function> cb = data->callback.Get(isolate);
const unsigned argc = 2;
const unsigned argc = 1;
Local<Value> result = Undefined(isolate);
if (!shutdown_complete) {
result = NanNew<Number>(data->result);
}
Local<Value> argv[argc] = {
v8::Null(isolate),
Local<Value>::New(isolate, NanNew<Number>(data->result))
Local<Value>::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) {