diff --git a/lib/bitcoind.js b/lib/bitcoind.js index a59a4142..756dc008 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -399,6 +399,9 @@ Block.toBinary = function(block, type) { off += utils.writeU32(p, block.nVersion || block.version, off); // prev_block + if (!block.previousblockhash) { + block.previousblockhash = '0000000000000000000000000000000000000000000000000000000000000000'; + } utils.toArray(block.previousblockhash, 'hex').forEach(function(ch) { p[off++] = ch; }); diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index be8502c8..d4397057 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -290,10 +290,17 @@ struct async_tx_data { * async_poll_blocks_data */ +typedef struct _poll_blocks_list { + CBlock block; + CBlockIndex *block_index; + struct _poll_blocks_list *next; +} poll_blocks_list; + struct async_poll_blocks_data { std::string err_msg; int poll_saved_height; int poll_top_height; + poll_blocks_list *head; Persistent result_array; Persistent callback; }; @@ -863,12 +870,38 @@ async_poll_blocks(uv_work_t *req) { if (cur_height != data->poll_top_height) { data->poll_top_height = cur_height; break; - } else { - // 100 milliseconds - useconds_t usec = 100 * 1000; - usleep(usec); + } + // 100 milliseconds + useconds_t usec = 100 * 1000; + usleep(usec); + } + + poll_blocks_list *head = NULL; + poll_blocks_list *cur = NULL; + + for (int i = data->poll_saved_height; i < data->poll_top_height; i++) { + if (i == -1) continue; + CBlockIndex *pindex = chainActive[i]; + if (pindex != NULL) { + CBlock block; + if (ReadBlockFromDisk(block, pindex)) { + // poll_blocks_list *next = (poll_blocks_list *)malloc(1 * sizeof(poll_blocks_list)); + poll_blocks_list *next = new poll_blocks_list(); + next->next = NULL; + if (cur == NULL) { + head = next; + cur = next; + } else { + cur->next = next; + cur = next; + } + cur->block = block; + cur->block_index = pindex; + } } } + + data->head = head; } static void @@ -889,19 +922,22 @@ async_poll_blocks_after(uv_work_t *req) { const unsigned argc = 2; Local blocks = NanNew(); - for (int i = data->poll_saved_height, j = 0; i < data->poll_top_height; i++) { - if (i == -1) continue; - CBlockIndex *pindex = chainActive[i]; - if (pindex != NULL) { - CBlock block; - // XXX Move this to async_poll_blocks! - if (ReadBlockFromDisk(block, pindex)) { - Local obj = NanNew(); - cblock_to_jsblock(block, pindex, obj); - blocks->Set(j, obj); - j++; - } - } + //poll_blocks_list *cur = data->head; + poll_blocks_list *cur = static_cast(data->head); + poll_blocks_list *next; + int i = 0; + + while (cur != NULL) { + CBlock block = cur->block; + CBlockIndex *pindex = cur->block_index; + Local obj = NanNew(); + cblock_to_jsblock(block, pindex, obj); + blocks->Set(i, obj); + i++; + next = cur->next; + //free(cur); + delete cur; + cur = next; } Local argv[argc] = {