From 6a19cd91a8f24b01d848e8455260ff2419b96b97 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 14 Apr 2023 16:41:25 +0300 Subject: [PATCH] return Sets from some Tangle APIs --- lib/tangle.js | 32 ++++++++++++++++++++++++------ test/tangle.test.js | 48 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/lib/tangle.js b/lib/tangle.js index 8db8ffb..88b299d 100644 --- a/lib/tangle.js +++ b/lib/tangle.js @@ -60,12 +60,18 @@ class Tangle { */ #perDepth = new Map() + /** + * @type {number} + */ + #maxDepth + /** * * @param {string} rootHash * @param {Iterable} recordsIter */ constructor(rootHash, recordsIter) { + this.#maxDepth = 0 for (const rec of recordsIter) { const msgHash = rec.hash const tangles = rec.msg.metadata.tangles @@ -81,6 +87,7 @@ class Tangle { } this.#prev.set(msgHash, prev) const depth = tangles[rootHash].depth + if (depth > this.#maxDepth) this.#maxDepth = depth this.#depth.set(msgHash, depth) const atDepth = this.#perDepth.get(depth) ?? [] atDepth.push(msgHash) @@ -103,7 +110,8 @@ class Tangle { */ topoSort() { const sorted = [] - for (let i = 0; i < 1e9; i++) { + const max = this.#maxDepth + for (let i = 0; i <= max; i++) { const atDepth = this.#getAllAtDepth(i) if (atDepth.length === 0) break for (const msgHash of atDepth) { @@ -114,19 +122,19 @@ class Tangle { } /** - * @returns {Array} + * @returns {Set} */ getTips() { - return [...this.#tips] + return this.#tips } /** * @param {number} depth - * @returns {Array} + * @returns {Set} */ - getLipmaa(depth) { + getLipmaaSet(depth) { const lipmaaDepth = lipmaa(depth + 1) - 1 - return this.#getAllAtDepth(lipmaaDepth) + return new Set(this.#getAllAtDepth(lipmaaDepth)) } /** @@ -137,6 +145,14 @@ class Tangle { return this.#depth.has(msgHash) } + /** + * @param {string} msgHash + * @returns {number} + */ + getDepth(msgHash) { + return this.#depth.get(msgHash) ?? -1 + } + #shortestPathToRoot(msgHash) { const path = [] let current = msgHash @@ -169,6 +185,10 @@ class Tangle { ) return { deletables, emptyables } } + + getMaxDepth() { + return this.#maxDepth + } } module.exports = Tangle diff --git a/test/tangle.test.js b/test/tangle.test.js index 5892221..fa90ae9 100644 --- a/test/tangle.test.js +++ b/test/tangle.test.js @@ -96,6 +96,23 @@ test('Tangle.has', (t) => { t.end() }) +test('Tangle.getDepth', t=> { + const tangle = new Tangle(rootPost, peer.db.records()) + t.equals(tangle.getDepth(rootPost), 0, 'depth of rootPost is 0') + t.equals(tangle.getDepth(reply1Lo), 1, 'depth of reply1Lo is 1') + t.equals(tangle.getDepth(reply1Hi), 1, 'depth of reply1Hi is 1') + t.equals(tangle.getDepth(reply2A), 2, 'depth of reply2A is 2') + t.equals(tangle.getDepth(reply3Lo), 3, 'depth of reply3Lo is 3') + t.equals(tangle.getDepth(reply3Hi), 3, 'depth of reply3Hi is 3') + t.end() +}) + +test('Tangle.getMaxDepth', t => { + const tangle = new Tangle(rootPost, peer.db.records()) + t.equals(tangle.getMaxDepth(), 3, 'max depth is 3') + t.end() +}) + test('Tangle.topoSort', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) const sorted = tangle.topoSort() @@ -115,20 +132,31 @@ test('Tangle.getTips', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) const tips = tangle.getTips() - t.equals(tips.length, 2, 'there are 2 tips') - t.true(tips.includes(reply3Lo), 'tips contains reply3Lo') - t.true(tips.includes(reply3Hi), 'tips contains reply3Hi') + t.equals(tips.size, 2, 'there are 2 tips') + t.true(tips.has(reply3Lo), 'tips contains reply3Lo') + t.true(tips.has(reply3Hi), 'tips contains reply3Hi') t.end() }) -test('Tangle.getLipmaa', (t) => { +test('Tangle.getLipmaaSet', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) - t.deepEquals(tangle.getLipmaa(0), [], 'lipmaa 0 (empty)') - t.deepEquals(tangle.getLipmaa(1), [rootPost], 'lipmaa 1 (-1)') - t.deepEquals(tangle.getLipmaa(2), [reply1Lo, reply1Hi], 'lipmaa 2 (-1)') - t.deepEquals(tangle.getLipmaa(3), [rootPost], 'lipmaa 3 (leap!)') - t.deepEquals(tangle.getLipmaa(4), [reply3Lo, reply3Hi], 'lipmaa 4 (-1)') - t.deepEquals(tangle.getLipmaa(5), [], 'lipmaa 5 (empty)') + t.equals(tangle.getLipmaaSet(0).size, 0, 'lipmaa 0 (empty)') + + t.equals(tangle.getLipmaaSet(1).size, 1, 'lipmaa 1 (-1)') + t.true(tangle.getLipmaaSet(1).has(rootPost), 'lipmaa 1 (-1)') + + t.equals(tangle.getLipmaaSet(2).size, 2, 'lipmaa 2 (-1)') + t.true(tangle.getLipmaaSet(2).has(reply1Lo), 'lipmaa 2 (-1)') + t.true(tangle.getLipmaaSet(2).has(reply1Hi), 'lipmaa 2 (-1)') + + t.equals(tangle.getLipmaaSet(3).size, 1, 'lipmaa 3 (leap!)') + t.true(tangle.getLipmaaSet(3).has(rootPost), 'lipmaa 3 (leap!)') + + t.equals(tangle.getLipmaaSet(4).size, 2, 'lipmaa 4 (-1)') + t.true(tangle.getLipmaaSet(4).has(reply3Lo), 'lipmaa 4 (-1)') + t.true(tangle.getLipmaaSet(4).has(reply3Hi), 'lipmaa 4 (-1)') + + t.equals(tangle.getLipmaaSet(5).size, 0, 'lipmaa 5 (empty)') t.end() })