Browse Source

broadcast tests

activeAddress
Ivan Socolsky 10 years ago
parent
commit
5f687ea5df
  1. 4
      lib/model/txproposal.js
  2. 24
      lib/server.js
  3. 93
      test/integration/server.js

4
lib/model/txproposal.js

@ -52,6 +52,7 @@ TxProposal.fromObj = function(obj) {
x.requiredRejections = obj.requiredRejections; x.requiredRejections = obj.requiredRejections;
x.status = obj.status; x.status = obj.status;
x.txid = obj.txid; x.txid = obj.txid;
x.broadcastedOn = obj.broadcastedOn;
x.inputPaths = obj.inputPaths; x.inputPaths = obj.inputPaths;
x.actions = _.map(obj.actions, function(action) { x.actions = _.map(obj.actions, function(action) {
return TxProposalAction.fromObj(action); return TxProposalAction.fromObj(action);
@ -172,7 +173,7 @@ TxProposal.prototype._addSignaturesToBitcoreTx = function(t, signatures, xpub) {
sigtype: Bitcore.crypto.Signature.SIGHASH_ALL, sigtype: Bitcore.crypto.Signature.SIGHASH_ALL,
publicKey: pub, publicKey: pub,
}; };
t.inputs[i].addSignature(t,s); t.inputs[i].addSignature(t, s);
i++; i++;
} catch (e) {}; } catch (e) {};
}); });
@ -220,6 +221,7 @@ TxProposal.prototype.isBroadcasted = function() {
TxProposal.prototype.setBroadcasted = function(txid) { TxProposal.prototype.setBroadcasted = function(txid) {
this.txid = txid; this.txid = txid;
this.status = 'broadcasted'; this.status = 'broadcasted';
this.broadcastedOn = Math.floor(Date.now() / 1000);
}; };
module.exports = TxProposal; module.exports = TxProposal;

24
lib/server.js

@ -691,12 +691,30 @@ WalletService.prototype.signTx = function(opts, cb) {
copayerId: self.copayerId, copayerId: self.copayerId,
}); });
// TODO: replace with .isAccepted() if (txp.isAccepted()) {
if (txp.status == 'accepted') {
self._notify('TxProposalFinallyAccepted', { self._notify('TxProposalFinallyAccepted', {
txProposalId: opts.txProposalId, 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); return cb(null, txp);

93
test/integration/server.js

@ -1257,14 +1257,87 @@ describe('Copay server', function() {
}); });
}); });
describe.skip('#broadcastTx', function() { describe('#broadcastTx', function() {
it.skip('should keep tx as accepted if unable to broadcast it', function(done) {}); var server, wallet, txpid;
it.skip('should brodcast a tx', function(done) { beforeEach(function(done) {
// TODO: check final status == 'broadcasted' & broadcastedOn 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();
});
});
}); });
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) {});
}); });
@ -1285,7 +1358,7 @@ describe('Copay server', function() {
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, 'some message', TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, 'some message', TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, txp) { server.createTx(txOpts, function(err, txp) {
should.not.exist(err); should.not.exist(err);
should.exist.txp; should.exist(txp);
helpers.getAuthServer(wallet.copayers[1].id, function(server2, wallet) { helpers.getAuthServer(wallet.copayers[1].id, function(server2, wallet) {
server2.getPendingTxs({}, function(err, txps) { server2.getPendingTxs({}, function(err, txps) {
should.not.exist(err); should.not.exist(err);
@ -1307,7 +1380,7 @@ describe('Copay server', function() {
server.createTx(txOpts, function(err, txp) { server.createTx(txOpts, function(err, txp) {
txpId = txp.id; txpId = txp.id;
should.not.exist(err); should.not.exist(err);
should.exist.txp; should.exist(txp);
next(); next();
}); });
}, },
@ -1393,7 +1466,7 @@ describe('Copay server', function() {
server.createTx(txOpts, function(err, txp) { server.createTx(txOpts, function(err, txp) {
txpId = txp.id; txpId = txp.id;
should.not.exist(err); should.not.exist(err);
should.exist.txp; should.exist(txp);
next(); next();
}); });
}, },

Loading…
Cancel
Save