Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from maraoz/add/NetworkMonitor
Add NetworkMonitor class
- Loading branch information
Showing
15 changed files
with
806 additions
and
39,388 deletions.
There are no files selected for viewing
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,29 @@ | ||
'use strict'; | ||
|
||
var run = function() { | ||
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. | ||
var bitcore = require('../bitcore'); | ||
var NetworkMonitor = bitcore.NetworkMonitor; | ||
|
||
var config = { | ||
networkName: 'testnet', | ||
host: 'localhost', | ||
port: 18333 | ||
}; | ||
|
||
|
||
var nm = new NetworkMonitor.create(config); | ||
// monitor incoming transactions to http://tpfaucet.appspot.com/ donation address | ||
nm.incoming('msj42CCGruhRsFrGATiUuh25dtxYtnpbTx', function(tx) { | ||
console.log('Donation to tpfaucet! '+JSON.stringify(tx.getStandardizedObject())); | ||
}); | ||
|
||
// connect to bitcoin network and start listening | ||
nm.start(); | ||
|
||
}; | ||
|
||
module.exports.run = run; | ||
if (require.main === module) { | ||
run(); | ||
} |
This file contains 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 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 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,82 @@ | ||
var log = require('../util/log'); | ||
var networks = require('../networks'); | ||
var Address = require('./Address'); | ||
var Peer = require('./Peer'); | ||
var PeerManager = require('./PeerManager'); | ||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var preconditions = require('preconditions').singleton(); | ||
|
||
var NetworkMonitor = function(peerman) { | ||
preconditions.checkArgument(peerman); | ||
this.peerman = peerman; | ||
this.networkName = peerman.config.network; | ||
this.init(); | ||
} | ||
|
||
util.inherits(NetworkMonitor, EventEmitter); | ||
|
||
NetworkMonitor.create = function(config) { | ||
var peerman = new PeerManager({ | ||
network: config.networkName | ||
}); | ||
|
||
peerman.addPeer(new Peer(config.host, config.port)); | ||
return new NetworkMonitor(peerman); | ||
}; | ||
|
||
NetworkMonitor.prototype.init = function() { | ||
var self = this; | ||
var handleInv = function(info) { | ||
var invs = info.message.invs; | ||
info.conn.sendGetData(invs); | ||
}; | ||
|
||
var handleBlock = function(info) { | ||
self.emit('block', info.message); | ||
}; | ||
|
||
var handleTx = function(info) { | ||
var tx = info.message.tx; | ||
self.emit('tx', tx); | ||
|
||
var from = tx.getSendingAddresses(self.networkName); | ||
for (var i = 0; i < from.length; i++) { | ||
var addr = from[i]; | ||
self.emit('out:'+addr, tx); | ||
} | ||
var to = tx.getReceivingAddresses(self.networkName); | ||
for (var i = 0; i < to.length; i++) { | ||
var addr = to[i]; | ||
self.emit('in:'+addr, tx); | ||
} | ||
}; | ||
|
||
this.peerman.on('connection', function(conn) { | ||
if (self.connection) throw new Error('Cant handle more than one connection'); | ||
self.connection = conn; | ||
conn.on('inv', handleInv); | ||
conn.on('block', handleBlock); | ||
conn.on('tx', handleTx); | ||
}); | ||
}; | ||
|
||
NetworkMonitor.prototype.incoming = function(addrStr, callback) { | ||
preconditions.checkArgument(Address.validate(addrStr)); | ||
this.on('in:'+addrStr, callback); | ||
}; | ||
|
||
NetworkMonitor.prototype.outgoing = function(addrStr, callback) { | ||
preconditions.checkArgument(Address.validate(addrStr)); | ||
this.on('out:'+addrStr, callback); | ||
}; | ||
|
||
NetworkMonitor.prototype.start = function() { | ||
this.peerman.start(); | ||
}; | ||
|
||
NetworkMonitor.prototype.stop = function() { | ||
this.peerman.stop(); | ||
}; | ||
|
||
module.exports = NetworkMonitor; |
This file contains 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 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 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 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 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
Oops, something went wrong.