mirror of https://codeberg.org/pzp/pzp-dict.git
refactor: renaming, reuse
This commit is contained in:
parent
46dafa436e
commit
afc7766326
33
lib/index.js
33
lib/index.js
|
@ -439,42 +439,15 @@ function initDict(peer, config) {
|
||||||
if (ghostableMsgID === tangleID) return false
|
if (ghostableMsgID === tangleID) return false
|
||||||
|
|
||||||
assertDBPlugin(peer)
|
assertDBPlugin(peer)
|
||||||
const tangle = peer.db.getTangle(tangleID)
|
|
||||||
const msg = peer.db.get(ghostableMsgID)
|
const msg = peer.db.get(ghostableMsgID)
|
||||||
|
|
||||||
// prettier-ignore
|
|
||||||
if (!tangle || tangle.size === 0) throw new Error(`isGhostable() tangleID "${tangleID}" is empty`)
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (!msg) throw new Error(`isGhostable() msgID "${ghostableMsgID}" does not exist in the database`)
|
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 minGhostDepth = minFieldRootDepth - ghostSpan
|
||||||
const ghostableMsgDepth = msg.metadata.tangles[tangleID].depth
|
const msgDepth = msg.metadata.tangles[tangleID].depth
|
||||||
if (ghostableMsgDepth >= minGhostDepth) return true
|
if (minGhostDepth <= msgDepth && msgDepth < minFieldRootDepth) return true
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue