diff --git a/integration/index.js b/integration/index.js index 5ffedeb3..3e2b12f2 100644 --- a/integration/index.js +++ b/integration/index.js @@ -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) { diff --git a/integration/livenet-spents.json b/integration/livenet-spents.json new file mode 100644 index 00000000..d8b84e52 --- /dev/null +++ b/integration/livenet-spents.json @@ -0,0 +1,18 @@ +{ + "unspent": [ + { + "txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098", + "outputIndex": 0 + } + ], + "spent": [ + { + "txid": "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9", + "outputIndex": 0 + }, + { + "txid": "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", + "outputIndex": 1 + } + ] +} diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 045f4039..e9b6095a 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -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 []; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 38e81bd9..cbf6870f 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -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(false)); + return; + } + } else { + NanReturnValue(NanNew(false)); + return; + } + + NanReturnValue(NanNew(true)); +}; + /** * GetInfo() * bitcoindjs.getInfo() @@ -997,6 +1034,7 @@ init(Handle 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); } diff --git a/src/bitcoindjs.h b/src/bitcoindjs.h index 571e0c39..3a965149 100644 --- a/src/bitcoindjs.h +++ b/src/bitcoindjs.h @@ -27,3 +27,4 @@ NAN_METHOD(StopBitcoind); NAN_METHOD(GetBlock); NAN_METHOD(GetTransaction); NAN_METHOD(GetInfo); +NAN_METHOD(IsSpent);