fix tangle getShortestPath against cycles

This commit is contained in:
Andre Staltz 2023-09-26 15:31:19 +03:00
parent fec2b46a3e
commit 31ec544522
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 8 additions and 1 deletions

View File

@ -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) {