start polling for events again.

This commit is contained in:
Christopher Jeffrey 2014-09-22 16:47:45 -07:00
parent fd34862c4c
commit fdc49acedc
2 changed files with 87 additions and 23 deletions

View File

@ -14,6 +14,15 @@ 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');
console.log('Next: %s', block.nextblockhash);
});
});
});
function getBlocks(bitcoind) {
setTimeout(function() {
(function next(hash) {
return bitcoind.getBlock(hash, function(err, block) {
@ -36,12 +45,7 @@ bitcoind.start(function(err) {
});
})(genesisBlock);
}, 1000);
bitcoind.on('block', function(block) {
console.log('Found block');
console.log('Next: %s', block.nextblockhash);
});
});
});
}
function inspect(obj) {
return util.inspect(obj, null, 20, true);

View File

@ -144,6 +144,9 @@ start_node(void);
static void
start_node_thread(void);
static void
poll_blocks(void);
#if OUTPUT_REDIR
static void
open_pipes(int **out_pipe, int **log_pipe);
@ -177,6 +180,8 @@ init(Handle<Object>);
* Private Variables
*/
static volatile CBlockIndex *lastindex = NULL;
static volatile bool shutdownComplete = false;
/**
@ -382,6 +387,8 @@ start_node(void) {
(boost::thread *)new boost::thread(boost::bind(&start_node_thread));
(boost::thread *)new boost::thread(boost::bind(&poll_blocks));
// horrible fix for a race condition
sleep(2);
signal(SIGINT, SIG_DFL);
@ -1085,6 +1092,9 @@ async_get_tx_after(uv_work_t *req) {
* bitcoind.onBlock(callback)
*/
Persistent<Function> onBlockCb;
static bool blockCbSet = false;
NAN_METHOD(OnBlock) {
NanScope();
@ -1095,9 +1105,12 @@ NAN_METHOD(OnBlock) {
Local<Function> callback = Local<Function>::Cast(args[0]);
onBlockCb = Persistent<Function>::New(callback);
blockCbSet = true;
#if 0
Persistent<Function> cb;
cb = Persistent<Function>::New(callback);
Local<Object> block = NanNew<Object>();
const unsigned argc = 1;
@ -1109,6 +1122,7 @@ NAN_METHOD(OnBlock) {
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
#endif
NanReturnValue(Undefined());
}
@ -1146,6 +1160,52 @@ NAN_METHOD(OnTx) {
NanReturnValue(Undefined());
}
static void
poll_blocks(void) {
if (!lastindex) {
lastindex = chainActive.Tip();
}
CBlockIndex *pnext = chainActive.Next((const CBlockIndex *)lastindex);
CBlockIndex *pcur = pnext;
if (pnext) {
// execute callback
printf("Found block\n");
if (blockCbSet) {
Local<Object> block = NanNew<Object>();
const unsigned argc = 1;
Local<Value> argv[argc] = {
Local<Value>::New(block)
};
TryCatch try_catch;
onBlockCb->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
while ((pcur = chainActive.Next(pcur))) {
// execute callback
printf("Found block\n");
if (blockCbSet) {
Local<Object> block = NanNew<Object>();
const unsigned argc = 1;
Local<Value> argv[argc] = {
Local<Value>::New(block)
};
TryCatch try_catch;
onBlockCb->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
pnext = pcur;
}
}
if (pnext) {
lastindex = pnext;
}
sleep(1);
}
/**
* Init
*/