From 1b321695418ed234b832e511397355052a1a3760 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Wed, 18 Oct 2023 13:14:40 +0300 Subject: [PATCH] correct calculation of ppppp-record purpose range --- lib/index.js | 16 ++++++++++++++-- package.json | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index fa92703..42b9668 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,7 @@ const Obz = require('obz') /** * @typedef {ReturnType} PPPPPDB + * @typedef {ReturnType} PPPPPRecord * @typedef {import('ppppp-db').RecPresent} RecPresent * @typedef {import('ppppp-db').Tangle} Tangle * @typedef {ReturnType} DBTangle @@ -33,6 +34,14 @@ function assertDBExists(peer) { if (!peer.db) throw new Error('goals plugin requires ppppp-db plugin') } +/** + * @param {{ record: PPPPPRecord | null }} peer + * @returns {asserts peer is { record: PPPPPRecord }} + */ +function assertRecordExists(peer) { + if (!peer.record) throw new Error('goals plugin requires ppppp-record plugin') +} + /** * @implements {Goal} */ @@ -102,7 +111,7 @@ class GoalImpl { } /** - * @param {{ db: PPPPPDB | null }} peer + * @param {{ db: PPPPPDB | null, record: PPPPPRecord | null }} peer * @param {unknown} config */ function initGoals(peer, config) { @@ -127,8 +136,11 @@ function initGoals(peer, config) { return EMPTY_RANGE case 'all': case 'set': - case 'record': return [0, maxDepth] + case 'record': + assertRecordExists(peer) + const minDepth = peer.record.getMinRequiredDepth(goal.id) + return [minDepth, maxDepth] case 'newest': const start = Math.max(0, maxDepth - goal.count + 1) return [start, maxDepth] diff --git a/package.json b/package.json index 148764d..05a0cfa 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "ppppp-db": "github:staltz/ppppp-db", "ppppp-caps": "github:staltz/ppppp-caps", "ppppp-keypair": "github:staltz/ppppp-keypair", + "ppppp-record": "github:staltz/ppppp-record", "prettier": "^2.6.2", "pretty-quick": "^3.1.3", "rimraf": "^4.4.0",