Skip to content

Commit

Permalink
Merge pull request #113 from matiu/bug/fix-reorg-tests
Browse files Browse the repository at this point in the history
Bug/fix reorg tests
  • Loading branch information
cmgustavo committed Jun 3, 2014
2 parents a77d5a9 + 65a5093 commit fcbd1f6
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 395 deletions.
2 changes: 1 addition & 1 deletion lib/PeerSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PeerSync.prototype.handleBlock = function(info) {
}, self.allowReorgs, function(err, height) {
if (err && err.message.match(/NEED_SYNC/) && self.historicSync) {
console.log('[p2p_sync] Orphan block received. Triggering sync');
self.historicSync.start({}, function(){
self.historicSync.start({forceRPC:1}, function(){
console.log('[p2p_sync] Done resync.');
});
}
Expand Down
18 changes: 16 additions & 2 deletions lib/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,18 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) {
allowReorgs = true;
}
if (!b) return cb();

var self = this;

if ( self.storingBlock ) {
console.log('## Storing a block already. Delaying storeTipBlock with:' +
b.hash);
return setTimeout( function() {
console.log('## Retrying storeTipBlock with: ' + b.hash);
self.storeTipBlock(b,allowReorgs,cb);
}, 1000);
}

self.storingBlock=1;
var oldTip, oldNext, oldHeight, needReorg = false, height = -1;
var newPrev = b.previousblockhash;

Expand Down Expand Up @@ -164,6 +174,7 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) {
if (err && err.toString().match(/WARN/)) {
err = null;
}
self.storingBlock=0;
return cb(err, height);
});
};
Expand All @@ -177,7 +188,10 @@ Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, oldHeight, cb)

function(c) {
self.bDb.getHeight(newPrev, function(err, height) {
if (!height) return c(new Error('Could not found block:' + newPrev));
if (!height) {
// Case 3 + allowReorgs = true
return c(new Error('Could not found block:' + newPrev));
}
if (height<0) return c();

newHeight = height + 1;
Expand Down
Loading

0 comments on commit fcbd1f6

Please sign in to comment.