diff --git a/lib/index.js b/lib/index.js index a54beae..f852934 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 }