From 9ab4831c8b57ada20a5f11281771e06ccd758c63 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Jan 2014 07:58:01 -0300 Subject: [PATCH] fixes sync.js, to work after p2p block --- app/models/Block.js | 1 + lib/PeerSync.js | 3 ++- lib/Sync.js | 37 ++++++++++++++++--------------------- util/sync.js | 3 +-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/app/models/Block.js b/app/models/Block.js index 7374943..19e6d53 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -23,6 +23,7 @@ var BlockSchema = new Schema({ unique: true, }, time: Number, + fromP2P: Boolean, }); diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 573c402..14d3acd 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -74,7 +74,8 @@ function spec() { console.log('[p2p_sync] Handle block: ' + blockHash); this.sync.storeBlock({ 'hash': blockHash, - 'time': now + 'time': now, + 'fromP2P': true, }, function(err) { if (err) { diff --git a/lib/Sync.js b/lib/Sync.js index b3dfc4a..6b4fe0c 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -36,27 +36,27 @@ function spec() { progress_bar(util.format('Height [txs:%d]',that.tx_count), h, h + d); } - that.storeBlock(blockInfo.result, function(err) { + that.storeBlock(blockInfo.result, function(err, existed) { + if (!err) { var txs = blockInfo.result.tx; that.storeTxs(txs, function(err) { - if (!err) { + if (!err) return that.getNextBlock(blockInfo.result.nextblockhash, cb); - } }); } + else { + if (err.toString().match(/E11000/)) + return that.getNextBlock(blockInfo.result.nextblockhash, cb); + else + return cb(err); + } }); }); }; Sync.prototype.storeBlock = function(block, cb) { - Block.create(block, function(err, inBlock) { - // E11000 => already exists - if (err && ! err.toString().match(/E11000/)) { - return cb(err); - } - cb(); - }); + Block.create(block, cb); }; Sync.prototype.storeTxs = function(txids, cb) { @@ -78,16 +78,14 @@ function spec() { }); }; - Sync.prototype.syncBlocks = function(reindex, cb) { + Sync.prototype.syncBlocks = function( cb) { var that = this; var genesisHash = this.network.genesisBlock.hash.reverse().toString('hex'); - console.log('Syncing Blocks... ' + reindex); - if (reindex) { - return this.getNextBlock(genesisHash, cb); - } + console.log('Syncing Blocks... ' ); - Block.findOne({}, + Block.findOne( + { 'fromP2P':{$in:[null, false]} }, {}, { sort: { @@ -106,14 +104,11 @@ function spec() { // This is not currently used. Transactions are represented by txid only // in mongodb - Sync.prototype.syncTXs = function(reindex, cb) { + Sync.prototype.syncTXs = function(cb) { var that = this; console.log('Syncing TXs...'); - if (reindex) { - // TODO? - } Transaction.find({ blockhash: null @@ -246,7 +241,7 @@ function spec() { function(cb) { if (!opts.skip_blocks) { - that.syncBlocks(opts.reindex, cb); + that.syncBlocks( cb); } else { cb(); } diff --git a/util/sync.js b/util/sync.js index 344c8a7..3d69804 100755 --- a/util/sync.js +++ b/util/sync.js @@ -14,8 +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('-D --destroy', 'Remove current DB (and start from there)', '0') .option('--skip_blocks', 'Sync blocks') .option('--skip_txs', 'Sync transactions') .parse(process.argv);