Skip to content

Commit 0a78c79

Browse files
authored
Merge pull request #1005 from synonymdev/feat/probe-reset-scores
feat: add resetScores devtools command
2 parents 7948a5b + 7956098 commit 0a78c79

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ suspend fun getData(): Result<Data> = withContext(Dispatchers.IO) {
178178
- ALWAYS check existing code patterns before implementing new features
179179
- USE existing extensions and utilities rather than creating new ones
180180
- ALWAYS use or create `Context` extension properties in `ext/Context.kt` instead of raw `context.getSystemService()` casts
181+
- NEVER use `System.currentTimeMillis()`, use time helpers from `ext/DateTime.kt` instead (e.g. `nowMillis()`, `Clock.nowMs()`) — they accept a `Clock` and are unit-testable
181182
- ALWAYS apply the YAGNI (You Ain't Gonna Need It) principle for new code
182183
- ALWAYS reuse existing constants
183184
- ALWAYS ensure a method exist before calling it

app/src/debug/java/to/bitkit/dev/DevToolsProvider.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private sealed interface DevCommand {
6464
ProbeInvoice.METHOD -> ProbeInvoice.parse(arg)
6565
ProbeNode.METHOD -> ProbeNode.parse(arg)
6666
ProbeReadiness.METHOD -> ProbeReadiness
67+
ResetScores.METHOD -> ResetScores
6768
else -> null
6869
}
6970
}
@@ -172,6 +173,21 @@ private sealed interface DevCommand {
172173
override suspend fun execute(deps: DevToolsProvider.Dependencies): DevResult =
173174
DevResult.ProbeReadiness.from(deps.lightningRepo().probeReadiness())
174175
}
176+
177+
data object ResetScores : DevCommand {
178+
const val METHOD = "resetScores"
179+
180+
override suspend fun execute(deps: DevToolsProvider.Dependencies): DevResult {
181+
Logger.info("Resetting pathfinding scores via devtools", context = TAG)
182+
return deps.lightningRepo().resetPathfindingScores().fold(
183+
onSuccess = { DevResult.Ack(timestamp = it) },
184+
onFailure = {
185+
Logger.error("Failed to reset pathfinding scores", it, context = TAG)
186+
DevResult.Error(it.message)
187+
},
188+
)
189+
}
190+
}
175191
}
176192

177193
@Serializable
@@ -183,6 +199,8 @@ private sealed interface DevResult {
183199

184200
@Serializable data class Invoice(val bolt11: String) : DevResult
185201

202+
@Serializable data class Ack(val success: Boolean = true, val timestamp: Long? = null) : DevResult
203+
186204
@Serializable
187205
data class ProbeSuccess(
188206
val success: Boolean = true,
@@ -224,6 +242,7 @@ private sealed interface DevResult {
224242
val graphNodeCount: Int? = null,
225243
val graphChannelCount: Int? = null,
226244
val latestRgsSyncTimestamp: ULong? = null,
245+
val latestPathfindingScoresSyncTimestamp: ULong? = null,
227246
) : DevResult {
228247
companion object {
229248
fun from(readiness: NodeProbeReadiness) = ProbeReadiness(
@@ -241,6 +260,7 @@ private sealed interface DevResult {
241260
graphNodeCount = readiness.graphNodeCount,
242261
graphChannelCount = readiness.graphChannelCount,
243262
latestRgsSyncTimestamp = readiness.latestRgsSyncTimestamp,
263+
latestPathfindingScoresSyncTimestamp = readiness.latestPathfindingScoresSyncTimestamp,
244264
)
245265
}
246266
}

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import to.bitkit.di.BgDispatcher
6161
import to.bitkit.env.Defaults
6262
import to.bitkit.env.Env
6363
import to.bitkit.ext.getSatsPerVByteFor
64+
import to.bitkit.ext.nowMillis
6465
import to.bitkit.ext.nowTimestamp
6566
import to.bitkit.ext.toPeerDetailsList
6667
import to.bitkit.ext.totalNextOutboundHtlcLimitSats
@@ -99,6 +100,7 @@ import kotlin.time.Duration
99100
import kotlin.time.Duration.Companion.milliseconds
100101
import kotlin.time.Duration.Companion.minutes
101102
import kotlin.time.Duration.Companion.seconds
103+
import kotlin.time.ExperimentalTime
102104

103105
@Singleton
104106
@Suppress("LongParameterList", "TooManyFunctions", "LargeClass")
@@ -1620,9 +1622,49 @@ class LightningRepo @Inject constructor(
16201622
graphNodeCount = graph?.nodeCount,
16211623
graphChannelCount = graph?.channelCount,
16221624
latestRgsSyncTimestamp = graph?.latestRgsSyncTimestamp,
1625+
latestPathfindingScoresSyncTimestamp = state.nodeStatus?.latestPathfindingScoresSyncTimestamp,
16231626
syncHealthy = state.isSyncHealthy,
16241627
)
16251628
}
1629+
1630+
/**
1631+
* Returns the device epoch seconds captured after the VSS deletes and before the node restart,
1632+
* so callers can require any scores sync timestamp to be strictly newer to prove a post-reset download.
1633+
*/
1634+
@OptIn(ExperimentalTime::class)
1635+
suspend fun resetPathfindingScores(walletIndex: Int = 0): Result<Long> = withContext(bgDispatcher) {
1636+
Logger.info("Resetting pathfinding scores", context = TAG)
1637+
1638+
waitForNodeToStop().onFailure { return@withContext Result.failure(it) }
1639+
stop().onFailure {
1640+
Logger.error("Failed to stop node during pathfinding scores reset", it, context = TAG)
1641+
return@withContext Result.failure(it)
1642+
}
1643+
1644+
runCatching {
1645+
val lifecycleState = _lightningState.value.nodeLifecycleState
1646+
check(lifecycleState == NodeLifecycleState.Stopped) {
1647+
"Node lifecycle changed to '$lifecycleState' during pathfinding scores reset"
1648+
}
1649+
vssBackupClientLdk.setup(walletIndex).getOrThrow()
1650+
vssBackupClientLdk.deleteObject(VSS_KEY_SCORER).getOrThrow()
1651+
vssBackupClientLdk.deleteObject(VSS_KEY_EXTERNAL_SCORES_CACHE).getOrThrow()
1652+
}.onFailure {
1653+
Logger.error("Failed to delete pathfinding scores from VSS", it, context = TAG)
1654+
start(walletIndex = walletIndex, shouldRetry = false).onFailure { startError ->
1655+
Logger.error("Failed to restart node after pathfinding scores reset failure", startError, context = TAG)
1656+
}
1657+
return@withContext Result.failure(it)
1658+
}
1659+
1660+
val resetAtSecs = nowMillis() / 1000
1661+
1662+
start(walletIndex = walletIndex, shouldRetry = false)
1663+
.map { resetAtSecs }
1664+
.onSuccess {
1665+
Logger.info("Pathfinding scores reset at '$resetAtSecs'", context = TAG)
1666+
}
1667+
}
16261668
// endregion
16271669

16281670
suspend fun restartNode(): Result<Unit> = withContext(bgDispatcher) {
@@ -1642,6 +1684,8 @@ class LightningRepo @Inject constructor(
16421684
companion object {
16431685
private const val TAG = "LightningRepo"
16441686
private const val LENGTH_CHANNEL_ID_PREVIEW = 10
1687+
private const val VSS_KEY_SCORER = "scorer"
1688+
private const val VSS_KEY_EXTERNAL_SCORES_CACHE = "external_pathfinding_scores_cache"
16451689
private const val MS_SYNC_LOOP_DEBOUNCE = 500L
16461690
private const val SYNC_RETRY_DELAY_MS = 15_000L
16471691
private val CHANNELS_USABLE_TIMEOUT = 15.seconds
@@ -1702,6 +1746,7 @@ data class ProbeReadiness(
17021746
val graphNodeCount: Int?,
17031747
val graphChannelCount: Int?,
17041748
val latestRgsSyncTimestamp: ULong?,
1749+
val latestPathfindingScoresSyncTimestamp: ULong?,
17051750
val syncHealthy: Boolean,
17061751
) {
17071752
val ready: Boolean

0 commit comments

Comments
 (0)