Merge pull request #35 from matiu/bug/01sync

fixes sync.js, to work after p2p block
This commit is contained in:
Gustavo Maximiliano Cortez 2014-01-14 04:17:00 -08:00
commit da0383d9d2
4 changed files with 20 additions and 24 deletions

View File

@ -23,6 +23,7 @@ var BlockSchema = new Schema({
unique: true,
},
time: Number,
fromP2P: Boolean,
});

View File

@ -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) {

View File

@ -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();
}

View File

@ -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);