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
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;

View File

@ -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();
});