finished tests for PeerSync.js
This commit is contained in:
parent
aeb95aa427
commit
62d469be0c
|
@ -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) {
|
||||||
|
if (self.verbose) {
|
||||||
console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type));
|
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,19 +66,22 @@ 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();
|
||||||
|
if (this.verbose) {
|
||||||
console.log('[p2p_sync] Handle tx: ' + tx.hash);
|
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));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
PeerSync.prototype.handle_block = function(info) {
|
PeerSync.prototype.handle_block = function(info) {
|
||||||
var self = this;
|
|
||||||
var block = info.message.block;
|
var block = info.message.block;
|
||||||
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
var blockHash = coinUtil.formatHashFull(block.calcHash());
|
||||||
|
if (this.verbose) {
|
||||||
console.log('[p2p_sync] Handle block: ' + blockHash);
|
console.log('[p2p_sync] Handle block: ' + blockHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var tx_hashes = block.txs.map(function(tx) {
|
var tx_hashes = block.txs.map(function(tx) {
|
||||||
|
@ -94,26 +103,27 @@ 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;
|
||||||
|
if (this.verbose) {
|
||||||
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': ''));
|
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() {
|
PeerSync.prototype.close = function() {
|
||||||
|
|
|
@ -78,13 +78,15 @@ function spec() {
|
||||||
that.opts = opts;
|
that.opts = opts;
|
||||||
|
|
||||||
if (!(opts && opts.skip_db_connection)) {
|
if (!(opts && opts.skip_db_connection)) {
|
||||||
|
if (!mongoose.connection) {
|
||||||
mongoose.connect(config.db, {server: {auto_reconnect: true}} );
|
mongoose.connect(config.db, {server: {auto_reconnect: true}} );
|
||||||
|
}
|
||||||
|
|
||||||
this.db = mongoose.connection;
|
this.db = mongoose.connection;
|
||||||
|
|
||||||
this.db.on('error', function(err) {
|
this.db.on('error', function(err) {
|
||||||
console.log('connection error:' + err);
|
console.log('connection error:' + err);
|
||||||
moogose.disconnect();
|
mongoose.disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.db.on('disconnect', function(err) {
|
this.db.on('disconnect', function(err) {
|
||||||
|
@ -99,7 +101,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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,7 +9,7 @@ describe('PeerSync', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ps = new PeerSync();
|
ps = new PeerSync();
|
||||||
ps.init();
|
ps.init({verbose: false});
|
||||||
});
|
});
|
||||||
afterEach(function(){
|
afterEach(function(){
|
||||||
ps.close();
|
ps.close();
|
||||||
|
@ -42,22 +42,37 @@ describe('PeerSync', function() {
|
||||||
describe('#handle_tx()', function() {
|
describe('#handle_tx()', function() {
|
||||||
var tx_info = {
|
var tx_info = {
|
||||||
message: { tx: {getStandardizedObject: function(){
|
message: { tx: {getStandardizedObject: function(){
|
||||||
return {hash: '00000000e3fe5b3b5416374d8d65560a0792a6da71546d67b00c9d37e8a4cf59'};}}}
|
return {hash: 'dac28b5c5e70c16942718f3a22438348c1b709e01d398795fce8fc455178b973'};}}}
|
||||||
};
|
};
|
||||||
it('should call storeTxs', function(){
|
it('should call storeTxs', function(){
|
||||||
var spy = sinon.spy(ps.sync, 'storeTxs');
|
var spy = sinon.spy(ps.sync, 'storeTxs');
|
||||||
ps.handle_tx(tx_info);
|
ps.handle_tx(tx_info);
|
||||||
expect(spy.calledOnce);
|
expect(spy.calledOnce).to.be.ok;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#handle_block()', function() {
|
describe('#handle_block()', function() {
|
||||||
it('should call storeBlock');
|
var block_info = {
|
||||||
it('should call storeTxs for each transaction');
|
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() {
|
describe('#run()', function() {
|
||||||
it('should setup peerman');
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue