diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml
index 307ca511..4e9ab9c7 100644
--- a/composeApp/src/commonMain/composeResources/values/strings.xml
+++ b/composeApp/src/commonMain/composeResources/values/strings.xml
@@ -134,7 +134,10 @@
Ask the group to sign the new shares
Asked the group to sign the new shares
See the proposal
- Share rotation proposed; every remaining member is asked to take part
+ Share rotation proposed; every other member is asked to take part
+ Rotate the group's shares?
+ Every member with a share takes part.
+ Left out again, until they are brought back:
Library
Lightning invoice
Open in a wallet app
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt
index 68eaa193..7202b594 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/ShareRefreshManager.kt
@@ -113,9 +113,13 @@ object ShareRefreshManager {
* Opens a refresh of [key], making this device the coordinator. Every live
* participant not in [withheld] is a recipient.
*
- * The proposer is auto-approved for nothing: proposing a refresh is not
- * agreeing to withhold whomever the form defaulted to, so it is asked at both
- * gates like everyone else.
+ * The proposer is approved at both gates on proposal
+ * (docs/rotation-waits-for-key-state.md, Phase 4). `DEAL` is the decision about
+ * the withheld list, and the screen only proposes after its owner has confirmed
+ * a dialog naming everyone withheld -- so the tap was that decision. `CONFIRM`
+ * guards against a coordinator substituting a table nobody dealt, and the
+ * proposer is the coordinator: its own CertEq check does that work, and a tap
+ * adds nothing to it. Every other recipient is asked at both gates as before.
*/
suspend fun proposeRefresh(
database: GizaDatabase,
@@ -158,6 +162,8 @@ object ShareRefreshManager {
withheld = ShareRefreshSession.formatWithheld(allWithheld),
round1Random = RandomInstance.bytes(32).toHex(),
auxRandom = RandomInstance.bytes(32).toHex(),
+ dealApprovedAt = Clock.System.now(),
+ confirmApprovedAt = Clock.System.now(),
)
database.shareRefreshDao().upsert(session)
announce(database, session, ChatMessage.TYPE_SHARE_REFRESH_STARTED, startedLine(session, shares), userPublicKey)
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt
index 272c0c54..79a5c50d 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt
@@ -123,6 +123,9 @@ import mobi.giza.compose.resources.share_rotation_requested
import mobi.giza.compose.resources.not_now
import mobi.giza.compose.resources.rotate_shares
import mobi.giza.compose.resources.rotation_held_running
+import mobi.giza.compose.resources.rotate_shares_confirm_title
+import mobi.giza.compose.resources.rotate_shares_confirm_everyone
+import mobi.giza.compose.resources.rotate_shares_confirm_left_out
import mobi.giza.compose.resources.rotation_held_awaiting_signatures
import mobi.giza.compose.resources.rotation_held_behind
import mobi.giza.compose.resources.rotation_held_after_removal
@@ -216,6 +219,37 @@ fun ChatRoomDetailScreen(
)
}
+ // The plain rotate button's confirmation. Proposing approves the proposer, so
+ // this names who the rotation leaves out (docs/rotation-waits-for-key-state.md,
+ // Phase 4).
+ chatRoomDetailViewModel.rotationConfirmation?.let { confirmation ->
+ AlertDialog(
+ onDismissRequest = { chatRoomDetailViewModel.dismissRotationConfirmation() },
+ title = { Text(stringResource(Res.string.rotate_shares_confirm_title)) },
+ text = {
+ Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) {
+ Text(stringResource(Res.string.shares_rotation_caveat))
+ if (confirmation.leftOut.isEmpty()) {
+ Text(stringResource(Res.string.rotate_shares_confirm_everyone))
+ } else {
+ Text(stringResource(Res.string.rotate_shares_confirm_left_out))
+ confirmation.leftOut.forEach { name -> Text(name, style = MaterialTheme.typography.bodyMedium) }
+ }
+ }
+ },
+ confirmButton = {
+ TextButton(onClick = { chatRoomDetailViewModel.confirmRotation(confirmation) { failure -> notify(failure ?: rotationRequested) } }) {
+ Text(stringResource(Res.string.rotate_shares))
+ }
+ },
+ dismissButton = {
+ TextButton(onClick = { chatRoomDetailViewModel.dismissRotationConfirmation() }) {
+ Text(stringResource(Res.string.not_now))
+ }
+ },
+ )
+ }
+
ScreenStateTransition(chatRoomDetailViewModel.chatRoomDetailUIState) { uiState ->
when (val chatRoomDetailUIState = uiState) {
is ChatRoomDetailUIState.Error -> {
@@ -463,9 +497,7 @@ fun ChatRoomDetailScreen(
FilledTonalButton(
enabled = rotation == SharesSummary.Rotation.Available && !actionPending,
onClick = {
- chatRoomDetailViewModel.rotateShares(chatRoomDetailUIState.localChatRoom) { failure ->
- notify(failure ?: rotationRequested)
- }
+ chatRoomDetailViewModel.askToRotate(chatRoomDetailUIState.localChatRoom, shares)
}
) {
Text(stringResource(Res.string.rotate_shares))
diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt
index 9112b03a..7b1080c1 100644
--- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt
+++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailViewModel.kt
@@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.launch
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.database.model.types.MarmotReindexReport
+import mobi.giza.compose.database.model.types.ShareStanding
import mobi.giza.compose.managers.WalletRefusal
import mobi.giza.compose.nostr.MarmotProfile
import mobi.giza.compose.repository.ChatRepository
@@ -203,6 +204,34 @@ class ChatRoomDetailViewModel(
fun askToBeRepaired(localChatRoom: LocalChatRoom, onDone: (String?) -> Unit) =
sharesAction(onDone) { chatRepository.askToBeRepaired(localChatRoom) }
+ /**
+ * A rotation this device's owner is being asked to confirm, and who it leaves
+ * out: the members stale at this generation, withheld again as stale. The
+ * proposal approves the proposer, so this is where they decide who is withheld
+ * (docs/rotation-waits-for-key-state.md, Phase 4).
+ */
+ data class RotationConfirmation(val localChatRoom: LocalChatRoom, val leftOut: List)
+
+ var rotationConfirmation: RotationConfirmation? by mutableStateOf(null)
+ private set
+
+ fun askToRotate(localChatRoom: LocalChatRoom, shares: SharesSummary) {
+ rotationConfirmation = RotationConfirmation(
+ localChatRoom,
+ shares.members.filter { it.standing == ShareStanding.STALE }.map { it.name },
+ )
+ }
+
+ fun dismissRotationConfirmation() {
+ rotationConfirmation = null
+ }
+
+ /** Proposes the rotation [rotationConfirmation] asked about. */
+ fun confirmRotation(confirmation: RotationConfirmation, onDone: (String?) -> Unit) {
+ rotationConfirmation = null
+ rotateShares(confirmation.localChatRoom, onDone)
+ }
+
/** A removed member who still holds a share of the room's key, for the offer to rotate without them. */
data class RotationOffer(val localChatRoom: LocalChatRoom, val memberPublicKey: HexKey)
diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt
index 42513bc5..895392b5 100644
--- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt
+++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/ShareRefreshRitualJvmTest.kt
@@ -38,6 +38,7 @@ import mobi.giza.compose.database.repository.DatabaseChatRepository
import mobi.giza.compose.database.model.types.DkgRitualStage
import mobi.giza.compose.database.model.types.FrostSigningStage
import mobi.giza.compose.database.model.types.ShareGenerationState
+import mobi.giza.compose.database.model.types.ShareRefreshApprovalStep
import mobi.giza.compose.database.model.types.ShareRefreshStage
import mobi.giza.compose.database.model.types.ShareStanding
import mobi.giza.compose.extensions.toHex
@@ -217,18 +218,56 @@ class ShareRefreshRitualJvmTest {
}
@Test
- fun `nothing of a member's own goes out before they approve`() = runBlocking {
+ fun `nothing of a member's own goes out before they approve, but the proposer is not asked`() = runBlocking {
val group = Group(3, 2)
val all = devicesFor(group)
val refresh = propose(all[0])
pump(all)
- all.forEach { device ->
- assertEquals(listOf(ShareRefreshEvents.PROPOSAL).takeIf { device.index == 0 } ?: emptyList(), outbox(device).map { it.kind }, "device ${device.index}")
- assertNotNull(ShareRefreshManager.pendingApproval(device.db, device.refresh(refresh.id)!!), "device ${device.index} must be asked")
+ // The proposer confirmed who is withheld before proposing, and is the
+ // coordinator CONFIRM guards against: it deals at once and is asked nothing
+ // (docs/rotation-waits-for-key-state.md, Phase 4).
+ assertEquals(listOf(ShareRefreshEvents.PROPOSAL, ShareRefreshEvents.ROUND_1), outbox(all[0]).map { it.kind })
+ assertNull(ShareRefreshManager.pendingApproval(all[0].db, all[0].refresh(refresh.id)!!), "the proposer must not be asked")
+ assertTrue(all[0].db.chatMessageDao().getChatMessagesByChatRoomId(group.roomId).none { it.chatMessage.messageType == ChatMessage.TYPE_SHARE_REFRESH_APPROVAL_NEEDED })
+
+ // Everyone else is asked, and has sent nothing.
+ all.drop(1).forEach { device ->
+ assertEquals(emptyList(), outbox(device).map { it.kind }, "device ${device.index}")
+ assertEquals(ShareRefreshApprovalStep.DEAL, ShareRefreshManager.pendingApproval(device.db, device.refresh(refresh.id)!!), "device ${device.index} must be asked")
}
- // The proposer is asked like anyone: proposing is not agreeing to withhold whoever the form defaulted to.
- assertTrue(all[0].db.chatMessageDao().getChatMessagesByChatRoomId(group.roomId).any { it.chatMessage.messageType == ChatMessage.TYPE_SHARE_REFRESH_APPROVAL_NEEDED })
+
+ // And once they have dealt, they are asked again before confirming; the proposer is not.
+ all.drop(1).forEach { ShareRefreshManager.approve(it.db, it.room, refresh.id, it.privateKey) }
+ pump(all)
+ assertNull(ShareRefreshManager.pendingApproval(all[0].db, all[0].refresh(refresh.id)!!))
+ all.drop(1).forEach { device ->
+ assertEquals(ShareRefreshApprovalStep.CONFIRM, ShareRefreshManager.pendingApproval(device.db, device.refresh(refresh.id)!!), "device ${device.index}")
+ }
+ }
+
+ /**
+ * Two members propose from one base before either hears of the other. Each is
+ * approved for its own and refuses the other on arrival, as a recipient,
+ * out loud -- so neither stalls and nothing forks.
+ */
+ @Test
+ fun `two proposals crossing on the wire end with neither stuck and at most one generation`() = runBlocking {
+ val group = Group(3, 2)
+ val all = devicesFor(group)
+ val first = propose(all[0])
+ val second = propose(all[1])
+ pump(all)
+ approveAll(all, first.id)
+ approveAll(all, second.id)
+
+ all.forEach { device ->
+ val stages = listOf(first.id, second.id).map { device.refresh(it)?.stage }
+ assertTrue(stages.none { it != null && it != ShareRefreshStage.COMPLETE && it != ShareRefreshStage.FAILED }, "device ${device.index} left one running: $stages")
+ assertTrue(stages.count { it == ShareRefreshStage.COMPLETE } <= 1, "device ${device.index}: $stages")
+ assertTrue(ShareRefreshManager.rotationHold(device.db, device.room, device.key()) !is RotationHold.Running, "device ${device.index}")
+ }
+ assertEquals(1, all.map { it.active().publicShares }.toSet().size, "every device on one table")
}
@Test
diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailSharesJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailSharesJvmTest.kt
index ad25eae1..a2f3e21b 100644
--- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailSharesJvmTest.kt
+++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/view/model/ChatRoomDetailSharesJvmTest.kt
@@ -13,6 +13,7 @@ import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.withTimeout
import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
+import mobi.giza.compose.database.model.types.ShareStanding
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.NostrRepository
import mobi.giza.compose.ui.view.state.ChatRoomDetailUIState
@@ -21,6 +22,7 @@ import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
+import kotlin.test.assertNull
import kotlin.test.assertTrue
/**
@@ -54,6 +56,8 @@ class ChatRoomDetailSharesJvmTest {
ChatRepository by ChatRepository.NO_OP_CHAT_REPOSITORY {
val inputs = MutableSharedFlow(replay = 1)
var reads = 0
+ var rotations = 0
+ override suspend fun rotateShares(localChatRoom: LocalChatRoom): String? { rotations++; return null }
override suspend fun getChatRoomByIdentifier(id: HexKey): LocalChatRoom? = room.takeIf { id == room.chatRoom.id }
override suspend fun sharesSummary(localChatRoom: LocalChatRoom): SharesSummary { reads++; return shares }
override fun observeSharesInputs(chatRoomId: String): Flow = inputs
@@ -102,4 +106,28 @@ class ChatRoomDetailSharesJvmTest {
assertTrue(reads in 1..2, "20 changes in a burst read the section $reads times")
assertEquals(SharesSummary.Rotation.Available, (viewModel.chatRoomDetailUIState as ChatRoomDetailUIState.Loaded).shares?.rotation)
}
+
+ @Test
+ fun `rotating asks first, naming who it leaves out`() = runBlocking {
+ val shares = summary(SharesSummary.Rotation.Available).copy(
+ members = listOf(
+ SharesSummary.Member("b".repeat(64), "Bongani", ShareStanding.LIVE, isSelf = false),
+ SharesSummary.Member("c".repeat(64), "Carol", ShareStanding.STALE, isSelf = false),
+ SharesSummary.Member("d".repeat(64), "Dumi", ShareStanding.RETIRED, isSelf = false),
+ )
+ )
+ val repository = Repository(localChatRoom, shares)
+ val viewModel = viewModel(repository)
+
+ viewModel.askToRotate(localChatRoom, shares)
+ val confirmation = viewModel.rotationConfirmation!!
+ assertEquals(listOf("Carol"), confirmation.leftOut, "the stale are withheld again; the retired are gone already")
+ assertEquals(0, repository.rotations, "nothing is proposed before the owner confirms")
+
+ val done = kotlinx.coroutines.CompletableDeferred()
+ viewModel.confirmRotation(confirmation) { done.complete(it) }
+ assertNull(withTimeout(5_000) { done.await() })
+ assertEquals(1, repository.rotations)
+ assertNull(viewModel.rotationConfirmation)
+ }
}