test blockchain monitor using stubs
This commit is contained in:
parent
cf73449e7c
commit
6209a2ac97
|
@ -27,10 +27,10 @@ function BlockChainExplorer(opts) {
|
||||||
url = 'https://test-insight.bitpay.com:443'
|
url = 'https://test-insight.bitpay.com:443'
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var bc = new Explorers.Insight(url, network);
|
var explorer = new Explorers.Insight(url, network);
|
||||||
bc.getTransactions = _.bind(getTransactionsInsight, bc, url);
|
explorer.getTransactions = _.bind(getTransactionsInsight, explorer, url);
|
||||||
bc.initSocket = _.bind(initSocketInsight, bc, url);
|
explorer.initSocket = _.bind(initSocketInsight, explorer, url);
|
||||||
return bc;
|
return explorer;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Provider ' + provider + ' not supported');
|
throw new Error('Provider ' + provider + ' not supported');
|
||||||
|
@ -38,7 +38,6 @@ function BlockChainExplorer(opts) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function getTransactionsInsight(url, addresses, cb) {
|
function getTransactionsInsight(url, addresses, cb) {
|
||||||
request({
|
request({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|
|
@ -13,27 +13,27 @@ var nodeutil = require('util');
|
||||||
var WalletUtils = require('bitcore-wallet-utils');
|
var WalletUtils = require('bitcore-wallet-utils');
|
||||||
var Bitcore = WalletUtils.Bitcore;
|
var Bitcore = WalletUtils.Bitcore;
|
||||||
var WalletService = require('./server');
|
var WalletService = require('./server');
|
||||||
var BlockExplorer = require('./blockexplorer');
|
var BlockchainExplorer = require('./blockchainexplorer');
|
||||||
|
|
||||||
var Notification = require('./model/notification');
|
var Notification = require('./model/notification');
|
||||||
|
|
||||||
function BlockchainMonitor() {
|
function BlockchainMonitor() {
|
||||||
|
var self = this;
|
||||||
this.subscriptions = {};
|
this.subscriptions = {};
|
||||||
this.sockets = {};
|
this.sockets = {};
|
||||||
|
this.sockets['livenet'] = self._getBlockchainExplorerSocket('insight', 'livenet');
|
||||||
this._initBlockExplorerSocket('insight', 'livenet');
|
this.sockets['testnet'] = self._getBlockchainExplorerSocket('insight', 'testnet');
|
||||||
this._initBlockExplorerSocket('insight', 'testnet');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodeutil.inherits(BlockchainMonitor, events.EventEmitter);
|
nodeutil.inherits(BlockchainMonitor, events.EventEmitter);
|
||||||
|
|
||||||
BlockchainMonitor.prototype._initBlockExplorerSocket = function(provider, network) {
|
BlockchainMonitor.prototype._getBlockchainExplorerSocket = function(provider, network) {
|
||||||
var explorer = new BlockExplorer({
|
var explorer = new BlockchainExplorer({
|
||||||
provider: provider,
|
provider: provider,
|
||||||
network: network,
|
network: network,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.sockets[network] = explorer.initSocket();
|
return explorer.initSocket();
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
|
BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
|
||||||
|
@ -60,6 +60,7 @@ BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
|
||||||
addresses: [],
|
addresses: [],
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
var addresses = [].concat(addresses);
|
var addresses = [].concat(addresses);
|
||||||
var network = Bitcore.Address.fromString(addresses[0]).network.name;
|
var network = Bitcore.Address.fromString(addresses[0]).network.name;
|
||||||
var socket = self.sockets[network];
|
var socket = self.sockets[network];
|
||||||
|
@ -70,7 +71,7 @@ BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockchainMonitor.prototype.subscribeWallet = function(walletService) {
|
BlockchainMonitor.prototype.subscribeWallet = function(walletService, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var walletId = walletService.walletId;
|
var walletId = walletService.walletId;
|
||||||
|
@ -79,10 +80,10 @@ BlockchainMonitor.prototype.subscribeWallet = function(walletService) {
|
||||||
walletService.getMainAddresses({}, function(err, addresses) {
|
walletService.getMainAddresses({}, function(err, addresses) {
|
||||||
if (err) {
|
if (err) {
|
||||||
delete self.subscriptions[walletId];
|
delete self.subscriptions[walletId];
|
||||||
log.warn('Could not subscribe to addresses for wallet ' + walletId);
|
return cb(new Error('Could not subscribe to addresses for wallet ' + walletId));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
self.subscribeAddresses(walletService.walletId, _.pluck(addresses, 'address'));
|
self.subscribeAddresses(walletService.walletId, _.pluck(addresses, 'address'));
|
||||||
|
return cb();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,9 @@ WsApp.start = function(server) {
|
||||||
socket.join(service.walletId);
|
socket.join(service.walletId);
|
||||||
socket.emit('authorized');
|
socket.emit('authorized');
|
||||||
|
|
||||||
bcMonitor.subscribeWallet(service);
|
bcMonitor.subscribeWallet(service, function(err) {
|
||||||
|
if (err) log.warn(err.message);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,6 +16,7 @@ var Utils = require('../../lib/utils');
|
||||||
var WalletUtils = require('bitcore-wallet-utils');
|
var WalletUtils = require('bitcore-wallet-utils');
|
||||||
var Bitcore = WalletUtils.Bitcore;
|
var Bitcore = WalletUtils.Bitcore;
|
||||||
var Storage = require('../../lib/storage');
|
var Storage = require('../../lib/storage');
|
||||||
|
var BlockchainMonitor = require('../../lib/blockchainmonitor');
|
||||||
|
|
||||||
var Wallet = require('../../lib/model/wallet');
|
var Wallet = require('../../lib/model/wallet');
|
||||||
var TxProposal = require('../../lib/model/txproposal');
|
var TxProposal = require('../../lib/model/txproposal');
|
||||||
|
@ -201,7 +202,101 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
|
||||||
var db, storage, blockchainExplorer;
|
var db, storage, blockchainExplorer;
|
||||||
|
|
||||||
|
|
||||||
describe('Copay server', function() {
|
describe('Blockchain monitor', function() {
|
||||||
|
var bcSocket, monitor;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
db = levelup(memdown, {
|
||||||
|
valueEncoding: 'json'
|
||||||
|
});
|
||||||
|
storage = new Storage({
|
||||||
|
db: db
|
||||||
|
});
|
||||||
|
blockchainExplorer = sinon.stub();
|
||||||
|
|
||||||
|
WalletService.initialize({
|
||||||
|
storage: storage,
|
||||||
|
blockchainExplorer: blockchainExplorer,
|
||||||
|
});
|
||||||
|
helpers.offset = 0;
|
||||||
|
|
||||||
|
bcSocket = sinon.stub();
|
||||||
|
bcSocket.emit = sinon.stub();
|
||||||
|
bcSocket.on = sinon.stub();
|
||||||
|
sinon.stub(BlockchainMonitor.prototype, '_getBlockchainExplorerSocket').onFirstCall().returns(bcSocket);
|
||||||
|
monitor = new BlockchainMonitor();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
BlockchainMonitor.prototype._getBlockchainExplorerSocket.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should subscribe wallet', function(done) {
|
||||||
|
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
|
||||||
|
server.createAddress({}, function(err, address1) {
|
||||||
|
should.not.exist(err);
|
||||||
|
server.createAddress({}, function(err, address2) {
|
||||||
|
should.not.exist(err);
|
||||||
|
monitor.subscribeWallet(server, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
bcSocket.emit.calledTwice.should.be.true;
|
||||||
|
bcSocket.emit.calledWith('subscribe', address1.address).should.be.true;
|
||||||
|
bcSocket.emit.calledWith('subscribe', address2.address).should.be.true;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to subscribe new address', function(done) {
|
||||||
|
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
|
||||||
|
server.createAddress({}, function(err, address1) {
|
||||||
|
should.not.exist(err);
|
||||||
|
monitor.subscribeWallet(server, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
bcSocket.emit.calledOnce.should.be.true;
|
||||||
|
bcSocket.emit.calledWith('subscribe', address1.address).should.be.true;
|
||||||
|
server.createAddress({}, function(err, address2) {
|
||||||
|
should.not.exist(err);
|
||||||
|
monitor.subscribeAddresses(wallet.id, address2.address);
|
||||||
|
bcSocket.emit.calledTwice.should.be.true;
|
||||||
|
bcSocket.emit.calledWith('subscribe', address2.address).should.be.true;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create NewIncomingTx notification when a new tx arrives on registered address', function(done) {
|
||||||
|
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
|
||||||
|
server.createAddress({}, function(err, address1) {
|
||||||
|
should.not.exist(err);
|
||||||
|
monitor.subscribeWallet(server, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
bcSocket.on.calledOnce.should.be.true;
|
||||||
|
bcSocket.on.getCall(0).args[0].should.equal(address1.address);
|
||||||
|
var handler = bcSocket.on.getCall(0).args[1];
|
||||||
|
_.isFunction(handler).should.be.true;
|
||||||
|
|
||||||
|
var emitSpy = sinon.spy(monitor, 'emit');
|
||||||
|
handler('txid');
|
||||||
|
emitSpy.calledOnce.should.be.true;
|
||||||
|
emitSpy.getCall(0).args[0].should.equal('notification');
|
||||||
|
var notification = emitSpy.getCall(0).args[1];
|
||||||
|
notification.type.should.equal('NewIncomingTx');
|
||||||
|
notification.data.address.should.equal(address1.address);
|
||||||
|
notification.data.txid.should.equal('txid');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('Wallet service', function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
db = levelup(memdown, {
|
db = levelup(memdown, {
|
||||||
valueEncoding: 'json'
|
valueEncoding: 'json'
|
||||||
|
|
Loading…
Reference in New Issue