diff --git a/integration/regtest.js b/integration/regtest.js index 55ba9e27..701c4038 100644 --- a/integration/regtest.js +++ b/integration/regtest.js @@ -430,4 +430,20 @@ describe('Daemon Binding Functionality', function() { 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); + }); + }); + }); diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 816b6d5e..d3d0e248 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -228,6 +228,10 @@ Bitcoin.prototype.getBestBlockHash = function() { return bindings.getBestBlockHash(); }; +Bitcoin.prototype.getNextBlockHash = function(hash) { + return bindings.getNextBlockHash(hash); +}; + Bitcoin.prototype.getTxOutSetInfo = function() { return bindings.getTxOutSetInfo(); }; diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index 78ddd5fc..cc7a9e7a 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -2,7 +2,7 @@ * bitcoind.js - a binding for node.js which links to libbitcoind.so/dylib. * Copyright (c) 2015, BitPay (MIT License) * - * bitcoindjs.cc: + * libbitcoind.cc: * A bitcoind node.js binding. */ @@ -91,7 +91,7 @@ init(Handle); /** * Private Global Variables - * Used only by bitcoindjs functions. + * Used only by bitcoind functions. */ static std::vector txmon_messages; 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(rethash)); + } else { + NanReturnValue(NanNull()); + } + +} + /** * IsSynced() * bitcoind.isSynced() @@ -914,7 +936,7 @@ NAN_METHOD(GetBlock) { || (!args[0]->IsString() && !args[0]->IsNumber()) || !args[1]->IsFunction()) { return NanThrowError( - "Usage: bitcoindjs.getBlock([blockhash,blockheight], callback)"); + "Usage: bitcoind.getBlock([blockhash,blockheight], callback)"); } async_block_data *req = new async_block_data(); @@ -1177,7 +1199,7 @@ NAN_METHOD(GetTransactionWithBlockInfo) { || !args[1]->IsBoolean() || !args[2]->IsFunction()) { return NanThrowError( - "Usage: bitcoindjs.getTransactionWithBlockInfo(txid, queryMempool, callback)"); + "Usage: bitcoind.getTransactionWithBlockInfo(txid, queryMempool, callback)"); } String::Utf8Value txid_(args[0]->ToString()); @@ -1307,7 +1329,7 @@ async_get_tx_and_info_after(uv_work_t *r) { /** * IsSpent() - * bitcoindjs.isSpent() + * bitcoind.isSpent() * Determine if an outpoint is spent */ NAN_METHOD(IsSpent) { @@ -1315,7 +1337,7 @@ NAN_METHOD(IsSpent) { if (args.Length() > 2) { return NanThrowError( - "Usage: bitcoindjs.isSpent(txid, outputIndex)"); + "Usage: bitcoind.isSpent(txid, outputIndex)"); } String::Utf8Value arg(args[0]->ToString()); @@ -1341,7 +1363,7 @@ NAN_METHOD(IsSpent) { /** * GetBlockIndex() - * bitcoindjs.getBlockIndex() + * bitcoind.getBlockIndex() * Get index information about a block by hash including: * - the total amount of work (expected number of hashes) in the chain up to * and including this block. @@ -1423,7 +1445,7 @@ NAN_METHOD(IsMainChain) { /** * GetInfo() - * bitcoindjs.getInfo() + * bitcoind.getInfo() * Get miscellaneous information */ @@ -1432,7 +1454,7 @@ NAN_METHOD(GetInfo) { if (args.Length() > 0) { return NanThrowError( - "Usage: bitcoindjs.getInfo()"); + "Usage: bitcoind.getInfo()"); } Local obj = NanNew(); @@ -1482,7 +1504,7 @@ NAN_METHOD(EstimateFee) { /** * Send Transaction - * bitcoindjs.sendTransaction() + * bitcoind.sendTransaction() * Will add a transaction to the mempool and broadcast to connected peers. * @param {string} - The serialized hex string of the transaction. * @param {boolean} - Skip absurdly high fee checks @@ -1637,7 +1659,7 @@ set_cooked(void) { /** * Init() - * Initialize the singleton object known as bitcoindjs. + * Initialize the singleton object known as bitcoind. */ extern "C" void @@ -1664,6 +1686,7 @@ init(Handle target) { NODE_SET_METHOD(target, "isSynced", IsSynced); NODE_SET_METHOD(target, "getTxOutSetInfo", GetTxOutSetInfo); NODE_SET_METHOD(target, "getBestBlockHash", GetBestBlockHash); + NODE_SET_METHOD(target, "getNextBlockHash", GetNextBlockHash); } diff --git a/src/libbitcoind.h b/src/libbitcoind.h index 8a214265..d7a971d3 100644 --- a/src/libbitcoind.h +++ b/src/libbitcoind.h @@ -38,3 +38,4 @@ NAN_METHOD(SyncPercentage); NAN_METHOD(IsSynced); NAN_METHOD(GetTxOutSetInfo); NAN_METHOD(GetBestBlockHash); +NAN_METHOD(GetNextBlockHash); diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 6b52b919..ec911372 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -410,6 +410,7 @@ describe('Bitcoin Service', function() { ['addMempoolUncheckedTransaction', 1], ['getTxOutSetInfo', 0], ['getBestBlockHash', 0], + ['getNextBlockHash', 1], ['getInfo', 0] ];