diff --git a/example/index.js b/example/index.js index 4cfc8c0d..3e3930c0 100755 --- a/example/index.js +++ b/example/index.js @@ -14,28 +14,7 @@ bitcoind.start(function(err) { }); bitcoind.on('open', function(status) { console.log('bitcoind: status="%s"', status); - setTimeout(function() { - (function next(hash) { - return bitcoind.getBlock(hash, function(err, block) { - if (err) return print(err.message); - print(block); - if (block.tx.length && block.tx[0].txid) { - var txid = block.tx[0].txid; - // XXX Dies with a segfault! - // bitcoind.getTx(txid, hash, function(err, tx) { - bitcoind.getTx(txid, function(err, tx) { - if (err) return print(err.message); - print('TX -----------------------------------------------------'); - print(tx); - print('/TX ----------------------------------------------------'); - }); - } - if (process.argv[2] === '-r' && block.nextblockhash) { - setTimeout(next.bind(null, block.nextblockhash), 500); - } - }); - })(genesisBlock); - }, 1000); + // getBlocks(bitcoind); bitcoind.on('block', function(block) { console.log('Found block'); console.log('Next: %s', block.nextblockhash); @@ -43,6 +22,31 @@ bitcoind.start(function(err) { }); }); +function getBlocks(bitcoind) { + setTimeout(function() { + (function next(hash) { + return bitcoind.getBlock(hash, function(err, block) { + if (err) return print(err.message); + print(block); + if (block.tx.length && block.tx[0].txid) { + var txid = block.tx[0].txid; + // XXX Dies with a segfault! + // bitcoind.getTx(txid, hash, function(err, tx) { + bitcoind.getTx(txid, function(err, tx) { + if (err) return print(err.message); + print('TX -----------------------------------------------------'); + print(tx); + print('/TX ----------------------------------------------------'); + }); + } + if (process.argv[2] === '-r' && block.nextblockhash) { + setTimeout(next.bind(null, block.nextblockhash), 500); + } + }); + })(genesisBlock); + }, 1000); +} + function inspect(obj) { return util.inspect(obj, null, 20, true); } diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index f20c8823..8fe07305 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -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); * 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 onBlockCb; +static bool blockCbSet = false; + NAN_METHOD(OnBlock) { NanScope(); @@ -1095,9 +1105,12 @@ NAN_METHOD(OnBlock) { Local callback = Local::Cast(args[0]); + onBlockCb = Persistent::New(callback); + blockCbSet = true; + +#if 0 Persistent cb; cb = Persistent::New(callback); - Local block = NanNew(); 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 block = NanNew(); + const unsigned argc = 1; + Local argv[argc] = { + Local::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 block = NanNew(); + const unsigned argc = 1; + Local argv[argc] = { + Local::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 */