Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/src/main/java/to/bitkit/repositories/WakeRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Clock
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -82,6 +83,13 @@ class WakeRepo @Inject constructor(

/** The longest a gateway's `Retry-After` is followed, as on iOS. */
val SYNC_RETRY_AFTER_MAX = 1.hours

/**
* How long an unchanged registration stands before it is sent again. The gateway retires a device that had no
* accepted push or registration for 270 days (`retention.idle_device_days`), and presence does not count, so
* an install that rarely gets a wake registers again long before that.
*/
val REGISTRATION_MAX_AGE = 30.days
}

private val scope = appScope(ioDispatcher, TAG)
Expand Down Expand Up @@ -179,7 +187,9 @@ class WakeRepo @Inject constructor(
pubkyIdentity = pubkyIdentity,
)
val fingerprint = registration.fingerprint()
if (!force && credentialStore.registrationState().fingerprint == fingerprint) return WakeSync.UpToDate
val state = credentialStore.registrationState()
val isFresh = !isRegistrationDue(nowSecs = clock.now().epochSeconds, lastSecs = state.lastTimestampSecs)
if (!force && state.fingerprint == fingerprint && isFresh) return WakeSync.UpToDate

val accepted = register(registration)
credentialStore.updateRegistrationState { it.copy(fingerprint = fingerprint) }
Expand Down Expand Up @@ -378,6 +388,13 @@ private const val HTTP_SERVER_ERROR = 500
internal fun nextRegistrationTimestamp(nowSecs: Long, lastSecs: Long?): Long =
if (lastSecs == null) nowSecs else maxOf(nowSecs, lastSecs + 1)

/**
* Whether an unchanged registration last sent at [lastSecs] is sent again, so the gateway never retires the device as
* idle (see [WakeRepo.REGISTRATION_MAX_AGE]). One with no recorded time is sent again once.
*/
internal fun isRegistrationDue(nowSecs: Long, lastSecs: Long?): Boolean =
lastSecs == null || nowSecs - lastSecs >= WakeRepo.REGISTRATION_MAX_AGE.inWholeSeconds

internal fun lightningWakeIdentity(nodeId: String): String = "ln:$nodeId"

/** The `pk:` wake identity of a Pubky public key: the gateway wants the 52 bare characters, without `pubky`. */
Expand Down
40 changes: 39 additions & 1 deletion app/src/test/java/to/bitkit/repositories/WakeRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ import to.bitkit.services.WakeService
import to.bitkit.test.BaseUnitTest
import to.bitkit.utils.AppError
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Clock
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
Expand Down Expand Up @@ -90,8 +92,9 @@ class WakeRepoTest : BaseUnitTest() {
private val preparedIdentities = mutableListOf<List<String>>()
private val preparedTokens = mutableListOf<String>()
private val sentProofs = mutableListOf<List<WakeIdentityProof>>()
private var nowSecs = NOW_SECS
private val clock = object : Clock {
override fun now(): Instant = Instant.fromEpochSeconds(NOW_SECS)
override fun now(): Instant = Instant.fromEpochSeconds(nowSecs)
}

private lateinit var sut: WakeRepo
Expand Down Expand Up @@ -217,6 +220,41 @@ class WakeRepoTest : BaseUnitTest() {
assertEquals(2, sentProofs.size)
}

@Test
fun `a registration is sent again before the gateway retires the device as idle`() = test {
sut.syncRegistration().getOrThrow()
// synonymdev/wake retires a device with no accepted push or registration for retention.idle_device_days (270)
// and then answers 401 to its secret; presence does not count as activity.
nowSecs += 271.days.inWholeSeconds

val result = sut.syncRegistration().getOrThrow()

assertIs<WakeSync.Registered>(result, "Got '$result' 271 days after the only registration")
}

@Test
fun `an unchanged registration is sent again once it reaches its max age, and not before`() = test {
sut.syncRegistration().getOrThrow()

nowSecs += WakeRepo.REGISTRATION_MAX_AGE.inWholeSeconds - 1
assertEquals(WakeSync.UpToDate, sut.syncRegistration().getOrThrow())

nowSecs += 1
assertIs<WakeSync.Registered>(sut.syncRegistration().getOrThrow())
assertEquals(WakeSync.UpToDate, sut.syncRegistration().getOrThrow(), "The age restarts from the new one")
assertEquals(listOf(NOW_SECS, NOW_SECS + WakeRepo.REGISTRATION_MAX_AGE.inWholeSeconds), preparedTimestamps)
}

@Test
fun `isRegistrationDue sends again from the max age, and once when no time is recorded`() {
val maxAgeSecs = WakeRepo.REGISTRATION_MAX_AGE.inWholeSeconds
assertTrue(isRegistrationDue(nowSecs = NOW_SECS, lastSecs = null))
assertFalse(isRegistrationDue(nowSecs = NOW_SECS, lastSecs = NOW_SECS))
assertFalse(isRegistrationDue(nowSecs = NOW_SECS, lastSecs = NOW_SECS - maxAgeSecs + 1))
assertTrue(isRegistrationDue(nowSecs = NOW_SECS, lastSecs = NOW_SECS - maxAgeSecs))
assertFalse(isRegistrationDue(nowSecs = NOW_SECS, lastSecs = NOW_SECS + 5), "A clock stepped back waits")
}

@Test
fun `each registration is timestamped after the last one, even within the same second`() = test {
sut.syncRegistration().getOrThrow()
Expand Down
Loading