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

View File

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