refactor: renaming, reuse

This commit is contained in:
Andre Staltz 2023-10-26 16:56:58 +03:00
parent 46dafa436e
commit afc7766326
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 3 additions and 30 deletions

View File

@ -439,42 +439,15 @@ function initDict(peer, config) {
if (ghostableMsgID === tangleID) return false
assertDBPlugin(peer)
const tangle = peer.db.getTangle(tangleID)
const msg = peer.db.get(ghostableMsgID)
// prettier-ignore
if (!tangle || tangle.size === 0) throw new Error(`isGhostable() tangleID "${tangleID}" is empty`)
// prettier-ignore
if (!msg) throw new Error(`isGhostable() msgID "${ghostableMsgID}" does not exist in the database`)
// prettier-ignore
if (!isValidDictMoot(tangle.root)) throw new Error(`isGhostable() tangleID "${tangleID}" is not a dict`)
// Discover field roots
const fieldRootIDs = new Set()
const msgIDs = tangle.topoSort()
for (const msgID of msgIDs) {
const msg = peer.db.get(msgID)
if (!msg?.data) continue
for (const supersededMsgID of msg.data.supersedes) {
fieldRootIDs.delete(supersededMsgID)
}
fieldRootIDs.add(msgID)
}
// Get minimum depth of all field roots
let minFieldRootDepth = Infinity
for (const fieldRootID of fieldRootIDs) {
const depth = tangle.getDepth(fieldRootID)
if (depth < minFieldRootDepth) minFieldRootDepth = depth
// field roots are not ghostables
if (fieldRootID === ghostableMsgID) return false
// msgs suceeding field roots are not ghostables
if (tangle.precedes(fieldRootID, ghostableMsgID)) return false
}
const minFieldRootDepth = minRequiredDepth(tangleID)
const minGhostDepth = minFieldRootDepth - ghostSpan
const ghostableMsgDepth = msg.metadata.tangles[tangleID].depth
if (ghostableMsgDepth >= minGhostDepth) return true
const msgDepth = msg.metadata.tangles[tangleID].depth
if (minGhostDepth <= msgDepth && msgDepth < minFieldRootDepth) return true
return false
}