insight-ui-zcash/lib/PeerSync.js

138 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
require('classtool');
function spec() {
var fs = require('fs');
var CoinConst = require('bitcore/const');
var coinUtil = require('bitcore/util/util');
var Sync = require('./Sync').class();
var Peer = require('bitcore/Peer').class();
var peerdb_fn = 'peerdb.json';
function PeerSync() {}
2014-01-16 11:11:20 -08:00
PeerSync.prototype.init = function(config, cb) {
2014-01-16 16:08:50 -08:00
if (!config) config = {};
2014-01-15 11:05:26 -08:00
var network = config && (config.network || 'testnet');
2014-01-16 16:08:50 -08:00
this.verbose = config.verbose;
this.peerdb = undefined;
this.sync = new Sync({
networkName: network
});
2014-01-16 16:08:50 -08:00
this.PeerManager = require('bitcore/PeerManager').createClass({
config: {
network: network
}
});
this.peerman = new this.PeerManager();
this.load_peers();
this.sync.init(config, function() {
2014-01-16 11:11:20 -08:00
return cb();
});
};
PeerSync.prototype.load_peers = function() {
try {
this.peerdb = JSON.parse(fs.readFileSync(peerdb_fn));
} catch(d) {
console.warn('Unable to read peer db', peerdb_fn, 'creating new one.');
this.peerdb = [{
ipv4: '127.0.0.1',
port: 18333
},
];
2014-01-13 13:23:27 -08:00
fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb));
}
};
PeerSync.prototype.handle_inv = function(info) {
2014-01-16 16:08:50 -08:00
var self = this;
var invs = info.message.invs;
invs.forEach(function(inv) {
2014-01-16 16:08:50 -08:00
if (self.verbose) {
console.log('[p2p_sync] Handle inv for a ' + CoinConst.MSG.to_str(inv.type));
}
});
2014-01-15 05:32:18 -08:00
// TODO: should limit the invs to objects we haven't seen yet
info.conn.sendGetData(invs);
};
PeerSync.prototype.handle_tx = function(info) {
var tx = info.message.tx.getStandardizedObject();
2014-01-16 16:08:50 -08:00
if (this.verbose) {
console.log('[p2p_sync] Handle tx: ' + tx.hash);
}
2014-01-16 08:01:32 -08:00
this.sync.storeTxs([tx.hash], null, function(err) {
if (err) {
console.log('[PeerSync.js.71:err:]',err); //TODO
2014-01-16 16:08:50 -08:00
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
}
});
};
PeerSync.prototype.handle_block = function(info) {
var block = info.message.block;
var blockHash = coinUtil.formatHashFull(block.calcHash());
2014-01-16 16:08:50 -08:00
if (this.verbose) {
console.log('[p2p_sync] Handle block: ' + blockHash);
}
2014-01-15 12:36:49 -08:00
var tx_hashes = block.txs.map(function(tx) {
return coinUtil.formatHashFull(tx.hash);
});
this.sync.storeBlock({
'hash': blockHash,
2014-01-15 12:36:49 -08:00
'tx': tx_hashes,
2014-01-16 08:01:32 -08:00
// TODO NEXT BLOCK / PREV BLOCK?
},
function(err) {
if (err) {
console.log('[p2p_sync] Error in handle Block: ' + err);
}
});
};
PeerSync.prototype.handle_connected = function(data) {
var peerman = data.pm;
var peers_n = peerman.peers.length;
2014-01-16 16:08:50 -08:00
if (this.verbose) {
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': ''));
}
};
PeerSync.prototype.run = function() {
var self = this;
this.peerdb.forEach(function(datum) {
var peer = new Peer(datum.ipv4, datum.port);
2014-01-16 16:08:50 -08:00
self.peerman.addPeer(peer);
});
2014-01-16 16:08:50 -08:00
this.peerman.on('connection', function(conn) {
conn.on('inv', self.handle_inv.bind(self));
conn.on('block', self.handle_block.bind(self));
conn.on('tx', self.handle_tx.bind(self));
});
2014-01-16 16:08:50 -08:00
this.peerman.on('connect', self.handle_connected.bind(self));
2014-01-16 16:08:50 -08:00
this.peerman.start();
};
2014-01-16 11:12:32 -08:00
PeerSync.prototype.close = function() {
this.sync.close();
};
return PeerSync;
}
module.defineClass(spec);