fix getOrphanRoot and usage.
This commit is contained in:
parent
d1f8e9b5ff
commit
7cabdcfda5
@ -387,6 +387,8 @@ Chain.prototype.hasBlock = function hasBlock(hash) {
|
||||
|
||||
if (Array.isArray(hash))
|
||||
hash = utils.toHex(hash);
|
||||
else if (hash.hash)
|
||||
hash = hash.hash('hex');
|
||||
|
||||
return this.index.bloom.test(hash, 'hex');
|
||||
};
|
||||
@ -397,6 +399,8 @@ Chain.prototype.hasOrphan = function hasOrphan(hash) {
|
||||
|
||||
if (Array.isArray(hash))
|
||||
hash = utils.toHex(hash);
|
||||
else if (hash.hash)
|
||||
hash = hash.hash('hex');
|
||||
|
||||
return !!this.orphan.bmap[hash];
|
||||
};
|
||||
@ -531,15 +535,17 @@ Chain.prototype.locatorHashes = function(index) {
|
||||
Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
|
||||
var self = this;
|
||||
|
||||
if (typeof hash !== 'string') {
|
||||
if (Array.isArray(hash))
|
||||
hash = utils.toHex(hash);
|
||||
else if (hash.hash)
|
||||
hash = hash.hash('hex');
|
||||
}
|
||||
|
||||
var orphans = this.orphan.bmap;
|
||||
|
||||
var orphanRoot = hash;
|
||||
while (orphans[orphanRoot.prevBlock])
|
||||
orphanRoot = orphans[orphanRoot.prevBlock];
|
||||
|
||||
while (this.orphan.bmap[hash]) {
|
||||
orphanRoot = hash;
|
||||
hash = this.orphan.bmap[hash].prevBlock;
|
||||
}
|
||||
|
||||
return orphanRoot;
|
||||
};
|
||||
|
||||
@ -357,14 +357,15 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
|
||||
var len = self.chain.index.hashes.length;
|
||||
var hash = block.hash('hex');
|
||||
|
||||
if (!self.chain.hasBlock(block.prevBlock))
|
||||
peer.loadBlocks(self.chain.locatorHashes(), self.chain.getOrphanRoot(block));
|
||||
|
||||
self._response(block);
|
||||
|
||||
var err = self.chain.add(block);
|
||||
if (err)
|
||||
self.emit('chain-error', err, peer);
|
||||
|
||||
if (self.chain.hasOrphan(block))
|
||||
peer.loadBlocks(self.chain.locatorHashes(), self.chain.getOrphanRoot(block));
|
||||
|
||||
self.emit('_block', block, peer);
|
||||
|
||||
if (self.chain.index.hashes.length === len)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user