Next Block Hash Bindings

- Added bindings for next block hash
- Added the reg/unit tests
This commit is contained in:
Chris Kleeschulte 2015-09-21 13:50:09 -04:00
parent 4a5031a917
commit 402d01d7ac
5 changed files with 56 additions and 11 deletions

View File

@ -430,4 +430,20 @@ describe('Daemon Binding Functionality', function() {
best.should.equal(bestblock); best.should.equal(bestblock);
}); });
}); });
describe('get next block hash', function() {
it('will get next block hash', function() {
var nextBlockHash = bitcoind.getNextBlockHash(blockHashes[0]);
nextBlockHash.should.equal(blockHashes[1]);
var nextnextBlockHash = bitcoind.getNextBlockHash(nextBlockHash);
nextnextBlockHash.should.equal(blockHashes[2]);
});
it('will get a null response if the tip hash is provided', function() {
var bestBlockHash = bitcoind.getBestBlockHash();
var nextBlockHash = bitcoind.getNextBlockHash(bestBlockHash);
should.not.exist(nextBlockHash);
});
});
}); });

View File

@ -228,6 +228,10 @@ Bitcoin.prototype.getBestBlockHash = function() {
return bindings.getBestBlockHash(); return bindings.getBestBlockHash();
}; };
Bitcoin.prototype.getNextBlockHash = function(hash) {
return bindings.getNextBlockHash(hash);
};
Bitcoin.prototype.getTxOutSetInfo = function() { Bitcoin.prototype.getTxOutSetInfo = function() {
return bindings.getTxOutSetInfo(); return bindings.getTxOutSetInfo();
}; };

View File

@ -2,7 +2,7 @@
* bitcoind.js - a binding for node.js which links to libbitcoind.so/dylib. * bitcoind.js - a binding for node.js which links to libbitcoind.so/dylib.
* Copyright (c) 2015, BitPay (MIT License) * Copyright (c) 2015, BitPay (MIT License)
* *
* bitcoindjs.cc: * libbitcoind.cc:
* A bitcoind node.js binding. * A bitcoind node.js binding.
*/ */
@ -91,7 +91,7 @@ init(Handle<Object>);
/** /**
* Private Global Variables * Private Global Variables
* Used only by bitcoindjs functions. * Used only by bitcoind functions.
*/ */
static std::vector<CDataStream> txmon_messages; static std::vector<CDataStream> txmon_messages;
static uv_async_t txmon_async; static uv_async_t txmon_async;
@ -236,6 +236,28 @@ NAN_METHOD(GetBestBlockHash) {
} }
} }
NAN_METHOD(GetNextBlockHash) {
if (args.Length() < 1 || !args[0]->IsString()) {
return NanThrowError("Usage: bitcoind.getNextBlockHash(blockhash)");
}
CBlockIndex* pblockindex;
v8::String::Utf8Value param1(args[0]->ToString());
std::string *hash = new std::string(*param1);
uint256 shash = uint256S(*hash);
pblockindex = mapBlockIndex[shash];
CBlockIndex* pnextblockindex = chainActive.Next(pblockindex);
if (pnextblockindex) {
uint256 nexthash = pnextblockindex->GetBlockHash();
std::string rethash = nexthash.ToString();
NanReturnValue(NanNew<String>(rethash));
} else {
NanReturnValue(NanNull());
}
}
/** /**
* IsSynced() * IsSynced()
* bitcoind.isSynced() * bitcoind.isSynced()
@ -914,7 +936,7 @@ NAN_METHOD(GetBlock) {
|| (!args[0]->IsString() && !args[0]->IsNumber()) || (!args[0]->IsString() && !args[0]->IsNumber())
|| !args[1]->IsFunction()) { || !args[1]->IsFunction()) {
return NanThrowError( return NanThrowError(
"Usage: bitcoindjs.getBlock([blockhash,blockheight], callback)"); "Usage: bitcoind.getBlock([blockhash,blockheight], callback)");
} }
async_block_data *req = new async_block_data(); async_block_data *req = new async_block_data();
@ -1177,7 +1199,7 @@ NAN_METHOD(GetTransactionWithBlockInfo) {
|| !args[1]->IsBoolean() || !args[1]->IsBoolean()
|| !args[2]->IsFunction()) { || !args[2]->IsFunction()) {
return NanThrowError( return NanThrowError(
"Usage: bitcoindjs.getTransactionWithBlockInfo(txid, queryMempool, callback)"); "Usage: bitcoind.getTransactionWithBlockInfo(txid, queryMempool, callback)");
} }
String::Utf8Value txid_(args[0]->ToString()); String::Utf8Value txid_(args[0]->ToString());
@ -1307,7 +1329,7 @@ async_get_tx_and_info_after(uv_work_t *r) {
/** /**
* IsSpent() * IsSpent()
* bitcoindjs.isSpent() * bitcoind.isSpent()
* Determine if an outpoint is spent * Determine if an outpoint is spent
*/ */
NAN_METHOD(IsSpent) { NAN_METHOD(IsSpent) {
@ -1315,7 +1337,7 @@ NAN_METHOD(IsSpent) {
if (args.Length() > 2) { if (args.Length() > 2) {
return NanThrowError( return NanThrowError(
"Usage: bitcoindjs.isSpent(txid, outputIndex)"); "Usage: bitcoind.isSpent(txid, outputIndex)");
} }
String::Utf8Value arg(args[0]->ToString()); String::Utf8Value arg(args[0]->ToString());
@ -1341,7 +1363,7 @@ NAN_METHOD(IsSpent) {
/** /**
* GetBlockIndex() * GetBlockIndex()
* bitcoindjs.getBlockIndex() * bitcoind.getBlockIndex()
* Get index information about a block by hash including: * Get index information about a block by hash including:
* - the total amount of work (expected number of hashes) in the chain up to * - the total amount of work (expected number of hashes) in the chain up to
* and including this block. * and including this block.
@ -1423,7 +1445,7 @@ NAN_METHOD(IsMainChain) {
/** /**
* GetInfo() * GetInfo()
* bitcoindjs.getInfo() * bitcoind.getInfo()
* Get miscellaneous information * Get miscellaneous information
*/ */
@ -1432,7 +1454,7 @@ NAN_METHOD(GetInfo) {
if (args.Length() > 0) { if (args.Length() > 0) {
return NanThrowError( return NanThrowError(
"Usage: bitcoindjs.getInfo()"); "Usage: bitcoind.getInfo()");
} }
Local<Object> obj = NanNew<Object>(); Local<Object> obj = NanNew<Object>();
@ -1482,7 +1504,7 @@ NAN_METHOD(EstimateFee) {
/** /**
* Send Transaction * Send Transaction
* bitcoindjs.sendTransaction() * bitcoind.sendTransaction()
* Will add a transaction to the mempool and broadcast to connected peers. * Will add a transaction to the mempool and broadcast to connected peers.
* @param {string} - The serialized hex string of the transaction. * @param {string} - The serialized hex string of the transaction.
* @param {boolean} - Skip absurdly high fee checks * @param {boolean} - Skip absurdly high fee checks
@ -1637,7 +1659,7 @@ set_cooked(void) {
/** /**
* Init() * Init()
* Initialize the singleton object known as bitcoindjs. * Initialize the singleton object known as bitcoind.
*/ */
extern "C" void extern "C" void
@ -1664,6 +1686,7 @@ init(Handle<Object> target) {
NODE_SET_METHOD(target, "isSynced", IsSynced); NODE_SET_METHOD(target, "isSynced", IsSynced);
NODE_SET_METHOD(target, "getTxOutSetInfo", GetTxOutSetInfo); NODE_SET_METHOD(target, "getTxOutSetInfo", GetTxOutSetInfo);
NODE_SET_METHOD(target, "getBestBlockHash", GetBestBlockHash); NODE_SET_METHOD(target, "getBestBlockHash", GetBestBlockHash);
NODE_SET_METHOD(target, "getNextBlockHash", GetNextBlockHash);
} }

View File

@ -38,3 +38,4 @@ NAN_METHOD(SyncPercentage);
NAN_METHOD(IsSynced); NAN_METHOD(IsSynced);
NAN_METHOD(GetTxOutSetInfo); NAN_METHOD(GetTxOutSetInfo);
NAN_METHOD(GetBestBlockHash); NAN_METHOD(GetBestBlockHash);
NAN_METHOD(GetNextBlockHash);

View File

@ -410,6 +410,7 @@ describe('Bitcoin Service', function() {
['addMempoolUncheckedTransaction', 1], ['addMempoolUncheckedTransaction', 1],
['getTxOutSetInfo', 0], ['getTxOutSetInfo', 0],
['getBestBlockHash', 0], ['getBestBlockHash', 0],
['getNextBlockHash', 1],
['getInfo', 0] ['getInfo', 0]
]; ];