feat(marmot): a commit every relay refuses is discarded, and the room is free again
docs/marmot-v2.md, Phase 6: "a terminal rejection resolves FAILED, discarding the pending state with the live group already correct." The phase's commit built the CONFIRMED half and left every other outcome pending, on the grounds that the broadcast queue reported a timeout and a relay's refusal the same way -- both were "failed" -- and a timeout must stay pending, since its retry re-sends the same bytes and may yet be accepted. The cost of that was a room whose commit a relay refused outright: its obligation never resolved, `canPrepareLocalCommit` stayed false, and every later invite, removal or disband was refused with "try again in a moment" for as long as the room existed. The refusal was re-sent on every launch and refused again. The two are distinguishable, and now are. `RelayPool` already surfaces an `["OK", id, false, reason]` as a `NostrPublishException` carrying the relay's reason, apart from socket errors and timeouts; `BroadcastStatus.of(error)` turns that into a status. A reason a relay will keep giving -- `blocked:`, `invalid:`, `pow:`, `restricted:` -- is the new "rejected", which the requeue on launch leaves alone: the same bytes to the same relay get the same answer. A `rate-limited:` or `error:` refusal is "failed" like a timeout, because those change. And a `duplicate:` refusal is "published": the relay is saying it has the event, which is what published means, and it is the answer a byte-identical retry gets from a relay that took the first copy. The four statuses that were string literals across the sync pump, the navigation gate and the DAO are named on the same object. `DatabaseNostrRepository.broadcastProcessed` gains the relay's reason and a "rejected" branch. One relay's refusal decides nothing -- another may still accept, and a timeout on another is retried -- so the obligation is discarded only when every request row for the event is "rejected": `MarmotEngine.discardRejected` resolves it FAILED, the gate drops the clone, the live group having never advanced, and the room can prepare a commit again. Nothing is told to convergence, since a commit no relay carries is not a branch any member will see. The room says what happened, in the relay's words, because the screen that asked closed when the commit was made: an invite gets the existing `TYPE_MEMBER_INVITE_FAILED` line, and a removal or a disband step gets a new `TYPE_CHANGE_REJECTED` line, since the caption written when it was asked for would otherwise stand uncorrected. `PublishBeforeApplyJvmTest` gains the plan's third case: with a two-relay scope, one refusal leaves the obligation, a timeout on the other leaves it, the other's refusal discards it -- epoch unchanged, the peer not a member, the line naming the reason -- and the same peer can be invited again. 1187 tests green, Android debug compile, M3 audit. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b3780c604
commit
2bbe1fc9bc
10 files changed
+213
-15
No files matched your search
+7
-1
@@ -17,6 +17,10 @@ interface BroadcastNostrEventRequestDao {
|
||||
@Query("SELECT * FROM BroadcastNostrEventRequest WHERE nostrEventId = :nostrEventId ORDER BY createdAt ASC")
|
||||
suspend fun getFirstBroadcastNostrEventRequestByNostrEventId(nostrEventId: String): BroadcastNostrEventRequest?
|
||||
|
||||
/** Every relay's request for one event: the obligation's scope, as rows. */
|
||||
@Query("SELECT * FROM BroadcastNostrEventRequest WHERE nostrEventId = :nostrEventId ORDER BY createdAt ASC, id ASC")
|
||||
suspend fun getBroadcastNostrEventRequestsByNostrEventId(nostrEventId: String): List<BroadcastNostrEventRequest>
|
||||
|
||||
// See NegentropySynchronizeRequestDao: the old `createdAt > :createdAt` bound a
|
||||
// once-evaluated `Clock.System.now()` for the life of the Flow. Because Instants are
|
||||
// persisted at second resolution, that hid every broadcast enqueued in the observer's
|
||||
@@ -33,7 +37,9 @@ interface BroadcastNostrEventRequestDao {
|
||||
* A request is flipped to "processing" before the publish is attempted, and the publish can
|
||||
* end in a timeout or a socket error that leaves it there. Nothing observes "processing" or
|
||||
* "failed", so those rows are dead weight. Requeue everything a previous process left behind
|
||||
* when we start up, so an interrupted publish is retried instead of lost.
|
||||
* when we start up, so an interrupted publish is retried instead of lost. Not "rejected":
|
||||
* that is a relay's answer to these exact bytes, and it does not change on a retry -- see
|
||||
* [to.curare.compose.database.model.BroadcastStatus].
|
||||
*/
|
||||
@Query("UPDATE BroadcastNostrEventRequest SET status = 'pending' WHERE status IN ('processing', 'failed') AND createdAt <= :staleBefore")
|
||||
suspend fun requeueStaleBroadcastNostrEventRequests(staleBefore: Instant): Int
|
||||
|
||||
@@ -522,6 +522,25 @@ abstract class MarmotOutboundDao(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A change to the room -- a removal, a disband step -- was refused by every
|
||||
* relay it was sent to, and so never happened; the line written when it was
|
||||
* asked for would otherwise stand uncorrected.
|
||||
*/
|
||||
suspend fun announceChangeRejected(
|
||||
chatRoomId: HexKey,
|
||||
userPublicKey: HexKey,
|
||||
reason: String?,
|
||||
) {
|
||||
val why = reason?.takeIf { it.isNotBlank() }?.let { ": $it" } ?: ""
|
||||
announceMembership(
|
||||
chatRoomId = chatRoomId,
|
||||
userPublicKey = userPublicKey,
|
||||
messageType = ChatMessage.TYPE_CHANGE_REJECTED,
|
||||
content = "The relays refused the change, so it was not made$why",
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun inviteMember(
|
||||
chatRoomId: HexKey,
|
||||
mlsGroup: MlsGroup,
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package to.curare.compose.database.model
|
||||
|
||||
import to.curare.compose.exceptions.NostrPublishException
|
||||
|
||||
/**
|
||||
* What a `BroadcastNostrEventRequest.status` can be, and how a relay's answer
|
||||
* becomes one.
|
||||
*
|
||||
* Four of these were string literals scattered over the sync pump, the
|
||||
* navigation gate and the DAO's requeue; the fifth is new. [REJECTED] is a
|
||||
* relay's `["OK", id, false, reason]` with a reason that will not change on a
|
||||
* retry -- `blocked:`, `invalid:`, `pow:`, `restricted:` -- and is the one
|
||||
* terminal status the requeue on launch leaves alone: the same bytes sent to
|
||||
* the same relay get the same answer. A `rate-limited:` or `error:` refusal is
|
||||
* [FAILED] like a timeout, because those do change. A `duplicate:` refusal is
|
||||
* [PUBLISHED]: the relay is saying it already has the event, which is what
|
||||
* published means, and it is the answer a byte-identical retry of a commit
|
||||
* gets from a relay that took the first copy.
|
||||
*
|
||||
* The distinction matters most to publish-before-apply: a commit's obligation
|
||||
* is discarded only when every relay in its scope has [REJECTED] it, and stays
|
||||
* pending through any number of [FAILED]s. See docs/marmot-v2.md, Phase 6.
|
||||
*/
|
||||
object BroadcastStatus {
|
||||
const val PENDING = "pending"
|
||||
const val PROCESSING = "processing"
|
||||
const val PUBLISHED = "published"
|
||||
const val FAILED = "failed"
|
||||
const val REJECTED = "rejected"
|
||||
|
||||
/** NIP-01 `OK` prefixes a relay will keep answering with. */
|
||||
private val TERMINAL_PREFIXES = listOf("blocked:", "invalid:", "pow:", "restricted:")
|
||||
|
||||
/** The status a publish attempt ends in, from what it threw or was answered. */
|
||||
fun of(error: Throwable?): String = when {
|
||||
error == null -> PUBLISHED
|
||||
error !is NostrPublishException -> FAILED
|
||||
else -> {
|
||||
val reason = error.message.orEmpty().trim()
|
||||
when {
|
||||
reason.startsWith("duplicate:") -> PUBLISHED
|
||||
TERMINAL_PREFIXES.any { reason.startsWith(it) } -> REJECTED
|
||||
else -> FAILED
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -541,6 +541,13 @@ data class ChatMessage(
|
||||
const val TYPE_MEMBER_LEFT = "memberLeft"
|
||||
const val TYPE_GROUP_DISBANDED = "groupDisbanded"
|
||||
|
||||
/**
|
||||
* A change this device asked for that every relay refused, so the line
|
||||
* written when it was asked for -- a removal, a disband -- did not come
|
||||
* true. The invite's own version is [TYPE_MEMBER_INVITE_FAILED].
|
||||
*/
|
||||
const val TYPE_CHANGE_REJECTED = "changeRejected"
|
||||
|
||||
/**
|
||||
* Every membership line, for the one check the transcript dispatches on.
|
||||
*
|
||||
@@ -554,6 +561,7 @@ data class ChatMessage(
|
||||
TYPE_MEMBER_ADDED,
|
||||
TYPE_MEMBER_LEFT,
|
||||
TYPE_GROUP_DISBANDED,
|
||||
TYPE_CHANGE_REJECTED,
|
||||
)
|
||||
|
||||
const val TYPE_UNDECRYPTABLE_OUTER_LAYER = "undecryptableOuterLayer"
|
||||
|
||||
+49
-2
@@ -3,6 +3,7 @@ package to.curare.compose.database.repository
|
||||
import to.curare.compose.database.model.MarmotCommitResult
|
||||
import to.curare.compose.database.GENESIS_AT
|
||||
import to.curare.compose.database.model.BroadcastNostrEventRequest
|
||||
import to.curare.compose.database.model.BroadcastStatus
|
||||
import to.curare.compose.database.model.NostrEvent
|
||||
import to.curare.compose.database.model.Profile
|
||||
import to.curare.compose.database.model.UnsignedNostrEvent
|
||||
@@ -473,7 +474,8 @@ class DatabaseNostrRepository(
|
||||
|
||||
override suspend fun broadcastProcessed(
|
||||
broadcastNostrEventRequest: BroadcastNostrEventRequest,
|
||||
status: String
|
||||
status: String,
|
||||
reason: String?,
|
||||
) {
|
||||
database.broadcastNostrEventRequestDao().upsert(
|
||||
broadcastNostrEventRequest.copy(
|
||||
@@ -481,7 +483,11 @@ class DatabaseNostrRepository(
|
||||
)
|
||||
)
|
||||
|
||||
if (status == "published") {
|
||||
if (status == BroadcastStatus.REJECTED) {
|
||||
discardCommitRejectedEverywhere(broadcastNostrEventRequest.nostrEventId, reason)
|
||||
}
|
||||
|
||||
if (status == BroadcastStatus.PUBLISHED) {
|
||||
val broadcastNostrEventReceiptId = database.broadcastNostrEventReceiptDao().upsert(
|
||||
to.curare.compose.database.model.BroadcastNostrEventReceipt(
|
||||
nostrEventId = broadcastNostrEventRequest.nostrEventId,
|
||||
@@ -572,6 +578,47 @@ class DatabaseNostrRepository(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish before apply, the other half: a commit every relay in its scope has
|
||||
* refused is discarded, and the room is free to make another.
|
||||
*
|
||||
* One relay's refusal decides nothing -- another may still accept, and a
|
||||
* timeout on another is retried on the next launch with the same bytes -- so
|
||||
* the obligation goes only when every request row for the event is
|
||||
* "rejected". Then the gate drops the pending state, the live group having
|
||||
* never advanced, and `canPrepareLocalCommit` is true again; the invite or
|
||||
* the removal that was asked for is simply not done, and the room says so in
|
||||
* the relay's words, because the screen that asked closed when the commit
|
||||
* was made and the line is the only account the user gets. See
|
||||
* docs/marmot-v2.md, Phase 6.
|
||||
*/
|
||||
private suspend fun discardCommitRejectedEverywhere(nostrEventId: HexKey, reason: String?) {
|
||||
val marmotCommitResult = database.marmotCommitResultDao().getMarmotCommitRequestById(nostrEventId) ?: return
|
||||
val obligationId = marmotCommitResult.obligationId ?: return
|
||||
val requests = database.broadcastNostrEventRequestDao().getBroadcastNostrEventRequestsByNostrEventId(nostrEventId)
|
||||
if (requests.any { it.status != BroadcastStatus.REJECTED }) return
|
||||
|
||||
if (!database.marmotEngine.discardRejected(obligationId)) return
|
||||
logger.w("Commit $nostrEventId rejected by every relay in scope; its obligation is discarded: $reason")
|
||||
|
||||
if (marmotCommitResult.peerKeyPackageEventId != MarmotCommitResult.NO_INVITEE) {
|
||||
database.marmotOutboundDao().announceInviteFailed(
|
||||
chatRoomId = marmotCommitResult.chatRoomId,
|
||||
userPublicKey = marmotCommitResult.userPublicKey,
|
||||
peerPublicKey = database.marmotKeyPackageDao()
|
||||
.getMarmotKeyPackageById(marmotCommitResult.peerKeyPackageEventId)
|
||||
?.publicKey,
|
||||
reason = reason?.let { "the relays refused it ($it)" } ?: "the relays refused it",
|
||||
)
|
||||
} else {
|
||||
database.marmotOutboundDao().announceChangeRejected(
|
||||
chatRoomId = marmotCommitResult.chatRoomId,
|
||||
userPublicKey = marmotCommitResult.userPublicKey,
|
||||
reason = reason,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun synchronizeNostrEventRequestProcessed(synchronizeNostrEventRequest: to.curare.compose.database.model.SynchronizeNostrEventRequest) {
|
||||
database.synchronizeNostrEventRequestDao().upsert(
|
||||
synchronizeNostrEventRequest.copy(
|
||||
|
||||
@@ -136,6 +136,23 @@ class MarmotEngine(
|
||||
database.marmotOutboundDao().continueDisband(localChatRoom)
|
||||
}
|
||||
|
||||
/**
|
||||
* Every relay in scope refused [obligationId]: discard its pending state.
|
||||
*
|
||||
* The live group never advanced, so there is nothing to undo; the gate drops
|
||||
* the clone, the lifecycle returns to `STABLE`, and the next commit may be
|
||||
* prepared. Nothing is told to convergence -- a commit no relay carries is
|
||||
* not a branch any member will ever see. Returns whether there was an
|
||||
* obligation to discard.
|
||||
*/
|
||||
suspend fun discardRejected(obligationId: HexKey): Boolean {
|
||||
ensureRestored()
|
||||
val obligation = gate.allPending().firstOrNull { it.obligationId == obligationId } ?: return false
|
||||
gate.resolve(obligationId, PublishOutcome.FAILED)
|
||||
logger.w("Commit ${obligationId.take(8)}… discarded; ${obligation.groupId.take(8)}… stays at epoch ${obligation.priorState.groupContext.epoch}")
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for [routingId]'s convergence pass, if one is open, to settle.
|
||||
*
|
||||
|
||||
@@ -111,7 +111,15 @@ interface NostrRepository {
|
||||
activeKeyPair: KeyPair
|
||||
)
|
||||
|
||||
suspend fun broadcastProcessed(broadcastNostrEventRequest: BroadcastNostrEventRequest, status: String = "processing")
|
||||
/**
|
||||
* Record how a publish attempt ended. [reason] is the relay's own words when it
|
||||
* refused, for the line that tells the user a change was rejected.
|
||||
*/
|
||||
suspend fun broadcastProcessed(
|
||||
broadcastNostrEventRequest: BroadcastNostrEventRequest,
|
||||
status: String = "processing",
|
||||
reason: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Move broadcasts a previous process left in "processing"/"failed" back to "pending" so an
|
||||
@@ -325,7 +333,8 @@ interface NostrRepository {
|
||||
|
||||
override suspend fun broadcastProcessed(
|
||||
broadcastNostrEventRequest: BroadcastNostrEventRequest,
|
||||
status: String
|
||||
status: String,
|
||||
reason: String?,
|
||||
) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
+3
-1
@@ -834,6 +834,7 @@ private fun RitualNotice(
|
||||
ChatMessage.TYPE_MEMBER_INVITED -> Icons.Default.PersonAdd
|
||||
ChatMessage.TYPE_MEMBER_INVITE_SENT -> Icons.Default.Upload
|
||||
ChatMessage.TYPE_MEMBER_INVITE_FAILED -> Icons.Default.ErrorOutline
|
||||
ChatMessage.TYPE_CHANGE_REJECTED -> Icons.Default.ErrorOutline
|
||||
|
||||
else -> Icons.Default.PanTool
|
||||
}
|
||||
@@ -854,7 +855,8 @@ private fun RitualNotice(
|
||||
val tint = when {
|
||||
chatMessage.messageType == ChatMessage.TYPE_DKG_FAILED ||
|
||||
chatMessage.messageType == ChatMessage.TYPE_FROST_FAILED ||
|
||||
chatMessage.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED ->
|
||||
chatMessage.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED ||
|
||||
chatMessage.messageType == ChatMessage.TYPE_CHANGE_REJECTED ->
|
||||
MaterialTheme.colorScheme.error
|
||||
isRequest -> MaterialTheme.colorScheme.primary
|
||||
else -> MaterialTheme.colorScheme.onSurfaceVariant
|
||||
|
||||
+12
-9
@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import to.curare.compose.database.model.BroadcastStatus
|
||||
import to.curare.compose.database.model.BroadcastNostrEventRequest
|
||||
import to.curare.compose.database.model.SynchronizeNostrEventRequest
|
||||
import to.curare.compose.database.model.types.SynchronizationFilter
|
||||
@@ -830,19 +831,21 @@ class SynchronizationViewModel(
|
||||
"failed"
|
||||
)
|
||||
}.collect { nostrPublishResult ->
|
||||
// A relay's answer decides the status, not merely whether
|
||||
// there was an error: a refusal it will keep giving is
|
||||
// "rejected" and is not retried, a `duplicate:` is the
|
||||
// event being there already. See BroadcastStatus.
|
||||
val status = BroadcastStatus.of(nostrPublishResult.error)
|
||||
if (nostrPublishResult.error != null) {
|
||||
logger.e("Error publishing note: $nostrPublishResult")
|
||||
nostrRepository.broadcastProcessed(
|
||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
||||
"failed"
|
||||
)
|
||||
logger.e("Error publishing note ($status): $nostrPublishResult")
|
||||
} else {
|
||||
logger.d("Nostr Publish Result: $nostrPublishResult")
|
||||
nostrRepository.broadcastProcessed(
|
||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
||||
"published"
|
||||
)
|
||||
}
|
||||
nostrRepository.broadcastProcessed(
|
||||
localBroadcastNostrEventRequest.broadcastNostrEventRequest,
|
||||
status,
|
||||
reason = nostrPublishResult.error?.message,
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: CancellationException) {
|
||||
|
||||
@@ -11,6 +11,9 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import to.curare.compose.database.CurareDatabase
|
||||
import to.curare.compose.database.builder.getRoomDatabase
|
||||
import to.curare.compose.database.model.BroadcastNostrEventRequest
|
||||
import to.curare.compose.database.model.BroadcastStatus
|
||||
import to.curare.compose.database.model.ChatMessage
|
||||
import to.curare.compose.database.model.MarmotKeyPackage
|
||||
import to.curare.compose.database.model.NostrEvent
|
||||
import to.curare.compose.database.model.Profile
|
||||
@@ -150,4 +153,41 @@ class PublishBeforeApplyJvmTest {
|
||||
assertNull(db.marmotPublishObligationDao().getAll().firstOrNull())
|
||||
assertTrue(rebuilt.gate.canPrepareLocalCommit(routingId))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a commit every relay refuses is discarded, and the room can invite again`() = runBlocking {
|
||||
val routingId = establishedRoom()
|
||||
val second = KeyPair()
|
||||
chatRepository.inviteMember(db.chatRoomDao().findChatRoomById(roomId)!!, second.pubKey.toHexKey(), peerPackage(second))
|
||||
val (commitEvent, _) = commitEventFor(second)
|
||||
// The default scope is one relay; a second row widens it to two, as a room
|
||||
// whose relay list names two would have.
|
||||
db.broadcastNostrEventRequestDao().insert(listOf(BroadcastNostrEventRequest(nostrEventId = commitEvent.id, relayURL = "wss://second.example")))
|
||||
val requests = db.broadcastNostrEventRequestDao().getBroadcastNostrEventRequestsByNostrEventId(commitEvent.id)
|
||||
assertEquals(2, requests.size)
|
||||
|
||||
// One relay's refusal decides nothing: the other may still take it.
|
||||
nostrRepository.broadcastProcessed(requests[0], BroadcastStatus.REJECTED, reason = "blocked: no")
|
||||
assertEquals(1, db.marmotPublishObligationDao().getAll().size)
|
||||
assertTrue(!db.marmotEngine.gate.canPrepareLocalCommit(routingId))
|
||||
|
||||
// Nor does a timeout on the other: that is retried with the same bytes.
|
||||
nostrRepository.broadcastProcessed(requests[1], BroadcastStatus.FAILED)
|
||||
assertEquals(1, db.marmotPublishObligationDao().getAll().size)
|
||||
|
||||
// The other relay refusing too is the end of it: the pending state is
|
||||
// dropped, the room is where it was, and it says why.
|
||||
nostrRepository.broadcastProcessed(requests[1], BroadcastStatus.REJECTED, reason = "blocked: no")
|
||||
assertEquals(0, db.marmotPublishObligationDao().getAll().size)
|
||||
assertEquals(1L, epochOnDisk())
|
||||
assertTrue(db.marmotEngine.gate.canPrepareLocalCommit(routingId))
|
||||
assertTrue(second.pubKey.toHexKey() !in db.marmotEngine.groupManager.getGroup(routingId)!!.currentMemberIdentities())
|
||||
val line = db.chatMessageDao().getChatMessagesByChatRoomId(roomId).map { it.chatMessage }
|
||||
.last { it.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED }
|
||||
assertTrue("blocked: no" in line.content, line.content)
|
||||
|
||||
// And the room is free: the same peer can be invited again.
|
||||
chatRepository.inviteMember(db.chatRoomDao().findChatRoomById(roomId)!!, second.pubKey.toHexKey(), peerPackage(second))
|
||||
assertEquals(1, db.marmotPublishObligationDao().getAll().size)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user