diff --git a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt index dbe042d68..f956f60dc 100644 --- a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt @@ -327,21 +327,12 @@ class LightningRepo @Inject constructor( } /**Updates the shouldBlockLightning state and returns the current value*/ - @Suppress("TooGenericExceptionCaught") private suspend fun updateGeoBlockState(): Boolean { - return try { - val shouldBlock = coreService.shouldBlockLightning() - _lightningState.update { - it.copy(shouldBlockLightning = shouldBlock) - } - shouldBlock - } catch (e: NetworkException) { - Logger.warn("Failed to check geo block status due to network, assuming not blocked", e, context = TAG) - false - } catch (e: Exception) { - Logger.error("Failed to check geo block status", e, context = TAG) - false + val shouldBlock = coreService.shouldBlockLightning() + _lightningState.update { + it.copy(shouldBlockLightning = shouldBlock) } + return shouldBlock } fun setInitNodeLifecycleState() { diff --git a/app/src/main/java/to/bitkit/repositories/WalletRepo.kt b/app/src/main/java/to/bitkit/repositories/WalletRepo.kt index fd9ae9a0b..257af9359 100644 --- a/app/src/main/java/to/bitkit/repositories/WalletRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/WalletRepo.kt @@ -424,7 +424,7 @@ class WalletRepo @Inject constructor( return@withContext try { if (!_walletState.value.receiveOnSpendingBalance) return@withContext Result.success(false) - if (coreService.checkGeoStatus() == true) return@withContext Result.success(false) + if (coreService.shouldBlockLightning()) return@withContext Result.success(false) val channels = lightningRepo.lightningState.value.channels val inboundBalanceSats = channels.sumOf { it.inboundCapacityMsat / 1000u } diff --git a/app/src/main/java/to/bitkit/services/CoreService.kt b/app/src/main/java/to/bitkit/services/CoreService.kt index 5023236af..4464dcca6 100644 --- a/app/src/main/java/to/bitkit/services/CoreService.kt +++ b/app/src/main/java/to/bitkit/services/CoreService.kt @@ -119,7 +119,7 @@ class CoreService @Inject constructor( * Returns true if geo blocked, false if allowed, null if unable to check */ @Suppress("InstanceOfCheckForException", "TooGenericExceptionCaught") - suspend fun checkGeoStatus(): Boolean? { + private suspend fun isGeoBlocked(): Boolean? { return try { ServiceQueue.CORE.background { Logger.verbose("Checking geo status…", context = "GeoCheck") @@ -177,37 +177,13 @@ class CoreService @Inject constructor( suspend fun hasExternalNode() = getConnectedPeers().any { connectedPeer -> connectedPeer !in getLspPeers() } suspend fun shouldBlockLightning(): Boolean { - return try { - val geoStatus = checkGeoStatus() - - when (geoStatus) { - true -> { - // Geo blocked - check if user has external nodes - val hasExternal = hasExternalNode() - val shouldBlock = !hasExternal - Logger.info( - "Geo blocked region, has external node: $hasExternal, blocking: $shouldBlock", - context = "GeoCheck" - ) - shouldBlock - } - - false -> { - // Geo allowed - Logger.debug("Geo allowed region", context = "GeoCheck") - false - } + if (hasExternalNode()) return false - null -> { - // Unable to check (network error) - use safe default - Logger.warn("Unable to check geo status, defaulting to not blocked", context = "GeoCheck") - false // Safe default: don't block if we can't verify - } + return runCatching { isGeoBlocked() ?: false } + .onFailure { + Logger.error("Error in shouldBlockLightning, defaulting to not blocked", context = "GeoCheck") } - } catch (e: Exception) { - Logger.error("Error in shouldBlockLightning, defaulting to not blocked", e, context = "GeoCheck") - false // Safe default: don't block on any error - } + .getOrDefault(false) } } diff --git a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt index ff9c8804c..3ff463fb8 100644 --- a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt +++ b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt @@ -274,7 +274,7 @@ class AppViewModel @Inject constructor( private fun checkGeoStatus() { viewModelScope.launch { try { - isGeoBlocked = coreService.checkGeoStatus() + isGeoBlocked = coreService.shouldBlockLightning() } catch (e: Throwable) { Logger.error("Failed to check geo status: ${e.message}", e, context = TAG) } diff --git a/app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt b/app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt index c227e3eee..49dd0e6e6 100644 --- a/app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt @@ -372,7 +372,7 @@ class WalletRepoTest : BaseUnitTest() { @Test fun `shouldRequestAdditionalLiquidity should return false when receiveOnSpendingBalance is false`() = test { // Given - whenever(coreService.checkGeoStatus()).thenReturn(false) + whenever(coreService.shouldBlockLightning()).thenReturn(false) sut.toggleReceiveOnSpendingBalance() // Set to false (initial is true) // When @@ -386,7 +386,7 @@ class WalletRepoTest : BaseUnitTest() { @Test fun `shouldRequestAdditionalLiquidity should return false when geo status is true`() = test { // Given - whenever(coreService.checkGeoStatus()).thenReturn(true) + whenever(coreService.shouldBlockLightning()).thenReturn(true) // When val result = sut.shouldRequestAdditionalLiquidity() @@ -399,7 +399,7 @@ class WalletRepoTest : BaseUnitTest() { @Test fun `shouldRequestAdditionalLiquidity should return true when amount exceeds inbound capacity`() = test { // Given - whenever(coreService.checkGeoStatus()).thenReturn(false) + whenever(coreService.shouldBlockLightning()).thenReturn(false) val testChannels = listOf( mock { on { inboundCapacityMsat } doReturn 500_000u // 500 sats @@ -422,7 +422,7 @@ class WalletRepoTest : BaseUnitTest() { @Test fun `shouldRequestAdditionalLiquidity should return false when amount is less than inbound capacity`() = test { // Given - whenever(coreService.checkGeoStatus()).thenReturn(false) + whenever(coreService.shouldBlockLightning()).thenReturn(false) val testChannels = listOf( mock { on { inboundCapacityMsat } doReturn 500_000u // 500 sats @@ -445,7 +445,7 @@ class WalletRepoTest : BaseUnitTest() { @Test fun `shouldRequestAdditionalLiquidity should return failure when exception occurs`() = test { // Given - whenever(coreService.checkGeoStatus()).thenThrow(RuntimeException("Test error")) + whenever(coreService.shouldBlockLightning()).thenThrow(RuntimeException("Test error")) // When val result = sut.shouldRequestAdditionalLiquidity()