Fix sync process by @matiu
This commit is contained in:
parent
cb1861f10f
commit
0779dd6da7
|
@ -16,6 +16,7 @@ function BlockExtractor(dataDir, network) {
|
||||||
self.dataDir = dataDir;
|
self.dataDir = dataDir;
|
||||||
self.files = glob.sync(path);
|
self.files = glob.sync(path);
|
||||||
self.nfiles = self.files.length;
|
self.nfiles = self.files.length;
|
||||||
|
self.errorCount =0;
|
||||||
|
|
||||||
if (self.nfiles === 0)
|
if (self.nfiles === 0)
|
||||||
throw new Error('Could not find block files at: ' + path);
|
throw new Error('Could not find block files at: ' + path);
|
||||||
|
@ -97,30 +98,41 @@ BlockExtractor.prototype.getNextBlock = function(cb) {
|
||||||
|
|
||||||
async.whilst(
|
async.whilst(
|
||||||
function() {
|
function() {
|
||||||
return (!magic);
|
return (!magic || magic === '00000000');
|
||||||
},
|
},
|
||||||
function(w_cb) {
|
function(w_cb) {
|
||||||
|
magic = null;
|
||||||
|
|
||||||
self.readCurrentFileSync();
|
self.readCurrentFileSync();
|
||||||
|
|
||||||
if (self.currentFileIndex < 0) return cb();
|
if (self.currentFileIndex < 0) return cb();
|
||||||
|
|
||||||
|
var byte0 = self.currentParser ? self.currentParser.buffer(1).toString('hex') : null;
|
||||||
|
|
||||||
magic = self.currentParser ? self.currentParser.buffer(4).toString('hex')
|
if (byte0) {
|
||||||
: null ;
|
// Grab 3 bytes from block without removing them
|
||||||
|
var p = self.currentParser.pos;
|
||||||
|
var bytes123 = self.currentParser.subject.toString('hex',p,p+3);
|
||||||
|
magic = byte0 + bytes123;
|
||||||
|
|
||||||
if (!self.currentParser || self.currentParser.eof() || magic === '00000000') {
|
if (magic !=='00000000' && magic !== self.magic) {
|
||||||
magic = null;
|
|
||||||
if (self.nextFile()) {
|
if (self.errorCount++ > 4)
|
||||||
|
return cb(new Error('CRITICAL ERROR: Magic number mismatch: ' +
|
||||||
|
magic + '!=' + self.magic));
|
||||||
|
|
||||||
|
magic=null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!self.currentParser || self.currentParser.eof() ) {
|
||||||
|
if (self.nextFile())
|
||||||
console.log('Moving forward to file:' + self.currentFile() );
|
console.log('Moving forward to file:' + self.currentFile() );
|
||||||
return w_cb();
|
else
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log('Finished all files');
|
console.log('Finished all files');
|
||||||
|
|
||||||
magic = null;
|
magic = null;
|
||||||
return w_cb();
|
return w_cb();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
return w_cb();
|
return w_cb();
|
||||||
}
|
}
|
||||||
|
@ -128,15 +140,8 @@ BlockExtractor.prototype.getNextBlock = function(cb) {
|
||||||
},
|
},
|
||||||
function (a_cb) {
|
function (a_cb) {
|
||||||
if (!magic) return a_cb();
|
if (!magic) return a_cb();
|
||||||
|
// Remove 3 bytes from magic and spacer
|
||||||
if (magic !== self.magic) {
|
self.currentParser.buffer(3+4);
|
||||||
var e = new Error('CRITICAL ERROR: Magic number mismatch: ' +
|
|
||||||
magic + '!=' + self.magic);
|
|
||||||
return a_cb(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// spacer?
|
|
||||||
self.currentParser.word32le();
|
|
||||||
return a_cb();
|
return a_cb();
|
||||||
},
|
},
|
||||||
function (a_cb) {
|
function (a_cb) {
|
||||||
|
@ -145,6 +150,7 @@ BlockExtractor.prototype.getNextBlock = function(cb) {
|
||||||
b = new Block();
|
b = new Block();
|
||||||
b.parse(self.currentParser);
|
b.parse(self.currentParser);
|
||||||
b.getHash();
|
b.getHash();
|
||||||
|
self.errorCount=0;
|
||||||
return a_cb();
|
return a_cb();
|
||||||
},
|
},
|
||||||
], function(err) {
|
], function(err) {
|
||||||
|
|
|
@ -11,6 +11,7 @@ var assert = require('assert'),
|
||||||
networks = require('bitcore/networks'),
|
networks = require('bitcore/networks'),
|
||||||
util = require('bitcore/util/util');
|
util = require('bitcore/util/util');
|
||||||
|
|
||||||
|
var should = require('chai');
|
||||||
//var txItemsValid = JSON.parse(fs.readFileSync('test/model/txitems.json'));
|
//var txItemsValid = JSON.parse(fs.readFileSync('test/model/txitems.json'));
|
||||||
|
|
||||||
describe('BlockExtractor', function(){
|
describe('BlockExtractor', function(){
|
||||||
|
@ -46,6 +47,8 @@ describe('BlockExtractor', function(){
|
||||||
it('should read next '+config.network+' block ', function(done) {
|
it('should read next '+config.network+' block ', function(done) {
|
||||||
be.getNextBlock(function(err,b) {
|
be.getNextBlock(function(err,b) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
|
// 2nd block of testnet3
|
||||||
|
util.formatHashFull(b.hash).should.equal('00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206');
|
||||||
assert(b.timestamp > lastTs, 'timestamp > genesis_ts');
|
assert(b.timestamp > lastTs, 'timestamp > genesis_ts');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue