Skip to content

Commit c5ad4fa

Browse files
committed
fix: refresh cjit on empty cache
1 parent 6fceabe commit c5ad4fa

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class BlocktankRepo @Inject constructor(
144144
val hasPendingCjit = cached.any {
145145
it.channel == null && it.state != CJitStateEnum.EXPIRED && it.state != CJitStateEnum.FAILED
146146
}
147-
if (!hasPendingCjit) return@withContext null
147+
if (cached.isNotEmpty() && !hasPendingCjit) return@withContext null
148148

149149
// Match against the freshly fetched list so a concurrent refreshOrders() can't clobber state before we read.
150150
return@withContext refreshCjitEntries().matching()

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,24 @@ class BlocktankRepoTest : BaseUnitTest() {
377377
assertEquals(entry, sut.getCjitEntry(channelDetails))
378378
}
379379

380+
@Test
381+
fun `getCjitEntry refreshes when the cache is empty`() = test {
382+
sut = createSut()
383+
// Cold start / right after createCjit: cjitEntries hasn't loaded yet, so an empty cache must not be
384+
// treated as a terminal no-match — the server refresh has to surface the matching entry.
385+
val fundingTxId = "cjit-funding-tx"
386+
val channel = mock<IBtChannel>()
387+
whenever(channel.fundingTx).thenReturn(FundingTx(id = fundingTxId, vout = 0u))
388+
val entry = mock<IcJitEntry>()
389+
whenever(entry.channel).thenReturn(channel)
390+
whenever(coreService.blocktank.cjitEntries(refresh = true)).thenReturn(listOf(entry))
391+
392+
val channelDetails = mock<ChannelDetails>()
393+
whenever(channelDetails.fundingTxo).thenReturn(OutPoint(txid = fundingTxId, vout = 0u))
394+
395+
assertEquals(entry, sut.getCjitEntry(channelDetails))
396+
}
397+
380398
private fun pendingCjitEntry(): IcJitEntry = mock<IcJitEntry>().apply {
381399
whenever(channel).thenReturn(null)
382400
whenever(state).thenReturn(CJitStateEnum.CREATED)

0 commit comments

Comments
 (0)