fix reorg issue
This commit is contained in:
parent
4a50df9d4c
commit
06f0593613
70
lib/node.js
70
lib/node.js
@ -123,6 +123,8 @@ Node.prototype._syncBitcoindAncestor = function(block, done) {
|
|||||||
done(err);
|
done(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('getHashes done');
|
||||||
|
|
||||||
// Create a hash map for faster lookups
|
// Create a hash map for faster lookups
|
||||||
var currentHashesMap = {};
|
var currentHashesMap = {};
|
||||||
var length = currentHashes.length;
|
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
|
// 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.
|
// and thus don't need to find the entire chain of hashes.
|
||||||
|
|
||||||
async.whilst(function() {
|
while(ancestorHash && !currentHashesMap[ancestorHash]) {
|
||||||
// Wait until the previous hash is in the current chain
|
var blockIndex = self.bitcoind.getBlockIndex(ancestorHash);
|
||||||
return ancestorHash && !currentHashesMap[ancestorHash];
|
console.log(blockIndex);
|
||||||
}, function(next) {
|
ancestorHash = blockIndex ? blockIndex.prevHash : null;
|
||||||
self.bitcoind.getBlockIndex(ancestorHash, function(err, blockIndex) {
|
}
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
ancestorHash = blockIndex.prevHash;
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
|
|
||||||
// Hash map is no-longer needed, quickly let
|
// Hash map is no-longer needed, quickly let
|
||||||
// scavenging garbage collection know to cleanup
|
// scavenging garbage collection know to cleanup
|
||||||
currentHashesMap = null;
|
currentHashesMap = null;
|
||||||
|
|
||||||
if (err) {
|
if (!ancestorHash) {
|
||||||
return done(err);
|
return done(new Error('Unknown common ancestor.'));
|
||||||
} else 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;
|
var self = this;
|
||||||
|
|
||||||
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
|
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
|
||||||
|
console.log('ancestorHash', ancestorHash);
|
||||||
|
|
||||||
// Rewind the chain to the common ancestor
|
// Rewind the chain to the common ancestor
|
||||||
async.whilst(
|
async.whilst(
|
||||||
@ -202,6 +196,7 @@ Node.prototype._syncBitcoindRewind = function(block, done) {
|
|||||||
self.chain.tip = previousTip;
|
self.chain.tip = previousTip;
|
||||||
self.chain.saveMetadata();
|
self.chain.saveMetadata();
|
||||||
self.chain.emit('removeblock', tip);
|
self.chain.emit('removeblock', tip);
|
||||||
|
console.log('removeblock', tip.hash);
|
||||||
removeDone();
|
removeDone();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -221,6 +216,8 @@ Node.prototype._syncBitcoindRewind = function(block, done) {
|
|||||||
Node.prototype._syncBitcoind = function() {
|
Node.prototype._syncBitcoind = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
console.log('_syncBitcoind');
|
||||||
|
|
||||||
if (self.bitcoindSyncing) {
|
if (self.bitcoindSyncing) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -235,6 +232,8 @@ Node.prototype._syncBitcoind = function() {
|
|||||||
var height;
|
var height;
|
||||||
|
|
||||||
async.whilst(function() {
|
async.whilst(function() {
|
||||||
|
console.log('height', self.chain.tip.__height);
|
||||||
|
console.log('bitcoindHeight', self.bitcoindHeight);
|
||||||
height = self.chain.tip.__height;
|
height = self.chain.tip.__height;
|
||||||
return height < self.bitcoindHeight && !self.stopping;
|
return height < self.bitcoindHeight && !self.stopping;
|
||||||
}, function(done) {
|
}, function(done) {
|
||||||
@ -252,22 +251,27 @@ Node.prototype._syncBitcoind = function() {
|
|||||||
// Populate height
|
// Populate height
|
||||||
block.__height = self.chain.tip.__height + 1;
|
block.__height = self.chain.tip.__height + 1;
|
||||||
|
|
||||||
// Update chain hashes
|
// Update chain.cache.hashes
|
||||||
self.chain.cache.hashes[block.hash] = block.prevHash;
|
self.chain.cache.hashes[block.hash] = block.prevHash;
|
||||||
|
|
||||||
// Create indexes
|
// Update chain.cache.chainHashes
|
||||||
self.db._onChainAddBlock(block, function(err) {
|
self.chain.getHashes(block.hash, function(err, hashes) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
|
// Create indexes
|
||||||
delete self.chain.tip.__transactions;
|
self.db._onChainAddBlock(block, function(err) {
|
||||||
self.chain.tip = block;
|
if (err) {
|
||||||
log.debug('Saving metadata');
|
return done(err);
|
||||||
self.chain.saveMetadata();
|
}
|
||||||
log.debug('Chain added block to main chain');
|
delete self.chain.tip.__transactions;
|
||||||
self.chain.emit('addblock', block);
|
self.chain.tip = block;
|
||||||
setImmediate(done);
|
log.debug('Saving metadata');
|
||||||
|
self.chain.saveMetadata();
|
||||||
|
log.debug('Chain added block to main chain');
|
||||||
|
self.chain.emit('addblock', block);
|
||||||
|
setImmediate(done);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user