Skip to content

Commit 055cba5

Browse files
committed
Merge branch 'master' into fix/all-warnings
2 parents f99f1ff + 528b028 commit 055cba5

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
@@ -564,6 +564,7 @@ class ActivityService(
564564
private fun buildUpdatedOnchainActivity(
565565
existingActivity: Activity.Onchain,
566566
confirmationData: ConfirmationData,
567+
ldkValue: ULong,
567568
channelId: String? = null,
568569
): OnchainActivity {
569570
var preservedIsTransfer = existingActivity.v1.isTransfer
@@ -576,13 +577,20 @@ class ActivityService(
576577

577578
val finalDoesExist = if (confirmationData.isConfirmed) true else existingActivity.v1.doesExist
578579

580+
val preservedValue = if (existingActivity.v1.value > ldkValue) {
581+
existingActivity.v1.value
582+
} else {
583+
ldkValue
584+
}
585+
579586
val updatedOnChain = existingActivity.v1.copy(
580587
confirmed = confirmationData.isConfirmed,
581588
confirmTimestamp = confirmationData.confirmedTimestamp,
582589
doesExist = finalDoesExist,
583590
updatedAt = confirmationData.timestamp,
584591
isTransfer = preservedIsTransfer,
585592
channelId = preservedChannelId,
593+
value = preservedValue,
586594
)
587595

588596
return updatedOnChain
@@ -631,10 +639,16 @@ class ActivityService(
631639
val timestamp = payment.latestUpdateTimestamp
632640
val confirmationData = getConfirmationStatus(kind, timestamp)
633641

634-
val existingActivity = getActivityById(payment.id)
642+
var existingActivity = getActivityById(payment.id)
643+
if (existingActivity == null) {
644+
getOnchainActivityByTxId(kind.txid)?.let {
645+
existingActivity = Activity.Onchain(it)
646+
}
647+
}
648+
635649
if (existingActivity != null &&
636650
existingActivity is Activity.Onchain &&
637-
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
651+
((existingActivity as Activity.Onchain).v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
638652
) {
639653
return
640654
}
@@ -655,10 +669,12 @@ class ActivityService(
655669

656670
val resolvedAddress = resolveAddressForInboundPayment(kind, existingActivity, payment, transactionDetails)
657671

672+
val ldkValue = payment.amountSats ?: 0u
658673
val onChain = if (existingActivity is Activity.Onchain) {
659674
buildUpdatedOnchainActivity(
660-
existingActivity = existingActivity,
675+
existingActivity = existingActivity as Activity.Onchain,
661676
confirmationData = confirmationData,
677+
ldkValue = ldkValue,
662678
channelId = resolvedChannelId,
663679
)
664680
} else {
@@ -676,8 +692,9 @@ class ActivityService(
676692
return
677693
}
678694

679-
if (existingActivity != null) {
680-
updateActivity(payment.id, Activity.Onchain(onChain))
695+
if (existingActivity != null && existingActivity is Activity.Onchain) {
696+
val existingOnchain = existingActivity.v1
697+
updateActivity(existingOnchain.id, Activity.Onchain(onChain))
681698
} else {
682699
upsertActivity(Activity.Onchain(onChain))
683700
}

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)