refactor: Quorum takes the threshold rules from ChatRoomType (remove-convenient-groups, Phase 1)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9396643a9
commit
0a4453da81
9 files changed
+115
-58
No files matched your search
+2
-33
@@ -16,38 +16,7 @@ enum class ChatRoomType {
|
||||
|
||||
/**
|
||||
* Every member administers the room, and a change needs a threshold of them
|
||||
* to approve it.
|
||||
* to approve it. The rules for that threshold are [mobi.giza.compose.managers.Quorum]'s.
|
||||
*/
|
||||
ROBUST;
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Smallest group [ROBUST] is offered for. Below three, a majority is not a
|
||||
* meaningful check: two admins means every change needs both of them, and
|
||||
* one means the creator is deciding alone — which is [CONVENIENT] already.
|
||||
*/
|
||||
const val MINIMUM_ROBUST_GROUP_SIZE = 3
|
||||
|
||||
/**
|
||||
* Fewest admins a [ROBUST] room can be made to run on. One approval is not a
|
||||
* quorum — it is one person acting alone, which is [CONVENIENT].
|
||||
*/
|
||||
const val MINIMUM_QUORUM = 2
|
||||
|
||||
/**
|
||||
* The quorum a [ROBUST] group of [adminCount] starts on — a simple majority,
|
||||
* so no half of the group can move without the other. The user can move it
|
||||
* anywhere in [quorumRange] from there.
|
||||
*/
|
||||
fun defaultQuorum(adminCount: Int): Int = maxOf(MINIMUM_QUORUM, adminCount / 2 + 1)
|
||||
|
||||
/**
|
||||
* Quorums a [ROBUST] group of [adminCount] can be run on: never fewer than
|
||||
* [MINIMUM_QUORUM], never more than the admins who exist to approve.
|
||||
*/
|
||||
fun quorumRange(adminCount: Int): IntRange = MINIMUM_QUORUM..adminCount
|
||||
|
||||
/** Whether a group of [memberCount] people can be run as [ROBUST]. */
|
||||
fun isRobustAvailable(memberCount: Int): Boolean = memberCount >= MINIMUM_ROBUST_GROUP_SIZE
|
||||
}
|
||||
ROBUST,
|
||||
}
|
||||
+5
-6
@@ -30,7 +30,6 @@ import mobi.giza.compose.database.model.DkgSession
|
||||
import mobi.giza.compose.database.model.GiftWrapPayload
|
||||
import mobi.giza.compose.database.model.MarmotInnerEvent
|
||||
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
|
||||
import mobi.giza.compose.database.model.types.ChatRoomType
|
||||
import mobi.giza.compose.database.model.types.DkgApprovalStep
|
||||
import mobi.giza.compose.database.model.types.DkgRitualStage
|
||||
import mobi.giza.compose.extensions.toHex
|
||||
@@ -105,7 +104,7 @@ object ChillDkgRitualManager {
|
||||
|
||||
/**
|
||||
* Smallest group a ritual can run in. A shared key held by one person is
|
||||
* just a key, and [ChatRoomType.quorumRange] has nothing to offer below two.
|
||||
* just a key, and [Quorum.range] has nothing to offer below two.
|
||||
*/
|
||||
const val MINIMUM_PARTICIPANTS = 2
|
||||
|
||||
@@ -272,8 +271,8 @@ object ChillDkgRitualManager {
|
||||
require(members.size >= MINIMUM_PARTICIPANTS) {
|
||||
"A shared key needs at least $MINIMUM_PARTICIPANTS members, this ceremony has ${members.size}"
|
||||
}
|
||||
require(threshold in ChatRoomType.quorumRange(members.size)) {
|
||||
"Quorum $threshold is outside ${ChatRoomType.quorumRange(members.size)} for ${members.size} members"
|
||||
require(threshold in Quorum.range(members.size)) {
|
||||
"Quorum $threshold is outside ${Quorum.range(members.size)} for ${members.size} members"
|
||||
}
|
||||
|
||||
// A subgroup is named by whoever is making it. Falling back to the room
|
||||
@@ -508,10 +507,10 @@ object ChillDkgRitualManager {
|
||||
// A ritual is worth joining only at a quorum the group could have agreed
|
||||
// to. `t = 1` is a threshold key any single member can sign with, and
|
||||
// ChillDKG will happily generate one — the check has to be here.
|
||||
if (threshold !in ChatRoomType.quorumRange(members.size)) {
|
||||
if (threshold !in Quorum.range(members.size)) {
|
||||
logger.w(
|
||||
"DKG proposal $sessionId asks for a $threshold-of-${members.size} key, " +
|
||||
"outside ${ChatRoomType.quorumRange(members.size)}; dropping"
|
||||
"outside ${Quorum.range(members.size)}; dropping"
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package mobi.giza.compose.managers
|
||||
|
||||
/**
|
||||
* How many of a group's members have to agree, and how big a group has to be for
|
||||
* that to mean anything.
|
||||
*
|
||||
* Every group a member makes is run on a t-of-n key its members generate together
|
||||
* (docs/remove-convenient-groups.md), and these are the rules for `t` and `n`.
|
||||
* [ChillDkgRitualManager] enforces them, on the proposing side and again on every
|
||||
* device a proposal reaches; the screens that ask for a quorum offer only what it
|
||||
* will accept.
|
||||
*/
|
||||
object Quorum {
|
||||
/**
|
||||
* Fewest people a group can be made with, its creator included.
|
||||
*
|
||||
* Below three a quorum is not a check. Two means every change needs both of
|
||||
* them, so either can freeze the group by going quiet; one means the creator
|
||||
* is deciding alone. Two people who want to talk have a direct message.
|
||||
*/
|
||||
const val MINIMUM_GROUP_SIZE = 3
|
||||
|
||||
/**
|
||||
* Fewest approvals a change can be made on. One approval is not a quorum -- it
|
||||
* is one person acting alone.
|
||||
*/
|
||||
const val MINIMUM = 2
|
||||
|
||||
/**
|
||||
* The quorum a group of [memberCount] starts on: a simple majority, so no half
|
||||
* of the group can move without the other. The member making it can move it
|
||||
* anywhere in [range] from there.
|
||||
*/
|
||||
fun default(memberCount: Int): Int = maxOf(MINIMUM, memberCount / 2 + 1)
|
||||
|
||||
/**
|
||||
* Quorums a group of [memberCount] can be run on: never fewer than [MINIMUM],
|
||||
* never more than the members who exist to approve.
|
||||
*/
|
||||
fun range(memberCount: Int): IntRange = MINIMUM..memberCount
|
||||
|
||||
/** Whether a group of [memberCount] people, its creator included, can be made. */
|
||||
fun isPossible(memberCount: Int): Boolean = memberCount >= MINIMUM_GROUP_SIZE
|
||||
}
|
||||
@@ -62,16 +62,14 @@ object SubgroupManager {
|
||||
/**
|
||||
* The fewest admins a subgroup can be run by, the coordinator included.
|
||||
*
|
||||
* The same floor [mobi.giza.compose.database.model.types.ChatRoomType.MINIMUM_ROBUST_GROUP_SIZE]
|
||||
* states and for the same reason: below three a quorum is not a check, since
|
||||
* two admins means every decision needs both of them and one means the
|
||||
* coordinator is deciding alone.
|
||||
* [Quorum.MINIMUM_GROUP_SIZE], which says why: a subgroup is a group, and the
|
||||
* floor is the same one.
|
||||
*
|
||||
* The coordinator counts. They are a ChillDKG participant by construction and
|
||||
* hold a share whether or not anybody ticked their name, so a picker that
|
||||
* asked for three *others* would be quietly building a group of four.
|
||||
*/
|
||||
const val MINIMUM_ADMINS: Int = 3
|
||||
const val MINIMUM_ADMINS: Int = Quorum.MINIMUM_GROUP_SIZE
|
||||
|
||||
/**
|
||||
* Why these admins cannot hold a ceremony together, or null when they can.
|
||||
|
||||
+5
-5
@@ -17,7 +17,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import mobi.giza.compose.database.model.types.ChatRoomType
|
||||
import mobi.giza.compose.managers.Quorum
|
||||
import mobi.giza.compose.database.model.types.DkgRitualStage
|
||||
import mobi.giza.compose.identity.Identity
|
||||
import mobi.giza.compose.managers.ChillDkgRitualManager
|
||||
@@ -57,7 +57,7 @@ class CreateSharedKeyViewModel(
|
||||
* The quorum the ceremony will generate a key for. Pre-filled with the same
|
||||
* majority default the group-creation screen offers.
|
||||
*/
|
||||
val threshold: MutableState<Int> = mutableStateOf(ChatRoomType.MINIMUM_QUORUM)
|
||||
val threshold: MutableState<Int> = mutableStateOf(Quorum.MINIMUM)
|
||||
|
||||
fun initiate() {
|
||||
uiState = CreateSharedKeyUIState.Loading
|
||||
@@ -91,7 +91,7 @@ class CreateSharedKeyViewModel(
|
||||
}
|
||||
|
||||
val participantCount = ChillDkgRitualManager.memberPublicKeys(localChatRoom).size
|
||||
threshold.value = ChatRoomType.defaultQuorum(participantCount)
|
||||
threshold.value = Quorum.default(participantCount)
|
||||
.coerceIn(quorumRange(participantCount))
|
||||
|
||||
uiState = CreateSharedKeyUIState.Loaded(
|
||||
@@ -103,7 +103,7 @@ class CreateSharedKeyViewModel(
|
||||
|
||||
fun quorumRange(): IntRange {
|
||||
val loaded = uiState as? CreateSharedKeyUIState.Loaded
|
||||
?: return ChatRoomType.quorumRange(ChatRoomType.MINIMUM_QUORUM)
|
||||
?: return Quorum.range(Quorum.MINIMUM)
|
||||
|
||||
return quorumRange(loaded.participantCount)
|
||||
}
|
||||
@@ -111,7 +111,7 @@ class CreateSharedKeyViewModel(
|
||||
// Below the minimum the range runs backwards and is empty, which `coerceIn`
|
||||
// rejects outright. There is no ceremony to run at that size either.
|
||||
private fun quorumRange(participantCount: Int): IntRange =
|
||||
ChatRoomType.quorumRange(maxOf(participantCount, ChatRoomType.MINIMUM_QUORUM))
|
||||
Quorum.range(maxOf(participantCount, Quorum.MINIMUM))
|
||||
|
||||
fun setThreshold(value: Int) {
|
||||
if (isActionPending.value) return
|
||||
|
||||
+6
-5
@@ -33,6 +33,7 @@ import mobi.giza.compose.extensions.toHex
|
||||
import mobi.giza.compose.identity.Identity
|
||||
import mobi.giza.compose.managers.MarmotGroupFactory
|
||||
import mobi.giza.compose.managers.MarmotGroupName
|
||||
import mobi.giza.compose.managers.Quorum
|
||||
import mobi.giza.compose.nostr.MarmotProfile
|
||||
import mobi.giza.compose.nostr.Relays
|
||||
import mobi.giza.compose.repository.ChatRepository
|
||||
@@ -104,25 +105,25 @@ class SelectChatRoomTypeViewModel(
|
||||
val adminCount: Int = (memberPublicKeys + activeUserPublicKey).distinct().size
|
||||
|
||||
/** Quorums this group can be run on, under [ChatRoomType.ROBUST]. */
|
||||
val quorumRange: IntRange = ChatRoomType.quorumRange(adminCount)
|
||||
val quorumRange: IntRange = Quorum.range(adminCount)
|
||||
|
||||
/**
|
||||
* How many of [adminCount] admins have to approve a change, under
|
||||
* [ChatRoomType.ROBUST]. Starts on a simple majority and is the user's to move.
|
||||
*/
|
||||
val quorum: MutableState<Int> = mutableStateOf(ChatRoomType.defaultQuorum(adminCount))
|
||||
val quorum: MutableState<Int> = mutableStateOf(Quorum.default(adminCount))
|
||||
|
||||
/** Whether this group is big enough to be run as [ChatRoomType.ROBUST]. */
|
||||
val isRobustAvailable: Boolean = ChatRoomType.isRobustAvailable(adminCount)
|
||||
val isRobustAvailable: Boolean = Quorum.isPossible(adminCount)
|
||||
|
||||
/** Why robust is off the table, or null when it is available. */
|
||||
val robustUnavailableReason: String? = if (isRobustAvailable) {
|
||||
null
|
||||
} else {
|
||||
val shortfall = ChatRoomType.MINIMUM_ROBUST_GROUP_SIZE - adminCount
|
||||
val shortfall = Quorum.MINIMUM_GROUP_SIZE - adminCount
|
||||
val missing = if (shortfall == 1) "1 more person" else "$shortfall more people"
|
||||
|
||||
"Needs at least ${ChatRoomType.MINIMUM_ROBUST_GROUP_SIZE} people in the group. Go back and add $missing."
|
||||
"Needs at least ${Quorum.MINIMUM_GROUP_SIZE} people in the group. Go back and add $missing."
|
||||
}
|
||||
|
||||
fun initiate() {
|
||||
|
||||
+3
-3
@@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import mobi.giza.compose.database.model.types.ChatRoomType
|
||||
import mobi.giza.compose.managers.Quorum
|
||||
import mobi.giza.compose.managers.MarmotGroupCreation
|
||||
import mobi.giza.compose.repository.ChatRepository
|
||||
import mobi.giza.compose.repository.DkgRepository
|
||||
@@ -82,7 +82,7 @@ class SelectSubgroupAdminsViewModel(
|
||||
*/
|
||||
val name: MutableState<String> = mutableStateOf("")
|
||||
|
||||
val threshold: MutableState<Int> = mutableStateOf(ChatRoomType.MINIMUM_QUORUM)
|
||||
val threshold: MutableState<Int> = mutableStateOf(Quorum.MINIMUM)
|
||||
|
||||
/** Whether [retrying] has been read into the form, which happens once. */
|
||||
private var prefilled = false
|
||||
@@ -196,7 +196,7 @@ class SelectSubgroupAdminsViewModel(
|
||||
fun adminCount(): Int = selectedPublicKeys.size + 1
|
||||
|
||||
fun quorumRange(): IntRange =
|
||||
ChatRoomType.quorumRange(maxOf(adminCount(), ChatRoomType.MINIMUM_QUORUM))
|
||||
Quorum.range(maxOf(adminCount(), Quorum.MINIMUM))
|
||||
|
||||
fun setThreshold(value: Int) {
|
||||
if (isActionPending.value) return
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package mobi.giza.compose.managers
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** The rules for `t` and `n` every group is made on. docs/remove-convenient-groups.md. */
|
||||
class QuorumTest {
|
||||
|
||||
@Test
|
||||
fun `a group starts on a simple majority`() {
|
||||
assertEquals(
|
||||
listOf(2, 2, 3, 3, 4, 4, 5, 5),
|
||||
(2..9).map(Quorum::default),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the default is never below the minimum`() {
|
||||
(0..9).forEach { size ->
|
||||
assertTrue(Quorum.default(size) >= Quorum.MINIMUM, "default for $size")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a quorum runs from two to everybody`() {
|
||||
assertEquals(2..3, Quorum.range(3))
|
||||
assertEquals(2..7, Quorum.range(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the default is always one the range offers`() {
|
||||
(Quorum.MINIMUM_GROUP_SIZE..9).forEach { size ->
|
||||
assertTrue(Quorum.default(size) in Quorum.range(size), "default for $size")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a group needs three people, its creator included`() {
|
||||
assertFalse(Quorum.isPossible(1))
|
||||
assertFalse(Quorum.isPossible(2))
|
||||
assertTrue(Quorum.isPossible(3))
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -27,6 +27,7 @@ import mobi.giza.compose.database.model.types.ChatRoomType
|
||||
import mobi.giza.compose.identity.Identity
|
||||
import mobi.giza.compose.identity.IdentityKind
|
||||
import mobi.giza.compose.identity.toWalletId
|
||||
import mobi.giza.compose.managers.Quorum
|
||||
import mobi.giza.compose.nostr.MarmotProfile
|
||||
import mobi.giza.compose.repository.ChatRepository
|
||||
import mobi.giza.compose.repository.DkgRepository
|
||||
@@ -180,7 +181,7 @@ class GroupCreationJvmTest {
|
||||
viewModel.awaitSettled()
|
||||
|
||||
assertEquals(listOf(listOf(bob, cleo, dora)), recorder.nip17Rooms)
|
||||
assertEquals(listOf(ChatRoomType.defaultQuorum(4)), recorder.thresholds)
|
||||
assertEquals(listOf(Quorum.default(4)), recorder.thresholds)
|
||||
assertEquals(0, recorder.addMembersCalls, "nobody is invited into a Marmot room at creation")
|
||||
assertEquals(0, recorder.keyPackageLookups, "a NIP-17 room waits on no key package")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user