Skip to content

Commit 93ddad6

Browse files
committed
fix: guard trezor broadcast reset
1 parent e497eb7 commit 93ddad6

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

app/src/main/java/to/bitkit/ui/screens/trezor/TrezorViewModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ class TrezorViewModel @Inject constructor(
369369
.onSuccess { txid ->
370370
TrezorDebugLog.log("BROADCAST", "SUCCESS txid=$txid")
371371
_uiState.update {
372+
if (it.send.step != signedStep) return@update it
373+
372374
it.copy(
373375
send = it.send.copy(
374376
isBroadcasting = false,

app/src/test/java/to/bitkit/ui/screens/trezor/TrezorViewModelTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package to.bitkit.ui.screens.trezor
22

3+
import kotlinx.coroutines.CompletableDeferred
34
import kotlinx.coroutines.ExperimentalCoroutinesApi
45
import kotlinx.coroutines.flow.MutableStateFlow
56
import kotlinx.coroutines.test.TestScope
@@ -8,6 +9,7 @@ import org.junit.Before
89
import org.junit.Test
910
import org.mockito.kotlin.any
1011
import org.mockito.kotlin.anyOrNull
12+
import org.mockito.kotlin.doSuspendableAnswer
1113
import org.mockito.kotlin.mock
1214
import org.mockito.kotlin.never
1315
import org.mockito.kotlin.verify
@@ -245,6 +247,26 @@ class TrezorViewModelTest : BaseUnitTest() {
245247
verify(trezorRepo, never()).broadcastRawTx(any(), any())
246248
}
247249

250+
@Test
251+
fun `broadcastSignedTx should not restore signed step after reset`() = test {
252+
loadSignedTx()
253+
val broadcastResult = CompletableDeferred<Result<String>>()
254+
whenever(trezorRepo.broadcastRawTx(any(), any()))
255+
.doSuspendableAnswer { broadcastResult.await() }
256+
257+
sut.broadcastSignedTx()
258+
assertTrue(sut.uiState.value.isBroadcasting)
259+
260+
sut.resetSendFlow()
261+
broadcastResult.complete(Result.success("broadcast-txid"))
262+
advanceUntilIdle()
263+
264+
val state = sut.uiState.value
265+
assertEquals(SendStep.Form, state.sendStep)
266+
assertFalse(state.isBroadcasting)
267+
assertNull(state.broadcastTxid)
268+
}
269+
248270
@Test
249271
fun `composeTx should not call repo when destination address is blank`() = test {
250272
loadAccountInfo()
@@ -323,4 +345,19 @@ class TrezorViewModelTest : BaseUnitTest() {
323345
sut.lookupBalanceInfo()
324346
advanceUntilIdle()
325347
}
348+
349+
private suspend fun TestScope.loadSignedTx() {
350+
loadAccountInfo()
351+
whenever(trezorRepo.composeTransaction(any(), any(), any(), any(), anyOrNull(), any()))
352+
.thenReturn(Result.success(listOf(TrezorPreviewData.sampleComposeResult)))
353+
whenever(trezorRepo.signTxFromPsbt(any(), anyOrNull()))
354+
.thenReturn(Result.success(TrezorPreviewData.sampleSignedTx))
355+
356+
sut.setSendAddress("bc1qtest123")
357+
sut.setSendAmount("1000")
358+
sut.composeTx()
359+
advanceUntilIdle()
360+
sut.signComposedTx()
361+
advanceUntilIdle()
362+
}
326363
}

0 commit comments

Comments
 (0)