From 73a04630f45bdc95a32d1b5293adf15e2eb9657d Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sat, 14 Nov 2026 16:41:14 -0800 Subject: [PATCH] fix(dashpay): floor contact-account coverage on our own requests (MO-1131) Post-cutover, no contact payment address is handed out until every DIP-15 receiving account is in the SDK's backfill record, covered from no later than the account's floor. The app computed that floor as the earliest contact request in EITHER direction, while the SDK covers an account from the earliest request WE sent (receiving_scan_checkpoint): the contact pays to the xpub inside our request, so their request to us bounds nothing. When the contact's request came first, the app demanded coverage from below the height the SDK covers from, so the account never read covered. The check is wallet-wide, so Pay to every contact stayed disabled, silently and permanently. Reproduced on testnet 12.0.2: incoming 1591826, outgoing 1591833, covered from 1591833, required 1591826. - sentRequestFloors(): floors from our outgoing requests only - the uncovered reason names each account with its coveredFrom and the height it needs, so a field log tells a missing entry from a mismatch - regression test with the device's heights Verified on the device: with this change Pay enables for both contacts. Co-Authored-By: Claude Opus 5.5 --- .../sdk/ContactPaymentAddressProvider.kt | 61 +++++++++++++---- .../sdk/ContactPaymentAddressProviderTest.kt | 68 ++++++++++++++++++- .../platform/sdk/SdkWalletBinderTest.kt | 4 +- 3 files changed, 115 insertions(+), 18 deletions(-) diff --git a/wallet/src/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProvider.kt b/wallet/src/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProvider.kt index 1f567f231c..13c24eed13 100644 --- a/wallet/src/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProvider.kt +++ b/wallet/src/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProvider.kt @@ -35,6 +35,7 @@ import org.bitcoinj.core.Utils import org.bitcoinj.script.ScriptBuilder import org.bitcoinj.wallet.FriendChainAccess import org.dashfoundation.dashsdk.persistence.DashDatabase +import org.dashfoundation.dashsdk.persistence.entities.DashpayContactRequestEntity import org.dashj.platform.dpp.identifier.Identifier import org.slf4j.LoggerFactory import java.util.concurrent.ConcurrentHashMap @@ -327,16 +328,24 @@ internal fun decodeDashPayBackfillCoverSet(blob: ByteArray?, ownerId: ByteArray) * - every contact in [sentRequestContacts] (we sent a request to them, so * they may have paid us) has a receiving account in [receivalAccounts]; * - every receiving account is in [coverSet] (null: unreadable), covered - * from no later than its earliest contact request ([requestFloors]) or the - * wallet's birth checkpoint ([birthHeight] - 1), the SDK's own checkpoint - * rule; nothing it receives can be older. + * from no later than the earliest request WE sent the contact + * ([sentRequestFloors]) or the wallet's birth checkpoint ([birthHeight] - 1), + * whichever is later — the SDK's own checkpoint rule + * (`receiving_scan_checkpoint`). Nothing the account receives can be older: + * the contact pays to the xpub inside our request, so their request to us + * bounds nothing. Flooring on it too (MO-1131) demanded coverage from below + * the height the SDK covers from whenever the contact's request came first, + * and that never resolved — every contact payment of the wallet stayed + * refused. * Ids are contact identity ids in hex; receiving accounts are - * (contact, account index). + * (contact, account index). The reason names each uncovered account (id + * prefix, the height it is covered from and the height it must be covered + * from), so a field log can tell a missing entry from a floor mismatch. */ internal fun contactHistoryUncoveredBecause( discoveryCompleted: Boolean, sentRequestContacts: Set, - requestFloors: Map, + sentRequestFloors: Map, birthHeight: Long, receivalAccounts: List>, coverSet: Map, Long>? @@ -348,17 +357,42 @@ internal fun contactHistoryUncoveredBecause( } coverSet ?: return "the SDK's backfill record cannot be read" val birthCheckpoint = (birthHeight - 1).coerceAtLeast(0L) - val uncovered = receivalAccounts.count { account -> - val coveredFrom = coverSet[account] ?: return@count true - coveredFrom > maxOf(birthCheckpoint, requestFloors[account.first] ?: birthCheckpoint) + val uncovered = receivalAccounts.mapNotNull { account -> + val required = maxOf(birthCheckpoint, sentRequestFloors[account.first] ?: birthCheckpoint) + val coveredFrom = coverSet[account] + when { + coveredFrom == null -> "${account.first.take(8)}…/${account.second} not in the record (needs $required)" + coveredFrom > required -> + "${account.first.take(8)}…/${account.second} covered from $coveredFrom, needs $required" + else -> null + } } - return if (uncovered > 0) { - "$uncovered receiving account(s) not yet covered by the SDK's backfill record" + return if (uncovered.isNotEmpty()) { + "${uncovered.size} receiving account(s) not yet covered by the SDK's backfill record: " + + uncovered.take(MAX_UNCOVERED_ACCOUNTS_NAMED).joinToString("; ") + + if (uncovered.size > MAX_UNCOVERED_ACCOUNTS_NAMED) "; …" else "" } else { null } } +/** How many uncovered accounts [contactHistoryUncoveredBecause] names in its reason. */ +private const val MAX_UNCOVERED_ACCOUNTS_NAMED = 5 + +/** + * Contact identity id (hex) → the core height of the earliest request WE sent + * that contact, from the SDK's stored contact requests: the floor a contact's + * receiving account has to be covered from ([contactHistoryUncoveredBecause]), + * and its keys are the contacts we sent a request to. Requests the contact + * sent us are ignored — they bound nothing, since the contact pays to the + * xpub inside our request (MO-1131: counting them demanded coverage from + * below the SDK's checkpoint whenever the contact's request came first). + */ +internal fun sentRequestFloors(requests: List): Map = + requests.filter { it.isOutgoing } + .groupBy { Utils.HEX.encode(it.contactIdentityId) } + .mapValues { (_, ours) -> ours.minOf { it.coreHeightCreatedAt.toLong() } } + /** [ContactPaymentChainView] over the app's SDK and its L1 sync progress. */ internal class SdkContactPaymentChainView( private val sdkService: DashSdkService, @@ -399,12 +433,11 @@ internal class SdkContactPaymentChainView( .observeByWalletAndType(id, ACCOUNT_TYPE_TAG_DASHPAY_RECEIVING_FUNDS) .first() .filter { it.userIdentityId.contentEquals(owner) } + val floors = sentRequestFloors(requests) contactHistoryUncoveredBecause( discoveryCompleted = discovered, - sentRequestContacts = requests.filter { it.isOutgoing } - .mapTo(HashSet()) { Utils.HEX.encode(it.contactIdentityId) }, - requestFloors = requests.groupBy { Utils.HEX.encode(it.contactIdentityId) } - .mapValues { (_, pair) -> pair.minOf { it.coreHeightCreatedAt.toLong() } }, + sentRequestContacts = floors.keys, + sentRequestFloors = floors, birthHeight = wallet.birthHeight.toLong(), receivalAccounts = receival.mapNotNull { account -> account.friendIdentityId?.let { Utils.HEX.encode(it) to account.accountIndex } diff --git a/wallet/test/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProviderTest.kt b/wallet/test/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProviderTest.kt index 84aa56682e..d063ccd6a0 100644 --- a/wallet/test/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProviderTest.kt +++ b/wallet/test/de/schildbach/wallet/service/platform/sdk/ContactPaymentAddressProviderTest.kt @@ -26,6 +26,7 @@ import org.bitcoinj.core.Address import org.bitcoinj.core.ECKey import org.bitcoinj.params.TestNet3Params import org.bitcoinj.wallet.FriendChainAccess +import org.dashfoundation.dashsdk.persistence.entities.DashpayContactRequestEntity import org.dashj.platform.dpp.identifier.Identifier import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -1882,7 +1883,7 @@ class ContactPaymentAddressProviderTest { ) = contactHistoryUncoveredBecause( discoveryCompleted = discovered, sentRequestContacts = sent, - requestFloors = mapOf(a to 500L, b to 700L), + sentRequestFloors = mapOf(a to 500L, b to 700L), birthHeight = 100, receivalAccounts = accounts, coverSet = coverSet @@ -1919,7 +1920,7 @@ class ContactPaymentAddressProviderTest { contactHistoryUncoveredBecause( discoveryCompleted = true, sentRequestContacts = setOf(a), - requestFloors = mapOf(a to 50L), + sentRequestFloors = mapOf(a to 50L), birthHeight = 100, receivalAccounts = listOf(a to 0), coverSet = mapOf((a to 0) to 99L) @@ -1927,6 +1928,69 @@ class ContactPaymentAddressProviderTest { ) } + /** + * MO-1131, with the heights read from a testnet device's SDK store. Contact + * A sent us a request (1591826) before we sent ours (1591833); the SDK + * covers A's receiving account from OUR request, 1591833. Contact B: we + * sent first, both requests in block 1591837, covered from 1591837. The + * gate floored A on the incoming request too, so A never read covered — + * and since the gate is wallet-wide, payments to B were refused as well. + */ + @Test + fun contactHistoryCovered_whenTheContactsRequestCameFirst() { + val ourId = ByteArray(32) { 1 } + val a = hex(contactA) + val b = hex(contactB) + fun request(contact: ByteArray, outgoing: Boolean, height: Int) = DashpayContactRequestEntity( + networkRaw = 1, + ownerIdentityId = ourId, + contactIdentityId = contact, + isOutgoing = outgoing, + senderKeyIndex = 0, + recipientKeyIndex = 0, + accountReference = 0, + encryptedPublicKey = ByteArray(96), + coreHeightCreatedAt = height, + createdAtMillis = 1 + ) + val floors = sentRequestFloors( + listOf( + request(contactA, outgoing = false, height = 1_591_826), + request(contactA, outgoing = true, height = 1_591_833), + request(contactB, outgoing = true, height = 1_591_837), + request(contactB, outgoing = false, height = 1_591_837) + ) + ) + assertEquals( + "floored on our own requests only", + mapOf(a to 1_591_833L, b to 1_591_837L), + floors + ) + fun uncovered(coverSet: Map, Long>) = contactHistoryUncoveredBecause( + discoveryCompleted = true, + sentRequestContacts = floors.keys, + sentRequestFloors = floors, + birthHeight = 1_401_408, + receivalAccounts = listOf(a to 0, b to 0), + coverSet = coverSet + ) + assertNull( + "both accounts covered from the SDK's own checkpoint", + uncovered(mapOf((a to 0) to 1_591_833L, (b to 0) to 1_591_837L)) + ) + val late = uncovered(mapOf((a to 0) to 1_591_834L, (b to 0) to 1_591_837L)) + assertTrue( + "coverage starting above our request is still refused, and named: $late", + late!!.contains("1 receiving account(s) not yet covered") && + late.contains("${a.take(8)}…/0 covered from 1591834, needs 1591833") + ) + val missing = uncovered(mapOf((b to 0) to 1_591_837L)) + assertTrue( + "an account missing from the record is named: $missing", + missing!!.contains("${a.take(8)}…/0 not in the record (needs 1591833)") + ) + } + private fun coverEntry(owner: ByteArray, contact: ByteArray, index: Int?, coveredFrom: Int): ByteArray { fun le(v: Int) = ByteArray(4) { i -> (v ushr (8 * i)).toByte() } return owner + contact + (index?.let(::le) ?: ByteArray(0)) + le(coveredFrom) diff --git a/wallet/test/de/schildbach/wallet/service/platform/sdk/SdkWalletBinderTest.kt b/wallet/test/de/schildbach/wallet/service/platform/sdk/SdkWalletBinderTest.kt index a59d4e93f2..51fbf371d8 100644 --- a/wallet/test/de/schildbach/wallet/service/platform/sdk/SdkWalletBinderTest.kt +++ b/wallet/test/de/schildbach/wallet/service/platform/sdk/SdkWalletBinderTest.kt @@ -1682,7 +1682,7 @@ class SdkWalletBinderTest { fun uncovered(walletId: String) = de.schildbach.wallet.service.platform.sdk.contactHistoryUncoveredBecause( discoveryCompleted = binder.contactDiscoveryCompleted(walletId), sentRequestContacts = setOf(contactHex), - requestFloors = mapOf(contactHex to 1_500L), + sentRequestFloors = mapOf(contactHex to 1_500L), birthHeight = 1_000L, receivalAccounts = listOf(contactHex to 0), coverSet = mapOf((contactHex to 0) to 999L) @@ -1903,7 +1903,7 @@ class SdkWalletBinderTest { fun uncovered(walletId: String) = de.schildbach.wallet.service.platform.sdk.contactHistoryUncoveredBecause( discoveryCompleted = binder.contactDiscoveryCompleted(walletId), sentRequestContacts = sdk.storedContactRequests?.outgoing.orEmpty(), - requestFloors = mapOf(contactA to 1_500L, contactB to 1_500L), + sentRequestFloors = mapOf(contactA to 1_500L, contactB to 1_500L), birthHeight = 1_000L, receivalAccounts = receivalAccounts, coverSet = coverSet