improve utxo mocking

This commit is contained in:
Ivan Socolsky 2016-03-01 12:51:59 -03:00
parent dbba3acfa8
commit 328f6250be
3 changed files with 46 additions and 17 deletions

View File

@ -273,16 +273,19 @@ TxProposal.prototype.getRawTx = function() {
return t.uncheckedSerialize(); return t.uncheckedSerialize();
}; };
TxProposal.prototype.getEstimatedSizeForSingleInput = function() {
return this.requiredSignatures * 72 + this.walletN * 36 + 44;
};
TxProposal.prototype.getEstimatedSize = function() { TxProposal.prototype.getEstimatedSize = function() {
// Note: found empirically based on all multisig P2SH inputs and within m & n allowed limits. // Note: found empirically based on all multisig P2SH inputs and within m & n allowed limits.
var safetyMargin = 0.05; var safetyMargin = 0.05;
var walletM = this.requiredSignatures;
var overhead = 4 + 4 + 9 + 9; var overhead = 4 + 4 + 9 + 9;
var inputSize = walletM * 72 + this.walletN * 36 + 44; var inputSize = this.getEstimatedSizeForSingleInput();
var outputSize = 34; var outputSize = 34;
var nbInputs = this.inputs.length; var nbInputs = this.inputs.length;
var nbOutputs = (_.isArray(this.outputs) ? this.outputs.length : 1) + 1; var nbOutputs = (_.isArray(this.outputs) ? Math.max(1, this.outputs.length) : 1) + 1;
var size = overhead + inputSize * nbInputs + outputSize * nbOutputs; var size = overhead + inputSize * nbInputs + outputSize * nbOutputs;

View File

@ -212,6 +212,40 @@ helpers.toSatoshi = function(btc) {
} }
}; };
helpers._parseAmount = function(str) {
var result = {
amount: +0,
confirmations: _.random(6, 100),
};
if (_.isNumber(str)) str = str.toString();
var re = /^((?:\d+c)|u)?\s*([\d\.]+)\s*(btc|bit|sat)?$/;
var match = str.match(re);
if (!match) throw new Error('Could not parse amount ' + str);
if (match[1]) {
if (match[1] == 'u') result.confirmations = 0;
if (_.endsWith(match[1], 'c')) result.confirmations = +match[1].slice(0, -1);
}
switch (match[3]) {
default:
case 'btc':
result.amount = Utils.strip(match[2] * 1e8);
break;
case 'bit':
result.amount = Utils.strip(match[2] * 1e6);
break
case 'sat':
result.amount = Utils.strip(match[2]);
break;
};
return result;
};
helpers.stubUtxos = function(server, wallet, amounts, opts, cb) { helpers.stubUtxos = function(server, wallet, amounts, opts, cb) {
if (_.isFunction(opts)) { if (_.isFunction(opts)) {
cb = opts; cb = opts;
@ -233,17 +267,9 @@ helpers.stubUtxos = function(server, wallet, amounts, opts, cb) {
addresses.should.not.be.empty; addresses.should.not.be.empty;
var utxos = _.compact(_.map([].concat(amounts), function(amount, i) { var utxos = _.compact(_.map([].concat(amounts), function(amount, i) {
var confirmations = _.random(6, 100); var parsed = helpers._parseAmount(amount);
if (_.isString(amount)) { console.log('*** [helpers.js ln270] amount, result:', amount, parsed); // TODO
if (_.startsWith(amount, 'u')) { if (parsed.amount <= 0) return null;
amount = parseFloat(amount.substring(1));
confirmations = 0;
} else if (_.startsWith(amount, '<6')) {
amount = parseFloat(amount.substring(2));
confirmations = _.random(1, 5);
}
}
if (amount <= 0) return null;
var address = addresses[i % addresses.length]; var address = addresses[i % addresses.length];
@ -261,10 +287,10 @@ helpers.stubUtxos = function(server, wallet, amounts, opts, cb) {
return { return {
txid: helpers.randomTXID(), txid: helpers.randomTXID(),
vout: _.random(0, 10), vout: _.random(0, 10),
satoshis: helpers.toSatoshi(amount), satoshis: parsed.amount,
scriptPubKey: scriptPubKey.toBuffer().toString('hex'), scriptPubKey: scriptPubKey.toBuffer().toString('hex'),
address: address.address, address: address.address,
confirmations: confirmations confirmations: parsed.confirmations,
}; };
})); }));

View File

@ -5778,7 +5778,7 @@ describe('Wallet service', function() {
}); });
}); });
it.skip('should select smallest big utxo if small utxos exceed maximum fee', function(done) {}); it.skip('should select smallest big utxo if small utxos exceed maximum fee', function(done) {});
it.only('should ignore utxos not contributing enough to cover increase in fee', function(done) { it('should ignore utxos not contributing enough to cover increase in fee', function(done) {
helpers.stubUtxos(server, wallet, [0.0001, 0.0001, 0.0001], function() { helpers.stubUtxos(server, wallet, [0.0001, 0.0001, 0.0001], function() {
var txOpts = { var txOpts = {
outputs: [{ outputs: [{