Merge pull request #71 from maraoz/test/test-p2p-sync

testing p2p sync
This commit is contained in:
Manuel Aráoz 2014-01-17 05:35:45 -08:00
commit 4b1fbf8877
5 changed files with 112 additions and 30 deletions

View File

@ -32,7 +32,6 @@
"afterEach", "afterEach",
"it", "it",
"inject", "inject",
"expect",
"$", "$",
"io", "io",
"app", "app",

View File

@ -14,22 +14,25 @@ function spec() {
PeerSync.prototype.init = function(config, cb) { PeerSync.prototype.init = function(config, cb) {
if (!config) config = {};
var that = this; var that = this;
var network = config && (config.network || 'testnet'); var network = config && (config.network || 'testnet');
that.peerdb = undefined; this.verbose = config.verbose;
that.sync = new Sync({ this.peerdb = undefined;
this.sync = new Sync({
networkName: network networkName: network
}); });
that.sync.init(config, function() {
that.PeerManager = require('bitcore/PeerManager').createClass({ this.PeerManager = require('bitcore/PeerManager').createClass({
config: { config: {
network: network network: network
} }
}); });
that.load_peers(); this.peerman = new this.PeerManager();
this.load_peers();
this.sync.init(config, function() {
return cb(); return cb();
}); });
}; };
@ -50,9 +53,12 @@ function spec() {
}; };
PeerSync.prototype.handle_inv = function(info) { PeerSync.prototype.handle_inv = function(info) {
var self = this;
var invs = info.message.invs; var invs = info.message.invs;
invs.forEach(function(inv) { invs.forEach(function(inv) {
console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type)); if (self.verbose) {
console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type));
}
}); });
// TODO: should limit the invs to objects we haven't seen yet // TODO: should limit the invs to objects we haven't seen yet
info.conn.sendGetData(invs); info.conn.sendGetData(invs);
@ -60,10 +66,12 @@ function spec() {
PeerSync.prototype.handle_tx = function(info) { PeerSync.prototype.handle_tx = function(info) {
var tx = info.message.tx.getStandardizedObject(); var tx = info.message.tx.getStandardizedObject();
console.log('[p2p_sync] Handle tx: ' + tx.hash); if (this.verbose) {
console.log('[p2p_sync] Handle tx: ' + tx.hash);
}
this.sync.storeTxs([tx.hash], null, function(err) { this.sync.storeTxs([tx.hash], null, function(err) {
if (err) { if (err) {
console.log('[p2p_sync] Error in handle TX: ' + err); console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
} }
}); });
}; };
@ -71,7 +79,9 @@ function spec() {
PeerSync.prototype.handle_block = function(info) { PeerSync.prototype.handle_block = function(info) {
var block = info.message.block; var block = info.message.block;
var blockHash = coinUtil.formatHashFull(block.calcHash()); var blockHash = coinUtil.formatHashFull(block.calcHash());
console.log('[p2p_sync] Handle block: ' + blockHash); if (this.verbose) {
console.log('[p2p_sync] Handle block: ' + blockHash);
}
var tx_hashes = block.txs.map(function(tx) { var tx_hashes = block.txs.map(function(tx) {
@ -93,27 +103,34 @@ function spec() {
PeerSync.prototype.handle_connected = function(data) { PeerSync.prototype.handle_connected = function(data) {
var peerman = data.pm; var peerman = data.pm;
var peers_n = peerman.peers.length; var peers_n = peerman.peers.length;
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': '')); if (this.verbose) {
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': ''));
}
}; };
PeerSync.prototype.run = function() { PeerSync.prototype.run = function() {
var self = this; var self = this;
var peerman = new this.PeerManager();
this.peerdb.forEach(function(datum) { this.peerdb.forEach(function(datum) {
var peer = new Peer(datum.ipv4, datum.port); var peer = new Peer(datum.ipv4, datum.port);
peerman.addPeer(peer); self.peerman.addPeer(peer);
}); });
peerman.on('connection', function(conn) { this.peerman.on('connection', function(conn) {
conn.on('inv', self.handle_inv.bind(self)); conn.on('inv', self.handle_inv.bind(self));
conn.on('block', self.handle_block.bind(self)); conn.on('block', self.handle_block.bind(self));
conn.on('tx', self.handle_tx.bind(self)); conn.on('tx', self.handle_tx.bind(self));
}); });
peerman.on('connect', self.handle_connected.bind(self)); this.peerman.on('connect', self.handle_connected.bind(self));
peerman.start(); this.peerman.start();
}; };
PeerSync.prototype.close = function() {
this.sync.close();
};
return PeerSync; return PeerSync;
} }

View File

@ -73,7 +73,9 @@ function spec() {
that.opts = opts; that.opts = opts;
if (!(opts && opts.skip_db_connection)) { if (!(opts && opts.skip_db_connection)) {
mongoose.connect(config.db, {server: {auto_reconnect: true}} ); if (!mongoose.connection) {
mongoose.connect(config.db, {server: {auto_reconnect: true}} );
}
this.db = mongoose.connection; this.db = mongoose.connection;
@ -94,7 +96,6 @@ function spec() {
Sync.prototype.close = function() { Sync.prototype.close = function() {
if (!(this.opts && this.opts.skip_db_connection)) { if (!(this.opts && this.opts.skip_db_connection)) {
console.log('closing connection');
this.db.close(); this.db.close();
} }
}; };

View File

@ -74,7 +74,8 @@
"view-helpers": "latest", "view-helpers": "latest",
"socket.io": "~0.9.16", "socket.io": "~0.9.16",
"moment": "~2.5.0", "moment": "~2.5.0",
"sinon": "~1.7.3" "sinon": "~1.7.3",
"chai": "~1.8.1"
}, },
"devDependencies": { "devDependencies": {
"grunt-contrib-watch": "latest", "grunt-contrib-watch": "latest",

View File

@ -1,13 +1,77 @@
'use strict'; 'use strict';
var assert = require('assert'); var chai = require('chai'),
expect = chai.expect,
sinon = require('sinon');
var PeerSync = require('../../lib/PeerSync.js').class(); var PeerSync = require('../../lib/PeerSync.js').class();
describe('Unit testing PeerSync', function() { describe('PeerSync', function() {
var ps = new PeerSync(); var ps;
beforeEach(function() {
ps = new PeerSync();
ps.init({verbose: false});
});
afterEach(function(){
ps.close();
});
describe('#init()', function() { describe('#init()', function() {
it('should return with no errors', function() { it('should return with no errors', function() {
assert.doesNotThrow(function(){ var other_ps = new PeerSync();
ps.init(); expect(other_ps.init.bind(other_ps)).not.to.throw(Error);
}); other_ps.close();
});
});
describe('#handle_inv()', function() {
var inv_info = {
message: {invs: []},
conn: {sendGetData: sinon.spy()}
};
it('should return with no errors', function(){
expect(function() {
ps.handle_inv(inv_info);
}).not.to.throw(Error);
});
it('should call sendGetData', function() {
ps.handle_inv(inv_info);
expect(inv_info.conn.sendGetData.calledTwice).to.be.ok;
});
});
describe('#handle_tx()', function() {
var tx_info = {
message: { tx: {getStandardizedObject: function(){
return {hash: 'dac28b5c5e70c16942718f3a22438348c1b709e01d398795fce8fc455178b973'};}}}
};
it('should call storeTxs', function(){
var spy = sinon.spy(ps.sync, 'storeTxs');
ps.handle_tx(tx_info);
expect(spy.calledOnce).to.be.ok;
});
});
describe('#handle_block()', function() {
var block_info = {
message: { block: {calcHash: function(){
return new Buffer('01234');
}, txs: [{hash: new Buffer('cabafeca')}, {hash: new Buffer('bacacafe')}]}}
};
it('should call storeBlock', function(){
var spy = sinon.spy(ps.sync, 'storeBlock');
ps.handle_block(block_info);
expect(spy.calledOnce).to.be.ok;
});
});
describe('#run()', function() {
it('should setup peerman', function() {
var startSpy = sinon.spy(ps.peerman, 'start');
var onSpy = sinon.spy(ps.peerman, 'on');
ps.run();
expect(startSpy.called).to.be.ok;
expect(onSpy.called).to.be.ok;
}); });
}); });
}); });