From ca28ed62b4fe153af7897268cb8505ccdf530470 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 8 Jan 2014 01:37:29 -0300 Subject: [PATCH] transaction syncing working! --- Sync.js | 115 ++++++++++++++++++++++++++++++++++---- app/models/Transaction.js | 6 +- util/sync.js | 9 ++- 3 files changed, 112 insertions(+), 18 deletions(-) diff --git a/Sync.js b/Sync.js index 287f8f80..788dbddd 100644 --- a/Sync.js +++ b/Sync.js @@ -24,9 +24,7 @@ function spec(b) { } this.rpc.getBlock(blockHash, function(err, blockInfo) { - if (err) { - return cb(err); - } + if (err) return cb(err); if ( ! ( blockInfo.result.height % 1000) ) { var h = blockInfo.result.height, @@ -57,6 +55,7 @@ function spec(b) { var that = this; var genesisHash = this.network.genesisBlock.hash.reverse().toString('hex'); + console.log("Syncing Blocks..."); if (reindex) return this.getNextBlock(genesisHash, cb); @@ -71,12 +70,76 @@ function spec(b) { ; - console.log('Starting at hash: ' + nextHash); + console.log('\tStarting at hash: ' + nextHash); return that.getNextBlock(nextHash, cb); }); } + var progress_bar = function(string, current, total) { + console.log( util.format("\t%s %d/%d [%d%%]", + string, current, total, parseInt(100 * current/total)) + ); + } + + Sync.prototype.syncTXs = function (reindex, cb) { + + var that = this; + + console.log("Syncing TXs..."); + if (reindex) { + // TODO? + } + + + Transaction.find({blockHash: null}, function(err, txs) { + if (err) return cb(err); + + var read = 0; + var pull = 0; + var write = 0; + var total = txs.length; + console.log("\tneed to pull %d txs", total); + + if (!total) return cb(); + + async.each(txs, + function(tx, next){ + if (! tx.txid) { + console.log("NO TXID skipping...", tx); + return next(); + } + + if ( ! ( read++ % 1000) ) + progress_bar('read', read, total); + + + that.rpc.getRawTransaction(tx.txid, 1, function(err, txInfo) { + + if ( ! ( pull++ % 1000) ) + progress_bar('\tpull', pull, total); + + if (!err && txInfo) { + Transaction.update({txid: tx.txid}, txInfo.result, function(err) { + if (err) return next(err); + + if ( ! ( write++ % 1000) ) + progress_bar('\t\twrite', write, total); + + return next(); + }); + } + else return next(); + }); + }, + function(err){ + if (err) return cb(err); + return cb(err); + } + ); + }); + } + Sync.prototype.start = function (opts, next) { @@ -106,18 +169,46 @@ function spec(b) { return cb(); }, function(cb) { - that.syncBlocks(opts.reindex, function(err) { - if (err) { - return cb(err); - } - db.close(); + if (! opts.skip_blocks) { + that.syncBlocks(opts.reindex, function(err) { + if (err) { + return cb(err); + + } + console.log("\tBlocks done."); + + return cb(); + }); + } + else { return cb(); - }); - } + } + }, + function(cb) { + if (! opts.skip_txs) { + that.syncTXs(opts.reindex, function(err) { + if (err) { + return cb(err); + + } + return cb(); + }); + } + else { + return cb(); + } + }, + function(cb) { + db.close(); + return cb(); + }, ], function(err) { - if (err) return next(er); + if (err) { + db.close(); + return next(err); + } return next(); }); }); diff --git a/app/models/Transaction.js b/app/models/Transaction.js index fcd1240e..566a480a 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -25,7 +25,11 @@ var TransactionSchema = new Schema({ type: Array, default: [], }, - blockhash: String, + blockhash: { + type: String, + index: true, + default: null, + }, confirmations: Number, time: Number, blocktime: Number, diff --git a/util/sync.js b/util/sync.js index 9a2bbbf8..22d1a53d 100755 --- a/util/sync.js +++ b/util/sync.js @@ -13,6 +13,8 @@ program .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({ networkName: program.network }); @@ -21,12 +23,9 @@ if (program.remove) { } -sync.start({ - reindex: program.reindex, - destroy: program.destroy, -}, function(err){ +sync.start( program, function(err){ if (err) { - console.log(err); + console.log("CRITICAL ERROR: ", err); } else { console.log('Done!');