Skip to content

Commit ee7c812

Browse files
committed
fix: migrate activities for unsupported addresses
1 parent 820762c commit ee7c812

2 files changed

Lines changed: 97 additions & 17 deletions

File tree

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ class ActivityService(
600600
private fun buildUpdatedOnchainActivity(
601601
existingActivity: Activity.Onchain,
602602
confirmationData: ConfirmationData,
603+
ldkValue: ULong,
603604
channelId: String? = null,
604605
): OnchainActivity {
605606
var preservedIsTransfer = existingActivity.v1.isTransfer
@@ -612,13 +613,20 @@ class ActivityService(
612613

613614
val finalDoesExist = if (confirmationData.isConfirmed) true else existingActivity.v1.doesExist
614615

616+
val preservedValue = if (existingActivity.v1.value > ldkValue) {
617+
existingActivity.v1.value
618+
} else {
619+
ldkValue
620+
}
621+
615622
val updatedOnChain = existingActivity.v1.copy(
616623
confirmed = confirmationData.isConfirmed,
617624
confirmTimestamp = confirmationData.confirmedTimestamp,
618625
doesExist = finalDoesExist,
619626
updatedAt = confirmationData.timestamp,
620627
isTransfer = preservedIsTransfer,
621628
channelId = preservedChannelId,
629+
value = preservedValue,
622630
)
623631

624632
return updatedOnChain
@@ -667,10 +675,16 @@ class ActivityService(
667675
val timestamp = payment.latestUpdateTimestamp
668676
val confirmationData = getConfirmationStatus(kind, timestamp)
669677

670-
val existingActivity = getActivityById(payment.id)
678+
var existingActivity = getActivityById(payment.id)
679+
if (existingActivity == null) {
680+
getOnchainActivityByTxId(kind.txid)?.let {
681+
existingActivity = Activity.Onchain(it)
682+
}
683+
}
684+
671685
if (existingActivity != null &&
672686
existingActivity is Activity.Onchain &&
673-
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
687+
((existingActivity as Activity.Onchain).v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
674688
) {
675689
return
676690
}
@@ -691,10 +705,12 @@ class ActivityService(
691705

692706
val resolvedAddress = resolveAddressForInboundPayment(kind, existingActivity, payment, transactionDetails)
693707

708+
val ldkValue = payment.amountSats ?: 0u
694709
val onChain = if (existingActivity is Activity.Onchain) {
695710
buildUpdatedOnchainActivity(
696-
existingActivity = existingActivity,
711+
existingActivity = existingActivity as Activity.Onchain,
697712
confirmationData = confirmationData,
713+
ldkValue = ldkValue,
698714
channelId = resolvedChannelId,
699715
)
700716
} else {
@@ -712,8 +728,9 @@ class ActivityService(
712728
return
713729
}
714730

715-
if (existingActivity != null) {
716-
updateActivity(payment.id, Activity.Onchain(onChain))
731+
if (existingActivity != null && existingActivity is Activity.Onchain) {
732+
val existingOnchain = existingActivity.v1
733+
updateActivity(existingOnchain.id, Activity.Onchain(onChain))
717734
} else {
718735
upsertActivity(Activity.Onchain(onChain))
719736
}

app/src/main/java/to/bitkit/services/MigrationService.kt

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,6 @@ class MigrationService @Inject constructor(
12391239
if (hasRNMmkvData()) {
12401240
val mmkvData = loadRNMmkvData() ?: return
12411241

1242-
extractRNMetadata(mmkvData)?.let { metadata ->
1243-
applyRNMetadata(metadata)
1244-
}
1245-
12461242
extractRNActivities(mmkvData)?.let { activities ->
12471243
applyOnchainMetadata(activities)
12481244
}
@@ -1257,6 +1253,10 @@ class MigrationService @Inject constructor(
12571253
applyBoostTransactions(boosts)
12581254
}
12591255
}
1256+
1257+
extractRNMetadata(mmkvData)?.let { metadata ->
1258+
applyRNMetadata(metadata)
1259+
}
12601260
}
12611261

12621262
pendingRemoteActivityData?.let { remoteActivities ->
@@ -1450,6 +1450,19 @@ class MigrationService @Inject constructor(
14501450
}
14511451
}
14521452

1453+
val backupValue = item.value.toULong()
1454+
if (backupValue > updated.value) {
1455+
updated = updated.copy(value = backupValue)
1456+
wasUpdated = true
1457+
}
1458+
1459+
item.fee?.let { backupFee ->
1460+
if (backupFee.toULong() > updated.fee) {
1461+
updated = updated.copy(fee = backupFee.toULong())
1462+
wasUpdated = true
1463+
}
1464+
}
1465+
14531466
item.address?.let { address ->
14541467
if (address.isNotEmpty() && updated.address != address) {
14551468
updated = updated.copy(address = address)
@@ -1460,30 +1473,80 @@ class MigrationService @Inject constructor(
14601473
return if (wasUpdated) updated else null
14611474
}
14621475

1463-
@Suppress("CyclomaticComplexMethod", "NestedBlockDepth")
1476+
@Suppress("CyclomaticComplexMethod", "NestedBlockDepth", "LongMethod")
14641477
private suspend fun applyOnchainMetadata(items: List<RNActivityItem>) {
14651478
val onchainItems = items.filter { it.activityType == "onchain" }
1479+
var updatedCount = 0
1480+
var createdCount = 0
14661481

14671482
onchainItems.forEach { item ->
14681483
val txId = item.txId ?: item.id.takeIf { it.isNotEmpty() } ?: return@forEach
14691484

14701485
val onchain = activityRepo.getOnchainActivityByTxId(txId)
1471-
if (onchain == null) {
1472-
Logger.warn("Onchain activity not found for txId: $txId", context = TAG)
1473-
return@forEach
1474-
}
1486+
if (onchain != null) {
1487+
updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1488+
activityRepo.updateActivity(updated.id, Activity.Onchain(updated))
1489+
.onSuccess { updatedCount++ }
1490+
.onFailure { e ->
1491+
Logger.error(
1492+
"Failed to update onchain activity metadata for $txId: $e",
1493+
e,
1494+
context = TAG
1495+
)
1496+
}
1497+
}
1498+
} else {
1499+
val timestampSecs = (item.timestamp / MS_PER_SEC).toULong()
1500+
val now = (System.currentTimeMillis() / MS_PER_SEC).toULong()
14751501

1476-
updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1477-
activityRepo.updateActivity(updated.id, Activity.Onchain(updated))
1502+
val activityTimestamp = if (timestampSecs > 0u) timestampSecs else now
1503+
1504+
val newOnchain = OnchainActivity(
1505+
id = item.id,
1506+
txType = if (item.txType == "sent") PaymentType.SENT else PaymentType.RECEIVED,
1507+
txId = txId,
1508+
value = item.value.toULong(),
1509+
fee = (item.fee ?: 0).toULong(),
1510+
feeRate = (item.feeRate ?: 1).toULong(),
1511+
address = item.address ?: "",
1512+
timestamp = activityTimestamp,
1513+
confirmed = item.confirmed ?: false,
1514+
isBoosted = item.isBoosted ?: false,
1515+
boostTxIds = emptyList(),
1516+
isTransfer = item.isTransfer ?: false,
1517+
confirmTimestamp = item.confirmTimestamp?.let { (it / MS_PER_SEC).toULong() },
1518+
channelId = item.channelId,
1519+
transferTxId = item.transferTxId,
1520+
doesExist = item.exists ?: true,
1521+
createdAt = activityTimestamp,
1522+
updatedAt = activityTimestamp,
1523+
seenAt = now,
1524+
)
1525+
1526+
activityRepo.upsertActivity(Activity.Onchain(newOnchain))
1527+
.onSuccess {
1528+
createdCount++
1529+
1530+
item.boostedParents?.takeIf { it.isNotEmpty() }?.let { parents ->
1531+
applyBoostedParents(parents, txId)
1532+
}
1533+
}
14781534
.onFailure { e ->
14791535
Logger.error(
1480-
"Failed to update onchain activity metadata for $txId: $e",
1536+
"Failed to create onchain activity for unsupported address $txId: $e",
14811537
e,
14821538
context = TAG
14831539
)
14841540
}
14851541
}
14861542
}
1543+
1544+
if (updatedCount > 0 || createdCount > 0) {
1545+
Logger.info(
1546+
"Applied metadata to $updatedCount onchain activities, created $createdCount for unsupported addresses",
1547+
context = TAG
1548+
)
1549+
}
14871550
}
14881551

14891552
@Suppress("LongMethod", "CyclomaticComplexMethod", "NestedBlockDepth")

0 commit comments

Comments
 (0)