Return null instead of throwing on property errs (#8)

Trying to fix https://codeberg.org/pzp/pzp-sync/issues/10

This doesn't solve the problem of missing rootMsg but maybe allows sync to fail more gracefully

Reviewed-on: https://codeberg.org/pzp/pzp-db/pulls/8
Co-authored-by: Jacob Karlsson <jacob.karlsson95@gmail.com>
Co-committed-by: Jacob Karlsson <jacob.karlsson95@gmail.com>
This commit is contained in:
Jacob Karlsson 2024-06-01 16:21:00 +00:00 committed by Powersource
parent eefe93820d
commit 047c88fb86
3 changed files with 23 additions and 15 deletions

View File

@ -131,7 +131,7 @@ class DBTangle extends MsgV4.Tangle {
* *Erasables* are msgs that precede `msgsIDs` and can be erased without
* losing a validation path toward the root.
* @param {Array<MsgID>} msgIDs
* @returns {{ deletables: Set<MsgID>, erasables: Set<MsgID> }}
* @returns {{ deletables: Set<MsgID>, erasables: Set<MsgID> } | null}
*/
getDeletablesAndErasables(...msgIDs) {
// Determine erasables
@ -139,6 +139,7 @@ class DBTangle extends MsgV4.Tangle {
const minimum = this.getMinimumAmong(msgIDs)
for (const msgID of minimum) {
const trail = this.shortestPathToRoot(msgID)
if (!trail) return null
for (const id of trail) {
erasables.add(id)
}
@ -174,6 +175,7 @@ class DBTangle extends MsgV4.Tangle {
const trail = new Set()
for (const msgID of minSetTight) {
const path = this.shortestPathToRoot(msgID)
if (!path) return cb(Error("Couldn't get shortest path to root when slicing dbtangle"))
for (const msgID of path) {
trail.add(msgID)
}
@ -529,16 +531,17 @@ function initDB(peer, config) {
rec.msg.metadata.accountTips,
(err, sigkeys) => {
if (err) return cb(err)
// TODO: how is sigkeys able to be undefined here? typechecker why are you weird?
if (!sigkeys) return cb(Error("Sigkeys missing somehow"))
// Don't accept ghosts to come back, unless they are trail msgs
if (!!rec.msg.data && ghosts.read(tangleID).has(rec.id)) {
return cb(Error('Refusing a ghost msg to come back'))
}
if (
(err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID))
) {
return cb(Error('Invalid msg', { cause: err }))
const valErr = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID)
if (valErr) {
return cb(Error('Invalid msg', { cause: valErr }))
}
/** @param {CB<void>} cb */

View File

@ -222,11 +222,12 @@ class Tangle {
}
/**
* @returns {'feed' | 'account' | 'weave'}
* @returns {'feed' | 'account' | 'weave' | null}
*/
get type() {
if (!this.#rootMsg) {
throw new Error(`Tangle "${this.#rootID}" is missing root message`)
console.trace(new Error(`Tangle "${this.#rootID}" is missing root message`))
return null
}
if (this.#isFeed()) return 'feed'
if (this.#rootMsg.metadata.account === 'self') return 'account'
@ -235,7 +236,8 @@ class Tangle {
get root() {
if (!this.#rootMsg) {
throw new Error(`Tangle "${this.#rootID}" is missing root message`)
console.trace(new Error(`Tangle "${this.#rootID}" is missing root message`))
return null
}
return this.#rootMsg
}
@ -256,7 +258,8 @@ class Tangle {
if (!prev) break
if (prev === lastPrev) {
// prettier-ignore
throw new Error(`Tangle "${this.#rootID}" has a cycle or lacking a trail to root`)
console.trace(new Error(`Tangle "${this.#rootID}" has a cycle or lacking a trail to root`))
return null
} else {
lastPrev = prev
}

View File

@ -107,6 +107,8 @@ function validateSigkeyAndAccount(msg, tangle, sigkeys) {
// prettier-ignore
return `invalid msg: accountTips "${msg.metadata.accountTips}" should have been null in an account tangle\n` + JSON.stringify(msg)
}
} else if (tangle.type === null) {
return "Cannot validate tangle of unknown type"
}
return undefined
}
@ -199,6 +201,8 @@ function validateTangle(msg, tangle, tangleID) {
// prettier-ignore
return `invalid msg: account "${msg.metadata.account}" should have been feed account "${account}"\n` + JSON.stringify(msg)
}
} else if (tangle.type === null) {
return "Unknown tangle type"
}
let lastPrev = null
let minDiff = Infinity
@ -342,11 +346,8 @@ function validate(msg, tangle, sigkeys, msgID, rootID) {
if ((err = validateSigkey(msg))) return err
if ((err = validateData(msg))) return err
try {
if (tangle.type === 'feed' && isMoot(msg)) return // nothing else to check
} catch (/** @type {any} */ err) {
return err
}
if (tangle.type === 'feed' && isMoot(msg)) return // nothing else to check
if (tangle.type === null) return "Missing tangle type when validating msg"
if ((err = validateDataSizeHash(msg))) return err
if ((err = validateDomain(msg.metadata.domain))) return err
@ -357,6 +358,7 @@ function validate(msg, tangle, sigkeys, msgID, rootID) {
if ((err = validateTangle(msg, tangle, rootID))) return err
}
if ((err = validateSignature(msg))) return err
return undefined
}
module.exports = {