Skip to content

Commit 7e82c93

Browse files
author
Braydon Fuller
committed
Merge pull request #249 from kleetus/feature/getNextBlockHash
Next Block Hash Bindings
2 parents 05812b2 + 402d01d commit 7e82c93

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

integration/regtest.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,20 @@ describe('Daemon Binding Functionality', function() {
430430
best.should.equal(bestblock);
431431
});
432432
});
433+
434+
describe('get next block hash', function() {
435+
it('will get next block hash', function() {
436+
var nextBlockHash = bitcoind.getNextBlockHash(blockHashes[0]);
437+
nextBlockHash.should.equal(blockHashes[1]);
438+
var nextnextBlockHash = bitcoind.getNextBlockHash(nextBlockHash);
439+
nextnextBlockHash.should.equal(blockHashes[2]);
440+
});
441+
442+
it('will get a null response if the tip hash is provided', function() {
443+
var bestBlockHash = bitcoind.getBestBlockHash();
444+
var nextBlockHash = bitcoind.getNextBlockHash(bestBlockHash);
445+
should.not.exist(nextBlockHash);
446+
});
447+
});
448+
433449
});

lib/services/bitcoind.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ Bitcoin.prototype.getBestBlockHash = function() {
228228
return bindings.getBestBlockHash();
229229
};
230230

231+
Bitcoin.prototype.getNextBlockHash = function(hash) {
232+
return bindings.getNextBlockHash(hash);
233+
};
234+
231235
Bitcoin.prototype.getTxOutSetInfo = function() {
232236
return bindings.getTxOutSetInfo();
233237
};

src/libbitcoind.cc

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* bitcoind.js - a binding for node.js which links to libbitcoind.so/dylib.
33
* Copyright (c) 2015, BitPay (MIT License)
44
*
5-
* bitcoindjs.cc:
5+
* libbitcoind.cc:
66
* A bitcoind node.js binding.
77
*/
88

@@ -91,7 +91,7 @@ init(Handle<Object>);
9191

9292
/**
9393
* Private Global Variables
94-
* Used only by bitcoindjs functions.
94+
* Used only by bitcoind functions.
9595
*/
9696
static std::vector<CDataStream> txmon_messages;
9797
static uv_async_t txmon_async;
@@ -236,6 +236,28 @@ NAN_METHOD(GetBestBlockHash) {
236236
}
237237
}
238238

239+
NAN_METHOD(GetNextBlockHash) {
240+
241+
if (args.Length() < 1 || !args[0]->IsString()) {
242+
return NanThrowError("Usage: bitcoind.getNextBlockHash(blockhash)");
243+
}
244+
245+
CBlockIndex* pblockindex;
246+
v8::String::Utf8Value param1(args[0]->ToString());
247+
std::string *hash = new std::string(*param1);
248+
uint256 shash = uint256S(*hash);
249+
pblockindex = mapBlockIndex[shash];
250+
CBlockIndex* pnextblockindex = chainActive.Next(pblockindex);
251+
if (pnextblockindex) {
252+
uint256 nexthash = pnextblockindex->GetBlockHash();
253+
std::string rethash = nexthash.ToString();
254+
NanReturnValue(NanNew<String>(rethash));
255+
} else {
256+
NanReturnValue(NanNull());
257+
}
258+
259+
}
260+
239261
/**
240262
* IsSynced()
241263
* bitcoind.isSynced()
@@ -914,7 +936,7 @@ NAN_METHOD(GetBlock) {
914936
|| (!args[0]->IsString() && !args[0]->IsNumber())
915937
|| !args[1]->IsFunction()) {
916938
return NanThrowError(
917-
"Usage: bitcoindjs.getBlock([blockhash,blockheight], callback)");
939+
"Usage: bitcoind.getBlock([blockhash,blockheight], callback)");
918940
}
919941

920942
async_block_data *req = new async_block_data();
@@ -1177,7 +1199,7 @@ NAN_METHOD(GetTransactionWithBlockInfo) {
11771199
|| !args[1]->IsBoolean()
11781200
|| !args[2]->IsFunction()) {
11791201
return NanThrowError(
1180-
"Usage: bitcoindjs.getTransactionWithBlockInfo(txid, queryMempool, callback)");
1202+
"Usage: bitcoind.getTransactionWithBlockInfo(txid, queryMempool, callback)");
11811203
}
11821204

11831205
String::Utf8Value txid_(args[0]->ToString());
@@ -1307,15 +1329,15 @@ async_get_tx_and_info_after(uv_work_t *r) {
13071329

13081330
/**
13091331
* IsSpent()
1310-
* bitcoindjs.isSpent()
1332+
* bitcoind.isSpent()
13111333
* Determine if an outpoint is spent
13121334
*/
13131335
NAN_METHOD(IsSpent) {
13141336
NanScope();
13151337

13161338
if (args.Length() > 2) {
13171339
return NanThrowError(
1318-
"Usage: bitcoindjs.isSpent(txid, outputIndex)");
1340+
"Usage: bitcoind.isSpent(txid, outputIndex)");
13191341
}
13201342

13211343
String::Utf8Value arg(args[0]->ToString());
@@ -1341,7 +1363,7 @@ NAN_METHOD(IsSpent) {
13411363

13421364
/**
13431365
* GetBlockIndex()
1344-
* bitcoindjs.getBlockIndex()
1366+
* bitcoind.getBlockIndex()
13451367
* Get index information about a block by hash including:
13461368
* - the total amount of work (expected number of hashes) in the chain up to
13471369
* and including this block.
@@ -1423,7 +1445,7 @@ NAN_METHOD(IsMainChain) {
14231445

14241446
/**
14251447
* GetInfo()
1426-
* bitcoindjs.getInfo()
1448+
* bitcoind.getInfo()
14271449
* Get miscellaneous information
14281450
*/
14291451

@@ -1432,7 +1454,7 @@ NAN_METHOD(GetInfo) {
14321454

14331455
if (args.Length() > 0) {
14341456
return NanThrowError(
1435-
"Usage: bitcoindjs.getInfo()");
1457+
"Usage: bitcoind.getInfo()");
14361458
}
14371459

14381460
Local<Object> obj = NanNew<Object>();
@@ -1482,7 +1504,7 @@ NAN_METHOD(EstimateFee) {
14821504

14831505
/**
14841506
* Send Transaction
1485-
* bitcoindjs.sendTransaction()
1507+
* bitcoind.sendTransaction()
14861508
* Will add a transaction to the mempool and broadcast to connected peers.
14871509
* @param {string} - The serialized hex string of the transaction.
14881510
* @param {boolean} - Skip absurdly high fee checks
@@ -1637,7 +1659,7 @@ set_cooked(void) {
16371659

16381660
/**
16391661
* Init()
1640-
* Initialize the singleton object known as bitcoindjs.
1662+
* Initialize the singleton object known as bitcoind.
16411663
*/
16421664

16431665
extern "C" void
@@ -1664,6 +1686,7 @@ init(Handle<Object> target) {
16641686
NODE_SET_METHOD(target, "isSynced", IsSynced);
16651687
NODE_SET_METHOD(target, "getTxOutSetInfo", GetTxOutSetInfo);
16661688
NODE_SET_METHOD(target, "getBestBlockHash", GetBestBlockHash);
1689+
NODE_SET_METHOD(target, "getNextBlockHash", GetNextBlockHash);
16671690

16681691
}
16691692

src/libbitcoind.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ NAN_METHOD(SyncPercentage);
3838
NAN_METHOD(IsSynced);
3939
NAN_METHOD(GetTxOutSetInfo);
4040
NAN_METHOD(GetBestBlockHash);
41+
NAN_METHOD(GetNextBlockHash);

test/services/bitcoind.unit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ describe('Bitcoin Service', function() {
410410
['addMempoolUncheckedTransaction', 1],
411411
['getTxOutSetInfo', 0],
412412
['getBestBlockHash', 0],
413+
['getNextBlockHash', 1],
413414
['getInfo', 0]
414415
];
415416

0 commit comments

Comments
 (0)