Merge pull request #32 from braydonf/getchainwork

Added getChainWork method for determining the best chain.
This commit is contained in:
Chris Kleeschulte 2015-07-20 09:35:38 -04:00
commit 78ede4ae9c
4 changed files with 51 additions and 0 deletions

View File

@ -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);
});
});
});

View File

@ -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);
};

View File

@ -925,6 +925,32 @@ NAN_METHOD(IsSpent) {
NanReturnValue(NanNew<Boolean>(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<Value>::New(isolate, NanNew<String>(cw.GetHex())));
}
};
/**
* GetInfo()
* bitcoindjs.getInfo()
@ -993,6 +1019,7 @@ init(Handle<Object> 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);
}

View File

@ -28,3 +28,4 @@ NAN_METHOD(GetBlock);
NAN_METHOD(GetTransaction);
NAN_METHOD(GetInfo);
NAN_METHOD(IsSpent);
NAN_METHOD(GetChainWork);