test minimum fee

This commit is contained in:
Ivan Socolsky 2015-02-16 14:27:01 -03:00
parent f87381577c
commit 787a3376be
4 changed files with 60 additions and 17 deletions

View File

@ -151,7 +151,7 @@ TxProposal.prototype._addSignaturesToBitcoreTx = function(t, signatures, xpub) {
var self = this;
if (signatures.length != this.inputs.length)
return false;
throw new Error('Number of signatures does not match number of inputs');
var oks = 0,
i = 0,
@ -176,16 +176,16 @@ TxProposal.prototype._addSignaturesToBitcoreTx = function(t, signatures, xpub) {
});
if (oks != t.inputs.length)
throw new Error('wrong signatures');
throw new Error('Wrong signatures');
};
TxProposal.prototype.sign = function(copayerId, signatures, xpub) {
// Tests signatures are OK
var t = this._getBitcoreTx();
try {
// Tests signatures are OK
var t = this._getBitcoreTx();
this._addSignaturesToBitcoreTx(t, signatures, xpub);
this.addAction(copayerId, 'accept', null, signatures, xpub);
return true;
} catch (e) {

View File

@ -25,6 +25,7 @@ var Address = require('./model/address');
var TxProposal = require('./model/txproposal');
var Notification = require('./model/Notification');
var MINIMUM_FEE_SAT = 10000;
var initialized = false;
var storage;
@ -425,13 +426,20 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
selected.push(inputs[i]);
total += this._inputSatoshis(inputs[i]);
if (total >= txp.amount) {
break;
if (total >= txp.amount + MINIMUM_FEE_SAT) {
try {
// Check if there are enough fees
txp.inputs = selected;
var raw = txp.getRawTx();
return;
} catch (ex) {
//if (ex.name != 'bitcore.ErrorTransactionFeeError') {}
}
}
i++;
};
return total >= txp.amount ? selected : null;
txp.inputs = null;
return;
};
@ -488,7 +496,7 @@ CopayServer.prototype.createTx = function(opts, cb) {
requiredRejections: Math.min(wallet.m, wallet.n - wallet.m + 1),
});
txp.inputs = self._selectUtxos(txp, utxos);
self._selectUtxos(txp, utxos);
if (!txp.inputs) {
return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds'));
}
@ -584,7 +592,12 @@ CopayServer.prototype.removePendingTx = function(opts, cb) {
CopayServer.prototype._broadcastTx = function(txp, cb) {
var raw = txp.getRawTx();
var raw;
try {
raw = txp.getRawTx();
} catch (ex) {
return cb(ex);
}
var bc = this._getBlockExplorer('insight', txp.getNetworkName());
bc.broadcast(raw, function(err, txid) {
return cb(err, txid);

View File

@ -18,7 +18,7 @@
},
"dependencies": {
"async": "^0.9.0",
"bitcore": "^0.10.3",
"bitcore": "git+https://github.com/eordano/bitcore.git#7e88167891811163071ae35dc3dbb705ab6ccff8",
"bitcore-explorers": "^0.9.1",
"body-parser": "^1.11.0",
"commander": "^2.6.0",

View File

@ -728,7 +728,7 @@ describe('Copay server', function() {
server.createTx(txOpts, function(err, tx) {
should.not.exist(tx);
err.should.exist;
should.exist(err);
err.code.should.equal('INVALIDADDRESS');
err.message.should.equal('Incorrect address network');
done();
@ -741,6 +741,7 @@ describe('Copay server', function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 120, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) {
should.exist(err);
err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds');
server.getPendingTxs({}, function(err, txs) {
@ -757,7 +758,18 @@ describe('Copay server', function() {
});
});
it.skip('should fail to create tx when insufficient funds for fee', function(done) {});
it('should fail to create tx when insufficient funds for fee', function(done) {
helpers.createUtxos(server, wallet, [100], function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 100, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) {
should.exist(err);
err.code.should.equal('INSUFFICIENTFUNDS');
err.message.should.equal('Insufficient funds');
done();
});
});
});
it.skip('should fail to create tx for dust amount', function(done) {});
@ -911,7 +923,7 @@ describe('Copay server', function() {
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
tx.should.exist;
should.exist(tx);
txid = tx.id;
done();
});
@ -986,12 +998,30 @@ describe('Copay server', function() {
var tx = txs[0];
tx.id.should.equal(txid);
var signatures = ['11', '22', '33', '44'];
var signatures = ['11', '22', '33', '44', '55'];
server.signTx({
txProposalId: txid,
signatures: signatures,
}, function(err) {
err.message.should.contain('signatures');
should.exist(err);
err.message.should.contain('Bad signatures');
done();
});
});
});
it('should fail on wrong number of invalid signatures', function(done) {
server.getPendingTxs({}, function(err, txs) {
var tx = txs[0];
tx.id.should.equal(txid);
var signatures = _.take(helpers.clientSign(tx, TestData.copayers[0].xPrivKey), 2);
server.signTx({
txProposalId: txid,
signatures: signatures,
}, function(err) {
should.exist(err);
err.message.should.contain('Bad signatures');
done();
});
});