From cfb186c0fe94ed62f21a793f292d67f2d5c102f7 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 8 Sep 2023 13:46:54 +0300 Subject: [PATCH] refactor algorithm commit() --- lib/algorithm.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/algorithm.js b/lib/algorithm.js index df231ce..05fb485 100644 --- a/lib/algorithm.js +++ b/lib/algorithm.js @@ -6,10 +6,8 @@ const { parseGoal } = require('./goal') /** * @typedef {import('./range').Range} Range - */ - -/** * @typedef {import('./goal').Goal} Goal + * @typedef {import('ppppp-db/msg-v3').Msg} Msg */ function countIter(iter) { @@ -160,10 +158,17 @@ class Algorithm { } } - async commit(rootID, newMsgs, goal, myWantRange) { - // Filter out dataful newMsgs that are not in my want-range + /** + * Filter out msgs I didn't actually ask for. "Trust but verify" + * @param {string} rootID + * @param {Array} msgs + * @param {Range} myWantRange + * @returns {Array} + */ + #filterReceivedMsgs(rootID, msgs, myWantRange) { const [minWant, maxWant] = myWantRange - const validNewMsgs = newMsgs + + const validNewMsgs = msgs .filter((msg) => { const depth = msg.metadata.tangles[rootID]?.depth ?? 0 if (depth === 0 && MsgV3.getMsgID(msg) !== rootID) { @@ -181,6 +186,12 @@ class Algorithm { return aDepth - bDepth }) + return validNewMsgs + } + + async commit(rootID, newMsgs, goal, myWantRange) { + const validNewMsgs = this.#filterReceivedMsgs(rootID, newMsgs, myWantRange) + // TODO: Simulate adding this whole tangle, and check if it's valid // Add new messages