Merge pull request #28 from braydonf/isspent

Add method to check if an outpoint is spent.
This commit is contained in:
Chris Kleeschulte 2015-07-16 11:02:41 -04:00
commit f999e7e30f
5 changed files with 86 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() {
@ -60,6 +62,22 @@ 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) {

View File

@ -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
}
]
}

View File

@ -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 [];

View File

@ -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<Boolean>(false));
return;
}
}
NanReturnValue(NanNew<Boolean>(true));
};
/**
* GetInfo()
* bitcoindjs.getInfo()
@ -1001,6 +1036,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);