chain: add getProofTime.

This commit is contained in:
Christopher Jeffrey 2017-01-20 22:05:04 -08:00
parent b60e0e20a4
commit de0a60340b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1825,6 +1825,34 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
return root;
};
/**
* Calculate the time difference (in seconds)
* between two blocks by examining chainworks.
* @param {ChainEntry} to
* @param {ChainEntry} from
* @returns {Number}
*/
Chain.prototype.getProofTime = function getProofTime(to, from) {
var sign, work;
if (to.chainwork.cmp(from.chainwork) > 0) {
work = to.chainwork.sub(from.chainwork);
sign = 1;
} else {
work = from.chainwork.sub(to.chainwork);
sign = -1;
}
work = work.imuln(this.network.pow.targetSpacing);
work = work.idiv(this.tip.getProof());
if (work.bitLength() > 53)
return sign * util.MAX_SAFE_INTEGER;
return sign * work.toNumber();
};
/**
* Calculate the next target based on the chain tip.
* @returns {Promise} - returns Number