emit events via height.

This commit is contained in:
Christopher Jeffrey 2014-09-22 18:34:38 -07:00
parent 4038258347
commit 4017d88d13
2 changed files with 22 additions and 29 deletions

View File

@ -130,10 +130,16 @@ Bitcoin.prototype.start = function(callback) {
this.pollInterval = 300; this.pollInterval = 300;
this._emitted = {};
(function next() { (function next() {
bitcoindjs.pollBlocks(function(err, blocks) { bitcoindjs.pollBlocks(function(err, blocks) {
if (err) return setTimeout(next, self.pollInterval); if (err) return setTimeout(next, self.pollInterval);
utils.forEach(blocks, function(block, nextBlock) { utils.forEach(blocks, function(block, nextBlock) {
var hash = block.hash || block;
// XXX Bad workaround
if (self._emitted[hash]) return nextBlock();
self._emitted[hash] = true;
self.emit('block', block); self.emit('block', block);
setImmediate(function() { setImmediate(function() {
nextBlock(); nextBlock();

View File

@ -238,9 +238,8 @@ struct async_tx_data {
struct async_poll_blocks_data { struct async_poll_blocks_data {
std::string err_msg; std::string err_msg;
uint256 poll_top_hash; int poll_saved_height;
CBlockIndex *poll_top_index; int poll_top_height;
CBlockIndex *poll_saved_index;
Persistent<Array> result_array; Persistent<Array> result_array;
Persistent<Function> callback; Persistent<Function> callback;
}; };
@ -1114,9 +1113,8 @@ NAN_METHOD(PollBlocks) {
Local<Function> callback = Local<Function>::Cast(args[0]); Local<Function> callback = Local<Function>::Cast(args[0]);
async_poll_blocks_data *data = new async_poll_blocks_data(); async_poll_blocks_data *data = new async_poll_blocks_data();
data->poll_top_hash = 0; data->poll_saved_height = -1;
data->poll_top_index = NULL; data->poll_top_height = -1;
data->poll_saved_index = NULL;
data->err_msg = std::string(""); data->err_msg = std::string("");
data->callback = Persistent<Function>::New(callback); data->callback = Persistent<Function>::New(callback);
@ -1136,14 +1134,12 @@ static void
async_poll_blocks(uv_work_t *req) { async_poll_blocks(uv_work_t *req) {
async_poll_blocks_data* data = static_cast<async_poll_blocks_data*>(req->data); async_poll_blocks_data* data = static_cast<async_poll_blocks_data*>(req->data);
data->poll_saved_index = data->poll_top_index; data->poll_saved_height = data->poll_top_height;
CBlockIndex *cur_index;
while ((cur_index = chainActive.Tip())) { while (chainActive.Tip()) {
uint256 cur_hash = cur_index->GetBlockHash(); int cur_height = chainActive.Height();
if (cur_hash != data->poll_top_hash) { if (cur_height != data->poll_top_height) {
data->poll_top_hash = cur_hash; data->poll_top_height = cur_height;
data->poll_top_index = cur_index;
break; break;
} else { } else {
// 100 milliseconds // 100 milliseconds
@ -1171,25 +1167,16 @@ async_poll_blocks_after(uv_work_t *req) {
const unsigned argc = 2; const unsigned argc = 2;
Local<Array> blocks = NanNew<Array>(); Local<Array> blocks = NanNew<Array>();
CBlockIndex *pnext = data->poll_saved_index; for (int i = data->poll_saved_height, j = 0; i < data->poll_top_height; i++) {
if (i == -1) continue;
if (!pnext) { CBlockIndex *pindex = chainActive[i];
if (data->poll_top_index) { if (pindex != NULL) {
CBlock block; CBlock block;
if (ReadBlockFromDisk(block, data->poll_top_index)) { if (ReadBlockFromDisk(block, pindex)) {
blocks->Set(0, NanNew<String>(block.GetHash().GetHex().c_str())); blocks->Set(j, NanNew<String>(block.GetHash().GetHex().c_str()));
j++;
} }
} }
} else {
int i = 0;
do {
CBlock block;
if (ReadBlockFromDisk(block, pnext)) {
blocks->Set(i, NanNew<String>(block.GetHash().GetHex().c_str()));
i++;
}
if (pnext == data->poll_top_index) break;
} while ((pnext = chainActive.Next((const CBlockIndex *)pnext)));
} }
Local<Value> argv[argc] = { Local<Value> argv[argc] = {