Merge pull request #35 from matiu/bug/01sync
fixes sync.js, to work after p2p block
This commit is contained in:
commit
da0383d9d2
|
@ -23,6 +23,7 @@ var BlockSchema = new Schema({
|
||||||
unique: true,
|
unique: true,
|
||||||
},
|
},
|
||||||
time: Number,
|
time: Number,
|
||||||
|
fromP2P: Boolean,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,8 @@ function spec() {
|
||||||
console.log('[p2p_sync] Handle block: ' + blockHash);
|
console.log('[p2p_sync] Handle block: ' + blockHash);
|
||||||
this.sync.storeBlock({
|
this.sync.storeBlock({
|
||||||
'hash': blockHash,
|
'hash': blockHash,
|
||||||
'time': now
|
'time': now,
|
||||||
|
'fromP2P': true,
|
||||||
},
|
},
|
||||||
function(err) {
|
function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
37
lib/Sync.js
37
lib/Sync.js
|
@ -36,27 +36,27 @@ function spec() {
|
||||||
progress_bar(util.format('Height [txs:%d]',that.tx_count), h, h + d);
|
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) {
|
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);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if (err.toString().match(/E11000/))
|
||||||
|
return that.getNextBlock(blockInfo.result.nextblockhash, cb);
|
||||||
|
else
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Sync.prototype.storeBlock = function(block, cb) {
|
Sync.prototype.storeBlock = function(block, cb) {
|
||||||
Block.create(block, function(err, inBlock) {
|
Block.create(block, cb);
|
||||||
// E11000 => already exists
|
|
||||||
if (err && ! err.toString().match(/E11000/)) {
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Sync.prototype.storeTxs = function(txids, 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 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... ' );
|
||||||
if (reindex) {
|
|
||||||
return this.getNextBlock(genesisHash, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
Block.findOne({},
|
Block.findOne(
|
||||||
|
{ 'fromP2P':{$in:[null, false]} },
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
sort: {
|
sort: {
|
||||||
|
@ -106,14 +104,11 @@ function spec() {
|
||||||
|
|
||||||
// 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(cb) {
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
console.log('Syncing TXs...');
|
console.log('Syncing TXs...');
|
||||||
if (reindex) {
|
|
||||||
// TODO?
|
|
||||||
}
|
|
||||||
|
|
||||||
Transaction.find({
|
Transaction.find({
|
||||||
blockhash: null
|
blockhash: null
|
||||||
|
@ -246,7 +241,7 @@ function spec() {
|
||||||
|
|
||||||
function(cb) {
|
function(cb) {
|
||||||
if (!opts.skip_blocks) {
|
if (!opts.skip_blocks) {
|
||||||
that.syncBlocks(opts.reindex, cb);
|
that.syncBlocks( cb);
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,7 @@ var async = require('async');
|
||||||
program
|
program
|
||||||
.version(SYNC_VERSION)
|
.version(SYNC_VERSION)
|
||||||
.option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet')
|
.option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet')
|
||||||
.option('-R --reindex', 'Force reindexing', '0')
|
.option('-D --destroy', 'Remove current DB (and start from there)', '0')
|
||||||
.option('-D --destroy', 'Remove current DB', '0')
|
|
||||||
.option('--skip_blocks', 'Sync blocks')
|
.option('--skip_blocks', 'Sync blocks')
|
||||||
.option('--skip_txs', 'Sync transactions')
|
.option('--skip_txs', 'Sync transactions')
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
Loading…
Reference in New Issue