mirror of https://codeberg.org/pzp/pzp-db.git
fix logic determining tips of the tangle
This commit is contained in:
parent
77a7d11044
commit
edfabd8d14
|
@ -91,7 +91,16 @@ function readDepth(msg, tangleId = null) {
|
||||||
return msg.metadata.tangles?.[tangleId]?.depth ?? 0
|
return msg.metadata.tangles?.[tangleId]?.depth ?? 0
|
||||||
} else {
|
} else {
|
||||||
return msg.metadata.depth
|
return msg.metadata.depth
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function readPrev(msg, tangleId = null) {
|
||||||
|
if (tangleId) {
|
||||||
|
return msg.metadata.tangles?.[tangleId]?.prev ?? []
|
||||||
|
} else {
|
||||||
|
return msg.metadata.prev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function calculateDepth(existing, tangleId = null) {
|
function calculateDepth(existing, tangleId = null) {
|
||||||
let max = -1
|
let max = -1
|
||||||
|
@ -132,12 +141,33 @@ function lipmaa(n) {
|
||||||
return n - po3
|
return n - po3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function determineTips(existing, tangleId = null) {
|
||||||
|
const tips = new Set()
|
||||||
|
for (const msg of existing.values()) {
|
||||||
|
tips.add(getMsgHash(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const msg of existing.values()) {
|
||||||
|
const prev = readPrev(msg, tangleId)
|
||||||
|
for (const p of prev) {
|
||||||
|
tips.delete(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tips
|
||||||
|
}
|
||||||
|
|
||||||
function calculatePrev(existing, depth, lipmaaDepth, tangleId = null) {
|
function calculatePrev(existing, depth, lipmaaDepth, tangleId = null) {
|
||||||
const prev = []
|
const prev = []
|
||||||
|
const tips = determineTips(existing, tangleId)
|
||||||
for (const msg of existing.values()) {
|
for (const msg of existing.values()) {
|
||||||
const msgDepth = readDepth(msg, tangleId)
|
const msgDepth = readDepth(msg, tangleId)
|
||||||
if (msgDepth === lipmaaDepth || msgDepth === depth - 1) {
|
const msgHash = getMsgHash(msg)
|
||||||
prev.push(getMsgHash(msg))
|
if (
|
||||||
|
msgDepth === depth - 1 ||
|
||||||
|
msgDepth === lipmaaDepth ||
|
||||||
|
tips.has(msgHash)
|
||||||
|
) {
|
||||||
|
prev.push(msgHash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return prev
|
return prev
|
||||||
|
|
|
@ -80,3 +80,72 @@ tape('FeedV1.create()', (t) => {
|
||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tape('create() handles DAG tips correctly', (t) => {
|
||||||
|
const keys = generateKeypair('alice')
|
||||||
|
const when = 1652037377204
|
||||||
|
const existing = new Map()
|
||||||
|
|
||||||
|
const msg1 = FeedV1.create({
|
||||||
|
keys,
|
||||||
|
content: { text: '1' },
|
||||||
|
type: 'post',
|
||||||
|
existing: new Map(),
|
||||||
|
when: when + 1,
|
||||||
|
})
|
||||||
|
const msgHash1 = FeedV1.getMsgHash(msg1)
|
||||||
|
t.deepEquals(msg1.metadata.prev, [], 'msg1.prev is empty')
|
||||||
|
|
||||||
|
existing.set(msgHash1, msg1)
|
||||||
|
|
||||||
|
const msg2A = FeedV1.create({
|
||||||
|
keys,
|
||||||
|
content: { text: '2A' },
|
||||||
|
type: 'post',
|
||||||
|
existing,
|
||||||
|
when: when + 2,
|
||||||
|
})
|
||||||
|
t.deepEquals(msg2A.metadata.prev, [msgHash1], 'msg2A.prev is msg1')
|
||||||
|
|
||||||
|
const msg2B = FeedV1.create({
|
||||||
|
keys,
|
||||||
|
content: { text: '2B' },
|
||||||
|
type: 'post',
|
||||||
|
existing,
|
||||||
|
when: when + 2,
|
||||||
|
})
|
||||||
|
const msgHash2B = FeedV1.getMsgHash(msg2B)
|
||||||
|
t.deepEquals(msg2B.metadata.prev, [msgHash1], 'msg2B.prev is msg1')
|
||||||
|
|
||||||
|
existing.set(msgHash2B, msg2B)
|
||||||
|
|
||||||
|
const msg3 = FeedV1.create({
|
||||||
|
keys,
|
||||||
|
content: { text: '3' },
|
||||||
|
type: 'post',
|
||||||
|
existing,
|
||||||
|
when: when + 3,
|
||||||
|
})
|
||||||
|
const msgHash3 = FeedV1.getMsgHash(msg3)
|
||||||
|
t.deepEquals(msg3.metadata.prev, [msgHash2B], 'msg3.prev is msg2B')
|
||||||
|
existing.set(msgHash3, msg3)
|
||||||
|
|
||||||
|
const msgHash2A = FeedV1.getMsgHash(msg2A)
|
||||||
|
existing.set(msgHash2A, msg2A)
|
||||||
|
t.pass('msg2A comes into awareness')
|
||||||
|
|
||||||
|
const msg4 = FeedV1.create({
|
||||||
|
keys,
|
||||||
|
content: { text: '4' },
|
||||||
|
type: 'post',
|
||||||
|
existing,
|
||||||
|
when: when + 4,
|
||||||
|
})
|
||||||
|
t.deepEquals(
|
||||||
|
msg4.metadata.prev,
|
||||||
|
[msgHash1, msgHash3, msgHash2A],
|
||||||
|
'msg4.prev is [msg1(lipmaa),msg3(previous),msg2A(old fork as tip)]'
|
||||||
|
)
|
||||||
|
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue