mirror of https://codeberg.org/pzp/pzp-db.git
fix tangle getShortestPath against cycles
This commit is contained in:
parent
fec2b46a3e
commit
31ec544522
|
@ -228,14 +228,21 @@ class Tangle {
|
||||||
}
|
}
|
||||||
const path = []
|
const path = []
|
||||||
let current = msgID
|
let current = msgID
|
||||||
|
let lastPrev = undefined
|
||||||
while (true) {
|
while (true) {
|
||||||
const prev = this.#prev.get(current)
|
const prev = this.#prev.get(current)
|
||||||
if (!prev) break
|
if (!prev) break
|
||||||
|
if (prev === lastPrev) {
|
||||||
|
// prettier-ignore
|
||||||
|
throw new Error(`Tangle "${this.#rootID}" has a cycle or lacking a trail to root`)
|
||||||
|
} else {
|
||||||
|
lastPrev = prev
|
||||||
|
}
|
||||||
let minDepth = /** @type {number} */ (this.#depth.get(current))
|
let minDepth = /** @type {number} */ (this.#depth.get(current))
|
||||||
let min = current
|
let min = current
|
||||||
for (const p of prev) {
|
for (const p of prev) {
|
||||||
const d = /** @type {number} */ (this.#depth.get(p))
|
const d = /** @type {number} */ (this.#depth.get(p))
|
||||||
if (d < minDepth) {
|
if (typeof d === 'number' && d < minDepth) {
|
||||||
minDepth = d
|
minDepth = d
|
||||||
min = p
|
min = p
|
||||||
} else if (d === minDepth && compareMsgIDs(p, min) < 0) {
|
} else if (d === minDepth && compareMsgIDs(p, min) < 0) {
|
||||||
|
|
Loading…
Reference in New Issue