Add isSpent call

This commit is contained in:
Braydon Fuller 2015-07-15 17:45:36 -04:00
parent a37631222c
commit 801679df75
5 changed files with 80 additions and 1 deletions

View File

@ -14,6 +14,8 @@ var sinon = require('sinon');
var txData = require('./livenet-tx-data.json');
var blockData = require('./livenet-block-data.json');
var testTxData = require('./livenet-tx-data.json');
var spentData = require('./livenet-spents.json').spent;
var unspentData = require('./livenet-spents.json').unspent;
var testBlockData = require('./testnet-block-data.json');
describe('Basic Functionality', function() {
@ -59,9 +61,25 @@ describe('Basic Functionality', function() {
});
});
});
describe.only('determine if outpoint is unspent/spent', function() {
spentData.forEach(function(data) {
it('for txid ' + data.txid + ' and output ' + data.outputIndex, function() {
var spent = bitcoind.isSpent(data.txid, data.outputIndex, true);
spent.should.equal(true);
});
});
unspentData.forEach(function(data) {
it('for txid ' + data.txid + ' and output ' + data.outputIndex, function() {
var spent = bitcoind.isSapent(data.txid, data.outputIndex, true);
spent.should.equal(false);
});
});
});
describe('get blocks by hash', function() {
blockData.forEach(function(data) {
var block = bitcore.Block.fromString(data);
it('block ' + block.hash, function(done) {

View File

@ -0,0 +1,18 @@
{
"unspent": [
{
"txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"outputIndex": 0
}
],
"spent": [
{
"txid": "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9",
"outputIndex": 0
},
{
"txid": "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
"outputIndex": 1
}
]
}

View File

@ -323,6 +323,10 @@ Bitcoin.prototype.getBlockHeight = function(height, callback) {
});
};
Bitcoin.prototype.isSpent = function(txid, outputIndex, queryMempool) {
return bitcoindjs.isSpent(txid, outputIndex, queryMempool);
};
Bitcoin.prototype.getTransaction =
Bitcoin.prototype.getTx = function(txid, blockhash, callback) {
if (bitcoin.stopping) return [];

View File

@ -930,6 +930,43 @@ async_get_tx_after(uv_work_t *req) {
delete req;
}
/**
* IsSpent()
* bitcoindjs.isSpent()
* Determine if an outpoint is spent
*/
NAN_METHOD(IsSpent) {
NanScope();
if (args.Length() > 3) {
return NanThrowError(
"Usage: bitcoindjs.isSpent(txid, outputIndex, queryMempool)");
}
// XXX: include the mempool in the coins viewcache
String::Utf8Value arg(args[0]->ToString());
std::string argStr = std::string(*arg);
const uint256 txid = uint256S(argStr);
int outputIndex = args[1]->IntegerValue();
bool queryMempool = args[2]->BooleanValue();
CCoinsViewCache &view = *pcoinsTip;
if (view.HaveCoins(txid)) {
const CCoins* coins = view.AccessCoins(txid);
if (!coins || !coins->IsAvailable(outputIndex)) {
NanReturnValue(NanNew<Boolean>(false));
return;
}
} else {
NanReturnValue(NanNew<Boolean>(false));
return;
}
NanReturnValue(NanNew<Boolean>(true));
};
/**
* GetInfo()
* bitcoindjs.getInfo()
@ -997,6 +1034,7 @@ init(Handle<Object> target) {
NODE_SET_METHOD(target, "getBlock", GetBlock);
NODE_SET_METHOD(target, "getTransaction", GetTransaction);
NODE_SET_METHOD(target, "getInfo", GetInfo);
NODE_SET_METHOD(target, "isSpent", IsSpent);
}

View File

@ -27,3 +27,4 @@ NAN_METHOD(StopBitcoind);
NAN_METHOD(GetBlock);
NAN_METHOD(GetTransaction);
NAN_METHOD(GetInfo);
NAN_METHOD(IsSpent);