Skip to content

Commit 57bc821

Browse files
committed
fix(receive): enable LN receive and QR only when having channels ready
1 parent 4e2750b commit 57bc821

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,10 @@ class LightningRepo @Inject constructor(
245245
}
246246
}
247247

248-
/**Updates the shouldBlockLightning state and returns the current value*/
249248
suspend fun updateGeoBlockState() {
250249
val (isGeoBlocked, shouldBlockLightning) = coreService.checkGeoBlock()
251250
_lightningState.update {
252-
it.copy(shouldBlockLightning = shouldBlockLightning, isGeoBlocked = isGeoBlocked)
251+
it.copy(isGeoBlocked = isGeoBlocked, shouldBlockLightning = shouldBlockLightning)
253252
}
254253
}
255254

@@ -673,7 +672,7 @@ class LightningRepo @Inject constructor(
673672
Result.success(Unit)
674673
}
675674

676-
suspend fun syncState() {
675+
fun syncState() {
677676
_lightningState.update {
678677
it.copy(
679678
nodeId = getNodeId().orEmpty(),
@@ -709,8 +708,11 @@ class LightningRepo @Inject constructor(
709708
fun getChannels(): List<ChannelDetails>? =
710709
if (_lightningState.value.nodeLifecycleState.isRunning()) lightningService.channels else null
711710

712-
fun hasChannels(): Boolean =
713-
_lightningState.value.nodeLifecycleState.isRunning() && lightningService.channels?.isNotEmpty() == true
711+
fun hasChannelsReady(): Boolean {
712+
val isRunning = _lightningState.value.nodeLifecycleState.isRunning()
713+
val hasChannelsReady = lightningService.channels?.any { it.isChannelReady } == true
714+
return isRunning && hasChannelsReady
715+
}
714716

715717
suspend fun registerForNotifications(token: String? = null) = executeWhenNodeRunning("registerForNotifications") {
716718
return@executeWhenNodeRunning try {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ class WalletRepo @Inject constructor(
102102
suspend fun refreshBip21(force: Boolean = false): Result<Unit> = withContext(bgDispatcher) {
103103
Logger.debug("Refreshing bip21 (force: $force)", context = TAG)
104104

105-
val shouldBlockLightning = coreService.checkGeoBlock().second
105+
val receiveOnSpendingBalance = !coreService.checkGeoBlock().second && lightningRepo.hasChannelsReady()
106106
_walletState.update {
107-
it.copy(receiveOnSpendingBalance = !shouldBlockLightning)
107+
it.copy(receiveOnSpendingBalance = receiveOnSpendingBalance)
108108
}
109109

110110
// Reset invoice state
@@ -382,9 +382,8 @@ class WalletRepo @Inject constructor(
382382
updateBip21AmountSats(amountSats)
383383
updateBip21Description(description)
384384

385-
val hasChannels = lightningRepo.hasChannels()
386-
387-
if (hasChannels && _walletState.value.receiveOnSpendingBalance) {
385+
val hasChannelsReady = lightningRepo.hasChannelsReady()
386+
if (hasChannelsReady && _walletState.value.receiveOnSpendingBalance) {
388387
lightningRepo.createInvoice(
389388
amountSats = _walletState.value.bip21AmountSats,
390389
description = _walletState.value.bip21Description,

app/src/main/java/to/bitkit/ui/screens/wallets/receive/ReceiveSheet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fun ReceiveSheet(
7575
walletState = walletState,
7676
onCjitToggle = { isOn ->
7777
when {
78-
isOn && lightningState.shouldBlockLightning -> { // TODO SHOULD BLOCK HERE
78+
isOn && lightningState.shouldBlockLightning -> {
7979
navController.navigate(ReceiveRoute.GeoBlock)
8080
}
8181

app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,25 @@ class LightningRepoTest : BaseUnitTest() {
254254
}
255255

256256
@Test
257-
fun `hasChannels should return false when node is not running`() = test {
258-
assertFalse(sut.hasChannels())
257+
fun `hasChannelsReady should return false when node is not running`() = test {
258+
assertFalse(sut.hasChannelsReady())
259+
}
260+
261+
@Test
262+
fun `hasChannelsReady should return false when having non-ready channels`() = test {
263+
startNodeForTesting()
264+
whenever(lightningService.channels).thenReturn(listOf(mock()))
265+
266+
assertFalse(sut.hasChannelsReady())
267+
}
268+
269+
@Test
270+
fun `hasChannelsReady should return true when having channels ready`() = test {
271+
startNodeForTesting()
272+
whenever(lightningService.channels)
273+
.thenReturn(listOf(mock<ChannelDetails> { on { isChannelReady } doReturn true }))
274+
275+
assertTrue(sut.hasChannelsReady())
259276
}
260277

261278
@Test

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ class WalletRepoTest : BaseUnitTest() {
258258
}
259259

260260
@Test
261-
fun `updateBip21Invoice should create bolt11 when channels exist`() = test {
261+
fun `updateBip21Invoice should create bolt11 when having channels ready`() = test {
262262
val testInvoice = "testInvoice"
263-
whenever(lightningRepo.hasChannels()).thenReturn(true)
264-
whenever(lightningRepo.createInvoice(1000uL, description = "test")).thenReturn(Result.success(testInvoice))
263+
whenever(lightningRepo.hasChannelsReady()).thenReturn(true)
264+
whenever(lightningRepo.createInvoice(anyOrNull(), any(), any())).thenReturn(Result.success(testInvoice))
265265

266266
sut.updateBip21Invoice(amountSats = 1000uL, description = "test").let { result ->
267267
assertTrue(result.isSuccess)
@@ -271,8 +271,7 @@ class WalletRepoTest : BaseUnitTest() {
271271

272272
@Test
273273
fun `updateBip21Invoice should not create bolt11 when no channels exist`() = test {
274-
whenever(lightningRepo.hasChannels()).thenReturn(false)
275-
274+
whenever(lightningRepo.hasChannelsReady()).thenReturn(false)
276275
sut.updateBip21Invoice(amountSats = 1000uL, description = "test").let { result ->
277276
assertTrue(result.isSuccess)
278277
assertEquals("", sut.walletState.value.bolt11)
@@ -283,7 +282,8 @@ class WalletRepoTest : BaseUnitTest() {
283282
fun `updateBip21Invoice should build correct BIP21 URL`() = test {
284283
val testAddress = "testAddress"
285284
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
286-
whenever(lightningRepo.hasChannels()).thenReturn(false)
285+
whenever(lightningRepo.hasChannelsReady()).thenReturn(false)
286+
whenever(lightningRepo.createInvoice(anyOrNull(), any(), any())).thenReturn(Result.success("testInvoice"))
287287
sut = createSut()
288288

289289
sut.updateBip21Invoice(amountSats = 1000uL, description = "test").let { result ->

0 commit comments

Comments
 (0)