diff --git a/lib/algorithm.js b/lib/algorithm.js index ad3f3a7..df231ce 100644 --- a/lib/algorithm.js +++ b/lib/algorithm.js @@ -1,7 +1,7 @@ const { BloomFilter } = require('bloom-filters') const MsgV3 = require('ppppp-db/msg-v3') const p = require('util').promisify -const { isEmptyRange, estimateMsgCount } = require('./range') +const { EMPTY_RANGE, isEmptyRange, estimateMsgCount } = require('./range') const { parseGoal } = require('./goal') /** @@ -27,7 +27,7 @@ class Algorithm { haveRange(rootID) { const rootMsg = this.#peer.db.get(rootID) - if (!rootMsg) return [1, 0] + if (!rootMsg) return EMPTY_RANGE let minDepth = Number.MAX_SAFE_INTEGER let maxDepth = 0 for (const rec of this.#peer.db.records()) { @@ -62,7 +62,7 @@ class Algorithm { #wantNewestRange(localHaveRange, remoteHaveRange, count) { const [minLocalHave, maxLocalHave] = localHaveRange const [minRemoteHave, maxRemoteHave] = remoteHaveRange - if (maxRemoteHave <= maxLocalHave) return [1, 0] + if (maxRemoteHave <= maxLocalHave) return EMPTY_RANGE const maxWant = maxRemoteHave const size = Math.max(maxWant - maxLocalHave, count) const minWant = Math.max(maxWant - size, maxLocalHave + 1, minRemoteHave) @@ -87,8 +87,8 @@ class Algorithm { * @returns {Range} */ wantRange(localHave, remoteHave, goal) { - if (!goal) return [1, 0] - if (isEmptyRange(remoteHave)) return [1, 0] + if (!goal) return EMPTY_RANGE + if (isEmptyRange(remoteHave)) return EMPTY_RANGE const { type, count } = parseGoal(goal) if (type === 'all') { return this.#wantAllRange(localHave, remoteHave) @@ -148,7 +148,7 @@ class Algorithm { const tangle = this.#peer.db.getTangle(rootID) const sorted = tangle.topoSort() if (sorted.length <= count) return - const msgID = sorted[sorted.length - count] + const msgID = sorted[sorted.length - count] // New "oldest dataful msg" const { deletables, erasables } = tangle.getDeletablesAndErasables(msgID) const del = p(this.#peer.db.del) const erase = p(this.#peer.db.erase) diff --git a/lib/range.js b/lib/range.js index da808f9..0f8aec8 100644 --- a/lib/range.js +++ b/lib/range.js @@ -23,7 +23,10 @@ function estimateMsgCount(range) { else return estimate } +const EMPTY_RANGE = [1, 0] + module.exports = { isEmptyRange, estimateMsgCount, + EMPTY_RANGE }