|
|
@ -202,100 +202,6 @@ helpers.createAddresses = function(server, wallet, main, change, cb) { |
|
|
|
var db, storage, blockchainExplorer; |
|
|
|
|
|
|
|
|
|
|
|
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() { |
|
|
|
db = levelup(memdown, { |
|
|
@ -2568,3 +2474,97 @@ describe('Wallet service', function() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('Blockchain monitor', function() { |
|
|
|
var addressSubscriber; |
|
|
|
|
|
|
|
beforeEach(function() { |
|
|
|
db = levelup(memdown, { |
|
|
|
valueEncoding: 'json' |
|
|
|
}); |
|
|
|
storage = new Storage({ |
|
|
|
db: db |
|
|
|
}); |
|
|
|
blockchainExplorer = sinon.stub(); |
|
|
|
|
|
|
|
WalletService.initialize({ |
|
|
|
storage: storage, |
|
|
|
blockchainExplorer: blockchainExplorer, |
|
|
|
}); |
|
|
|
helpers.offset = 0; |
|
|
|
|
|
|
|
addressSubscriber = sinon.stub(); |
|
|
|
addressSubscriber.subscribe = sinon.stub(); |
|
|
|
sinon.stub(BlockchainMonitor.prototype, '_getAddressSubscriber').onFirstCall().returns(addressSubscriber); |
|
|
|
}); |
|
|
|
|
|
|
|
afterEach(function() { |
|
|
|
BlockchainMonitor.prototype._getAddressSubscriber.restore(); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should subscribe wallet', function(done) { |
|
|
|
var monitor = new BlockchainMonitor(); |
|
|
|
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); |
|
|
|
addressSubscriber.subscribe.calledTwice.should.be.true; |
|
|
|
addressSubscriber.subscribe.calledWith(address1.address).should.be.true; |
|
|
|
addressSubscriber.subscribe.calledWith(address2.address).should.be.true; |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should be able to subscribe new address', function(done) { |
|
|
|
var monitor = new BlockchainMonitor(); |
|
|
|
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); |
|
|
|
addressSubscriber.subscribe.calledOnce.should.be.true; |
|
|
|
addressSubscriber.subscribe.calledWith(address1.address).should.be.true; |
|
|
|
server.createAddress({}, function(err, address2) { |
|
|
|
should.not.exist(err); |
|
|
|
monitor.subscribeAddresses(wallet.id, address2.address); |
|
|
|
addressSubscriber.subscribe.calledTwice.should.be.true; |
|
|
|
addressSubscriber.subscribe.calledWith(address2.address).should.be.true; |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should create NewIncomingTx notification when a new tx arrives on registered address', function(done) { |
|
|
|
var monitor = new BlockchainMonitor(); |
|
|
|
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); |
|
|
|
addressSubscriber.subscribe.calledOnce.should.be.true; |
|
|
|
addressSubscriber.subscribe.getCall(0).args[0].should.equal(address1.address); |
|
|
|
var handler = addressSubscriber.subscribe.getCall(0).args[1]; |
|
|
|
_.isFunction(handler).should.be.true; |
|
|
|
|
|
|
|
monitor.on('notification', function(notification) { |
|
|
|
notification.type.should.equal('NewIncomingTx'); |
|
|
|
notification.data.address.should.equal(address1.address); |
|
|
|
notification.data.txid.should.equal('txid'); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
|
|
|
|
handler('txid'); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|