coins: get rid of return value polymorphism.

This commit is contained in:
Christopher Jeffrey 2017-07-17 21:33:53 -07:00
parent 50c75aeea8
commit 0805babec1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 72 additions and 77 deletions

View File

@ -13,13 +13,12 @@ const CoinEntry = require('./coinentry');
* Represents the outputs for a single transaction. * Represents the outputs for a single transaction.
* @alias module:coins.Coins * @alias module:coins.Coins
* @constructor * @constructor
* @property {Hash} hash - Transaction hash. * @property {Map[]} outputs - Coins.
* @property {CoinEntry[]} outputs - Coins.
*/ */
function Coins(options) { function Coins() {
if (!(this instanceof Coins)) if (!(this instanceof Coins))
return new Coins(options); return new Coins();
this.outputs = new Map(); this.outputs = new Map();
} }
@ -28,35 +27,39 @@ function Coins(options) {
* Add a single entry to the collection. * Add a single entry to the collection.
* @param {Number} index * @param {Number} index
* @param {CoinEntry} coin * @param {CoinEntry} coin
* @returns {CoinEntry}
*/ */
Coins.prototype.add = function add(index, coin) { Coins.prototype.add = function add(index, coin) {
assert(index >= 0); assert(index >= 0);
assert(!this.outputs.has(index)); assert(!this.outputs.has(index));
this.outputs.set(index, coin); this.outputs.set(index, coin);
return coin;
}; };
/** /**
* Add a single output to the collection. * Add a single output to the collection.
* @param {Number} index * @param {Number} index
* @param {Output} output * @param {Output} output
* @returns {CoinEntry}
*/ */
Coins.prototype.addOutput = function addOutput(index, output) { Coins.prototype.addOutput = function addOutput(index, output) {
assert(!output.script.isUnspendable()); assert(!output.script.isUnspendable());
this.add(index, CoinEntry.fromOutput(output)); return this.add(index, CoinEntry.fromOutput(output));
}; };
/** /**
* Add a single coin to the collection. * Add a single coin to the collection.
* @param {Coin} coin * @param {Coin} coin
* @returns {CoinEntry}
*/ */
Coins.prototype.addCoin = function addCoin(coin) { Coins.prototype.addCoin = function addCoin(coin) {
assert(!coin.script.isUnspendable()); assert(!coin.script.isUnspendable());
this.add(coin.index, CoinEntry.fromCoin(coin)); return this.add(coin.index, CoinEntry.fromCoin(coin));
}; };
/** /**
@ -87,24 +90,24 @@ Coins.prototype.isUnspent = function isUnspent(index) {
/** /**
* Get a coin entry. * Get a coin entry.
* @param {Number} index * @param {Number} index
* @returns {CoinEntry} * @returns {CoinEntry|null}
*/ */
Coins.prototype.get = function get(index) { Coins.prototype.get = function get(index) {
return this.outputs.get(index); return this.outputs.get(index) || null;
}; };
/** /**
* Get an output. * Get an output.
* @param {Number} index * @param {Number} index
* @returns {Output} * @returns {Output|null}
*/ */
Coins.prototype.getOutput = function getOutput(index) { Coins.prototype.getOutput = function getOutput(index) {
let coin = this.outputs.get(index); let coin = this.outputs.get(index);
if (!coin) if (!coin)
return; return null;
return coin.output; return coin.output;
}; };
@ -112,14 +115,14 @@ Coins.prototype.getOutput = function getOutput(index) {
/** /**
* Get a coin. * Get a coin.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Coin} * @returns {Coin|null}
*/ */
Coins.prototype.getCoin = function getCoin(prevout) { Coins.prototype.getCoin = function getCoin(prevout) {
let coin = this.outputs.get(prevout.index); let coin = this.outputs.get(prevout.index);
if (!coin) if (!coin)
return; return null;
return coin.toCoin(prevout); return coin.toCoin(prevout);
}; };
@ -127,14 +130,14 @@ Coins.prototype.getCoin = function getCoin(prevout) {
/** /**
* Spend a coin entry and return it. * Spend a coin entry and return it.
* @param {Number} index * @param {Number} index
* @returns {CoinEntry} * @returns {CoinEntry|null}
*/ */
Coins.prototype.spend = function spend(index) { Coins.prototype.spend = function spend(index) {
let coin = this.get(index); let coin = this.get(index);
if (!coin || coin.spent) if (!coin || coin.spent)
return; return null;
coin.spent = true; coin.spent = true;
@ -144,38 +147,20 @@ Coins.prototype.spend = function spend(index) {
/** /**
* Remove a coin entry and return it. * Remove a coin entry and return it.
* @param {Number} index * @param {Number} index
* @returns {CoinEntry} * @returns {CoinEntry|null}
*/ */
Coins.prototype.remove = function remove(index) { Coins.prototype.remove = function remove(index) {
let coin = this.get(index); let coin = this.get(index);
if (!coin) if (!coin)
return false; return null;
this.outputs.delete(index); this.outputs.delete(index);
return coin; return coin;
}; };
/**
* Calculate unspent length of coins.
* @returns {Number}
*/
Coins.prototype.length = function length() {
let len = -1;
for (let [index, coin] of this.outputs) {
if (!coin.spent) {
if (index > len)
len = index;
}
}
return len + 1;
};
/** /**
* Test whether the coins are fully spent. * Test whether the coins are fully spent.
* @returns {Boolean} * @returns {Boolean}
@ -190,6 +175,7 @@ Coins.prototype.isEmpty = function isEmpty() {
* @private * @private
* @param {TX} tx * @param {TX} tx
* @param {Number} height * @param {Number} height
* @returns {Coins}
*/ */
Coins.prototype.fromTX = function fromTX(tx, height) { Coins.prototype.fromTX = function fromTX(tx, height) {

View File

@ -51,6 +51,7 @@ CoinView.prototype.has = function has(hash) {
* Add coins to the collection. * Add coins to the collection.
* @param {Hash} hash * @param {Hash} hash
* @param {Coins} coins * @param {Coins} coins
* @returns {Coins}
*/ */
CoinView.prototype.add = function add(hash, coins) { CoinView.prototype.add = function add(hash, coins) {
@ -61,49 +62,55 @@ CoinView.prototype.add = function add(hash, coins) {
/** /**
* Remove coins from the collection. * Remove coins from the collection.
* @param {Coins} coins * @param {Coins} coins
* @returns {Boolean} * @returns {Coins|null}
*/ */
CoinView.prototype.remove = function remove(hash) { CoinView.prototype.remove = function remove(hash) {
if (!this.map.has(hash)) let coins = this.map.get(hash);
return false;
if (!coins)
return null;
this.map.delete(hash); this.map.delete(hash);
return true; return coins;
}; };
/** /**
* Add a tx to the collection. * Add a tx to the collection.
* @param {TX} tx * @param {TX} tx
* @param {Number} height * @param {Number} height
* @returns {Coins}
*/ */
CoinView.prototype.addTX = function addTX(tx, height) { CoinView.prototype.addTX = function addTX(tx, height) {
let hash = tx.hash('hex');
let coins = Coins.fromTX(tx, height); let coins = Coins.fromTX(tx, height);
return this.add(tx.hash('hex'), coins); return this.add(hash, coins);
}; };
/** /**
* Remove a tx from the collection. * Remove a tx from the collection.
* @param {TX} tx * @param {TX} tx
* @param {Number} height * @param {Number} height
* @returns {Coins}
*/ */
CoinView.prototype.removeTX = function removeTX(tx, height) { CoinView.prototype.removeTX = function removeTX(tx, height) {
let hash = tx.hash('hex');
let coins = Coins.fromTX(tx, height); let coins = Coins.fromTX(tx, height);
for (let coin of coins.outputs.values()) for (let coin of coins.outputs.values())
coin.spent = true; coin.spent = true;
return this.add(tx.hash('hex'), coins); return this.add(hash, coins);
}; };
/** /**
* Add an entry to the collection. * Add an entry to the collection.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @param {CoinEntry} coin * @param {CoinEntry} coin
* @returns {Coins|null} * @returns {CoinEntry|null}
*/ */
CoinView.prototype.addEntry = function addEntry(prevout, coin) { CoinView.prototype.addEntry = function addEntry(prevout, coin) {
@ -116,17 +123,18 @@ CoinView.prototype.addEntry = function addEntry(prevout, coin) {
} }
if (coin.output.script.isUnspendable()) if (coin.output.script.isUnspendable())
return; return null;
if (!coins.has(index)) if (coins.has(index))
coins.add(index, coin); return null;
return coins; return coins.add(index, coin);
}; };
/** /**
* Add a coin to the collection. * Add a coin to the collection.
* @param {Coin} coin * @param {Coin} coin
* @returns {CoinEntry|null}
*/ */
CoinView.prototype.addCoin = function addCoin(coin) { CoinView.prototype.addCoin = function addCoin(coin) {
@ -139,18 +147,19 @@ CoinView.prototype.addCoin = function addCoin(coin) {
} }
if (coin.script.isUnspendable()) if (coin.script.isUnspendable())
return; return null;
if (!coins.has(index)) if (coins.has(index))
coins.addCoin(coin); return null;
return coins; return coins.addCoin(coin);
}; };
/** /**
* Add an output to the collection. * Add an output to the collection.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @param {Output} output * @param {Output} output
* @returns {CoinEntry|null}
*/ */
CoinView.prototype.addOutput = function addOutput(prevout, output) { CoinView.prototype.addOutput = function addOutput(prevout, output) {
@ -163,16 +172,18 @@ CoinView.prototype.addOutput = function addOutput(prevout, output) {
} }
if (output.script.isUnspendable()) if (output.script.isUnspendable())
return; return null;
if (!coins.has(index)) if (coins.has(index))
coins.addOutput(index, output); return null;
return coins.addOutput(index, output);
}; };
/** /**
* Spend an output. * Spend an output.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Boolean} * @returns {CoinEntry|null}
*/ */
CoinView.prototype.spendOutput = function spendOutput(prevout) { CoinView.prototype.spendOutput = function spendOutput(prevout) {
@ -181,22 +192,22 @@ CoinView.prototype.spendOutput = function spendOutput(prevout) {
let coin; let coin;
if (!coins) if (!coins)
return false; return null;
coin = coins.spend(index); coin = coins.spend(index);
if (!coin) if (!coin)
return false; return null;
this.undo.push(coin); this.undo.push(coin);
return true; return coin;
}; };
/** /**
* Remove an output. * Remove an output.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Boolean} * @returns {CoinEntry|null}
*/ */
CoinView.prototype.removeOutput = function removeOutput(prevout) { CoinView.prototype.removeOutput = function removeOutput(prevout) {
@ -204,7 +215,7 @@ CoinView.prototype.removeOutput = function removeOutput(prevout) {
let coins = this.get(hash); let coins = this.get(hash);
if (!coins) if (!coins)
return false; return null;
return coins.remove(index); return coins.remove(index);
}; };
@ -226,9 +237,9 @@ CoinView.prototype.hasEntry = function hasEntry(prevout) {
}; };
/** /**
* Get a single entry by input. * Get a single entry by prevout.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {CoinEntry} * @returns {CoinEntry|null}
*/ */
CoinView.prototype.getEntry = function getEntry(prevout) { CoinView.prototype.getEntry = function getEntry(prevout) {
@ -236,7 +247,7 @@ CoinView.prototype.getEntry = function getEntry(prevout) {
let coins = this.get(hash); let coins = this.get(hash);
if (!coins) if (!coins)
return; return null;
return coins.get(index); return coins.get(index);
}; };
@ -244,14 +255,14 @@ CoinView.prototype.getEntry = function getEntry(prevout) {
/** /**
* Get a single coin by prevout. * Get a single coin by prevout.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Coin} * @returns {Coin|null}
*/ */
CoinView.prototype.getCoin = function getCoin(prevout) { CoinView.prototype.getCoin = function getCoin(prevout) {
let coins = this.get(prevout.hash); let coins = this.get(prevout.hash);
if (!coins) if (!coins)
return; return null;
return coins.getCoin(prevout); return coins.getCoin(prevout);
}; };
@ -259,7 +270,7 @@ CoinView.prototype.getCoin = function getCoin(prevout) {
/** /**
* Get a single output by prevout. * Get a single output by prevout.
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Output} * @returns {Output|null}
*/ */
CoinView.prototype.getOutput = function getOutput(prevout) { CoinView.prototype.getOutput = function getOutput(prevout) {
@ -267,7 +278,7 @@ CoinView.prototype.getOutput = function getOutput(prevout) {
let coins = this.get(hash); let coins = this.get(hash);
if (!coins) if (!coins)
return; return null;
return coins.getOutput(index); return coins.getOutput(index);
}; };
@ -315,7 +326,7 @@ CoinView.prototype.hasEntryFor = function hasEntryFor(input) {
/** /**
* Get a single entry by input. * Get a single entry by input.
* @param {Input} input * @param {Input} input
* @returns {CoinEntry} * @returns {CoinEntry|null}
*/ */
CoinView.prototype.getEntryFor = function getEntryFor(input) { CoinView.prototype.getEntryFor = function getEntryFor(input) {
@ -325,7 +336,7 @@ CoinView.prototype.getEntryFor = function getEntryFor(input) {
/** /**
* Get a single coin by input. * Get a single coin by input.
* @param {Input} input * @param {Input} input
* @returns {Coin} * @returns {Coin|null}
*/ */
CoinView.prototype.getCoinFor = function getCoinFor(input) { CoinView.prototype.getCoinFor = function getCoinFor(input) {
@ -335,7 +346,7 @@ CoinView.prototype.getCoinFor = function getCoinFor(input) {
/** /**
* Get a single output by input. * Get a single output by input.
* @param {Input} input * @param {Input} input
* @returns {Output} * @returns {Output|null}
*/ */
CoinView.prototype.getOutputFor = function getOutputFor(input) { CoinView.prototype.getOutputFor = function getOutputFor(input) {
@ -367,7 +378,7 @@ CoinView.prototype.isCoinbaseFor = function isCoinbaseFor(input) {
* @method * @method
* @param {ChainDB} db * @param {ChainDB} db
* @param {Outpoint} prevout * @param {Outpoint} prevout
* @returns {Promise} - Returns {@link Coins}. * @returns {Promise} - Returns {@link CoinEntry}.
*/ */
CoinView.prototype.readCoin = async function readCoin(db, prevout) { CoinView.prototype.readCoin = async function readCoin(db, prevout) {
@ -381,9 +392,7 @@ CoinView.prototype.readCoin = async function readCoin(db, prevout) {
if (!coin) if (!coin)
return null; return null;
this.addEntry(prevout, coin); return this.addEntry(prevout, coin);
return coin;
}; };
/** /**

View File

@ -66,7 +66,7 @@ describe('Coins', function() {
coins = view.get(hash); coins = view.get(hash);
assert(coins); assert(coins);
length = coins.length(); length = coins.outputs.size;
view.spendOutput(new Outpoint(hash, 0)); view.spendOutput(new Outpoint(hash, 0));
@ -78,7 +78,7 @@ describe('Coins', function() {
assert(entry.spent); assert(entry.spent);
deepCoinsEqual(entry, reserialize(entry)); deepCoinsEqual(entry, reserialize(entry));
assert.strictEqual(coins.length(), length); assert.strictEqual(coins.outputs.size, length);
assert.equal(view.undo.items.length, 1); assert.equal(view.undo.items.length, 1);
}); });
@ -102,8 +102,8 @@ describe('Coins', function() {
prev = tx1.inputs[0].prevout; prev = tx1.inputs[0].prevout;
coins = res.get(prev.hash); coins = res.get(prev.hash);
assert.strictEqual(coins.length(), 2); assert.strictEqual(coins.outputs.size, 1);
assert.strictEqual(coins.get(0), undefined); assert.strictEqual(coins.get(0), null);
deepCoinsEqual(coins.get(1), reserialize(coins.get(1))); deepCoinsEqual(coins.get(1), reserialize(coins.get(1)));
}); });
}); });