-
Notifications
You must be signed in to change notification settings - Fork 4
feat(send): improve Lightning send failure recovery #1140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pwltr
wants to merge
4
commits into
master
Choose a base branch
from
feat/reset-routing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package to.bitkit.models | ||
|
|
||
| data class SendFailureDetails( | ||
| val message: String, | ||
| val failureType: String, | ||
| val resetRoutingCachesOnRetry: Boolean, | ||
| val paymentRequest: String? = null, | ||
| ) { | ||
| fun shouldResetRoutingCaches(routingCacheResetAttempted: Boolean): Boolean { | ||
| return resetRoutingCachesOnRetry && !routingCacheResetAttempted | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ import org.lightningdevkit.ldknode.ChannelDetails | |
| import org.lightningdevkit.ldknode.ClosureReason | ||
| import org.lightningdevkit.ldknode.CoinSelectionAlgorithm | ||
| import org.lightningdevkit.ldknode.Event | ||
| import org.lightningdevkit.ldknode.Network | ||
| import org.lightningdevkit.ldknode.NodeStatus | ||
| import org.lightningdevkit.ldknode.PaymentDetails | ||
| import org.lightningdevkit.ldknode.PaymentHash | ||
|
|
@@ -92,6 +93,7 @@ import to.bitkit.services.LnurlChannelResponse | |
| import to.bitkit.services.LnurlService | ||
| import to.bitkit.services.LnurlWithdrawResponse | ||
| import to.bitkit.services.LspNotificationsService | ||
| import to.bitkit.services.NetworkGraphInfo | ||
| import to.bitkit.services.NodeEventHandler | ||
| import to.bitkit.utils.AppError | ||
| import to.bitkit.utils.Logger | ||
|
|
@@ -637,8 +639,14 @@ class LightningRepo @Inject constructor( | |
| } | ||
|
|
||
| private suspend fun clearNetworkGraph(walletIndex: Int): Result<Unit> { | ||
| lightningService.resetNetworkGraph(walletIndex) | ||
| return runCatching { | ||
| runSuspendCatching { | ||
| lightningService.resetNetworkGraph(walletIndex) | ||
| }.onFailure { | ||
| Logger.warn("Failed to clear local network graph", it, context = TAG) | ||
| return Result.failure(it) | ||
| } | ||
|
|
||
| return runSuspendCatching { | ||
| vssBackupClientLdk.setup(walletIndex).getOrThrow() | ||
| vssBackupClientLdk.deleteObject("network_graph").getOrThrow() | ||
| Logger.info("Cleared network graph from VSS", context = TAG) | ||
|
|
@@ -1859,20 +1867,111 @@ class LightningRepo @Inject constructor( | |
| vssBackupClientLdk.deleteObject(VSS_KEY_EXTERNAL_SCORES_CACHE).getOrThrow() | ||
| }.onFailure { | ||
| Logger.error("Failed to delete pathfinding scores from VSS", it, context = TAG) | ||
| start(walletIndex = walletIndex, shouldRetry = false).onFailure { startError -> | ||
| start(walletIndex = walletIndex, shouldRetry = false, shouldValidateGraph = false).onFailure { startError -> | ||
| Logger.error("Failed to restart node after pathfinding scores reset failure", startError, context = TAG) | ||
| } | ||
| return@withContext Result.failure(it) | ||
| } | ||
|
|
||
| val resetAtSecs = nowMillis() / 1000 | ||
|
|
||
| start(walletIndex = walletIndex, shouldRetry = false) | ||
| start(walletIndex = walletIndex, shouldRetry = false, shouldValidateGraph = false) | ||
| .map { resetAtSecs } | ||
| .onSuccess { | ||
| Logger.info("Pathfinding scores reset at '$resetAtSecs'", context = TAG) | ||
| } | ||
| } | ||
|
|
||
| suspend fun resetPaymentRoutingCachesAndWait(walletIndex: Int = 0): Result<Unit> = withContext(bgDispatcher) { | ||
| val refreshStartedAtMs = nowMillis() | ||
| val refreshStartedAtSecs = (refreshStartedAtMs / 1000).toULong() | ||
| val requiresRgsRefresh = Env.network != Network.REGTEST && | ||
| !settingsStore.data.first().rgsServerUrl.isNullOrEmpty() | ||
| val requiresScorerRefresh = Env.ldkScorerUrl != null | ||
| val resetErrors = mutableListOf<Throwable>() | ||
|
|
||
| Logger.info( | ||
| "Started payment routing refresh rgs='$requiresRgsRefresh' scorer='$requiresScorerRefresh'", | ||
| context = TAG, | ||
| ) | ||
|
|
||
| stop().onFailure { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the node stops here and I only see it starting again on the VSS-delete failure path worth double checking if the is some path that Kees the node on stopped state |
||
| return@withContext Result.failure(it) | ||
| } | ||
|
|
||
| clearNetworkGraph(walletIndex).onFailure { | ||
| resetErrors.add(it) | ||
| } | ||
|
|
||
| resetPathfindingScores(walletIndex).onFailure { | ||
| resetErrors.add(it) | ||
| } | ||
|
|
||
| resetErrors.firstOrNull()?.let { | ||
| return@withContext Result.failure(it) | ||
| } | ||
|
|
||
| val result = waitForPaymentRoutingDataRefresh( | ||
| walletIndex = walletIndex, | ||
| refreshStartedAtMs = refreshStartedAtMs, | ||
| refreshStartedAtSecs = refreshStartedAtSecs, | ||
| requiresRgsRefresh = requiresRgsRefresh, | ||
| requiresScorerRefresh = requiresScorerRefresh, | ||
| ) | ||
| result.onSuccess { | ||
| Logger.info( | ||
| "Finished payment routing refresh elapsedMs='${nowMillis() - refreshStartedAtMs}'", | ||
| context = TAG, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun waitForPaymentRoutingDataRefresh( | ||
| walletIndex: Int, | ||
| refreshStartedAtMs: Long, | ||
| refreshStartedAtSecs: ULong, | ||
| requiresRgsRefresh: Boolean, | ||
| requiresScorerRefresh: Boolean, | ||
| ): Result<Unit> = withContext(bgDispatcher) { | ||
| if (!requiresRgsRefresh && !requiresScorerRefresh) { | ||
| Logger.info( | ||
| "Skipped payment routing refresh wait because no routing sources are required", | ||
| context = TAG, | ||
| ) | ||
| return@withContext Result.success(Unit) | ||
| } | ||
|
|
||
| var lastStatus: PaymentRoutingRefreshStatus? = null | ||
| val refreshed = withTimeoutOrNull(PAYMENT_ROUTING_REFRESH_TIMEOUT) { | ||
| while (isActive) { | ||
| syncState() | ||
| val status = _lightningState.value.paymentRoutingRefreshStatus( | ||
| graphCacheModificationDate = lightningService.networkGraphCacheModificationDate(walletIndex), | ||
| networkGraphInfo = getNetworkGraphInfo(), | ||
| refreshStartedAtSecs = refreshStartedAtSecs, | ||
| requiresRgsRefresh = requiresRgsRefresh, | ||
| requiresScorerRefresh = requiresScorerRefresh, | ||
| ) | ||
| lastStatus = status | ||
| if (status.isFresh) { | ||
| return@withTimeoutOrNull true | ||
| } | ||
| delay(PAYMENT_ROUTING_REFRESH_POLL_DELAY) | ||
| } | ||
| false | ||
| } == true | ||
|
|
||
| if (refreshed) { | ||
| Result.success(Unit) | ||
| } else { | ||
| Logger.warn( | ||
| "Timed out payment routing refresh elapsedMs='${nowMillis() - refreshStartedAtMs}' " + | ||
| lastStatus?.toLogFields().orEmpty(), | ||
| context = TAG, | ||
| ) | ||
| Result.failure(PaymentRoutingRefreshTimeoutError()) | ||
| } | ||
| } | ||
| // endregion | ||
|
|
||
| suspend fun restartNode(): Result<Unit> = withContext(bgDispatcher) { | ||
|
|
@@ -1901,6 +2000,64 @@ class LightningRepo @Inject constructor( | |
| private val NO_USABLE_CHANNELS_FEEDBACK_DELAY = 2_500.milliseconds | ||
| val SEND_LN_TIMEOUT = 10.seconds | ||
| private val PROBE_TIMEOUT = 60.seconds | ||
| private val PAYMENT_ROUTING_REFRESH_TIMEOUT = 20.seconds | ||
| private val PAYMENT_ROUTING_REFRESH_POLL_DELAY = 500.milliseconds | ||
| } | ||
| } | ||
|
|
||
| private fun LightningState.paymentRoutingRefreshStatus( | ||
| graphCacheModificationDate: Long?, | ||
| networkGraphInfo: NetworkGraphInfo?, | ||
| refreshStartedAtSecs: ULong, | ||
| requiresRgsRefresh: Boolean, | ||
| requiresScorerRefresh: Boolean, | ||
| ): PaymentRoutingRefreshStatus { | ||
| val status = nodeStatus | ||
| val nodeRunning = nodeLifecycleState.isRunning() | ||
| val graphNodeCount = networkGraphInfo?.nodeCount | ||
| val graphChannelCount = networkGraphInfo?.channelCount | ||
| val hasInMemoryGraph = (graphNodeCount ?: 0) > 0 && (graphChannelCount ?: 0) > 0 | ||
|
|
||
| val hasFreshRgs = !requiresRgsRefresh || | ||
| graphCacheModificationDate != null && (graphCacheModificationDate / 1000).toULong() >= refreshStartedAtSecs || | ||
| hasInMemoryGraph | ||
|
|
||
| val latestScoresTimestamp = status?.latestPathfindingScoresSyncTimestamp | ||
| val hasFreshScores = !requiresScorerRefresh || | ||
| latestScoresTimestamp != null && latestScoresTimestamp >= refreshStartedAtSecs | ||
|
|
||
| return PaymentRoutingRefreshStatus( | ||
| nodeRunning = nodeRunning, | ||
| graphFresh = hasFreshRgs, | ||
| scorerFresh = hasFreshScores, | ||
| graphCacheModificationDate = graphCacheModificationDate, | ||
| graphNodeCount = graphNodeCount, | ||
| graphChannelCount = graphChannelCount, | ||
| latestPathfindingScoresSyncTimestamp = latestScoresTimestamp, | ||
| refreshStartedAtSecs = refreshStartedAtSecs, | ||
| ) | ||
| } | ||
|
|
||
| private data class PaymentRoutingRefreshStatus( | ||
| val nodeRunning: Boolean, | ||
| val graphFresh: Boolean, | ||
| val scorerFresh: Boolean, | ||
| val graphCacheModificationDate: Long?, | ||
| val graphNodeCount: Int?, | ||
| val graphChannelCount: Int?, | ||
| val latestPathfindingScoresSyncTimestamp: ULong?, | ||
| val refreshStartedAtSecs: ULong, | ||
| ) { | ||
| val isFresh: Boolean = nodeRunning && graphFresh && scorerFresh | ||
|
|
||
| fun toLogFields(): String { | ||
| return "nodeRunning='$nodeRunning' graphFresh='$graphFresh' scorerFresh='$scorerFresh' " + | ||
| "graphMtime='${graphCacheModificationDate ?: "-"}' " + | ||
| "graphMtimeSecs='${graphCacheModificationDate?.let { it / 1000 } ?: "-"}' " + | ||
| "graphNodes='${graphNodeCount ?: "-"}' " + | ||
| "graphChannels='${graphChannelCount ?: "-"}' " + | ||
| "scorerTs='${latestPathfindingScoresSyncTimestamp ?: "-"}' " + | ||
| "refreshStartedAtSecs='$refreshStartedAtSecs'" | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1912,6 +2069,7 @@ class NodeRunTimeoutError(opName: String) : AppError("Timeout waiting for node t | |
| class GetPaymentsError : AppError("It wasn't possible get the payments") | ||
| class SyncUnhealthyError : AppError("Wallet sync failed before send") | ||
| class LnurlPayInvoiceMismatchError : AppError("The invoice did not match the requested payment. Payment cancelled.") | ||
| class PaymentRoutingRefreshTimeoutError : AppError("Timeout waiting for payment routing data refresh") | ||
|
|
||
| data class NodeEventUpdate( | ||
| val event: Event, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never called