Skip to content
Closed
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
17 changes: 4 additions & 13 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/to/bitkit/repositories/WalletRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
36 changes: 6 additions & 30 deletions app/src/main/java/to/bitkit/services/CoreService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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<ChannelDetails> {
on { inboundCapacityMsat } doReturn 500_000u // 500 sats
Expand All @@ -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<ChannelDetails> {
on { inboundCapacityMsat } doReturn 500_000u // 500 sats
Expand All @@ -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()
Expand Down