From d57dac13e03d505d17da4fb6e3a45455a36de254 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 17 Jul 2017 21:47:32 -0700 Subject: [PATCH] coins: refactor method names. add isUnspent. --- lib/coins/coinview.js | 30 ++++++++++++++++++++++++++++-- test/coins-test.js | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/coins/coinview.js b/lib/coins/coinview.js index 2b30ac5f..0bce7a62 100644 --- a/lib/coins/coinview.js +++ b/lib/coins/coinview.js @@ -186,7 +186,7 @@ CoinView.prototype.addOutput = function addOutput(prevout, output) { * @returns {CoinEntry|null} */ -CoinView.prototype.spendOutput = function spendOutput(prevout) { +CoinView.prototype.spendEntry = function spendEntry(prevout) { let {hash, index} = prevout; let coins = this.get(hash); let coin; @@ -210,7 +210,7 @@ CoinView.prototype.spendOutput = function spendOutput(prevout) { * @returns {CoinEntry|null} */ -CoinView.prototype.removeOutput = function removeOutput(prevout) { +CoinView.prototype.removeEntry = function removeEntry(prevout) { let {hash, index} = prevout; let coins = this.get(hash); @@ -252,6 +252,22 @@ CoinView.prototype.getEntry = function getEntry(prevout) { return coins.get(index); }; +/** + * Test whether an entry has been spent by prevout. + * @param {Outpoint} prevout + * @returns {Boolean} + */ + +CoinView.prototype.isUnspent = function isUnspent(prevout) { + let {hash, index} = prevout; + let coins = this.get(hash); + + if (!coins) + return false; + + return coins.isUnspent(index); +}; + /** * Get a single coin by prevout. * @param {Outpoint} prevout @@ -333,6 +349,16 @@ CoinView.prototype.getEntryFor = function getEntryFor(input) { return this.getEntry(input.prevout); }; +/** + * Test whether an entry has been spent by input. + * @param {Input} input + * @returns {Boolean} + */ + +CoinView.prototype.isUnspentFor = function isUnspentFor(input) { + return this.isUnspent(input.prevout); +}; + /** * Get a single coin by input. * @param {Input} input diff --git a/test/coins-test.js b/test/coins-test.js index 9c7236bb..8b4302d1 100644 --- a/test/coins-test.js +++ b/test/coins-test.js @@ -68,7 +68,7 @@ describe('Coins', function() { assert(coins); length = coins.outputs.size; - view.spendOutput(new Outpoint(hash, 0)); + view.spendEntry(new Outpoint(hash, 0)); coins = view.get(hash); assert(coins);