-
Notifications
You must be signed in to change notification settings - Fork 635
Add NetworkMonitor #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 'use strict'; | ||
|
|
||
| var util = require('util'); | ||
| var EventEmitter = require('events').EventEmitter; | ||
|
|
||
| var bitcore = require('bitcore'); | ||
| var Networks = bitcore.Networks; | ||
| var $ = bitcore.util.preconditions; | ||
| var p2p = require('bitcore-p2p'); | ||
| var Peer = p2p.Peer; | ||
| var Messages = p2p.Messages; | ||
|
|
||
| function NetworkMonitor(eventBus, peer) { | ||
| $.checkArgument(eventBus); | ||
| $.checkArgument(peer); | ||
| this.bus = eventBus; | ||
| this.peer = peer; | ||
| this.setupPeer(peer); | ||
| } | ||
| util.inherits(NetworkMonitor, EventEmitter); | ||
|
|
||
| NetworkMonitor.create = function(eventBus, opts) { | ||
| opts = opts || {}; | ||
| opts.network = opts.network || Networks.defaultNetwork; | ||
| opts.host = opts.host || 'localhost'; | ||
| opts.port = opts.port || Networks.defaultNetwork.port; | ||
|
|
||
| var peer = new Peer(opts.host, opts.port, opts.network); | ||
| return new NetworkMonitor(eventBus, peer); | ||
| }; | ||
|
|
||
| NetworkMonitor.prototype.setupPeer = function(peer) { | ||
| var self = this; | ||
|
|
||
| peer.on('ready', function() { | ||
| self.emit('ready'); | ||
| }); | ||
| peer.on('inv', function(m) { | ||
| // TODO only ask for data if tx or block is unknown | ||
| peer.sendMessage(new Messages.GetData(m.inventory)); | ||
| }); | ||
| peer.on('tx', function(m) { | ||
| self.bus.process(m.transaction); | ||
| }); | ||
| peer.on('block', function(m) { | ||
| self.bus.process(m.block); | ||
| }); | ||
| }; | ||
|
|
||
| NetworkMonitor.prototype.start = function() { | ||
| this.peer.connect(); | ||
| }; | ||
|
|
||
|
|
||
| module.exports = NetworkMonitor; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| 'use strict'; | ||
|
|
||
| var chai = require('chai'); | ||
| var should = chai.should(); | ||
| var bitcore = require('bitcore'); | ||
| var Networks = bitcore.Networks; | ||
| var sinon = require('sinon'); | ||
| var util = require('util'); | ||
| var Transaction = bitcore.Transaction; | ||
| var Block = bitcore.Block; | ||
| var EventEmitter = require('events').EventEmitter; | ||
|
|
||
| var NetworkMonitor = require('../lib/networkmonitor'); | ||
| var EventBus = require('../lib/eventbus'); | ||
|
|
||
| describe('NetworkMonitor', function() { | ||
|
|
||
| // mocks | ||
| var mockTx, mockBlock, busMock, peerMock; | ||
| beforeEach(function() { | ||
| mockTx = new Transaction(); | ||
| var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'; | ||
| var genesisbuf = new Buffer(genesishex, 'hex'); | ||
| mockBlock = new Block(genesisbuf); | ||
| mockBlock.id = 'asd'; | ||
| busMock = new EventBus(); | ||
| peerMock = new EventEmitter(); | ||
| peerMock.sendMessage = sinon.spy(); | ||
| peerMock.connect = function() { | ||
| this.emit('ready'); | ||
| this.emit('inv', { | ||
| inventory: [] | ||
| }); | ||
| this.emit('tx', { | ||
| transaction: mockTx | ||
| }); | ||
| this.emit('block', { | ||
| block: mockBlock | ||
| }); | ||
| }; | ||
| }); | ||
|
|
||
| it('instantiates correctly from constructor', function() { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| should.exist(nm); | ||
| }); | ||
|
|
||
| it('instantiates correctly from create', function() { | ||
| var nm = NetworkMonitor.create(busMock); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you test that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| should.exist(nm); | ||
| }); | ||
|
|
||
| it('instantiates correctly from create with opts', function() { | ||
| var opts = { | ||
| network: Networks.livenet, | ||
| host: '8.8.8.8', | ||
| port: 3001 | ||
| }; | ||
| var nm = NetworkMonitor.create(busMock); | ||
| should.exist(nm); | ||
| }); | ||
|
|
||
| it('start', function() { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| nm.start.bind(nm).should.not.throw(); | ||
| }); | ||
|
|
||
| it('broadcasts ready after start', function(cb) { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| nm.on('ready', cb) | ||
| nm.start(); | ||
| }); | ||
|
|
||
| it('sends getdatas when receiving invs', function() { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| nm.start(); | ||
| peerMock.sendMessage.calledOnce.should.equal(true); | ||
| }); | ||
|
|
||
| it('sends transactions to bus', function(cb) { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| busMock.register(bitcore.Transaction, function(tx) { | ||
| tx.id.should.equal(mockTx.id); | ||
| cb(); | ||
| }); | ||
| nm.start(); | ||
| }); | ||
|
|
||
| it('sends blocks to bus', function(cb) { | ||
| var nm = new NetworkMonitor(busMock, peerMock); | ||
| busMock.register(bitcore.Block, function(block) { | ||
| block.id.should.equal(mockBlock.id); | ||
| cb(); | ||
| }); | ||
| nm.start(); | ||
| }); | ||
|
|
||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you sort this list? A good practice is to separate it in 3 groups:
1 - Third party libraries
2 - Our external libraries (bitcore)
3 - Project specific clases
E.g: