feat: when a NIP-17 rumor expires, as pure rules (disappearing-messages, Phase 1)
Written kinds follow the person's policy, Off included; every protocol kind expires after the longer of the policy and its protocol's floor; a rumor that already carries an expiry keeps it. The wrap's expiry is quartz's formula. A test fails on any kind the app sends that nobody classified. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c5618a706c
commit
962bdbf631
2 files changed
+309
No files matched your search
@@ -0,0 +1,163 @@
|
||||
package mobi.giza.compose.nostr
|
||||
|
||||
import com.vitorpamplona.quartz.marmot.mip02Welcome.WelcomeEvent
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Kind
|
||||
import com.vitorpamplona.quartz.nip09Deletions.DeletionRequestEvent
|
||||
import com.vitorpamplona.quartz.nip17Dm.files.ChatMessageEncryptedFileHeaderEvent
|
||||
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
|
||||
import com.vitorpamplona.quartz.nip25Reactions.ReactionEvent
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationKinds
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationRitualEvents
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationSigningEvents
|
||||
import mobi.giza.compose.nostr.dkg.DkgRitualEvents
|
||||
import mobi.giza.compose.nostr.frost.FrostSigningEvents
|
||||
import mobi.giza.compose.nostr.frost.ShareRefreshEvents
|
||||
import mobi.giza.compose.nostr.frost.ShareRepairEvents
|
||||
import mobi.giza.compose.nostr.groupdm.GroupEcdhEvents
|
||||
import mobi.giza.compose.nostr.inbox.InboxKeyRitualEvents
|
||||
|
||||
/**
|
||||
* When a NIP-17 rumor expires, worked out once, when it is queued. See
|
||||
* docs/disappearing-messages.md.
|
||||
*
|
||||
* Every event Giza sends in a NIP-17 wrap expires (D2). What a person writes expires after their
|
||||
* policy, which may be Off. A protocol message always expires, after the longer of the policy and
|
||||
* the floor its protocol needs, because a ceremony round that outlived its ceremony on a relay
|
||||
* would be the one exception to "everything expires".
|
||||
*
|
||||
* Pure functions of their arguments, with no database, so the rules are testable without Room.
|
||||
*/
|
||||
object DisappearingMessages {
|
||||
/** NIP-40's tag. */
|
||||
const val TAG = "expiration"
|
||||
|
||||
/** The policy that means "what I write does not expire". Protocol messages still do. */
|
||||
const val OFF: Long = 0L
|
||||
|
||||
const val MINUTE: Long = 60L
|
||||
const val HOUR: Long = 60 * MINUTE
|
||||
const val DAY: Long = 24 * HOUR
|
||||
|
||||
/** Below this, two phones' clocks and a slow catch-up can deliver a message already expired (D3). */
|
||||
const val MIN_SECONDS: Long = 5 * MINUTE
|
||||
|
||||
/** Long enough that "a long time" is a real choice, short enough that relays treat it sanely (D3). */
|
||||
const val MAX_SECONDS: Long = 365 * DAY
|
||||
|
||||
/** The policy a new identity starts with, and an existing one is given on upgrade (D9). */
|
||||
const val DEFAULT_SECONDS: Long = DAY
|
||||
|
||||
/** What the settings list offers, Off first. Anything else in bounds is a custom length. */
|
||||
val PRESETS: List<Long> = listOf(OFF, HOUR, 8 * HOUR, DAY, 7 * DAY, 28 * DAY)
|
||||
|
||||
/**
|
||||
* A signing round's floor. `docs/frost-session-resend.md` already repairs a round a relay
|
||||
* dropped, so this only stops a one-hour policy from turning every signing into a race.
|
||||
*/
|
||||
const val SIGNING_FLOOR: Long = DAY
|
||||
|
||||
/** A key ceremony's floor: it waits for every admin, and a DKG round has no resend. */
|
||||
const val CEREMONY_FLOOR: Long = 7 * DAY
|
||||
|
||||
/** What a kind nobody classified gets: it still expires, never "never" (D2). */
|
||||
const val UNCLASSIFIED_FLOOR: Long = CEREMONY_FLOOR
|
||||
|
||||
/**
|
||||
* How far past the rumor's expiry the wrap's can fall, the same two days quartz adds
|
||||
* (`GiftWrapEvent.build`), so that the randomized `created_at` never expires a wrap early.
|
||||
*/
|
||||
const val WRAP_SLACK: Long = 2 * DAY
|
||||
|
||||
/** The kinds a person writes: their words, a reaction to them, the deletion of one (D2). */
|
||||
val WRITTEN_KINDS: Set<Kind> = setOf(
|
||||
ChatMessageEvent.KIND,
|
||||
ChatMessageEncryptedFileHeaderEvent.KIND,
|
||||
ReactionEvent.KIND,
|
||||
DeletionRequestEvent.KIND,
|
||||
)
|
||||
|
||||
private val SIGNING_KINDS: Set<Kind> =
|
||||
FrostSigningEvents.ALL + ConfederationSigningEvents.ALL + GroupEcdhEvents.ALL
|
||||
|
||||
private val CEREMONY_KINDS: Set<Kind> =
|
||||
DkgRitualEvents.ALL + InboxKeyRitualEvents.ALL + ShareRefreshEvents.ALL +
|
||||
ShareRepairEvents.ALL + ConfederationRitualEvents.ALL + setOf(WelcomeEvent.KIND)
|
||||
|
||||
/** Confederation statements carry their own expiry, the proposal's; this is only its ceiling. */
|
||||
private val STATEMENT_KINDS: Set<Kind> = ConfederationKinds.ALL
|
||||
|
||||
sealed interface ExpiryClass {
|
||||
/** Something a person wrote: it follows their policy, Off included. */
|
||||
data object Written : ExpiryClass
|
||||
|
||||
/**
|
||||
* A protocol message: it always expires, after at least [floorSeconds]. [classified] is
|
||||
* false for a kind that fell through to [UNCLASSIFIED_FLOOR], which a test refuses for
|
||||
* every kind the app is known to send.
|
||||
*/
|
||||
data class Protocol(val floorSeconds: Long, val classified: Boolean = true) : ExpiryClass
|
||||
}
|
||||
|
||||
fun classOf(kind: Kind): ExpiryClass = when (kind) {
|
||||
in WRITTEN_KINDS -> ExpiryClass.Written
|
||||
in SIGNING_KINDS -> ExpiryClass.Protocol(SIGNING_FLOOR)
|
||||
in CEREMONY_KINDS -> ExpiryClass.Protocol(CEREMONY_FLOOR)
|
||||
in STATEMENT_KINDS -> ExpiryClass.Protocol(ConfederationKinds.MAX_EXPIRY_SECONDS)
|
||||
else -> ExpiryClass.Protocol(UNCLASSIFIED_FLOOR, classified = false)
|
||||
}
|
||||
|
||||
/** Whether [seconds] is a policy a person may choose: Off, or within the bounds. */
|
||||
fun isValidPolicy(seconds: Long): Boolean = seconds == OFF || seconds in MIN_SECONDS..MAX_SECONDS
|
||||
|
||||
/** The policy in force: the conversation's override when it has one, else the identity's default (D4). */
|
||||
fun policyFor(roomOverride: Long?, identityDefault: Long): Long = roomOverride ?: identityDefault
|
||||
|
||||
/**
|
||||
* The rumor's expiry, or null when it has none: a written kind under the policy Off, and
|
||||
* nothing else. A rumor that already carries a valid expiry keeps it (D5): a second tag would
|
||||
* get a confederation statement refused.
|
||||
*/
|
||||
fun expiryFor(kind: Kind, createdAt: Long, policy: Long, tags: Array<Array<String>>): Long? {
|
||||
expirationOf(tags, createdAt)?.let { return it }
|
||||
return when (val expiryClass = classOf(kind)) {
|
||||
ExpiryClass.Written -> if (policy == OFF) null else createdAt + policy
|
||||
is ExpiryClass.Protocol -> createdAt + maxOf(policy, expiryClass.floorSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [tags] with the rumor's expiry added, or unchanged when it already has one or should have
|
||||
* none. Call it before hashing the rumor's id: the tag is one of the fields the id covers.
|
||||
*/
|
||||
fun stamp(kind: Kind, createdAt: Long, policy: Long, tags: Array<Array<String>>): Array<Array<String>> {
|
||||
if (expirationOf(tags, createdAt) != null) return tags
|
||||
val expiry = expiryFor(kind, createdAt, policy, tags) ?: return tags
|
||||
return tags + arrayOf(arrayOf(TAG, expiry.toString()))
|
||||
}
|
||||
|
||||
/**
|
||||
* The wrap's expiry: quartz's formula, `wrap.created_at + (rumor's lifetime) + 2 days`. The
|
||||
* wrap's `created_at` is already up to two days in the past, so this is the rumor's expiry
|
||||
* plus a random `[0, 2 days]`: later than the sender meant, never earlier, and not an exact
|
||||
* offset from a time the randomization was there to hide. Null when the rumor has none.
|
||||
*/
|
||||
fun wrapExpiration(rumorExpiration: Long?, rumorCreatedAt: Long, wrapCreatedAt: Long): Long? {
|
||||
val lifetime = (rumorExpiration ?: return null) - rumorCreatedAt
|
||||
if (lifetime <= 0) return null
|
||||
return wrapCreatedAt + lifetime + WRAP_SLACK
|
||||
}
|
||||
|
||||
/**
|
||||
* The first `expiration` tag's time, or null when there is none or it is malformed: not a
|
||||
* number, or not after [createdAt]. quartz reads an expiry at or before `created_at` as no
|
||||
* lifetime at all, and so does this.
|
||||
*/
|
||||
fun expirationOf(tags: Array<Array<String>>, createdAt: Long? = null): Long? {
|
||||
val value = tags.firstOrNull { it.size >= 2 && it[0] == TAG }?.get(1)?.toLongOrNull() ?: return null
|
||||
if (createdAt != null && value <= createdAt) return null
|
||||
return value
|
||||
}
|
||||
|
||||
/** NIP-40: an event is expired from its expiry on. */
|
||||
fun isExpired(expiresAt: Long, now: Long): Boolean = now >= expiresAt
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package mobi.giza.compose.nostr
|
||||
|
||||
import com.vitorpamplona.quartz.marmot.mip02Welcome.WelcomeEvent
|
||||
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
|
||||
import mobi.giza.compose.nostr.DisappearingMessages.DAY
|
||||
import mobi.giza.compose.nostr.DisappearingMessages.ExpiryClass
|
||||
import mobi.giza.compose.nostr.DisappearingMessages.HOUR
|
||||
import mobi.giza.compose.nostr.DisappearingMessages.OFF
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationKinds
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationRitualEvents
|
||||
import mobi.giza.compose.nostr.confederation.ConfederationSigningEvents
|
||||
import mobi.giza.compose.nostr.dkg.DkgRitualEvents
|
||||
import mobi.giza.compose.nostr.frost.FrostSigningEvents
|
||||
import mobi.giza.compose.nostr.frost.ShareRefreshEvents
|
||||
import mobi.giza.compose.nostr.frost.ShareRepairEvents
|
||||
import mobi.giza.compose.nostr.groupdm.GroupEcdhEvents
|
||||
import mobi.giza.compose.nostr.inbox.InboxKeyRitualEvents
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertSame
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** docs/disappearing-messages.md, D2 to D5: the rules, without a database. */
|
||||
class DisappearingMessagesTest {
|
||||
|
||||
private val createdAt = 1_800_000_000L
|
||||
private val chat = ChatMessageEvent.KIND
|
||||
|
||||
/**
|
||||
* Every kind Giza is known to put in a NIP-17 wrap, or in a ceremony that could. A kind missing
|
||||
* here falls through to the unclassified floor, which still expires but was nobody's decision;
|
||||
* classifying it means adding it to `DisappearingMessages` with a reason, and then here.
|
||||
*/
|
||||
private val knownProtocolKinds =
|
||||
DkgRitualEvents.ALL + FrostSigningEvents.ALL + ConfederationRitualEvents.ALL + ConfederationSigningEvents.ALL +
|
||||
InboxKeyRitualEvents.ALL + ShareRefreshEvents.ALL + ShareRepairEvents.ALL + GroupEcdhEvents.ALL +
|
||||
ConfederationKinds.ALL + setOf(WelcomeEvent.KIND)
|
||||
|
||||
@Test
|
||||
fun `every kind the app sends is classified on purpose`() {
|
||||
val unclassified = knownProtocolKinds.filter { (DisappearingMessages.classOf(it) as? ExpiryClass.Protocol)?.classified == false }
|
||||
assertEquals(emptyList(), unclassified)
|
||||
DisappearingMessages.WRITTEN_KINDS.forEach { assertSame(ExpiryClass.Written, DisappearingMessages.classOf(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a kind nobody classified still expires, after a week`() {
|
||||
val expiryClass = DisappearingMessages.classOf(99_999)
|
||||
assertEquals(ExpiryClass.Protocol(DisappearingMessages.UNCLASSIFIED_FLOOR, classified = false), expiryClass)
|
||||
assertEquals(createdAt + 7 * DAY, DisappearingMessages.expiryFor(99_999, createdAt, OFF, emptyArray()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `what a person writes follows the policy, and Off means no expiry`() {
|
||||
assertEquals(createdAt + DAY, DisappearingMessages.expiryFor(chat, createdAt, DAY, emptyArray()))
|
||||
assertEquals(createdAt + HOUR, DisappearingMessages.expiryFor(chat, createdAt, HOUR, emptyArray()))
|
||||
assertNull(DisappearingMessages.expiryFor(chat, createdAt, OFF, emptyArray()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a protocol message expires even with the policy Off, at its floor`() {
|
||||
val nonce = FrostSigningEvents.NONCE
|
||||
assertEquals(createdAt + DisappearingMessages.SIGNING_FLOOR, DisappearingMessages.expiryFor(nonce, createdAt, OFF, emptyArray()))
|
||||
assertEquals(createdAt + DisappearingMessages.SIGNING_FLOOR, DisappearingMessages.expiryFor(nonce, createdAt, HOUR, emptyArray()))
|
||||
assertEquals(createdAt + 7 * DAY, DisappearingMessages.expiryFor(DkgRitualEvents.ROUND_1, createdAt, DAY, emptyArray()))
|
||||
assertEquals(createdAt + 7 * DAY, DisappearingMessages.expiryFor(WelcomeEvent.KIND, createdAt, OFF, emptyArray()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a policy longer than the floor wins`() {
|
||||
assertEquals(createdAt + 28 * DAY, DisappearingMessages.expiryFor(DkgRitualEvents.ROUND_1, createdAt, 28 * DAY, emptyArray()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a rumor that carries an expiry keeps it and gets no second one`() {
|
||||
val proposalLapses = createdAt + 3 * DAY
|
||||
val tags = arrayOf(arrayOf("p", "a".repeat(64)), arrayOf("expiration", proposalLapses.toString()))
|
||||
assertEquals(proposalLapses, DisappearingMessages.expiryFor(ConfederationKinds.PROPOSAL, createdAt, DAY, tags))
|
||||
val stamped = DisappearingMessages.stamp(ConfederationKinds.PROPOSAL, createdAt, DAY, tags)
|
||||
assertSame(tags, stamped)
|
||||
assertEquals(1, stamped.count { it[0] == "expiration" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stamping appends one tag, after the others`() {
|
||||
val tags = arrayOf(arrayOf("p", "b".repeat(64)))
|
||||
val stamped = DisappearingMessages.stamp(chat, createdAt, DAY, tags)
|
||||
assertEquals(2, stamped.size)
|
||||
assertEquals(listOf("p", "b".repeat(64)), stamped[0].toList())
|
||||
assertEquals(listOf("expiration", (createdAt + DAY).toString()), stamped[1].toList())
|
||||
assertSame(tags, DisappearingMessages.stamp(chat, createdAt, OFF, tags))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the wrap expires later than the rumor by at most two days, never earlier`() {
|
||||
val rumorExpiry = createdAt + DAY
|
||||
// A wrap dated anywhere in the two days before the rumor, as quartz's randomization does.
|
||||
listOf(0L, 1L, HOUR, DAY, 2 * DAY - 1, 2 * DAY).forEach { pushedBack ->
|
||||
val wrapExpiry = DisappearingMessages.wrapExpiration(rumorExpiry, createdAt, createdAt - pushedBack)!!
|
||||
assertTrue(wrapExpiry >= rumorExpiry, "pushed back $pushedBack: $wrapExpiry < $rumorExpiry")
|
||||
assertTrue(wrapExpiry <= rumorExpiry + 2 * DAY, "pushed back $pushedBack: $wrapExpiry too late")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a rumor with no expiry gives a wrap with none`() {
|
||||
assertNull(DisappearingMessages.wrapExpiration(null, createdAt, createdAt - HOUR))
|
||||
assertNull(DisappearingMessages.wrapExpiration(createdAt, createdAt, createdAt - HOUR))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a malformed expiry reads as none`() {
|
||||
assertNull(DisappearingMessages.expirationOf(arrayOf(arrayOf("expiration", "soon"))))
|
||||
assertNull(DisappearingMessages.expirationOf(arrayOf(arrayOf("expiration"))))
|
||||
assertNull(DisappearingMessages.expirationOf(arrayOf(arrayOf("expiration", createdAt.toString())), createdAt))
|
||||
assertNull(DisappearingMessages.expirationOf(arrayOf(arrayOf("expiration", (createdAt - 1).toString())), createdAt))
|
||||
assertEquals(createdAt + 1, DisappearingMessages.expirationOf(arrayOf(arrayOf("expiration", (createdAt + 1).toString())), createdAt))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the policy bounds`() {
|
||||
assertTrue(DisappearingMessages.isValidPolicy(OFF))
|
||||
assertTrue(DisappearingMessages.isValidPolicy(DisappearingMessages.MIN_SECONDS))
|
||||
assertTrue(DisappearingMessages.isValidPolicy(DisappearingMessages.MAX_SECONDS))
|
||||
assertFalse(DisappearingMessages.isValidPolicy(DisappearingMessages.MIN_SECONDS - 1))
|
||||
assertFalse(DisappearingMessages.isValidPolicy(DisappearingMessages.MAX_SECONDS + 1))
|
||||
assertFalse(DisappearingMessages.isValidPolicy(-1))
|
||||
DisappearingMessages.PRESETS.forEach { assertTrue(DisappearingMessages.isValidPolicy(it)) }
|
||||
assertEquals(DAY, DisappearingMessages.DEFAULT_SECONDS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the override wins over the default, and null follows it`() {
|
||||
assertEquals(DAY, DisappearingMessages.policyFor(null, DAY))
|
||||
assertEquals(OFF, DisappearingMessages.policyFor(OFF, DAY))
|
||||
assertEquals(HOUR, DisappearingMessages.policyFor(HOUR, DAY))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `expired from its expiry on`() {
|
||||
assertFalse(DisappearingMessages.isExpired(createdAt, createdAt - 1))
|
||||
assertTrue(DisappearingMessages.isExpired(createdAt, createdAt))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user