Skip to content

Commit

Permalink
Tangle can be built from randomly ordered msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Feb 28, 2024
1 parent c31580a commit cbeabab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/msg-v4/tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Tangle {
* @param {Msg} msg
*/
add(msgID, msg) {
// Add the root msg
if (msgID === this.#rootID && !this.#rootMsg) {
this.#tips.add(msgID)
this.#perDepth.set(0, [msgID])
Expand All @@ -101,10 +102,20 @@ class Tangle {
return
}

// Add affix msg
const tangles = msg.metadata.tangles
if (msgID !== this.#rootID && tangles[this.#rootID]) {
if (this.#depth.has(msgID)) return
this.#tips.add(msgID)
let hasSuccessor = false
for (const prevs of this.#prev.values()) {
if (prevs.includes(msgID)) {
hasSuccessor = true
break
}
}
if (!hasSuccessor) {
this.#tips.add(msgID)
}
const prev = tangles[this.#rootID].prev
for (const p of prev) {
this.#tips.delete(p)
Expand Down
46 changes: 46 additions & 0 deletions test/msg-v4/tangles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,49 @@ test('MsgV4.Tangle lipmaa in multi-author tangle', (t) => {
'A:msg4 points to A:msg1,B:msg3'
)
})

test('MsgV4.Tangle can add msgs in random order', (t) => {
const keypairA = Keypair.generate('ed25519', 'alice')
const accountA = MsgV4.getMsgID(
MsgV4.createAccount(keypairA, 'person', 'alice')
)
const mootA = MsgV4.createMoot(accountA, 'post', keypairA)
const mootAID = MsgV4.getMsgID(mootA)

const tangleBuilder = new MsgV4.Tangle(mootAID)
tangleBuilder.add(mootAID, mootA)

const msg1 = MsgV4.create({
account: accountA,
accountTips: [accountA],
domain: 'post',
data: { text: 'Hello world!' },
tangles: {
[mootAID]: tangleBuilder,
},
keypair: keypairA,
})
const msgID1 = MsgV4.getMsgID(msg1)
tangleBuilder.add(msgID1, msg1)

const msg2 = MsgV4.create({
account: accountA,
accountTips: [accountA],
domain: 'post',
data: { text: 'Hello world!' },
tangles: {
[mootAID]: tangleBuilder,
},
keypair: keypairA,
})
const msgID2 = MsgV4.getMsgID(msg2)
tangleBuilder.add(msgID1, msg1)

const tangle = new MsgV4.Tangle(mootAID)
tangle.add(mootAID, mootA)
tangle.add(msgID2, msg2)
tangle.add(msgID1, msg1)

assert.deepEqual(tangle.topoSort(), [mootAID, msgID1, msgID2]);
assert.deepEqual([...tangle.tips], [msgID2], 'tangle tips')
})

0 comments on commit cbeabab

Please sign in to comment.