fix reorg issue

This commit is contained in:
Patrick Nagurny 2015-08-18 17:36:54 -04:00 committed by Braydon Fuller
parent 4a50df9d4c
commit 06f0593613
1 changed files with 37 additions and 33 deletions

View File

@ -123,6 +123,8 @@ Node.prototype._syncBitcoindAncestor = function(block, done) {
done(err);
}
console.log('getHashes done');
// Create a hash map for faster lookups
var currentHashesMap = {};
var length = currentHashes.length;
@ -135,31 +137,22 @@ Node.prototype._syncBitcoindAncestor = function(block, done) {
// We only need to go back until we meet the main chain for the forked block
// and thus don't need to find the entire chain of hashes.
async.whilst(function() {
// Wait until the previous hash is in the current chain
return ancestorHash && !currentHashesMap[ancestorHash];
}, function(next) {
self.bitcoind.getBlockIndex(ancestorHash, function(err, blockIndex) {
if (err) {
return next(err);
}
ancestorHash = blockIndex.prevHash;
next();
});
}, function(err) {
while(ancestorHash && !currentHashesMap[ancestorHash]) {
var blockIndex = self.bitcoind.getBlockIndex(ancestorHash);
console.log(blockIndex);
ancestorHash = blockIndex ? blockIndex.prevHash : null;
}
// Hash map is no-longer needed, quickly let
// scavenging garbage collection know to cleanup
currentHashesMap = null;
// Hash map is no-longer needed, quickly let
// scavenging garbage collection know to cleanup
currentHashesMap = null;
if (err) {
return done(err);
} else if (!ancestorHash) {
return done(new Error('Unknown common ancestor.'));
}
if (!ancestorHash) {
return done(new Error('Unknown common ancestor.'));
}
done(null, ancestorHash);
done(null, ancestorHash);
});
});
};
@ -174,6 +167,7 @@ Node.prototype._syncBitcoindRewind = function(block, done) {
var self = this;
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
console.log('ancestorHash', ancestorHash);
// Rewind the chain to the common ancestor
async.whilst(
@ -202,6 +196,7 @@ Node.prototype._syncBitcoindRewind = function(block, done) {
self.chain.tip = previousTip;
self.chain.saveMetadata();
self.chain.emit('removeblock', tip);
console.log('removeblock', tip.hash);
removeDone();
});
@ -221,6 +216,8 @@ Node.prototype._syncBitcoindRewind = function(block, done) {
Node.prototype._syncBitcoind = function() {
var self = this;
console.log('_syncBitcoind');
if (self.bitcoindSyncing) {
return;
}
@ -235,6 +232,8 @@ Node.prototype._syncBitcoind = function() {
var height;
async.whilst(function() {
console.log('height', self.chain.tip.__height);
console.log('bitcoindHeight', self.bitcoindHeight);
height = self.chain.tip.__height;
return height < self.bitcoindHeight && !self.stopping;
}, function(done) {
@ -252,22 +251,27 @@ Node.prototype._syncBitcoind = function() {
// Populate height
block.__height = self.chain.tip.__height + 1;
// Update chain hashes
// Update chain.cache.hashes
self.chain.cache.hashes[block.hash] = block.prevHash;
// Create indexes
self.db._onChainAddBlock(block, function(err) {
// Update chain.cache.chainHashes
self.chain.getHashes(block.hash, function(err, hashes) {
if (err) {
return done(err);
}
delete self.chain.tip.__transactions;
self.chain.tip = block;
log.debug('Saving metadata');
self.chain.saveMetadata();
log.debug('Chain added block to main chain');
self.chain.emit('addblock', block);
setImmediate(done);
// Create indexes
self.db._onChainAddBlock(block, function(err) {
if (err) {
return done(err);
}
delete self.chain.tip.__transactions;
self.chain.tip = block;
log.debug('Saving metadata');
self.chain.saveMetadata();
log.debug('Chain added block to main chain');
self.chain.emit('addblock', block);
setImmediate(done);
});
});
} else {