Skip to content

Commit 2da923f

Browse files
authored
Merge pull request #347 from synonymdev/fix/reduce-shouldBlockLightning-calls
Reduce geoBlock calls
2 parents 7a48a8e + 6305b4f commit 2da923f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class LightningRepo @Inject constructor(
205205

206206
// Initial state sync
207207
syncState()
208+
updateGeoBlockState()
208209

209210
// Perform post-startup tasks
210211
connectToTrustedPeers().onFailure { e ->
@@ -238,6 +239,15 @@ class LightningRepo @Inject constructor(
238239
}
239240
}
240241

242+
/**Updates the shouldBlockLightning state and returns the current value*/
243+
private suspend fun updateGeoBlockState(): Boolean {
244+
val shouldBlock = coreService.shouldBlockLightning()
245+
_lightningState.update {
246+
it.copy(shouldBlockLightning = shouldBlock)
247+
}
248+
return shouldBlock
249+
}
250+
241251
fun setInitNodeLifecycleState() {
242252
_lightningState.update { it.copy(nodeLifecycleState = NodeLifecycleState.Initializing) }
243253
}
@@ -414,7 +424,7 @@ class LightningRepo @Inject constructor(
414424
description: String,
415425
expirySeconds: UInt = 86_400u,
416426
): Result<String> = executeWhenNodeRunning("Create invoice") {
417-
if (coreService.shouldBlockLightning()) {
427+
if (updateGeoBlockState()) {
418428
return@executeWhenNodeRunning Result.failure(ServiceError.GeoBlocked)
419429
}
420430

@@ -661,7 +671,6 @@ class LightningRepo @Inject constructor(
661671
nodeStatus = getStatus(),
662672
peers = getPeers().orEmpty(),
663673
channels = getChannels().orEmpty(),
664-
shouldBlockLightning = coreService.shouldBlockLightning()
665674
)
666675
}
667676
}

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ class CoreService @Inject constructor(
145145

146146
suspend fun hasExternalNode() = getConnectedPeers().any { connectedPeer -> connectedPeer !in getLspPeers() }
147147

148-
// TODO this is business logic, should be moved to the domain layer in the future
149-
// TODO this spams network calls too often, it needs a caching mechanism
150148
suspend fun shouldBlockLightning() = checkGeoStatus() == true && !hasExternalNode()
151149
}
152150

0 commit comments

Comments
 (0)