diff --git a/lib/server.js b/lib/server.js index 8675d11..6090b33 100644 --- a/lib/server.js +++ b/lib/server.js @@ -33,7 +33,7 @@ var storage, blockExplorer; * @constructor */ function WalletService() { - if (!initialized) + if (!initialized) throw new Error('Server not initialized'); this.storage = storage; @@ -358,8 +358,7 @@ WalletService.prototype._getUtxos = function(cb) { bc.getUnspentUtxos(addressStrs, function(err, inutxos) { if (err) return cb(err); var utxos = _.map(inutxos, function(i) { - var r = i.toObject(); - return _.pick(r,['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); + return _.pick(i, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); }); self.getPendingTxs({}, function(err, txps) { if (err) return cb(err); diff --git a/test/integration/server.js b/test/integration/server.js index d64fc6e..5b2ae94 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -88,18 +88,15 @@ helpers.toSatoshi = function(btc) { }; // Amounts in satoshis -helpers.createUtxos = function(server, wallet, amounts, cb) { - var addresses = []; +helpers.stubUtxos = function(server, wallet, amounts, cb) { + var amounts = [].concat(amounts); - async.each(amounts, function(a, next) { + async.map(amounts, function(a, next) { server.createAddress({}, function(err, address) { - addresses.push(address); - next(err); + next(err, address); }); }, - function(err) { - amounts = [].concat(amounts); - + function(err, addresses) { var i = 0; var utxos = _.map(amounts, function(amount) { return { @@ -110,32 +107,20 @@ helpers.createUtxos = function(server, wallet, amounts, cb) { address: addresses[i++].address, }; }); + + blockExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos); + return cb(utxos); }); }; - -helpers.stubBlockExplorer = function(server, inUtxos, txid) { - - var utxos = _.map(inUtxos, function(x) { - x.toObject = function() { - return this; - }; - return x; - }); - - var bc = sinon.stub(); - bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos); - - if (txid) { - bc.broadcast = sinon.stub().callsArgWith(1, null, txid); - } else { - bc.broadcast = sinon.stub().callsArgWith(1, 'broadcast error'); - } - - server._getBlockExplorer = sinon.stub().returns(bc); +helpers.stubBroadcast = function(txid) { + blockExplorer.broadcast = sinon.stub().callsArgWith(1, null, txid); }; +helpers.stubBroadcastFail = function() { + blockExplorer.broadcast = sinon.stub().callsArgWith(1, 'broadcast error'); +}; helpers.clientSign = function(tx, xprivHex) { @@ -185,7 +170,7 @@ helpers.createProposalOpts = function(toAddress, amount, message, signingKey) { return opts; }; -var db, storage; +var db, storage, blockExplorer; describe('Copay server', function() { @@ -196,8 +181,11 @@ describe('Copay server', function() { storage = new Storage({ db: db }); + blockExplorer = sinon.stub(); + WalletService.initialize({ - storage: storage + storage: storage, + blockExplorer: blockExplorer, }); helpers.offset = 0; }); @@ -662,8 +650,7 @@ describe('Copay server', function() { }); it('should create a tx', function(done) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.not.exist(err); @@ -687,8 +674,7 @@ describe('Copay server', function() { it('should fail to create tx with invalid proposal signature', function(done) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, 'dummy'); server.createTx(txOpts, function(err, tx) { @@ -701,8 +687,7 @@ describe('Copay server', function() { }); it('should fail to create tx with proposal signed by another copayer', function(done) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, TestData.copayers[1].privKey); server.createTx(txOpts, function(err, tx) { @@ -715,8 +700,7 @@ describe('Copay server', function() { }); it('should fail to create tx for invalid address', function(done) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('invalid address', 80, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { @@ -730,8 +714,7 @@ describe('Copay server', function() { }); it('should fail to create tx for address of different network', function(done) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('myE38JHdxmQcTJGP1ZiX4BiGhDxMJDvLJD', 80, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { @@ -745,8 +728,7 @@ describe('Copay server', function() { }); it('should fail to create tx when insufficient funds', function(done) { - helpers.createUtxos(server, wallet, [100], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 120, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.exist(err); @@ -767,8 +749,7 @@ describe('Copay server', function() { }); it('should fail to create tx when insufficient funds for fee', function(done) { - helpers.createUtxos(server, wallet, [100], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 100, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.exist(err); @@ -780,8 +761,7 @@ describe('Copay server', function() { }); it('should fail to create tx for dust amount', function(done) { - helpers.createUtxos(server, wallet, [1], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [1], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.00000001, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.exist(err); @@ -793,8 +773,7 @@ describe('Copay server', function() { }); it('should create tx when there is a pending tx and enough UTXOs', function(done) { - helpers.createUtxos(server, wallet, [10.1, 10.2, 10.3], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [10.1, 10.2, 10.3], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.not.exist(err); @@ -819,8 +798,7 @@ describe('Copay server', function() { }); it('should fail to create tx when there is a pending tx and not enough UTXOs', function(done) { - helpers.createUtxos(server, wallet, [10.1, 10.2, 10.3], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [10.1, 10.2, 10.3], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.not.exist(err); @@ -847,10 +825,9 @@ describe('Copay server', function() { it('should create tx using different UTXOs for simultaneous requests', function(done) { var N = 5; - helpers.createUtxos(server, wallet, _.times(N, function() { + helpers.stubUtxos(server, wallet, _.times(N, function() { return 100; }), function(utxos) { - helpers.stubBlockExplorer(server, utxos); server.getBalance({}, function(err, balance) { should.not.exist(err); balance.totalAmount.should.equal(helpers.toSatoshi(N * 100)); @@ -887,8 +864,7 @@ describe('Copay server', function() { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(1, 9), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, _.range(1, 9), function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.not.exist(err); @@ -937,8 +913,7 @@ describe('Copay server', function() { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(1, 9), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, _.range(1, 9), function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { should.not.exist(err); @@ -1090,14 +1065,13 @@ describe('Copay server', function() { describe('#signTx and broadcast', function() { - var server, wallet, utxos; + var server, wallet; beforeEach(function(done) { helpers.createAndJoinWallet(1, 1, function(s, w) { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(1, 9), function(inutxos) { - utxos = inutxos; + helpers.stubUtxos(server, wallet, _.range(1, 9), function() { done(); }); }); @@ -1105,7 +1079,7 @@ describe('Copay server', function() { }); it('should sign and broadcast a tx', function(done) { - helpers.stubBlockExplorer(server, utxos, '1122334455'); + helpers.stubBroadcast('1122334455'); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, txp) { should.not.exist(err); @@ -1138,7 +1112,7 @@ describe('Copay server', function() { it('should keep tx as *accepted* if unable to broadcast it', function(done) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubBroadcastFail(); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); server.createTx(txOpts, function(err, txp) { should.not.exist(err); @@ -1170,15 +1144,14 @@ describe('Copay server', function() { }); describe('Tx proposal workflow', function() { - var server, wallet, utxos; + var server, wallet; beforeEach(function(done) { helpers.createAndJoinWallet(2, 3, function(s, w) { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(1, 9), function(inUtxos) { - utxos = inUtxos; - helpers.stubBlockExplorer(server, utxos, '999'); + helpers.stubUtxos(server, wallet, _.range(1, 9), function() { + helpers.stubBroadcast('999'); done(); }); }); @@ -1250,7 +1223,6 @@ describe('Copay server', function() { }, function(txp, next) { helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) { - helpers.stubBlockExplorer(server, utxos, '999'); var signatures = helpers.clientSign(txp, TestData.copayers[1].xPrivKey); server.signTx({ txProposalId: txpId, @@ -1333,7 +1305,6 @@ describe('Copay server', function() { }, function(next) { helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) { - helpers.stubBlockExplorer(server, utxos, '999'); server.rejectTx({ txProposalId: txpId, reason: 'some other reason' @@ -1379,8 +1350,7 @@ describe('Copay server', function() { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(10), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, _.range(10), function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.1, null, TestData.copayers[0].privKey); async.eachSeries(_.range(10), function(i, next) { clock.tick(10000); @@ -1466,8 +1436,7 @@ describe('Copay server', function() { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(4)), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, helpers.toSatoshi(_.range(4)), function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.01, null, TestData.copayers[0].privKey); async.eachSeries(_.range(3), function(i, next) { server.createTx(txOpts, function(err, tx) { @@ -1522,6 +1491,7 @@ describe('Copay server', function() { it('should notify sign and acceptance', function(done) { server.getPendingTxs({}, function(err, txs) { + helpers.stubBroadcastFail(); var tx = txs[0]; var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey); server.signTx({ @@ -1566,7 +1536,7 @@ describe('Copay server', function() { server.getPendingTxs({}, function(err, txs) { var tx = txs[2]; var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey); - helpers.stubBlockExplorer(server, [], '1122334455'); + helpers.stubBroadcast('1122334455'); sinon.spy(server, 'emit'); server.signTx({ txProposalId: tx.id, @@ -1600,8 +1570,7 @@ describe('Copay server', function() { wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(2), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, _.range(2), function() { var txOpts = { toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', amount: helpers.toSatoshi(0.1), @@ -1650,8 +1619,7 @@ describe('Copay server', function() { wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, _.range(2), function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, _.range(2), function() { var txOpts = { toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', amount: helpers.toSatoshi(0.1), @@ -1685,8 +1653,7 @@ describe('Copay server', function() { server = s; wallet = w; server.createAddress({}, function(err, address) { - helpers.createUtxos(server, wallet, [100, 200], function(utxos) { - helpers.stubBlockExplorer(server, utxos); + helpers.stubUtxos(server, wallet, [100, 200], function() { var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey); server.createTx(txOpts, function(err, tx) { server.getPendingTxs({}, function(err, txs) {