diff --git a/integration/index.js b/integration/index.js index bd2c2f22..fd89630b 100644 --- a/integration/index.js +++ b/integration/index.js @@ -117,4 +117,23 @@ describe('Basic Functionality', function() { }); }); + describe('get chain work', function() { + it('will get the total work for the genesis block via hash', function() { + var hash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; + var work = bitcoind.getChainWork(hash); + work.should.equal('0000000000000000000000000000000000000000000000000000000100010001'); + }); + it('will get the total work for block #300000 via hash', function() { + var hash = '000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254'; + var work = bitcoind.getChainWork(hash); + work.should.equal('000000000000000000000000000000000000000000005a7b3c42ea8b844374e9'); + }); + it('will return undefined for unknown block', function() { + var hash = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'; + var work = bitcoind.getChainWork(hash); + should.equal(work, undefined); + }); + + }); + }); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 9d1c0b5f..46218747 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -327,6 +327,10 @@ Bitcoin.prototype.isSpent = function(txid, outputIndex) { return bitcoindjs.isSpent(txid, outputIndex); }; +Bitcoin.prototype.getChainWork = function(blockHash) { + return bitcoindjs.getChainWork(blockHash); +}; + Bitcoin.prototype.getTransaction = function(txid, queryMempool, callback) { return bitcoindjs.getTransaction(txid, queryMempool, callback); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index cf0bd614..fcff27a0 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -925,6 +925,32 @@ NAN_METHOD(IsSpent) { NanReturnValue(NanNew(true)); }; +/** + * GetChainWork() + * bitcoindjs.getChainWork() + * Get the total amount of work (expected number of hashes) in the chain up to + * and including this block. + */ +NAN_METHOD(GetChainWork) { + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + + String::Utf8Value hash_(args[0]->ToString()); + std::string hashStr = std::string(*hash_); + uint256 hash = uint256S(hashStr); + + CBlockIndex* blockIndex; + + if (mapBlockIndex.count(hash) == 0) { + NanReturnValue(Undefined(isolate)); + } else { + blockIndex = mapBlockIndex[hash]; + arith_uint256 cw = blockIndex->nChainWork; + NanReturnValue(Local::New(isolate, NanNew(cw.GetHex()))); + } + +}; + /** * GetInfo() * bitcoindjs.getInfo() @@ -993,6 +1019,7 @@ init(Handle target) { NODE_SET_METHOD(target, "getTransaction", GetTransaction); NODE_SET_METHOD(target, "getInfo", GetInfo); NODE_SET_METHOD(target, "isSpent", IsSpent); + NODE_SET_METHOD(target, "getChainWork", GetChainWork); } diff --git a/src/bitcoindjs.h b/src/bitcoindjs.h index 3a965149..ea825a87 100644 --- a/src/bitcoindjs.h +++ b/src/bitcoindjs.h @@ -28,3 +28,4 @@ NAN_METHOD(GetBlock); NAN_METHOD(GetTransaction); NAN_METHOD(GetInfo); NAN_METHOD(IsSpent); +NAN_METHOD(GetChainWork);