diff --git a/integration/index.js b/integration/index.js index c36d50b0..e42fad3d 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('determine if outpoint is unspent/spent', function() { + spentData.forEach(function(data) { + it('for spent 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 unspent txid ' + data.txid + ' and output ' + data.outputIndex, function() { + var spent = bitcoind.isSpent(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..8c1eed28 --- /dev/null +++ b/integration/livenet-spents.json @@ -0,0 +1,26 @@ +{ + "unspent": [ + { + "txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098", + "outputIndex": 0 + }, + { + "txid": "64b2da257d93ab44d19ef9b374788d82b6885bc3fb3c17704f42f09b1096e982", + "outputIndex": 0 + }, + { + "txid": "226bbc4b1f851857f37aa96e9eb702946fc128b055e4decc684740005f5044cf", + "outputIndex": 0 + } + ], + "spent": [ + { + "txid": "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9", + "outputIndex": 0 + }, + { + "txid": "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", + "outputIndex": 1 + } + ] +} diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 045f4039..bb022a84 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) { + return bitcoindjs.isSpent(txid, outputIndex); +}; + 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 3cc49187..d4b34ae2 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -22,6 +22,7 @@ using namespace v8; extern void WaitForShutdown(boost::thread_group* threadGroup); static termios orig_termios; +extern CTxMemPool mempool; /** * Node.js Internal Function Templates @@ -934,6 +935,40 @@ 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() > 2) { + return NanThrowError( + "Usage: bitcoindjs.isSpent(txid, outputIndex)"); + } + + String::Utf8Value arg(args[0]->ToString()); + std::string argStr = std::string(*arg); + const uint256 txid = uint256S(argStr); + int outputIndex = args[1]->IntegerValue(); + + CCoinsView dummy; + CCoinsViewCache view(&dummy); + + CCoinsViewMemPool viewMemPool(pcoinsTip, mempool); + view.SetBackend(viewMemPool); + + if (view.HaveCoins(txid)) { + const CCoins* coins = view.AccessCoins(txid); + if (coins && coins->IsAvailable(outputIndex)) { + NanReturnValue(NanNew(false)); + return; + } + } + NanReturnValue(NanNew(true)); +}; + /** * GetInfo() * bitcoindjs.getInfo() @@ -1001,6 +1036,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);