chain: optimize isMainChain.
This commit is contained in:
parent
b0b8d333a5
commit
ab9cf9ec7e
@ -596,27 +596,17 @@ ChainDB.prototype.getNextHash = co(function* getNextHash(hash) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
|
ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
|
||||||
var query, height, existing;
|
assert(typeof hash === 'string');
|
||||||
|
|
||||||
if (hash instanceof ChainEntry) {
|
|
||||||
query = hash.height;
|
|
||||||
hash = hash.hash;
|
|
||||||
} else {
|
|
||||||
query = hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hash === this.chain.tip.hash
|
if (hash === this.chain.tip.hash
|
||||||
|| hash === this.network.genesis.hash) {
|
|| hash === this.network.genesis.hash) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
height = yield this.getHeight(query);
|
if (yield this.getNextHash(hash))
|
||||||
existing = yield this.getHash(height);
|
return true;
|
||||||
|
|
||||||
if (!existing)
|
return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
return hash === existing;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -223,9 +223,27 @@ ChainEntry.prototype.getAncestors = co(function* getAncestors(max) {
|
|||||||
* @returns {Promise} - Return Boolean.
|
* @returns {Promise} - Return Boolean.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ChainEntry.prototype.isMainChain = function isMainChain() {
|
ChainEntry.prototype.isMainChain = co(function* isMainChain() {
|
||||||
return this.chain.db.isMainChain(this);
|
var entry;
|
||||||
};
|
|
||||||
|
if (this.hash === this.chain.tip.hash
|
||||||
|
|| this.hash === this.network.genesis.hash) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry = this.chain.db.getCache(this.height);
|
||||||
|
|
||||||
|
if (entry) {
|
||||||
|
if (entry.hash === this.hash)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (yield this.chain.db.getNextHash(this.hash))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collect ancestors up to `height`.
|
* Collect ancestors up to `height`.
|
||||||
@ -234,7 +252,7 @@ ChainEntry.prototype.isMainChain = function isMainChain() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ChainEntry.prototype.getAncestorByHeight = co(function* getAncestorByHeight(height) {
|
ChainEntry.prototype.getAncestorByHeight = co(function* getAncestorByHeight(height) {
|
||||||
var main, entry;
|
var entry;
|
||||||
|
|
||||||
if (height < 0)
|
if (height < 0)
|
||||||
return yield co.wait();
|
return yield co.wait();
|
||||||
@ -242,9 +260,7 @@ ChainEntry.prototype.getAncestorByHeight = co(function* getAncestorByHeight(heig
|
|||||||
assert(height >= 0);
|
assert(height >= 0);
|
||||||
assert(height <= this.height);
|
assert(height <= this.height);
|
||||||
|
|
||||||
main = yield this.isMainChain();
|
if (yield this.isMainChain())
|
||||||
|
|
||||||
if (main)
|
|
||||||
return yield this.chain.db.get(height);
|
return yield this.chain.db.get(height);
|
||||||
|
|
||||||
entry = yield this.getAncestor(this.height - height);
|
entry = yield this.getAncestor(this.height - height);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user