refactor tx inputs selection

This commit is contained in:
Ivan Socolsky 2015-03-11 15:04:42 -03:00
parent b088183ce9
commit b5f6582b77
2 changed files with 49 additions and 51 deletions

View File

@ -464,8 +464,16 @@ WalletService.prototype.getBalance = function(opts, cb) {
}); });
}; };
WalletService.prototype._selectTxInputs = function(txp, cb) {
var self = this;
self._getUtxos(function(err, utxos) {
if (err) return cb(err);
utxos = _.reject(utxos, {
locked: true
});
WalletService.prototype._selectUtxos = function(txp, utxos) {
var i = 0; var i = 0;
var total = 0; var total = 0;
var selected = []; var selected = [];
@ -480,17 +488,18 @@ WalletService.prototype._selectUtxos = function(txp, utxos) {
// Check if there are enough fees // Check if there are enough fees
txp.inputs = selected; txp.inputs = selected;
var raw = txp.getRawTx(); var raw = txp.getRawTx();
return; txp.inputPaths = _.pluck(txp.inputs, 'path');
return cb();
} catch (ex) { } catch (ex) {
if (ex.name != 'bitcore.ErrorTransactionFeeError') { if (ex.name != 'bitcore.ErrorTransactionFeeError') {
throw ex.message; return cb(ex);
} }
} }
} }
i++; i++;
}; };
txp.inputs = null; return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds'));
return; });
}; };
@ -534,15 +543,9 @@ WalletService.prototype.createTx = function(opts, cb) {
if (opts.amount < Bitcore.Transaction.DUST_AMOUNT) if (opts.amount < Bitcore.Transaction.DUST_AMOUNT)
return cb(new ClientError('DUSTAMOUNT', 'Amount below dust threshold')); return cb(new ClientError('DUSTAMOUNT', 'Amount below dust threshold'));
self._getUtxos(function(err, utxos) {
if (err) return cb(err);
var changeAddress = wallet.createAddress(true); var changeAddress = wallet.createAddress(true);
utxos = _.reject(utxos, {
locked: true
});
var txp = TxProposal.create({ var txp = TxProposal.create({
creatorId: self.copayerId, creatorId: self.copayerId,
toAddress: opts.toAddress, toAddress: opts.toAddress,
@ -554,16 +557,11 @@ WalletService.prototype.createTx = function(opts, cb) {
requiredRejections: Math.min(wallet.m, wallet.n - wallet.m + 1), requiredRejections: Math.min(wallet.m, wallet.n - wallet.m + 1),
}); });
try {
self._selectUtxos(txp, utxos);
} catch (ex) {
return cb(new ClientError(ex.toString()));
}
if (!txp.inputs) self._selectTxInputs(txp, function(err) {
return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds')); if (err) return cb(err);
txp.inputPaths = _.pluck(txp.inputs, 'path'); $.checkState(txp.inputs);
self.storage.storeAddressAndWallet(wallet, changeAddress, function(err) { self.storage.storeAddressAndWallet(wallet, changeAddress, function(err) {
if (err) return cb(err); if (err) return cb(err);

View File

@ -984,7 +984,7 @@ describe('Copay server', function() {
}); });
}); });
it.only('should fail with different error for insufficient funds and locked funds', function(done) { it('should fail with different error for insufficient funds and locked funds', function(done) {
helpers.stubUtxos(server, wallet, [10, 10], function() { helpers.stubUtxos(server, wallet, [10, 10], function() {
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 11, null, TestData.copayers[0].privKey_1H_0); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 11, null, TestData.copayers[0].privKey_1H_0);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {