From 5f687ea5dfc8fe753110023ee24abebc75115b20 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 24 Feb 2015 12:27:44 -0300 Subject: [PATCH] broadcast tests --- lib/model/txproposal.js | 4 +- lib/server.js | 24 ++++++++-- test/integration/server.js | 95 +++++++++++++++++++++++++++++++++----- 3 files changed, 108 insertions(+), 15 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 2729963..ce7de15 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -52,6 +52,7 @@ TxProposal.fromObj = function(obj) { x.requiredRejections = obj.requiredRejections; x.status = obj.status; x.txid = obj.txid; + x.broadcastedOn = obj.broadcastedOn; x.inputPaths = obj.inputPaths; x.actions = _.map(obj.actions, function(action) { return TxProposalAction.fromObj(action); @@ -172,7 +173,7 @@ TxProposal.prototype._addSignaturesToBitcoreTx = function(t, signatures, xpub) { sigtype: Bitcore.crypto.Signature.SIGHASH_ALL, publicKey: pub, }; - t.inputs[i].addSignature(t,s); + t.inputs[i].addSignature(t, s); i++; } catch (e) {}; }); @@ -220,6 +221,7 @@ TxProposal.prototype.isBroadcasted = function() { TxProposal.prototype.setBroadcasted = function(txid) { this.txid = txid; this.status = 'broadcasted'; + this.broadcastedOn = Math.floor(Date.now() / 1000); }; module.exports = TxProposal; diff --git a/lib/server.js b/lib/server.js index ee5b583..98114f7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -691,12 +691,30 @@ WalletService.prototype.signTx = function(opts, cb) { copayerId: self.copayerId, }); - // TODO: replace with .isAccepted() - if (txp.status == 'accepted') { - + if (txp.isAccepted()) { self._notify('TxProposalFinallyAccepted', { txProposalId: opts.txProposalId, }); +<<<<<<< HEAD +======= + + self._broadcastTx(txp, function(err, txid) { + if (err) return cb(err, txp); + + txp.setBroadcasted(txid); + self.storage.storeTx(self.walletId, txp, function(err) { + if (err) return cb(err); + + self._notify('NewOutgoingTx', { + txProposalId: opts.txProposalId, + txid: txid + }); + return cb(null, txp); + }); + }); + } else { + return cb(null, txp); +>>>>>>> broadcast tests } return cb(null, txp); diff --git a/test/integration/server.js b/test/integration/server.js index 990704d..cf251e1 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1257,14 +1257,87 @@ describe('Copay server', function() { }); }); - describe.skip('#broadcastTx', function() { - it.skip('should keep tx as accepted if unable to broadcast it', function(done) {}); - it.skip('should brodcast a tx', function(done) { - // TODO: check final status == 'broadcasted' & broadcastedOn - }); - it.skip('should fail to brodcast an already broadcasted tx', function(done) {}); - it.skip('should brodcast a not yet accepted tx', function(done) {}); - it.skip('should brodcast a tx', function(done) {}); + describe('#broadcastTx', function() { + var server, wallet, txpid; + beforeEach(function(done) { + helpers.createAndJoinWallet(1, 1, function(s, w) { + server = s; + wallet = w; + helpers.stubUtxos(server, wallet, [10, 10], function() { + var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 9, 'some message', TestData.copayers[0].privKey); + server.createTx(txOpts, function(err, txp) { + should.not.exist(err); + should.exist(txp); + helpers.stubBroadcastFail(); + var signatures = helpers.clientSign(txp, TestData.copayers[0].xPrivKey); + server.signTx({ + txProposalId: txp.id, + signatures: signatures, + }, function(err, txp) { + should.exist(err); + should.exist(txp); + txp.isAccepted().should.be.true; + txpid = txp.id; + done(); + }); + }); + }); + }); + }); + + it('should brodcast a tx', function(done) { + var clock = sinon.useFakeTimers(1234000); + helpers.stubBroadcast('999'); + server.broadcastTx({ + txProposalId: txpid + }, function(err) { + should.not.exist(err); + server.getTx({ + id: txpid + }, function(err, txp) { + should.not.exist(err); + txp.txid.should.equal('999'); + txp.isBroadcasted().should.be.true; + txp.broadcastedOn.should.equal(1234); + clock.restore(); + done(); + }); + }); + }); + + it('should fail to brodcast an already broadcasted tx', function(done) { + helpers.stubBroadcast('999'); + server.broadcastTx({ + txProposalId: txpid + }, function(err) { + should.not.exist(err); + server.broadcastTx({ + txProposalId: txpid + }, function(err, txid) { + should.exist(err); + should.not.exist(txid); + err.code.should.equal('TXALREADYBROADCASTED'); + done(); + }); + }); + }); + + it('should fail to brodcast a not yet accepted tx', function(done) { + helpers.stubBroadcast('999'); + var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 9, 'some other message', TestData.copayers[0].privKey); + server.createTx(txOpts, function(err, txp) { + should.not.exist(err); + should.exist(txp); + server.broadcastTx({ + txProposalId: txp.id + }, function(err, txid) { + should.exist(err); + should.not.exist(txid); + err.code.should.equal('TXNOTACCEPTED'); + done(); + }); + }); + }); }); @@ -1285,7 +1358,7 @@ describe('Copay server', function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, 'some message', TestData.copayers[0].privKey); server.createTx(txOpts, function(err, txp) { should.not.exist(err); - should.exist.txp; + should.exist(txp); helpers.getAuthServer(wallet.copayers[1].id, function(server2, wallet) { server2.getPendingTxs({}, function(err, txps) { should.not.exist(err); @@ -1307,7 +1380,7 @@ describe('Copay server', function() { server.createTx(txOpts, function(err, txp) { txpId = txp.id; should.not.exist(err); - should.exist.txp; + should.exist(txp); next(); }); }, @@ -1393,7 +1466,7 @@ describe('Copay server', function() { server.createTx(txOpts, function(err, txp) { txpId = txp.id; should.not.exist(err); - should.exist.txp; + should.exist(txp); next(); }); },