correct calculation of ppppp-record purpose range

This commit is contained in:
Andre Staltz 2023-10-18 13:14:40 +03:00
parent 4e855f4f59
commit 1b32169541
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 15 additions and 2 deletions

View File

@ -3,6 +3,7 @@ const Obz = require('obz')
/** /**
* @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB * @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB
* @typedef {ReturnType<import('ppppp-record').init>} PPPPPRecord
* @typedef {import('ppppp-db').RecPresent} RecPresent * @typedef {import('ppppp-db').RecPresent} RecPresent
* @typedef {import('ppppp-db').Tangle} Tangle * @typedef {import('ppppp-db').Tangle} Tangle
* @typedef {ReturnType<PPPPPDB['getTangle']>} DBTangle * @typedef {ReturnType<PPPPPDB['getTangle']>} DBTangle
@ -33,6 +34,14 @@ function assertDBExists(peer) {
if (!peer.db) throw new Error('goals plugin requires ppppp-db plugin') 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} * @implements {Goal}
*/ */
@ -102,7 +111,7 @@ class GoalImpl {
} }
/** /**
* @param {{ db: PPPPPDB | null }} peer * @param {{ db: PPPPPDB | null, record: PPPPPRecord | null }} peer
* @param {unknown} config * @param {unknown} config
*/ */
function initGoals(peer, config) { function initGoals(peer, config) {
@ -127,8 +136,11 @@ function initGoals(peer, config) {
return EMPTY_RANGE return EMPTY_RANGE
case 'all': case 'all':
case 'set': case 'set':
case 'record':
return [0, maxDepth] return [0, maxDepth]
case 'record':
assertRecordExists(peer)
const minDepth = peer.record.getMinRequiredDepth(goal.id)
return [minDepth, maxDepth]
case 'newest': case 'newest':
const start = Math.max(0, maxDepth - goal.count + 1) const start = Math.max(0, maxDepth - goal.count + 1)
return [start, maxDepth] return [start, maxDepth]

View File

@ -33,6 +33,7 @@
"ppppp-db": "github:staltz/ppppp-db", "ppppp-db": "github:staltz/ppppp-db",
"ppppp-caps": "github:staltz/ppppp-caps", "ppppp-caps": "github:staltz/ppppp-caps",
"ppppp-keypair": "github:staltz/ppppp-keypair", "ppppp-keypair": "github:staltz/ppppp-keypair",
"ppppp-record": "github:staltz/ppppp-record",
"prettier": "^2.6.2", "prettier": "^2.6.2",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"rimraf": "^4.4.0", "rimraf": "^4.4.0",