diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 11a94914..948a42be 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -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