refactor block explorer stubbing

This commit is contained in:
Ivan Socolsky 2015-02-20 17:23:42 -03:00
parent 9cda811691
commit e7aa060e15
2 changed files with 47 additions and 81 deletions

View File

@ -358,8 +358,7 @@ WalletService.prototype._getUtxos = function(cb) {
bc.getUnspentUtxos(addressStrs, function(err, inutxos) { bc.getUnspentUtxos(addressStrs, function(err, inutxos) {
if (err) return cb(err); if (err) return cb(err);
var utxos = _.map(inutxos, function(i) { var utxos = _.map(inutxos, function(i) {
var r = i.toObject(); return _.pick(i, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']);
return _.pick(r,['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']);
}); });
self.getPendingTxs({}, function(err, txps) { self.getPendingTxs({}, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);

View File

@ -88,18 +88,15 @@ helpers.toSatoshi = function(btc) {
}; };
// Amounts in satoshis // Amounts in satoshis
helpers.createUtxos = function(server, wallet, amounts, cb) { helpers.stubUtxos = function(server, wallet, amounts, cb) {
var addresses = []; var amounts = [].concat(amounts);
async.each(amounts, function(a, next) { async.map(amounts, function(a, next) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
addresses.push(address); next(err, address);
next(err);
}); });
}, },
function(err) { function(err, addresses) {
amounts = [].concat(amounts);
var i = 0; var i = 0;
var utxos = _.map(amounts, function(amount) { var utxos = _.map(amounts, function(amount) {
return { return {
@ -110,32 +107,20 @@ helpers.createUtxos = function(server, wallet, amounts, cb) {
address: addresses[i++].address, address: addresses[i++].address,
}; };
}); });
blockExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos);
return cb(utxos); return cb(utxos);
}); });
}; };
helpers.stubBroadcast = function(txid) {
helpers.stubBlockExplorer = function(server, inUtxos, txid) { blockExplorer.broadcast = sinon.stub().callsArgWith(1, null, 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.stubBroadcastFail = function() {
blockExplorer.broadcast = sinon.stub().callsArgWith(1, 'broadcast error');
};
helpers.clientSign = function(tx, xprivHex) { helpers.clientSign = function(tx, xprivHex) {
@ -185,7 +170,7 @@ helpers.createProposalOpts = function(toAddress, amount, message, signingKey) {
return opts; return opts;
}; };
var db, storage; var db, storage, blockExplorer;
describe('Copay server', function() { describe('Copay server', function() {
@ -196,8 +181,11 @@ describe('Copay server', function() {
storage = new Storage({ storage = new Storage({
db: db db: db
}); });
blockExplorer = sinon.stub();
WalletService.initialize({ WalletService.initialize({
storage: storage storage: storage,
blockExplorer: blockExplorer,
}); });
helpers.offset = 0; helpers.offset = 0;
}); });
@ -662,8 +650,7 @@ describe('Copay server', function() {
}); });
it('should create a tx', function(done) { it('should create a tx', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
@ -687,8 +674,7 @@ describe('Copay server', function() {
it('should fail to create tx with invalid proposal signature', function(done) { it('should fail to create tx with invalid proposal signature', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, 'dummy'); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, 'dummy');
server.createTx(txOpts, function(err, tx) { 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) { it('should fail to create tx with proposal signed by another copayer', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, TestData.copayers[1].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, null, TestData.copayers[1].privKey);
server.createTx(txOpts, function(err, tx) { 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) { it('should fail to create tx for invalid address', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('invalid address', 80, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('invalid address', 80, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { 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) { it('should fail to create tx for address of different network', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('myE38JHdxmQcTJGP1ZiX4BiGhDxMJDvLJD', 80, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('myE38JHdxmQcTJGP1ZiX4BiGhDxMJDvLJD', 80, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { 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) { it('should fail to create tx when insufficient funds', function(done) {
helpers.createUtxos(server, wallet, [100], function(utxos) { helpers.stubUtxos(server, wallet, [100], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 120, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 120, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.exist(err); should.exist(err);
@ -767,8 +749,7 @@ describe('Copay server', function() {
}); });
it('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.stubUtxos(server, wallet, [100], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 100, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 100, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.exist(err); should.exist(err);
@ -780,8 +761,7 @@ describe('Copay server', function() {
}); });
it('should fail to create tx for dust amount', function(done) { it('should fail to create tx for dust amount', function(done) {
helpers.createUtxos(server, wallet, [1], function(utxos) { helpers.stubUtxos(server, wallet, [1], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.00000001, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.00000001, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.exist(err); 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) { 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.stubUtxos(server, wallet, [10.1, 10.2, 10.3], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); 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) { 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.stubUtxos(server, wallet, [10.1, 10.2, 10.3], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 12, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
@ -847,10 +825,9 @@ describe('Copay server', function() {
it('should create tx using different UTXOs for simultaneous requests', function(done) { it('should create tx using different UTXOs for simultaneous requests', function(done) {
var N = 5; var N = 5;
helpers.createUtxos(server, wallet, _.times(N, function() { helpers.stubUtxos(server, wallet, _.times(N, function() {
return 100; return 100;
}), function(utxos) { }), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
server.getBalance({}, function(err, balance) { server.getBalance({}, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(helpers.toSatoshi(N * 100)); balance.totalAmount.should.equal(helpers.toSatoshi(N * 100));
@ -887,8 +864,7 @@ describe('Copay server', function() {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(1, 9), function(utxos) { helpers.stubUtxos(server, wallet, _.range(1, 9), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
@ -937,8 +913,7 @@ describe('Copay server', function() {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(1, 9), function(utxos) { helpers.stubUtxos(server, wallet, _.range(1, 9), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
@ -1090,14 +1065,13 @@ describe('Copay server', function() {
describe('#signTx and broadcast', function() { describe('#signTx and broadcast', function() {
var server, wallet, utxos; var server, wallet;
beforeEach(function(done) { beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, function(s, w) { helpers.createAndJoinWallet(1, 1, function(s, w) {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(1, 9), function(inutxos) { helpers.stubUtxos(server, wallet, _.range(1, 9), function() {
utxos = inutxos;
done(); done();
}); });
}); });
@ -1105,7 +1079,7 @@ describe('Copay server', function() {
}); });
it('should sign and broadcast a tx', function(done) { 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); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, txp) { server.createTx(txOpts, function(err, txp) {
should.not.exist(err); 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) { 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); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 10, null, TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, txp) { server.createTx(txOpts, function(err, txp) {
should.not.exist(err); should.not.exist(err);
@ -1170,15 +1144,14 @@ describe('Copay server', function() {
}); });
describe('Tx proposal workflow', function() { describe('Tx proposal workflow', function() {
var server, wallet, utxos; var server, wallet;
beforeEach(function(done) { beforeEach(function(done) {
helpers.createAndJoinWallet(2, 3, function(s, w) { helpers.createAndJoinWallet(2, 3, function(s, w) {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(1, 9), function(inUtxos) { helpers.stubUtxos(server, wallet, _.range(1, 9), function() {
utxos = inUtxos; helpers.stubBroadcast('999');
helpers.stubBlockExplorer(server, utxos, '999');
done(); done();
}); });
}); });
@ -1250,7 +1223,6 @@ describe('Copay server', function() {
}, },
function(txp, next) { function(txp, next) {
helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) { helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) {
helpers.stubBlockExplorer(server, utxos, '999');
var signatures = helpers.clientSign(txp, TestData.copayers[1].xPrivKey); var signatures = helpers.clientSign(txp, TestData.copayers[1].xPrivKey);
server.signTx({ server.signTx({
txProposalId: txpId, txProposalId: txpId,
@ -1333,7 +1305,6 @@ describe('Copay server', function() {
}, },
function(next) { function(next) {
helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) { helpers.getAuthServer(wallet.copayers[1].id, function(server, wallet) {
helpers.stubBlockExplorer(server, utxos, '999');
server.rejectTx({ server.rejectTx({
txProposalId: txpId, txProposalId: txpId,
reason: 'some other reason' reason: 'some other reason'
@ -1379,8 +1350,7 @@ describe('Copay server', function() {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(10), function(utxos) { helpers.stubUtxos(server, wallet, _.range(10), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.1, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.1, null, TestData.copayers[0].privKey);
async.eachSeries(_.range(10), function(i, next) { async.eachSeries(_.range(10), function(i, next) {
clock.tick(10000); clock.tick(10000);
@ -1466,8 +1436,7 @@ describe('Copay server', function() {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(4)), function(utxos) { helpers.stubUtxos(server, wallet, helpers.toSatoshi(_.range(4)), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.01, null, TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.01, null, TestData.copayers[0].privKey);
async.eachSeries(_.range(3), function(i, next) { async.eachSeries(_.range(3), function(i, next) {
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
@ -1522,6 +1491,7 @@ describe('Copay server', function() {
it('should notify sign and acceptance', function(done) { it('should notify sign and acceptance', function(done) {
server.getPendingTxs({}, function(err, txs) { server.getPendingTxs({}, function(err, txs) {
helpers.stubBroadcastFail();
var tx = txs[0]; var tx = txs[0];
var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey); var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey);
server.signTx({ server.signTx({
@ -1566,7 +1536,7 @@ describe('Copay server', function() {
server.getPendingTxs({}, function(err, txs) { server.getPendingTxs({}, function(err, txs) {
var tx = txs[2]; var tx = txs[2];
var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey); var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey);
helpers.stubBlockExplorer(server, [], '1122334455'); helpers.stubBroadcast('1122334455');
sinon.spy(server, 'emit'); sinon.spy(server, 'emit');
server.signTx({ server.signTx({
txProposalId: tx.id, txProposalId: tx.id,
@ -1600,8 +1570,7 @@ describe('Copay server', function() {
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(2), function(utxos) { helpers.stubUtxos(server, wallet, _.range(2), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1), amount: helpers.toSatoshi(0.1),
@ -1650,8 +1619,7 @@ describe('Copay server', function() {
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, _.range(2), function(utxos) { helpers.stubUtxos(server, wallet, _.range(2), function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1), amount: helpers.toSatoshi(0.1),
@ -1685,8 +1653,7 @@ describe('Copay server', function() {
server = s; server = s;
wallet = w; wallet = w;
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) { helpers.stubUtxos(server, wallet, [100, 200], function() {
helpers.stubBlockExplorer(server, utxos);
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey); var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, 'some message', TestData.copayers[0].privKey);
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
server.getPendingTxs({}, function(err, txs) { server.getPendingTxs({}, function(err, txs) {