From e259634513461ba787c729c12e91d69a09fa0665 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 23 Oct 2026 16:19:28 +0200 Subject: [PATCH] fix(dkg): a Marmot room hosts its subgroups' ceremonies and never one of its own (Phase 1) proposeRitual now refuses a ceremony with no parent in a Marmot room, and acceptProposal drops one arriving there. A Marmot room's id is derived from the key it already has, so a key of its own is one it could never use -- and the shared-key screen offering to start one in a parent's room is how a second, whole-room ceremony opened beside a subgroup's. Co-Authored-By: Claude Opus 5.5 --- .../compose/managers/ChillDkgRitualManager.kt | 31 ++++++++ .../SubgroupCeremonyInParentRoomTest.kt | 73 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/ChillDkgRitualManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/ChillDkgRitualManager.kt index 28cd7985..a242d222 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/ChillDkgRitualManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/ChillDkgRitualManager.kt @@ -138,6 +138,14 @@ object ChillDkgRitualManager { fun canRunRitual(localChatRoom: LocalChatRoom): Boolean = memberPublicKeys(localChatRoom).size >= MINIMUM_PARTICIPANTS + /** + * Whether [localChatRoom] is a Marmot room, and so can host a subgroup's + * ceremony but never one of its own. The same reading [broadcast] makes to + * pick a transport. + */ + fun isMarmotRoom(localChatRoom: LocalChatRoom): Boolean = + localChatRoom.chatRoom.mlsGroupState != null + /** * Everyone a ceremony is being run with — which is not the same question as * who is in the room it runs in. @@ -245,6 +253,15 @@ object ChillDkgRitualManager { "${(members - room).joinToString { it.take(8) }}, who are not in it" } + // A Marmot room's id is derived from the key it already has, so a ceremony + // of its own would make a key the room could never answer to. It hosts its + // subgroups' ceremonies and nothing else. The screens stopped offering one + // here, and this is what makes that a rule rather than a screen not drawing + // a button -- see docs/ceremony-creation.md. + require(parentChatRoomId != null || !isMarmotRoom(localChatRoom)) { + "${localChatRoom.chatRoom.id} is a Marmot room; it can only host a subgroup's ceremony" + } + require(members.size >= MINIMUM_PARTICIPANTS) { "A shared key needs at least $MINIMUM_PARTICIPANTS members, this ceremony has ${members.size}" } @@ -399,6 +416,20 @@ object ChillDkgRitualManager { ): DkgSession? { database.dkgSessionDao().getSessionById(sessionId)?.let { return it } + // The receiving half of `proposeRitual`'s refusal. A build from before it + // can still put a group's own ceremony on the wire in a Marmot room, and + // recording it would show every member a ceremony whose key the room can + // never use -- which is how a second ceremony ended up beside a subgroup's. + if (isMarmotRoom(localChatRoom) && + DkgRitualEvents.parseParentChatRoomId(innerEvent.tags) == null + ) { + logger.w( + "DKG proposal $sessionId names no parent in Marmot room " + + "${localChatRoom.chatRoom.id}; dropping" + ) + return null + } + // The participant set is the proposal's own p-tags plus its sender, NOT // this device's view of the room. `n` is hashed into the session identity, // so two devices that disagree on it cannot complete a ritual together — diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupCeremonyInParentRoomTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupCeremonyInParentRoomTest.kt index ed890bc3..6d38ea26 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupCeremonyInParentRoomTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/SubgroupCeremonyInParentRoomTest.kt @@ -369,6 +369,79 @@ class SubgroupCeremonyInParentRoomTest { ) } + /** + * A Marmot room hosts its subgroups' ceremonies and never one of its own. + * + * Its id is derived from the key it already has, so a key of its own is one + * the room could never answer to. This is what opened a whole-room ceremony + * beside a subgroup's when the shared-key screen offered "Start key ceremony" + * in the parent's room -- see docs/ceremony-creation.md. + */ + @Test + fun `a Marmot room refuses a ceremony of its own`(): Unit = runBlocking { + val room = parentRoom(withOwnKey = false) + + val refused = runCatching { + ChillDkgRitualManager.proposeRitual( + database = parent, + localChatRoom = room, + userPublicKey = user, + nostrPrivateKey = coordinator.privKey!!, + threshold = quorum, + ) + } + + assertTrue(refused.isFailure, "a ceremony with no parent is refused in a Marmot room") + assertNull( + parent.dkgSessionDao().getLatestSessionForChatRoom(parentRoomId), + "and no session is recorded", + ) + assertTrue(innerEvents(parent).isEmpty(), "and nothing goes out") + + // A subgroup's, in the same room, is still what the room is for. + openCeremony(room) + assertNotNull(parent.dkgSessionDao().getLatestSessionForChatRoom(parentRoomId)) + } + + /** + * The receiving half: a build from before the refusal can still send one. + */ + @Test + fun `a proposal naming no parent in a Marmot room is dropped`(): Unit = runBlocking { + val theirRoom = receiverRoom() + + val proposal = Event( + id = "dd".repeat(32), + pubKey = user, + createdAt = 1, + kind = DkgRitualEvents.PROPOSAL, + // The shape `broadcast` gives a group's own proposal: the roster as + // p-tags, a threshold, a name, and no parent. + tags = admins.map { arrayOf("p", it) }.toTypedArray() + + DkgRitualEvents.assembleTags( + sessionId = "ff".repeat(32), + threshold = quorum, + subject = "Ekklesia", + ), + content = "", + sig = "" + ) + + ChillDkgRitualManager.processRitualPayload( + database = receiver, + localChatRoom = theirRoom, + innerEvent = proposal, + userPublicKey = alice, + nostrPrivateKey = aliceKeys.privKey!! + ) + + assertNull( + receiver.dkgSessionDao().getSessionById("ff".repeat(32)), + "a whole-room ceremony is not recorded in a Marmot room", + ) + assertTrue(innerEvents(receiver).isEmpty(), "and nothing is published") + } + /** * The parent does not start signing as its own child. *