fix(frost): resume a session whose own turn was cut short
Every step a session takes is driven by a message arriving, and a device killed part way through its own pass is waiting on one that has already been delivered. The signer set is recorded, so it will never be processed again; the partial signature it would have produced was never made. The coordinator asking again cannot reach past it either -- what a resend asks a member for is the message this device believes it already sent -- so the session is a dead end on both sides, and in a room with several open proposals it is the one that was mid-pass that stops. So one sweep that no message drives: on opening a room, every session in it that is not settled and whose owner has answered is handed to `advance`, which does what it always does. It publishes nothing new -- each step asks whether its output exists rather than whether a stage says it ran -- and only answered sessions are passed in, so the approval gate is not approached, let alone reached past. A refusal counts as an answer for the same reason: a device killed between deciding and publishing says it on the next open rather than never. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1e59fd2334
commit
a9532ff115
5 files changed
+229
-1
No files matched your search
+11
@@ -138,6 +138,17 @@ class DatabaseFrostSigningRepository(
|
||||
emptySet()
|
||||
}
|
||||
|
||||
override suspend fun resumeAnswered(localChatRoom: LocalChatRoom) {
|
||||
try {
|
||||
FrostSigningManager.resumeAnswered(database = database, localChatRoom = localChatRoom)
|
||||
} catch (e: Throwable) {
|
||||
// Each session already settles its own failure inside `advance`, so
|
||||
// anything reaching here is the sweep itself, and a repair that
|
||||
// takes the room down with it is worse than one that did not happen.
|
||||
logger.e("Error resuming signing sessions in ${localChatRoom.chatRoom.id}", e)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String) {
|
||||
try {
|
||||
FrostSigningManager.approve(
|
||||
|
||||
@@ -2119,6 +2119,52 @@ object FrostSigningManager {
|
||||
return asked - answered - session.userPublicKey
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes every answered session in a room as far as its stored messages now
|
||||
* allow. The one thing here that no message drives.
|
||||
*
|
||||
* Being message-driven is this manager's whole shape, and it is not quite
|
||||
* enough on its own. A pass of [advance] is where a device publishes its
|
||||
* nonce and its partial signature, and a device killed part way through one
|
||||
* -- the coordinator's signer set recorded, the signature not yet made --
|
||||
* has nothing left to wake it. The message that would have is already filed,
|
||||
* so it is never processed again; and the coordinator's [resend] cannot
|
||||
* reach past that either, because what it asks a quiet member for is a
|
||||
* message this device believes it has already sent.
|
||||
*
|
||||
* **It publishes nothing new.** [advance]'s own gates decide what goes out,
|
||||
* and each asks whether a message exists rather than whether a stage says it
|
||||
* ran, so a session with nothing outstanding takes no steps at all. Only
|
||||
* sessions whose owner has answered are passed in, so this cannot put this
|
||||
* device's material behind events nobody agreed to: the approval gate is the
|
||||
* one thing a resume must not reach past, and this does not come near it.
|
||||
*
|
||||
* A refusal is an answer too, and resumes for the same reason: a device
|
||||
* killed between deciding and publishing says it on the next pass rather
|
||||
* than leaving the group waiting on a member who has already made up their
|
||||
* mind.
|
||||
*/
|
||||
suspend fun resumeAnswered(database: CurareDatabase, localChatRoom: LocalChatRoom) {
|
||||
val answered = database.frostSigningSessionDao()
|
||||
.getSessionsForChatRoom(localChatRoom.chatRoom.id)
|
||||
.filter { session ->
|
||||
session.stage != FrostSigningStage.COMPLETE &&
|
||||
session.stage != FrostSigningStage.FAILED &&
|
||||
(session.signApprovedAt != null || session.signDeclinedAt != null)
|
||||
}
|
||||
|
||||
if (answered.isEmpty()) return
|
||||
|
||||
logger.i(
|
||||
"Resuming ${answered.size} answered signing session(s) in " +
|
||||
localChatRoom.chatRoom.id
|
||||
)
|
||||
|
||||
// One at a time, and one failing does not stop the rest: [advance]
|
||||
// settles its own session's failure and returns.
|
||||
answered.forEach { session -> advance(database, localChatRoom, session.id) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-publishes the messages *this* device authored for a session, because
|
||||
* the coordinator says it never received them.
|
||||
|
||||
+14
@@ -162,6 +162,18 @@ interface FrostSigningRepository {
|
||||
*/
|
||||
suspend fun membersOwing(session: FrostSigningSession): Set<HexKey>
|
||||
|
||||
/**
|
||||
* Takes every session in a room whose owner has answered as far as its
|
||||
* stored messages allow, for the steps no arriving message will drive.
|
||||
*
|
||||
* A device killed part way through a session's turn -- the signer set
|
||||
* recorded, its own partial signature not yet published -- is waiting on a
|
||||
* message that has already been delivered and will not arrive twice. This is
|
||||
* what gets it moving again, and it publishes nothing its owner has not
|
||||
* already agreed to.
|
||||
*/
|
||||
suspend fun resumeAnswered(localChatRoom: LocalChatRoom)
|
||||
|
||||
/** Agrees to sign, letting the session publish this device's part and run on. */
|
||||
suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String)
|
||||
|
||||
@@ -232,6 +244,8 @@ interface FrostSigningRepository {
|
||||
|
||||
override suspend fun membersOwing(session: FrostSigningSession): Set<HexKey> = emptySet()
|
||||
|
||||
override suspend fun resumeAnswered(localChatRoom: LocalChatRoom) = Unit
|
||||
|
||||
override suspend fun approve(localChatRoom: LocalChatRoom, sessionId: String) = Unit
|
||||
|
||||
override suspend fun decline(localChatRoom: LocalChatRoom, sessionId: String) = Unit
|
||||
|
||||
+23
@@ -202,6 +202,7 @@ class ChatMessageListViewModel(
|
||||
observeProposalsAwaitingYou()
|
||||
observeCeremoniesAwaitingYou()
|
||||
askForGroupHistory()
|
||||
resumeSigningSessions()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,6 +302,28 @@ class ChatMessageListViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets this room's answered signing sessions take any step they are able to.
|
||||
*
|
||||
* Everything else in a session moves on a message arriving, and a device
|
||||
* killed part way through its own turn is waiting on one that has already
|
||||
* been delivered: the signer set is filed, so it will not be processed
|
||||
* twice, and the coordinator asking again cannot help, because what it asks
|
||||
* a quiet member for is the partial signature this device believes it sent.
|
||||
* Opening the room is the next moment the app has.
|
||||
*
|
||||
* Cheap to call every time, and safe to: the manager publishes nothing that
|
||||
* is already on file and touches no session whose owner has yet to answer.
|
||||
* See `FrostSigningRepository.resumeAnswered`.
|
||||
*/
|
||||
private fun resumeSigningSessions() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
runCatching {
|
||||
frostSigningRepository.resumeAnswered(localChatRoom)
|
||||
}.onFailure { logger.w(throwable = it) { "Could not resume this room's signing sessions" } }
|
||||
}
|
||||
}
|
||||
|
||||
fun observeChatRoomFeed() {
|
||||
logger.d("observeChatRoomFeed")
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
|
||||
+135
-1
@@ -9,6 +9,7 @@ import fr.acinq.bitcoin.ByteVector
|
||||
import fr.acinq.bitcoin.ByteVector32
|
||||
import fr.acinq.bitcoin.PrivateKey
|
||||
import fr.acinq.bitcoin.crypto.frost.Frost
|
||||
import fr.acinq.bitcoin.crypto.frost.IndividualNonce
|
||||
import fr.acinq.bitcoin.crypto.frost.KeyMaterial
|
||||
import fr.acinq.bitcoin.crypto.frost.SecretNonce
|
||||
import kotlin.test.AfterTest
|
||||
@@ -57,7 +58,10 @@ import to.curare.compose.nostr.frost.FrostSigningEvents
|
||||
* tests below are mostly about the limits of that: the same bytes and not new
|
||||
* ones, nothing at all from a device whose owner has not decided, and nothing
|
||||
* from a nudge that did not come from the coordinator.
|
||||
|
||||
*
|
||||
* And a third repair at the end, for the stall neither half reaches: a device
|
||||
* killed part way through its own turn, waiting on a message that has already
|
||||
* been delivered. See `FrostSigningManager.resumeAnswered`.
|
||||
*/
|
||||
class FrostSigningResendJvmTest {
|
||||
private val participants = 3
|
||||
@@ -1097,4 +1101,134 @@ class FrostSigningResendJvmTest {
|
||||
assertEquals(1, queued(FrostSigningEvents.SIGNATURE).size)
|
||||
assertEquals(2, queued(FrostSigningEvents.PROPOSAL).size)
|
||||
}
|
||||
|
||||
// ---- the step no message drives ---------------------------------------
|
||||
|
||||
/**
|
||||
* Bob's device at the moment process death is worst: he agreed, his nonce
|
||||
* went out, the coordinator's signer set arrived and was filed -- and the
|
||||
* pass that would have signed his part never finished.
|
||||
*
|
||||
* Written into the row rather than received, because that is what the state
|
||||
* is: the signer set has been *recorded*, so redelivering it changes
|
||||
* nothing, and nothing else in the protocol will ever ask Bob again.
|
||||
*/
|
||||
private suspend fun killedBeforeSigning(): FrostSigningSession {
|
||||
val session = proposalArrivedAt(bob)
|
||||
|
||||
FrostSigningManager.approve(database = db, localChatRoom = room, sessionId = session.id)
|
||||
|
||||
// The aggregate the coordinator would have sent: Alice's nonce and the
|
||||
// one Bob's device published from the seed on its item row.
|
||||
val stored = db.frostSigningSessionDao().getItems(session.id).single()
|
||||
val bobsNonce = db.frostSigningSessionDao()
|
||||
.getMessage(session.id, FrostSigningEvents.NONCE, bob)!!
|
||||
.payload
|
||||
|
||||
val aggregated = IndividualNonce.aggregate(
|
||||
listOf(
|
||||
IndividualNonce(publicNonceOf(alice).hexToByteArray()),
|
||||
IndividualNonce(bobsNonce.hexToByteArray())
|
||||
)
|
||||
).right!!
|
||||
|
||||
db.frostSigningSessionDao().upsertItems(
|
||||
listOf(stored.copy(aggregatedNonce = aggregated.toByteArray().toHex()))
|
||||
)
|
||||
db.frostSigningSessionDao().upsert(
|
||||
session(session.id).copy(
|
||||
signerIds = "0,1",
|
||||
stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES
|
||||
)
|
||||
)
|
||||
|
||||
return session(session.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an answered session signs its part when the room is next opened`() = runBlocking {
|
||||
openDevice(bob)
|
||||
val session = killedBeforeSigning()
|
||||
|
||||
assertTrue(queued(FrostSigningEvents.PARTIAL_SIGNATURE).isEmpty())
|
||||
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
|
||||
// The step the session had everything for and no way to reach.
|
||||
val part = queued(FrostSigningEvents.PARTIAL_SIGNATURE).single()
|
||||
val filed = db.frostSigningSessionDao()
|
||||
.getMessage(session.id, FrostSigningEvents.PARTIAL_SIGNATURE, bob)
|
||||
|
||||
assertEquals(64, part.content.length)
|
||||
assertEquals(part.content, filed?.payload)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resuming twice publishes one partial signature`() = runBlocking {
|
||||
openDevice(bob)
|
||||
killedBeforeSigning()
|
||||
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
|
||||
// Every step asks whether its output exists rather than whether a stage
|
||||
// says it ran, which is what makes a sweep safe to call on every open.
|
||||
assertEquals(1, queued(FrostSigningEvents.PARTIAL_SIGNATURE).size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a session its owner has not answered is left alone`() = runBlocking {
|
||||
openDevice(bob)
|
||||
val session = proposalArrivedAt(bob)
|
||||
|
||||
// The gate a resume must not reach past. Bob has the proposal and
|
||||
// nothing else; a sweep that signed here would have a relay delivering
|
||||
// an event to a phone in a pocket be enough to make the group sign.
|
||||
db.frostSigningSessionDao().upsert(
|
||||
session(session.id).copy(
|
||||
signerIds = "0,1",
|
||||
stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES
|
||||
)
|
||||
)
|
||||
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
|
||||
assertTrue(queued(FrostSigningEvents.NONCE).isEmpty())
|
||||
assertTrue(queued(FrostSigningEvents.PARTIAL_SIGNATURE).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a refusal decided but never sent goes out on the next open`() = runBlocking {
|
||||
openDevice(bob)
|
||||
val session = proposalArrivedAt(bob)
|
||||
|
||||
// Killed between deciding and publishing: the row says no, the group has
|
||||
// not been told, and the coordinator is waiting on a member who has
|
||||
// already made up their mind.
|
||||
db.frostSigningSessionDao().upsert(
|
||||
session(session.id).copy(signDeclinedAt = Clock.System.now())
|
||||
)
|
||||
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
|
||||
assertEquals(1, queued(FrostSigningEvents.DECLINE).size)
|
||||
assertTrue(queued(FrostSigningEvents.NONCE).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a settled session is not swept`() = runBlocking {
|
||||
openDevice(bob)
|
||||
val session = proposalArrivedAt(bob)
|
||||
|
||||
FrostSigningManager.approve(database = db, localChatRoom = room, sessionId = session.id)
|
||||
db.frostSigningSessionDao().upsert(
|
||||
session(session.id).copy(stage = FrostSigningStage.FAILED)
|
||||
)
|
||||
|
||||
val before = queued(FrostSigningEvents.NONCE).size
|
||||
|
||||
FrostSigningManager.resumeAnswered(database = db, localChatRoom = room)
|
||||
|
||||
assertEquals(before, queued(FrostSigningEvents.NONCE).size)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user