update bitcoind.js for insight.

This commit is contained in:
Christopher Jeffrey 2014-12-04 11:12:29 -08:00
parent 42b15dd24d
commit 3427124270
2 changed files with 50 additions and 20 deletions

View File

@ -493,8 +493,9 @@ Bitcoin.prototype.getBlockByTx = function(txid, callback) {
return bitcoindjs.getBlockByTx(txid, callback); return bitcoindjs.getBlockByTx(txid, callback);
}; };
Bitcoin.prototype.getBlockByTime = function(options, callback) { Bitcoin.prototype.getBlocksByDate =
return bitcoindjs.getBlockByTime(options, callback); Bitcoin.prototype.getBlocksByTime = function(options, callback) {
return bitcoindjs.getBlocksByTime(options, callback);
}; };
Bitcoin.prototype.log = Bitcoin.prototype.log =

View File

@ -214,7 +214,7 @@ NAN_METHOD(GetAddrTransactions);
NAN_METHOD(GetBestBlock); NAN_METHOD(GetBestBlock);
NAN_METHOD(GetChainHeight); NAN_METHOD(GetChainHeight);
NAN_METHOD(GetBlockByTx); NAN_METHOD(GetBlockByTx);
NAN_METHOD(GetBlockByTime); NAN_METHOD(GetBlocksByTime);
NAN_METHOD(GetBlockHex); NAN_METHOD(GetBlockHex);
NAN_METHOD(GetTxHex); NAN_METHOD(GetTxHex);
@ -475,12 +475,19 @@ struct async_block_tx_data {
* async_block_time_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 { struct async_block_time_data {
std::string err_msg; std::string err_msg;
uint32_t gte; uint32_t gte;
uint32_t lte; uint32_t lte;
CBlock cblock; int64_t limit;
CBlockIndex* cblock_index; cblocks_list *cblocks;
Persistent<Function> callback; Persistent<Function> callback;
}; };
@ -2244,19 +2251,19 @@ async_block_tx_after(uv_work_t *req) {
} }
/** /**
* GetBlockByTime() * GetBlocksByTime()
* bitcoindjs.getBlockByTime() * bitcoindjs.getBlocksByTime()
* Get block by tx hash (requires -txindex or it's very slow) * Get block by tx hash (requires -txindex or it's very slow)
*/ */
NAN_METHOD(GetBlockByTime) { NAN_METHOD(GetBlocksByTime) {
NanScope(); NanScope();
if (args.Length() < 2 if (args.Length() < 2
|| !args[0]->IsString() || !args[0]->IsString()
|| !args[1]->IsFunction()) { || !args[1]->IsFunction()) {
return NanThrowError( return NanThrowError(
"Usage: bitcoindjs.getBlockByTime(options, callback)"); "Usage: bitcoindjs.getBlocksByTime(options, callback)");
} }
async_block_time_data *data = new async_block_time_data(); async_block_time_data *data = new async_block_time_data();
@ -2274,7 +2281,11 @@ NAN_METHOD(GetBlockByTime) {
if (options->Get(NanNew<String>("lte"))->IsNumber()) { if (options->Get(NanNew<String>("lte"))->IsNumber()) {
data->lte = options->Get(NanNew<String>("lte"))->IntegerValue(); data->lte = options->Get(NanNew<String>("lte"))->IntegerValue();
} }
if (options->Get(NanNew<String>("limit"))->IsNumber()) {
data->limit = options->Get(NanNew<String>("limit"))->IntegerValue();
}
data->err_msg = std::string(""); data->err_msg = std::string("");
data->cblocks = NULL;
Local<Function> callback = Local<Function>::Cast(args[1]); Local<Function> callback = Local<Function>::Cast(args[1]);
data->callback = Persistent<Function>::New(callback); data->callback = Persistent<Function>::New(callback);
@ -2298,15 +2309,28 @@ async_block_time(uv_work_t *req) {
int64_t i = 0; int64_t i = 0;
// XXX Slow: figure out how to ballpark the height based on gte and lte. // XXX Slow: figure out how to ballpark the height based on gte and lte.
int64_t height = chainActive.Height(); int64_t height = chainActive.Height();
bool found_range = false;
int64_t found = 0;
for (; i <= height; i++) { for (; i <= height; i++) {
CBlockIndex* pblockindex = chainActive[i]; CBlockIndex* pblockindex = chainActive[i];
CBlock cblock; CBlock cblock;
if (ReadBlockFromDisk(cblock, pblockindex)) { if (ReadBlockFromDisk(cblock, pblockindex)) {
uint32_t blocktime = cblock.GetBlockTime(); uint32_t blocktime = cblock.GetBlockTime();
if (blocktime >= data->gte && blocktime <= data->lte) { if (blocktime >= data->gte && blocktime <= data->lte) {
data->cblock = cblock; found_range = true;
data->cblock_index = pblockindex; cblocks_list *item = new cblocks_list();
return; 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); node::FatalException(try_catch);
} }
} else { } else {
const CBlock& cblock = data->cblock; Local<Array> jsblocks = NanNew<Array>();
CBlockIndex* cblock_index = data->cblock_index; int i = 0;
cblocks_list *next;
Local<Object> jsblock = NanNew<Object>(); for (cblocks_list *item = data->cblocks; item; item = next) {
cblock_to_jsblock(cblock, cblock_index, jsblock, false); Local<Object> jsblock = NanNew<Object>();
cblock_to_jsblock(item->cblock, item->cblock_index, jsblock, false);
jsblocks->Set(i, jsblock);
i++;
next = item->next;
delete item;
}
const unsigned argc = 2; const unsigned argc = 2;
Local<Value> argv[argc] = { Local<Value> argv[argc] = {
Local<Value>::New(Null()), Local<Value>::New(Null()),
Local<Value>::New(jsblock) Local<Value>::New(jsblocks)
}; };
TryCatch try_catch; TryCatch try_catch;
data->callback->Call(Context::GetCurrent()->Global(), argc, argv); data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
@ -6464,7 +6493,7 @@ init(Handle<Object> target) {
NODE_SET_METHOD(target, "getBestBlock", GetBestBlock); NODE_SET_METHOD(target, "getBestBlock", GetBestBlock);
NODE_SET_METHOD(target, "getChainHeight", GetChainHeight); NODE_SET_METHOD(target, "getChainHeight", GetChainHeight);
NODE_SET_METHOD(target, "getBlockByTx", GetBlockByTx); 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, "getBlockHex", GetBlockHex);
NODE_SET_METHOD(target, "getTxHex", GetTxHex); NODE_SET_METHOD(target, "getTxHex", GetTxHex);
NODE_SET_METHOD(target, "blockFromHex", BlockFromHex); NODE_SET_METHOD(target, "blockFromHex", BlockFromHex);