refactor algorithm commit()

This commit is contained in:
Andre Staltz 2023-09-08 13:46:54 +03:00
parent fc30b3e2d2
commit cfb186c0fe
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 17 additions and 6 deletions

View File

@ -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<Msg>} msgs
* @param {Range} myWantRange
* @returns {Array<Msg>}
*/
#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