trying to fix the mess I left from jsbeautify

This commit is contained in:
Manuel Araoz 2014-01-10 16:57:16 -03:00
parent 3c22a323c9
commit 3ab968eece
3 changed files with 276 additions and 276 deletions

View File

@ -7,206 +7,206 @@ require('classtool');
var isSyncTxEnabled = 0; var isSyncTxEnabled = 0;
function spec() { function spec() {
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var util = require('util'); var util = require('util');
var RpcClient = require('bitcore/RpcClient').class(); var RpcClient = require('bitcore/RpcClient').class();
var networks = require('bitcore/networks'); var networks = require('bitcore/networks');
var async = require('async'); var async = require('async');
var config = require('../config/config'); var config = require('../config/config');
var Block = require('../app/models/Block'); var Block = require('../app/models/Block');
var Transaction = require('../app/models/Transaction'); var Transaction = require('../app/models/Transaction');
function Sync(config) { function Sync(config) {
this.network = config.networkName === 'testnet' ? networks.testnet: networks.livenet; this.network = config.networkName === 'testnet' ? networks.testnet: networks.livenet;
} }
var progress_bar = function(string, current, total) { var progress_bar = function(string, current, total) {
console.log(util.format('\t%s %d/%d [%d%%]', string, current, total, parseInt(100 * current / total))); console.log(util.format('\t%s %d/%d [%d%%]', string, current, total, parseInt(100 * current / total)));
}; };
Sync.prototype.getNextBlock = function(blockHash, cb) { Sync.prototype.getNextBlock = function(blockHash, cb) {
var that = this; var that = this;
if (!blockHash) { if (!blockHash) {
return cb(); return cb();
} }
this.rpc.getBlock(blockHash, function(err, blockInfo) { this.rpc.getBlock(blockHash, function(err, blockInfo) {
if (err) return cb(err); if (err) return cb(err);
if (blockInfo.result.height % 1000 === 0) { if (blockInfo.result.height % 1000 === 0) {
var h = blockInfo.result.height, var h = blockInfo.result.height,
d = blockInfo.result.confirmations; d = blockInfo.result.confirmations;
progress_bar('height', h, h + d); progress_bar('height', h, h + d);
} }
that.storeBlock(blockInfo.result, function(err) { that.storeBlock(blockInfo.result, function(err) {
if (!err) { if (!err) {
var txs = blockInfo.result.tx; var txs = blockInfo.result.tx;
that.storeTxs(txs, function(err) { that.storeTxs(txs, function(err) {
if (!err) { if (!err) {
return that.getNextBlock(blockInfo.result.nextblockhash, cb); return that.getNextBlock(blockInfo.result.nextblockhash, cb);
} }
}); });
} }
}); });
}); });
}; };
Sync.prototype.storeBlock = function(block, cb) { Sync.prototype.storeBlock = function(block, cb) {
Block.create(block, function(err, inBlock) { Block.create(block, function(err, inBlock) {
// E11000 => already exists // E11000 => already exists
if (err && ! err.toString().match(/E11000/)) { if (err && ! err.toString().match(/E11000/)) {
return cb(err); return cb(err);
} }
cb(); cb();
}); });
}; };
Sync.prototype.storeTxs = function(txs, cb) { Sync.prototype.storeTxs = function(txs, cb) {
Transaction.createFromArray(txs, cb); Transaction.createFromArray(txs, cb);
}; };
Sync.prototype.syncBlocks = function(reindex, cb) { Sync.prototype.syncBlocks = function(reindex, cb) {
var that = this; var that = this;
var genesisHash = this.network.genesisBlock.hash.reverse().toString('hex'); var genesisHash = this.network.genesisBlock.hash.reverse().toString('hex');
console.log('Syncing Blocks... ' + reindex); console.log('Syncing Blocks... ' + reindex);
if (reindex) { if (reindex) {
return this.getNextBlock(genesisHash, cb); return this.getNextBlock(genesisHash, cb);
} }
Block.findOne({}, Block.findOne({},
{}, {},
{ {
sort: { sort: {
'time': - 1 'time': - 1
} }
}, },
function(err, block) { function(err, block) {
if (err) return cb(err); if (err) return cb(err);
var nextHash = block && block.hash ? block.hash: genesisHash; var nextHash = block && block.hash ? block.hash: genesisHash;
console.log('\tStarting at hash: ' + nextHash); console.log('\tStarting at hash: ' + nextHash);
return that.getNextBlock(nextHash, cb); return that.getNextBlock(nextHash, cb);
}); });
}; };
// This is not currently used. Transactions are represented by txid only // This is not currently used. Transactions are represented by txid only
// in mongodb // in mongodb
Sync.prototype.syncTXs = function(reindex, cb) { Sync.prototype.syncTXs = function(reindex, cb) {
var that = this; var that = this;
console.log('Syncing TXs...'); console.log('Syncing TXs...');
if (reindex) { if (reindex) {
// TODO? // TODO?
} }
Transaction.find({ Transaction.find({
blockhash: null blockhash: null
}, },
function(err, txs) { function(err, txs) {
if (err) return cb(err); if (err) return cb(err);
var read = 0; var read = 0;
var pull = 0; var pull = 0;
var write = 0; var write = 0;
var total = txs.length; var total = txs.length;
console.log('\tneed to pull %d txs', total); console.log('\tneed to pull %d txs', total);
if (!total) return cb(); if (!total) return cb();
async.each(txs, function(tx, next) { async.each(txs, function(tx, next) {
if (!tx.txid) { if (!tx.txid) {
console.log('NO TXID skipping...', tx); console.log('NO TXID skipping...', tx);
return next(); return next();
} }
if (read++ % 1000 === 0) progress_bar('read', read, total); if (read++ % 1000 === 0) progress_bar('read', read, total);
that.rpc.getRawTransaction(tx.txid, 1, function(err, txInfo) { that.rpc.getRawTransaction(tx.txid, 1, function(err, txInfo) {
if (pull++ % 1000 === 0) progress_bar('\tpull', pull, total); if (pull++ % 1000 === 0) progress_bar('\tpull', pull, total);
if (!err && txInfo) { if (!err && txInfo) {
Transaction.update({ Transaction.update({
txid: tx.txid txid: tx.txid
}, },
txInfo.result, function(err) { txInfo.result, function(err) {
if (err) return next(err); if (err) return next(err);
if (write++ % 1000 === 0) progress_bar('\t\twrite', write, total); if (write++ % 1000 === 0) progress_bar('\t\twrite', write, total);
return next(); return next();
}); });
} }
else return next(); else return next();
}); });
}, },
function(err) { function(err) {
if (err) return cb(err); if (err) return cb(err);
return cb(err); return cb(err);
}); });
}); });
}; };
Sync.prototype.init = function(opts) { Sync.prototype.init = function(opts) {
mongoose.connect(config.db); mongoose.connect(config.db);
this.db = mongoose.connection; this.db = mongoose.connection;
this.rpc = new RpcClient(config.bitcoind); this.rpc = new RpcClient(config.bitcoind);
this.db.on('error', console.error.bind(console, 'connection error:')); this.db.on('error', console.error.bind(console, 'connection error:'));
}; };
Sync.prototype.import_history = function(opts, next) { Sync.prototype.import_history = function(opts, next) {
var that = this; var that = this;
this.db.once('open', function() { this.db.once('open', function() {
async.series([ async.series([
function(cb) { function(cb) {
if (opts.destroy) { if (opts.destroy) {
console.log('Deleting Blocks...'); console.log('Deleting Blocks...');
that.db.collections.blocks.drop(cb); that.db.collections.blocks.drop(cb);
} else { } else {
cb(); cb();
} }
}, },
function(cb) { function(cb) {
if (opts.destroy) { if (opts.destroy) {
console.log('Deleting TXs...'); console.log('Deleting TXs...');
that.db.collections.transactions.drop(cb); that.db.collections.transactions.drop(cb);
} else { } else {
cb(); cb();
} }
}, },
function(cb) { function(cb) {
if (!opts.skip_blocks) { if (!opts.skip_blocks) {
that.syncBlocks(opts.reindex, cb); that.syncBlocks(opts.reindex, cb);
} else { } else {
cb(); cb();
} }
}, },
function(cb) { function(cb) {
if (isSyncTxEnabled && ! opts.skip_txs) { if (isSyncTxEnabled && ! opts.skip_txs) {
that.syncTXs(opts.reindex, cb); that.syncTXs(opts.reindex, cb);
} }
else { else {
return cb(); return cb();
} }
}], function(err) { }], function(err) {
return next(err); return next(err);
}); });
}); });
}; };
Sync.prototype.close = function() { Sync.prototype.close = function() {
console.log("closing connection"); console.log("closing connection");
this.db.close(); this.db.close();
}; };
return Sync; return Sync;
} }
module.defineClass(spec); module.defineClass(spec);

188
p2p.js
View File

@ -18,153 +18,153 @@ var peerdb = undefined;
var hdrdb = undefined; var hdrdb = undefined;
var network = networks.testnet; var network = networks.testnet;
var config = { var config = {
network: network.name network: network.name
}; };
var PeerManager = require('bitcore/PeerManager').createClass({ var PeerManager = require('bitcore/PeerManager').createClass({
config: config config: config
}); });
var Peer = require('bitcore/Peer').class(); var Peer = require('bitcore/Peer').class();
function peerdb_load() { function peerdb_load() {
try { try {
peerdb = JSON.parse(fs.readFileSync(peerdb_fn)); peerdb = JSON.parse(fs.readFileSync(peerdb_fn));
} catch(d) { } catch(d) {
console.warn('Unable to read peer db', peerdb_fn, 'creating new one.'); console.warn('Unable to read peer db', peerdb_fn, 'creating new one.');
peerdb = [{ peerdb = [{
ipv4: '127.0.0.1', ipv4: '127.0.0.1',
port: 18333 port: 18333
}, },
]; ];
fs.writeFileSync(peerdb_fn, JSON.stringify(peerdb)); fs.writeFileSync(peerdb_fn, JSON.stringify(peerdb));
} }
} }
function hdrdb_load() { function hdrdb_load() {
hdrdb = new HeaderDB({ hdrdb = new HeaderDB({
network: network network: network
}); });
} }
function get_more_headers(info) { function get_more_headers(info) {
var conn = info.conn; var conn = info.conn;
var loc = hdrdb.locator(); var loc = hdrdb.locator();
conn.sendGetHeaders(loc, coinUtil.NULL_HASH); conn.sendGetHeaders(loc, coinUtil.NULL_HASH);
} }
function add_header(info, block) { function add_header(info, block) {
var hashStr = coinUtil.formatHashFull(block.calcHash()); var hashStr = coinUtil.formatHashFull(block.calcHash());
try { try {
hdrdb.add(block); hdrdb.add(block);
} catch(e) { } catch(e) {
return; return;
} }
} }
function handle_headers(info) { function handle_headers(info) {
console.log('handle headers'); console.log('handle headers');
var headers = info.message.headers; var headers = info.message.headers;
headers.forEach(function(hdr) { headers.forEach(function(hdr) {
add_header(info, hdr); add_header(info, hdr);
}); });
// We persist the header DB after each batch // We persist the header DB after each batch
//hdrdb.writeFile(hdrdb_fn); //hdrdb.writeFile(hdrdb_fn);
// Only one request per batch of headers we receive. // Only one request per batch of headers we receive.
get_more_headers(info); get_more_headers(info);
} }
function handle_verack(info) { function handle_verack(info) {
var inv = { var inv = {
type: CoinConst.MSG.BLOCK, type: CoinConst.MSG.BLOCK,
hash: network.genesisBlock.hash, hash: network.genesisBlock.hash,
}; };
var invs = [inv]; var invs = [inv];
// Asks for the genesis block // Asks for the genesis block
// console.log('p2psync: Asking for the genesis block'); // console.log('p2psync: Asking for the genesis block');
// info.conn.sendGetData(invs); // info.conn.sendGetData(invs);
} }
function handle_inv(info) { function handle_inv(info) {
// TODO: should limit the invs to objects we haven't seen yet // TODO: should limit the invs to objects we haven't seen yet
var invs = info.message.invs; var invs = info.message.invs;
invs.forEach(function(inv) { invs.forEach(function(inv) {
console.log('Handle inv for a ' + CoinConst.MSG.to_str(inv.type)); console.log('Handle inv for a ' + CoinConst.MSG.to_str(inv.type));
}); });
info.conn.sendGetData(invs); info.conn.sendGetData(invs);
} }
var sync = new Sync({ var sync = new Sync({
networkName: networks.testnet networkName: networks.testnet
}); });
sync.init(); sync.init();
function handle_tx(info) { function handle_tx(info) {
var tx = info.message.tx.getStandardizedObject(); var tx = info.message.tx.getStandardizedObject();
console.log('Handle tx: ' + tx.hash); console.log('Handle tx: ' + tx.hash);
sync.storeTxs([tx.hash], function(err) { sync.storeTxs([tx.hash], function(err) {
if (err) { if (err) {
console.log('error in handle TX: ' + err); console.log('error in handle TX: ' + err);
} }
}); });
} }
function handle_block(info) { function handle_block(info) {
var block = info.message.block; var block = info.message.block;
var now = Math.round(new Date().getTime() / 1000); var now = Math.round(new Date().getTime() / 1000);
var blockHash = coinUtil.formatHashFull(block.calcHash()); var blockHash = coinUtil.formatHashFull(block.calcHash());
console.log('Handle block: ' + blockHash); console.log('Handle block: ' + blockHash);
sync.storeBlock({ sync.storeBlock({
'hash': blockHash, 'hash': blockHash,
'time': now 'time': now
}, },
function(err) { function(err) {
if (err) { if (err) {
console.log('error in handle Block: ' + err); console.log('error in handle Block: ' + err);
} else { } else {
var hashes = block.txs.map(function(tx) { var hashes = block.txs.map(function(tx) {
return coinUtil.formatHashFull(tx.hash); return coinUtil.formatHashFull(tx.hash);
}); });
sync.storeTxs(hashes, function() {}); sync.storeTxs(hashes, function() {});
} }
}); });
} }
function handle_connected(data) { function handle_connected(data) {
var peerman = data.pm; var peerman = data.pm;
var peers_n = peerman.peers.length; var peers_n = peerman.peers.length;
console.log('p2psync: Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': '')); console.log('p2psync: Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's': ''));
} }
function p2psync() { function p2psync() {
var peerman = new PeerManager(); var peerman = new PeerManager();
peerdb.forEach(function(datum) { peerdb.forEach(function(datum) {
var peer = new Peer(datum.ipv4, datum.port); var peer = new Peer(datum.ipv4, datum.port);
peerman.addPeer(peer); peerman.addPeer(peer);
}); });
peerman.on('connection', function(conn) { peerman.on('connection', function(conn) {
conn.on('verack', handle_verack); conn.on('verack', handle_verack);
conn.on('block', handle_block); conn.on('block', handle_block);
conn.on('headers', handle_headers); conn.on('headers', handle_headers);
conn.on('inv', handle_inv); conn.on('inv', handle_inv);
conn.on('tx', handle_tx); conn.on('tx', handle_tx);
}); });
peerman.on('connect', handle_connected); peerman.on('connect', handle_connected);
peerman.start(); peerman.start();
} }
function main() { function main() {
peerdb_load(); peerdb_load();
hdrdb_load(); hdrdb_load();
p2psync(); p2psync();
} }
main(); main();

View File

@ -1,4 +1,4 @@
# ! /usr/bin / env node #! /usr/bin/env node
'use strict'; 'use strict';
@ -14,7 +14,7 @@ var async = require('async');
program.version(SYNC_VERSION).option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet').option('-R --reindex', 'Force reindexing', '0').option('-D --destroy', 'Remove current DB', '0').option('--skip_blocks', 'Sync blocks').option('--skip_txs', 'Sync transactions').parse(process.argv); program.version(SYNC_VERSION).option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet').option('-R --reindex', 'Force reindexing', '0').option('-D --destroy', 'Remove current DB', '0').option('--skip_blocks', 'Sync blocks').option('--skip_txs', 'Sync transactions').parse(process.argv);
var sync = new Sync({ var sync = new Sync({
networkName: program.network networkName: program.network
}); });
if (program.remove) { if (program.remove) {
@ -23,22 +23,22 @@ if (program.remove) {
async.series([ async.series([
function(cb) { function(cb) {
sync.init(program); sync.init(program);
cb(); cb();
}, },
function(cb) { function(cb) {
sync.import_history(program, function(err) { sync.import_history(program, function(err) {
if (err) { if (err) {
console.log('CRITICAL ERROR: ', err); console.log('CRITICAL ERROR: ', err);
} }
else { else {
console.log('Done!'); console.log('Done!');
} }
cb(); cb();
}); });
}, },
function(cb) { function(cb) {
sync.close(); sync.close();
cb(); cb();
}]); }]);