From 63fb94c2be9b9a32cabeb371ca69024ce2425ef5 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 27 Aug 2014 09:47:45 -0300 Subject: [PATCH 1/2] Check fail when sending incomplete tx --- js/models/core/Wallet.js | 1 - test/mocks/FakeBuilder.js | 2 -- test/test.Wallet.js | 17 +++++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index a85aadc43..385368c17 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -798,7 +798,6 @@ Wallet.prototype.sendTx = function(ntxid, cb) { if (txp.merchant) { return this.sendPaymentTx(ntxid, cb); } - var tx = txp.builder.build(); if (!tx.isComplete()) throw new Error('Tx is not complete. Can not broadcast'); diff --git a/test/mocks/FakeBuilder.js b/test/mocks/FakeBuilder.js index ace53be70..9b3aea95b 100644 --- a/test/mocks/FakeBuilder.js +++ b/test/mocks/FakeBuilder.js @@ -8,7 +8,6 @@ function Tx() { this.ins = [{s: VALID_SCRIPTSIG_BUF }]; }; - Tx.prototype.getHashType = function() { return 1; }; @@ -20,7 +19,6 @@ Tx.prototype.hashForSignature = function() { return new Buffer('31103626e162f1cbfab6b95b08c9f6e78aae128523261cb37f8dfd4783cb09a7', 'hex'); }; - function FakeBuilder() { this.test = 1; this.tx = new Tx(); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 3406cf42c..5e79f01d7 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -15,6 +15,7 @@ var PrivateKey = copay.PrivateKey; var Storage = require('./mocks/FakeStorage'); var Network = require('./mocks/FakeNetwork'); var Blockchain = require('./mocks/FakeBlockchain'); +var Builder = require('./mocks/FakeBuilder'); var bitcore = bitcore || require('bitcore'); var TransactionBuilder = bitcore.TransactionBuilder; var Transaction = bitcore.Transaction; @@ -104,6 +105,7 @@ describe('Wallet model', function() { c.verbose = config.verbose; c.version = '0.0.1'; + return new Wallet(c); } @@ -739,6 +741,21 @@ describe('Wallet model', function() { }); }); }); + it('should fail to send incomplete transaction', function(done) { + var w = createW2(null, 1); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + w.createTx(toAddress, amountSatStr, null, function(ntxid) { + var txp = w.txProposals.get(ntxid); + // Assign fake builder + txp.builder = new Builder(); + sinon.stub(txp.builder, 'build').returns({ isComplete: function () { return false; }}); + (function () { + w.sendTx(ntxid); + }).should.throw('Tx is not complete. Can not broadcast'); + done(); + }); + }); it('should send TxProposal', function(done) { var w = cachedCreateW2(); var utxo = createUTXO(w); From 0710c002ca9a9d9fb153a2e03ab6b27a3637b6ad Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 27 Aug 2014 10:36:01 -0300 Subject: [PATCH 2/2] Fail to send should check if already sent --- js/models/core/Wallet.js | 2 +- test/mocks/FakeBlockchain.js | 5 +++++ test/test.Wallet.js | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 385368c17..f214625a6 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -817,7 +817,7 @@ Wallet.prototype.sendTx = function(ntxid, cb) { self.store(); return cb(txid); } else { - self.log('Sent failed. Checking is the TX was sent already'); + self.log('Sent failed. Checking if the TX was sent already'); self._checkSentTx(ntxid, function(txid) { if (txid) self.store(); diff --git a/test/mocks/FakeBlockchain.js b/test/mocks/FakeBlockchain.js index e4c3997bc..8438d8a75 100644 --- a/test/mocks/FakeBlockchain.js +++ b/test/mocks/FakeBlockchain.js @@ -46,4 +46,9 @@ FakeBlockchain.prototype.sendRawTransaction = function(rawtx, cb) { return cb(txid); }; +FakeBlockchain.prototype.checkSentTx = function (tx, cb) { + var txid = '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa'; + return cb(null, txid); +}; + module.exports = FakeBlockchain; diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 5e79f01d7..cdd8af1a1 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -756,6 +756,18 @@ describe('Wallet model', function() { done(); }); }); + it('should check if transaction already sent when failing to send', function(done) { + var w = createW2(null, 1); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + w.createTx(toAddress, amountSatStr, null, function(ntxid) { + sinon.stub(w.blockchain, 'sendRawTransaction').yields(undefined); + var spyCheckSentTx = sinon.spy(w, '_checkSentTx'); + w.sendTx(ntxid, function () {}); + chai.expect(spyCheckSentTx.calledOnce).to.be.true; + done(); + }); + }); it('should send TxProposal', function(done) { var w = cachedCreateW2(); var utxo = createUTXO(w);