From 3427124270a3b7812a2b29d36d66388d1379ab25 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 4 Dec 2014 11:12:29 -0800 Subject: [PATCH] update bitcoind.js for insight. --- lib/bitcoind.js | 5 ++-- src/bitcoindjs.cc | 65 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 57899216..e03bf259 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -493,8 +493,9 @@ Bitcoin.prototype.getBlockByTx = function(txid, callback) { return bitcoindjs.getBlockByTx(txid, callback); }; -Bitcoin.prototype.getBlockByTime = function(options, callback) { - return bitcoindjs.getBlockByTime(options, callback); +Bitcoin.prototype.getBlocksByDate = +Bitcoin.prototype.getBlocksByTime = function(options, callback) { + return bitcoindjs.getBlocksByTime(options, callback); }; Bitcoin.prototype.log = diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 65b6ec7e..54ac777a 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -214,7 +214,7 @@ NAN_METHOD(GetAddrTransactions); NAN_METHOD(GetBestBlock); NAN_METHOD(GetChainHeight); NAN_METHOD(GetBlockByTx); -NAN_METHOD(GetBlockByTime); +NAN_METHOD(GetBlocksByTime); NAN_METHOD(GetBlockHex); NAN_METHOD(GetTxHex); @@ -475,12 +475,19 @@ struct async_block_tx_data { * async_block_time_data */ +typedef struct _cblocks_list { + CBlock cblock; + CBlockIndex* cblock_index; + struct _cblocks_list *next; + std::string err_msg; +} cblocks_list; + struct async_block_time_data { std::string err_msg; uint32_t gte; uint32_t lte; - CBlock cblock; - CBlockIndex* cblock_index; + int64_t limit; + cblocks_list *cblocks; Persistent callback; }; @@ -2244,19 +2251,19 @@ async_block_tx_after(uv_work_t *req) { } /** - * GetBlockByTime() - * bitcoindjs.getBlockByTime() + * GetBlocksByTime() + * bitcoindjs.getBlocksByTime() * Get block by tx hash (requires -txindex or it's very slow) */ -NAN_METHOD(GetBlockByTime) { +NAN_METHOD(GetBlocksByTime) { NanScope(); if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsFunction()) { return NanThrowError( - "Usage: bitcoindjs.getBlockByTime(options, callback)"); + "Usage: bitcoindjs.getBlocksByTime(options, callback)"); } async_block_time_data *data = new async_block_time_data(); @@ -2274,7 +2281,11 @@ NAN_METHOD(GetBlockByTime) { if (options->Get(NanNew("lte"))->IsNumber()) { data->lte = options->Get(NanNew("lte"))->IntegerValue(); } + if (options->Get(NanNew("limit"))->IsNumber()) { + data->limit = options->Get(NanNew("limit"))->IntegerValue(); + } data->err_msg = std::string(""); + data->cblocks = NULL; Local callback = Local::Cast(args[1]); data->callback = Persistent::New(callback); @@ -2298,15 +2309,28 @@ async_block_time(uv_work_t *req) { int64_t i = 0; // XXX Slow: figure out how to ballpark the height based on gte and lte. int64_t height = chainActive.Height(); + bool found_range = false; + int64_t found = 0; for (; i <= height; i++) { CBlockIndex* pblockindex = chainActive[i]; CBlock cblock; if (ReadBlockFromDisk(cblock, pblockindex)) { uint32_t blocktime = cblock.GetBlockTime(); if (blocktime >= data->gte && blocktime <= data->lte) { - data->cblock = cblock; - data->cblock_index = pblockindex; - return; + found_range = true; + cblocks_list *item = new cblocks_list(); + item->cblock = cblock; + item->cblock_index = pblockindex; + if (data->cblocks == NULL) { + data->cblocks = item; + } else { + data->cblocks->next = item; + data->cblocks = item; + } + found++; + if (found >= data->limit) return; + } else { + if (found_range) return; } } } @@ -2328,16 +2352,21 @@ async_block_time_after(uv_work_t *req) { node::FatalException(try_catch); } } else { - const CBlock& cblock = data->cblock; - CBlockIndex* cblock_index = data->cblock_index; - - Local jsblock = NanNew(); - cblock_to_jsblock(cblock, cblock_index, jsblock, false); - + Local jsblocks = NanNew(); + int i = 0; + cblocks_list *next; + for (cblocks_list *item = data->cblocks; item; item = next) { + Local jsblock = NanNew(); + cblock_to_jsblock(item->cblock, item->cblock_index, jsblock, false); + jsblocks->Set(i, jsblock); + i++; + next = item->next; + delete item; + } const unsigned argc = 2; Local argv[argc] = { Local::New(Null()), - Local::New(jsblock) + Local::New(jsblocks) }; TryCatch try_catch; data->callback->Call(Context::GetCurrent()->Global(), argc, argv); @@ -6464,7 +6493,7 @@ init(Handle target) { NODE_SET_METHOD(target, "getBestBlock", GetBestBlock); NODE_SET_METHOD(target, "getChainHeight", GetChainHeight); NODE_SET_METHOD(target, "getBlockByTx", GetBlockByTx); - NODE_SET_METHOD(target, "getBlockByTime", GetBlockByTime); + NODE_SET_METHOD(target, "getBlocksByTime", GetBlocksByTime); NODE_SET_METHOD(target, "getBlockHex", GetBlockHex); NODE_SET_METHOD(target, "getTxHex", GetTxHex); NODE_SET_METHOD(target, "blockFromHex", BlockFromHex);