Skip to content

Commit

Permalink
Make TxRepository refresh the whole tx list every time I get a new tx…
Browse files Browse the repository at this point in the history
…, increase version to 0.29.0(317)
  • Loading branch information
igordanilcenko committed Nov 14, 2024
1 parent 8380bb6 commit d2cfcfa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ class WalletCallbackListener @Inject constructor() {
fun onTxBroadcast(contextPtr: ByteArray, completedTxPtr: FFIPointer) {
val walletContextId = BigInteger(1, contextPtr).toInt()
val tx = FFICompletedTx(completedTxPtr)
log(walletContextId, "Tx broadcast ${tx.getId()}")
when (tx.getDirection()) {
Tx.Direction.INBOUND -> {
val pendingInboundTx = PendingInboundTx(tx)
log(walletContextId, "Tx inbound broadcast ${tx.getId()}")
listeners[walletContextId]?.onInboundTxBroadcast(pendingInboundTx)
}

Tx.Direction.OUTBOUND -> {
val pendingOutboundTx = PendingOutboundTx(tx)
log(walletContextId, "Tx outbound broadcast ${tx.getId()}")
listeners[walletContextId]?.onOutboundTxBroadcast(pendingOutboundTx)
}
}
Expand All @@ -99,7 +100,7 @@ class WalletCallbackListener @Inject constructor() {
val walletContextId = BigInteger(1, contextPtr).toInt()
val confirmationCount = BigInteger(1, confirmationCountBytes).toInt()
val completed = CompletedTx(completedTxPtr)
log(walletContextId, "Tx mined & unconfirmed ${completed.id} $confirmationCount")
log(walletContextId, "Tx mined & unconfirmed ${completed.id} ($confirmationCount confirmations)")
listeners[walletContextId]?.onTxMinedUnconfirmed(completed, confirmationCount)
}

Expand All @@ -114,7 +115,7 @@ class WalletCallbackListener @Inject constructor() {
val walletContextId = BigInteger(1, contextPtr).toInt()
val confirmationCount = BigInteger(1, confirmationCountBytes).toInt()
val completed = CompletedTx(completedTxPtr)
log(walletContextId, "Tx faux unconfirmed ${completed.id}")
log(walletContextId, "Tx faux unconfirmed ${completed.id} ($confirmationCount confirmations)")
listeners[walletContextId]?.onTxMinedUnconfirmed(completed, confirmationCount)
}

Expand Down
123 changes: 11 additions & 112 deletions app/src/main/java/com/tari/android/wallet/data/tx/TxRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ import com.tari.android.wallet.application.walletManager.WalletManager
import com.tari.android.wallet.application.walletManager.WalletManager.WalletEvent
import com.tari.android.wallet.application.walletManager.doOnWalletRunning
import com.tari.android.wallet.di.ApplicationScope
import com.tari.android.wallet.extension.replaceItem
import com.tari.android.wallet.extension.replaceOrAddItem
import com.tari.android.wallet.extension.withItem
import com.tari.android.wallet.extension.withoutItem
import com.tari.android.wallet.model.CancelledTx
import com.tari.android.wallet.model.CompletedTx
import com.tari.android.wallet.model.PendingInboundTx
import com.tari.android.wallet.model.PendingOutboundTx
import com.tari.android.wallet.model.Tx
import com.tari.android.wallet.model.TxId
import com.tari.android.wallet.model.TxStatus
import com.tari.android.wallet.ui.fragment.contactBook.data.ContactsRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -66,21 +55,19 @@ class TxRepository @Inject constructor(
applicationScope.launch(Dispatchers.IO) {
walletManager.walletEvent.collect { event ->
when (event) {
is WalletEvent.Tx.TxReceived -> onTxReceived(event.tx)
is WalletEvent.Tx.TxReplyReceived -> onTxReplyReceived(event.tx)
is WalletEvent.Tx.TxFinalized -> onTxFinalized(event.tx)
is WalletEvent.Tx.InboundTxBroadcast -> onInboundTxBroadcast(event.tx)
is WalletEvent.Tx.OutboundTxBroadcast -> onOutboundTxBroadcast(event.tx)
is WalletEvent.Tx.TxMinedUnconfirmed -> onTxMinedUnconfirmed(event.tx)
is WalletEvent.Tx.TxMined -> onTxMined(event.tx)
is WalletEvent.Tx.TxFauxMinedUnconfirmed -> onTxFauxMinedUnconfirmed(event.tx)
is WalletEvent.Tx.TxFauxConfirmed -> onFauxTxMined(event.tx)
is WalletEvent.Tx.TxCancelled -> onTxCancelled(event.tx)

is WalletEvent.Tx.TxReceived,
is WalletEvent.Tx.TxReplyReceived,
is WalletEvent.Tx.TxFinalized,
is WalletEvent.Tx.InboundTxBroadcast,
is WalletEvent.Tx.OutboundTxBroadcast,
is WalletEvent.Tx.TxMinedUnconfirmed,
is WalletEvent.Tx.TxMined,
is WalletEvent.Tx.TxFauxMinedUnconfirmed,
is WalletEvent.Tx.TxFauxConfirmed,
is WalletEvent.Tx.TxCancelled,
is WalletEvent.TxSend.TxSendSuccessful,
is WalletEvent.UtxosSplit -> refreshTxList()

is WalletEvent.TxSend.TxSendSuccessful -> onTxSendSuccessful(event.txId)

else -> Unit
}
}
Expand Down Expand Up @@ -113,94 +100,6 @@ class TxRepository @Inject constructor(
}
}

private fun onTxReceived(tx: PendingInboundTx) {
_txs.update { it.copy(pendingInboundTxs = it.pendingInboundTxs.withItem(tx)) }
}

private fun onTxReplyReceived(tx: PendingOutboundTx) {
_txs.update { txs ->
txs.copy(pendingOutboundTxs = txs.pendingOutboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = tx.status) }))
}
}

private fun onTxFinalized(tx: PendingInboundTx) {
_txs.update { txs ->
txs.copy(pendingInboundTxs = txs.pendingInboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = tx.status) }))
}
}

private fun onInboundTxBroadcast(tx: PendingInboundTx) {
_txs.update { txs ->
txs.copy(pendingInboundTxs = txs.pendingInboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = TxStatus.BROADCAST) }))
}
}

private fun onOutboundTxBroadcast(tx: PendingOutboundTx) {
_txs.update { txs ->
txs.copy(pendingOutboundTxs = txs.pendingOutboundTxs.replaceItem({ it.id == tx.id }, { it.copy(status = TxStatus.BROADCAST) }))
}
}

private fun onTxMinedUnconfirmed(tx: CompletedTx) {
_txs.update { txs ->
txs.copy(
completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx),
pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs,
pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs,
)
}
}

private fun onTxMined(tx: CompletedTx) {
_txs.update { txs ->
txs.copy(
completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx),
pendingInboundTxs = txs.pendingInboundTxs.withoutItem { it.id == tx.id },
pendingOutboundTxs = txs.pendingOutboundTxs.withoutItem { it.id == tx.id },
)
}
}

private fun onTxFauxMinedUnconfirmed(tx: CompletedTx) {
_txs.update { txs ->
txs.copy(
completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx),
pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs,
pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs,
)
}
}

private fun onFauxTxMined(tx: CompletedTx) {
_txs.update { txs ->
txs.copy(
completedTxs = txs.completedTxs.replaceOrAddItem({ it.id == tx.id }, tx),
pendingInboundTxs = txs.pendingInboundTxs.withoutItem { it.id == tx.id },
pendingOutboundTxs = txs.pendingOutboundTxs.withoutItem { it.id == tx.id },
)
}
}

private fun onTxCancelled(tx: CancelledTx) {
_txs.update { txs ->
txs.copy(
cancelledTxs = txs.cancelledTxs.withItem(tx),
pendingInboundTxs = if (tx.isInbound) txs.pendingInboundTxs.withoutItem { it.id == tx.id } else txs.pendingInboundTxs,
pendingOutboundTxs = if (tx.isOutbound) txs.pendingOutboundTxs.withoutItem { it.id == tx.id } else txs.pendingOutboundTxs,
)
}
}

private fun onTxSendSuccessful(txId: TxId) {
try {
val tx = walletManager.requireWalletInstance.getPendingOutboundTxById(txId)
_txs.update { it.copy(pendingOutboundTxs = it.pendingOutboundTxs.withItem(tx)) }
} catch (e: Exception) {
logger.i("onTxSendSuccessful: error getting tx by id")
refreshTxList()
}
}

private fun Tx.toDto() = TxDto(
tx = this,
contact = contactsRepository.getContactForTx(this),
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
ext.coroutines_version = '1.8.0'

// build & version
ext.buildNumber = 316
ext.buildNumber = 317
ext.versionNumber = "0.29.0"

// JNI libs
Expand Down

0 comments on commit d2cfcfa

Please sign in to comment.