feat: an admin's device joins the confederation's key ceremony on its own (confederation-auto-join)
Each group's signed acceptance already names the admins it puts forward, and every admin must join for the ceremony to finish, so the tap could only say yes or stall everyone. Once every group has accepted, each admin's device now joins attempt 1 by itself, and follows a retry another person in the roster started, but never starts one. Join holds a mutex so a tap and the device's own join cannot send two different host-key messages. The accept and propose screens say what accepting now commits each admin to. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f25c4bea8f
commit
e7336d8ef1
7 files changed
+280
-7
No files matched your search
@@ -714,7 +714,7 @@
|
||||
<string name="open_for_days">Open for %1$d days</string>
|
||||
<string name="shorter">Shorter</string>
|
||||
<string name="longer">Longer</string>
|
||||
<string name="proposing_shows_admins_to_the_groups">Proposing shows this group's admins to the admins of every group named here, and each group that accepts shows its own. Once every group has accepted, every admin of every group can join the key ceremony.</string>
|
||||
<string name="proposing_shows_admins_to_the_groups">Proposing shows this group's admins to the admins of every group named here, and each group that accepts shows its own. Once every group has accepted, every admin's device joins the key ceremony on its own.</string>
|
||||
<string name="no_inbox_announced">Has announced no inbox, so the proposal could not reach it</string>
|
||||
<string name="this_group_has_no_inbox">Set up this group's inbox first: a confederation's proposal and every answer are sent from it.</string>
|
||||
<string name="too_many_groups_for_a_confederation">A confederation can name at most %1$d groups</string>
|
||||
@@ -735,7 +735,7 @@
|
||||
<string name="reason_optional">Reason (optional)</string>
|
||||
<string name="waiting_for_the_group_to_sign_its_answer">Waiting for the group to sign its answer</string>
|
||||
<string name="open_the_signing_session">Open the signing session</string>
|
||||
<string name="accepting_discloses_admins">Accepting shows this group's admins and threshold to the admins of every group the proposal names.</string>
|
||||
<string name="accepting_discloses_admins">Accepting shows this group's admins and threshold to the admins of every group the proposal names. Once every group has accepted, each of this group's admins joins the key ceremony and holds a share of the confederation's key.</string>
|
||||
<string name="this_device_does_not_hold_that_proposal">This device does not hold that proposal</string>
|
||||
<string name="could_not_ask_the_group_to_answer">Couldn't ask the group to answer</string>
|
||||
<string name="every_group_accepted_admins_across_groups">Every group accepted. %1$d admins across %2$d groups would hold the confederation's key.</string>
|
||||
|
||||
+10
@@ -123,6 +123,16 @@ interface ConfederationCeremonyDao {
|
||||
@Query("SELECT * FROM ConfederationCeremonyHeard WHERE sessionId = :sessionId ORDER BY heardAt ASC, kind ASC")
|
||||
suspend fun getCeremonyHeardForSession(sessionId: String): List<mobi.giza.compose.database.model.ConfederationCeremonyHeard>
|
||||
|
||||
/**
|
||||
* Changes whenever a ceremony message is heard or a ceremony ends: what the device's own join
|
||||
* looks again on, for a retry somebody else started (docs/confederation-auto-join.md).
|
||||
*/
|
||||
@Query(
|
||||
"SELECT (SELECT COUNT(*) FROM ConfederationCeremonyHeard) + " +
|
||||
"(SELECT COUNT(*) FROM ConfederationCeremony WHERE stage IN ('COMPLETE', 'FAILED'))"
|
||||
)
|
||||
fun observeCeremonyActivity(): Flow<Int>
|
||||
|
||||
@Query("SELECT * FROM ConfederationCeremonyHeard WHERE proposalId = :proposalId ORDER BY heardAt ASC, kind ASC")
|
||||
suspend fun getCeremonyHeardForProposal(proposalId: HexKey): List<mobi.giza.compose.database.model.ConfederationCeremonyHeard>
|
||||
|
||||
|
||||
+6
@@ -211,6 +211,12 @@ class DatabaseChatRepository(
|
||||
// the other groups' inboxes. docs/confederation-room-removal.md, D4.
|
||||
launch { mobi.giza.compose.managers.ConfederationBridge.observe(database = database, keyPair = keyPair) }
|
||||
keyPair.privKey?.let { secret ->
|
||||
// The key ceremony of a proposal every group accepted, joined with nobody asked:
|
||||
// each group's signed acceptance already named its admins.
|
||||
// docs/confederation-auto-join.md.
|
||||
if (ExperimentalFeatures.groupInbox) {
|
||||
launch { ConfederationRitualManager.observeAndJoin(database, keyPair.pubKey.toHex(), secret) }
|
||||
}
|
||||
launch {
|
||||
runCatching { ConfederationRitualManager.resumeRunning(database, keyPair.pubKey.toHex(), secret) }
|
||||
.onFailure { logger.e("Could not resume confederation ceremonies", it) }
|
||||
|
||||
+100
-3
@@ -26,8 +26,18 @@ import fr.acinq.bitcoin.crypto.confederation.ConfederationRoleId
|
||||
import fr.acinq.bitcoin.crypto.confederation.ConfederationRoster
|
||||
import fr.acinq.bitcoin.crypto.confederation.ConfederationTransition
|
||||
import fr.acinq.bitcoin.utils.Either
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.merge
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import mobi.giza.compose.database.GENESIS_AT
|
||||
import mobi.giza.compose.database.GizaDatabase
|
||||
import mobi.giza.compose.database.model.ChatMessage
|
||||
@@ -61,8 +71,10 @@ import mobi.giza.compose.nostr.confederation.ConfederationStatus
|
||||
* The shape is `ChillDkgRitualManager`'s and for its reasons. Every step is a pure function of what
|
||||
* this device has stored, so [advance] decides what to do next from which messages exist, never
|
||||
* from the stage, and a device that dies between rounds picks up on the next message or the next
|
||||
* [resumeRunning]. Joining is the only thing a member is asked; the rounds after it run by
|
||||
* themselves (docs/automatic-key-ceremony.md).
|
||||
* [resumeRunning]. Nobody is asked anything: a member's device joins on its own once every group has
|
||||
* accepted, since their group's signed acceptance already named them ([joinOnItsOwn],
|
||||
* docs/confederation-auto-join.md), and the rounds after it run by themselves
|
||||
* (docs/automatic-key-ceremony.md).
|
||||
*
|
||||
* What a coordinator did is done on every device:
|
||||
*
|
||||
@@ -87,6 +99,13 @@ object ConfederationRitualManager {
|
||||
|
||||
const val ABANDONED = "Abandoned by a participant"
|
||||
|
||||
/**
|
||||
* One join at a time on a device, whoever asked: the device on its own or a person's tap. Two at
|
||||
* once would each make the ceremony with its own random seed and send a 30381 for it, and two
|
||||
* different signed host-key messages from one person fail the ceremony as an equivocation.
|
||||
*/
|
||||
private val joining = Mutex()
|
||||
|
||||
// ---- what a ceremony is built from ------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -229,7 +248,10 @@ object ConfederationRitualManager {
|
||||
* running or finished ceremony is returned as it is. Throws, with a reason a person can read,
|
||||
* when this device cannot take part.
|
||||
*/
|
||||
suspend fun join(database: GizaDatabase, proposalId: HexKey, userPublicKey: HexKey, nostrPrivateKey: ByteArray): ConfederationCeremony {
|
||||
suspend fun join(database: GizaDatabase, proposalId: HexKey, userPublicKey: HexKey, nostrPrivateKey: ByteArray): ConfederationCeremony =
|
||||
joining.withLock { joinLocked(database, proposalId, userPublicKey, nostrPrivateKey) }
|
||||
|
||||
private suspend fun joinLocked(database: GizaDatabase, proposalId: HexKey, userPublicKey: HexKey, nostrPrivateKey: ByteArray): ConfederationCeremony {
|
||||
val latest = database.confederationCeremonyDao().getLatestForProposal(proposalId)?.takeIf { it.userPublicKey == userPublicKey }
|
||||
if (latest != null && latest.stage == ConfederationCeremonyStage.COMPLETE) return latest
|
||||
if (latest != null && latest.isRunning) {
|
||||
@@ -262,6 +284,81 @@ object ConfederationRitualManager {
|
||||
return database.confederationCeremonyDao().getCeremony(ceremony.id)!!
|
||||
}
|
||||
|
||||
/** How often [observeAndJoin] looks again with nothing new: what lets a device join once it can, such as when its inbox key is announced. */
|
||||
private val LOOK_AGAIN = 1.minutes
|
||||
|
||||
/**
|
||||
* Joins on its own for as long as it runs (docs/confederation-auto-join.md): whenever a statement
|
||||
* is filed, a ceremony message is heard or a ceremony ends, and every [LOOK_AGAIN] besides.
|
||||
*/
|
||||
suspend fun observeAndJoin(database: GizaDatabase, userPublicKey: HexKey, nostrPrivateKey: ByteArray) {
|
||||
val ticks: Flow<Unit> = flow { while (true) { emit(Unit); delay(LOOK_AGAIN) } }
|
||||
merge(
|
||||
ticks,
|
||||
database.confederationDao().observeProposals().map { },
|
||||
database.confederationCeremonyDao().observeCeremonyActivity().map { },
|
||||
).conflate().collect {
|
||||
try {
|
||||
joinOnItsOwn(database, userPublicKey, nostrPrivateKey)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
logger.e("Could not look for key ceremonies to join", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins, as [userPublicKey], every ceremony this device should join without asking anyone
|
||||
* (docs/confederation-auto-join.md), and returns the ceremonies it joined. See [shouldJoinOnItsOwn]
|
||||
* for which. A join that fails is logged and left for the next pass or the button.
|
||||
*/
|
||||
suspend fun joinOnItsOwn(database: GizaDatabase, userPublicKey: HexKey, nostrPrivateKey: ByteArray): List<ConfederationCeremony> =
|
||||
database.confederationDao().getProposals().mapNotNull { row ->
|
||||
try {
|
||||
if (!shouldJoinOnItsOwn(database, row.id, userPublicKey)) return@mapNotNull null
|
||||
join(database, row.id, userPublicKey, nostrPrivateKey).also {
|
||||
logger.i { "Joined the key ceremony for ${row.id.take(8)} on its own, as attempt ${it.attempt}" }
|
||||
}
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
logger.e("Could not join the key ceremony for ${row.id.take(8)} on its own", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether [userPublicKey]'s device joins [proposalId]'s ceremony now, with nobody asked:
|
||||
*
|
||||
* - **the first attempt**, once every group has accepted and [joinable] passes. Their group's
|
||||
* quorum signed an acceptance that names them as one of its admins, and every admin must
|
||||
* join for the ceremony to finish, so a tap could only ever say yes or stall everybody. The
|
||||
* way to say no is the group's answer, or **Abandon** once it runs;
|
||||
* - **a ceremony made by an older build** that is still waiting on its member's tap;
|
||||
* - **a retry somebody else started**: after this device's attempt `k` failed or was abandoned,
|
||||
* once it holds a message for attempt `k + 1` signed by another person in the roster.
|
||||
* It never starts `k + 1` itself. A failure that repeats -- a group that told two groups
|
||||
* different things -- would otherwise go round on every device for good; a person pressing
|
||||
* **Try again** is the decision, and everybody else follows it.
|
||||
*/
|
||||
suspend fun shouldJoinOnItsOwn(database: GizaDatabase, proposalId: HexKey, userPublicKey: HexKey): Boolean {
|
||||
if (ConfederationManager.status(database, proposalId)?.state != ConfederationProposalState.ACCEPTED) return false
|
||||
val latest = database.confederationCeremonyDao().getLatestForProposal(proposalId)?.takeIf { it.userPublicKey == userPublicKey }
|
||||
return when {
|
||||
latest == null -> joinable(database, proposalId, userPublicKey) is Either.Right
|
||||
latest.stage == ConfederationCeremonyStage.COMPLETE -> false
|
||||
latest.isRunning -> latest.approvedAt == null
|
||||
else -> {
|
||||
val people = planOf(latest)?.people ?: return false
|
||||
val next = ConfederationAttestation.ceremonyId(proposalId, latest.attempt + 1)
|
||||
val started = database.confederationCeremonyDao().getCeremonyHeardForSession(next)
|
||||
.any { it.pubKey != userPublicKey && it.pubKey in people }
|
||||
started && joinable(database, proposalId, userPublicKey) is Either.Right
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun newCeremony(user: HexKey, basis: Basis, plan: Plan, attempt: Int): ConfederationCeremony {
|
||||
val groups = plan.seatsOf(user).map { it.group }
|
||||
return ConfederationCeremony(
|
||||
|
||||
Reference in new issue
Block a user