From bfd570e204b3ea7dda294cb55523587fbf07d07d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 12 Feb 2016 04:18:01 -0800 Subject: [PATCH] coin selection. misc. --- lib/bcoin/tx.js | 39 +++++++++++++++++++++++++++------------ test/wallet-test.js | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ae09b575..b2a1beee 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1024,10 +1024,17 @@ TX.prototype.getInputs = function getInputs(unspent, options) { }; } - // Oldest unspents first - unspent = unspent.slice().sort(function(a, b) { - return a.height - b.height; - }); + if (!options.selection || options.selection === 'age') { + // Oldest unspents first + unspent = unspent.slice().sort(function(a, b) { + return a.height - b.height; + }); + } else if (options.selection === 'random' || options.selection === 'all') { + // Random unspents + unspent = unspent.slice().sort(function(a, b) { + return Math.random() > 0.5 ? 1 : -1; + }); + } function total() { if (options.subtractFee) @@ -1053,6 +1060,9 @@ TX.prototype.getInputs = function getInputs(unspent, options) { if (options.wallet) options.wallet.scriptInputs(tx, index); + if (options.selection === 'all') + continue; + // Stop once we're full. if (isFull()) break; @@ -1083,6 +1093,11 @@ TX.prototype.getInputs = function getInputs(unspent, options) { // Calculate max possible size after signing. size = tx.maxSize(options.m, options.n); + // if (newkb == null && tx.isFree(size)) { + // fee = new bn(0); + // break; + // } + newkb = Math.ceil(size / 1024) - totalkb; fee.iaddn(newkb * minFee); totalkb += newkb; @@ -1140,7 +1155,7 @@ TX.prototype.fill = function fill(unspent, options) { result = this.getInputs(unspent, options); - this.total = result.total; + this.requiredFunds = result.total; if (!result.inputs) return result; @@ -1599,8 +1614,8 @@ TX.prototype.isStandardInputs = function isStandardInputs(flags) { return true; }; -TX.prototype.getPriority = function getPriority() { - var size, sum, i, input, age, height; +TX.prototype.getPriority = function getPriority(size) { + var sum, i, input, age, height; height = this.height; @@ -1610,7 +1625,7 @@ TX.prototype.getPriority = function getPriority() { if (!this.hasPrevout()) return new bn(0); - size = this.maxSize(); + size = size || this.maxSize(); sum = new bn(0); for (i = 0; i < this.inputs.length; i++) { @@ -1633,13 +1648,13 @@ TX.prototype.getPriority = function getPriority() { return sum.divn(size); }; -TX.prototype.isFree = function isFree() { - var size, priority; +TX.prototype.isFree = function isFree(size) { + var priority; if (!this.hasPrevout()) return false; - size = this.maxSize(); + size = size || this.maxSize(); if (size >= constants.tx.maxFreeSize) return false; @@ -1735,7 +1750,7 @@ TX.prototype.inspect = function inspect() { copy.block = this.block; delete copy._raw; delete copy._chain; - delete copy.total; + delete copy.requiredFunds; copy.hash = this.hash('hex'); copy.rhash = this.rhash; copy.rblock = this.rblock; diff --git a/test/wallet-test.js b/test/wallet-test.js index 600bdbb5..126af919 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -170,7 +170,7 @@ describe('Wallet', function() { // Create new transaction var t3 = bcoin.tx().out(w2, 15000); assert(!w1.fill(t3)); - assert.equal(t3.total.toString(10), 25000); + assert.equal(t3.requiredFunds.toString(10), 25000); cb(); });