From 31ec544522f1d706503cd0b5587ae67a182cbe50 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 26 Sep 2023 15:31:19 +0300 Subject: [PATCH] fix tangle getShortestPath against cycles --- lib/msg-v3/tangle.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/msg-v3/tangle.js b/lib/msg-v3/tangle.js index a540ecb..96cdc3e 100644 --- a/lib/msg-v3/tangle.js +++ b/lib/msg-v3/tangle.js @@ -228,14 +228,21 @@ class Tangle { } const path = [] let current = msgID + let lastPrev = undefined while (true) { const prev = this.#prev.get(current) 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 min = current for (const p of prev) { const d = /** @type {number} */ (this.#depth.get(p)) - if (d < minDepth) { + if (typeof d === 'number' && d < minDepth) { minDepth = d min = p } else if (d === minDepth && compareMsgIDs(p, min) < 0) {