coin selection. misc.

This commit is contained in:
Christopher Jeffrey 2016-02-12 04:18:01 -08:00
parent 697718c940
commit bfd570e204
2 changed files with 28 additions and 13 deletions

View File

@ -1024,10 +1024,17 @@ TX.prototype.getInputs = function getInputs(unspent, options) {
}; };
} }
// Oldest unspents first if (!options.selection || options.selection === 'age') {
unspent = unspent.slice().sort(function(a, b) { // Oldest unspents first
return a.height - b.height; 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() { function total() {
if (options.subtractFee) if (options.subtractFee)
@ -1053,6 +1060,9 @@ TX.prototype.getInputs = function getInputs(unspent, options) {
if (options.wallet) if (options.wallet)
options.wallet.scriptInputs(tx, index); options.wallet.scriptInputs(tx, index);
if (options.selection === 'all')
continue;
// Stop once we're full. // Stop once we're full.
if (isFull()) if (isFull())
break; break;
@ -1083,6 +1093,11 @@ TX.prototype.getInputs = function getInputs(unspent, options) {
// Calculate max possible size after signing. // Calculate max possible size after signing.
size = tx.maxSize(options.m, options.n); size = tx.maxSize(options.m, options.n);
// if (newkb == null && tx.isFree(size)) {
// fee = new bn(0);
// break;
// }
newkb = Math.ceil(size / 1024) - totalkb; newkb = Math.ceil(size / 1024) - totalkb;
fee.iaddn(newkb * minFee); fee.iaddn(newkb * minFee);
totalkb += newkb; totalkb += newkb;
@ -1140,7 +1155,7 @@ TX.prototype.fill = function fill(unspent, options) {
result = this.getInputs(unspent, options); result = this.getInputs(unspent, options);
this.total = result.total; this.requiredFunds = result.total;
if (!result.inputs) if (!result.inputs)
return result; return result;
@ -1599,8 +1614,8 @@ TX.prototype.isStandardInputs = function isStandardInputs(flags) {
return true; return true;
}; };
TX.prototype.getPriority = function getPriority() { TX.prototype.getPriority = function getPriority(size) {
var size, sum, i, input, age, height; var sum, i, input, age, height;
height = this.height; height = this.height;
@ -1610,7 +1625,7 @@ TX.prototype.getPriority = function getPriority() {
if (!this.hasPrevout()) if (!this.hasPrevout())
return new bn(0); return new bn(0);
size = this.maxSize(); size = size || this.maxSize();
sum = new bn(0); sum = new bn(0);
for (i = 0; i < this.inputs.length; i++) { for (i = 0; i < this.inputs.length; i++) {
@ -1633,13 +1648,13 @@ TX.prototype.getPriority = function getPriority() {
return sum.divn(size); return sum.divn(size);
}; };
TX.prototype.isFree = function isFree() { TX.prototype.isFree = function isFree(size) {
var size, priority; var priority;
if (!this.hasPrevout()) if (!this.hasPrevout())
return false; return false;
size = this.maxSize(); size = size || this.maxSize();
if (size >= constants.tx.maxFreeSize) if (size >= constants.tx.maxFreeSize)
return false; return false;
@ -1735,7 +1750,7 @@ TX.prototype.inspect = function inspect() {
copy.block = this.block; copy.block = this.block;
delete copy._raw; delete copy._raw;
delete copy._chain; delete copy._chain;
delete copy.total; delete copy.requiredFunds;
copy.hash = this.hash('hex'); copy.hash = this.hash('hex');
copy.rhash = this.rhash; copy.rhash = this.rhash;
copy.rblock = this.rblock; copy.rblock = this.rblock;

View File

@ -170,7 +170,7 @@ describe('Wallet', function() {
// Create new transaction // Create new transaction
var t3 = bcoin.tx().out(w2, 15000); var t3 = bcoin.tx().out(w2, 15000);
assert(!w1.fill(t3)); assert(!w1.fill(t3));
assert.equal(t3.total.toString(10), 25000); assert.equal(t3.requiredFunds.toString(10), 25000);
cb(); cb();
}); });