Skip to content

platform-wallet: TXO reconcile follow-ups from #5796 and #5767 review #5977

Open

Description

@HashEngineering

Non-blocking points from the reviews of #5796 (items 1–3, llbartekll) and #5767 (item 4, CodeRabbit and PastaClaw; items 5–6, the Swift-owner review) that were acknowledged but not changed. None blocks a release on its own; this issue keeps them from being lost.

1. Engine read lock held across a page's store write

hold_engine (rs-platform-wallet/src/manager/txo_reconcile.rs) keeps the wallet manager's read guard until the host saves the page and calls releaseVerdicts(). While it is held, a block or ChainLock waits, and with tokio's fair RwLock so does every later reader.

Measured on the Kotlin host (Robolectric on a Mac, on-disk Room, median of 10):

Page 64 rows 128 rows 512 rows
Heal (insert missing coins) 7 ms 13 ms 53 ms
Classify (flip to spent) 1.2 ms 2.7 ms 9 ms

To do:

  • Time a full 512-row heal page on a real Android device and on iOS (SwiftData). Neither has been measured.
  • Move the per-row log lines (commitStoreStep in Kotlin, the Swift equivalent) out of the guard window, e.g. one summary line after the release.
  • If the device numbers are long: have the hosts ask for a smaller page (the limit is a parameter), or lower WALLET_UTXO_PAGE_DEFAULT (512, manager/accessors.rs).

2. finalized_spends grows for the life of the process

PlatformWallet::finalized_spends (rs-platform-wallet/src/wallet/platform_wallet.rs) gains one outpoint per ChainLocked spend of the wallet's own coins and is never pruned. It is also in memory only, so a store row left unspent by a spend finalized in an earlier session reads Unknown after a restart.

To do:

  • Add the growth to the doc comment (the restart limit is already there).
  • Decide whether it needs a bound. Pruning needs a signal that the store has recorded the spend.

3. Swift classify cursor is a store offset

PlatformWalletManagerTxoReconcile.swift resumes the classify pass by offset (coreTxoStoreCursor(offset:)). If the store changed between runs, a resumed offset can skip or repeat rows; a skipped row waits one cadence (30 min). Kotlin already pages by outpoint.

To do:

  • Switch Swift to a keyset cursor (e.g. creation time plus outpoint). The cursor is opaque to Rust (up to TXO_RECONCILE_STORE_CURSOR_MAX = 64 bytes), so this is Swift-only.

4. Kotlin: no index for the paged unspent scan

TxoDao.pageUnspentByWallet (kotlin-sdk/.../persistence/dao/TxoDao.kt) runs

SELECT * FROM txos WHERE walletId = :walletId AND isSpent = 0 AND outpoint > :after ORDER BY outpoint LIMIT :limit

but TxoEntity indexes only walletId (plus txid, spendingTxid, accountId, coreAddressId). Spent rows are never deleted, so each page reads every TXO the wallet has ever had and sorts the unspent ones past the cursor. The cost grows with history, not with the page size. The DAO's KDoc already says so.

To do:

  • Add Index(value = ["walletId", "isSpent", "outpoint"]) to TxoEntity, so a page seeks straight to the cursor and reads only limit rows in order.
  • Bump DashDatabase (version 16 today) with a migration that creates the index (CREATE INDEX IF NOT EXISTS index_txos_walletId_isSpent_outpoint ...), and a Room migration test.
  • Optional: check with EXPLAIN QUERY PLAN that SQLite uses the new index, and time a page on a wallet with a long spent history.

Kept out of #5767 because it is a schema change with a migration, and the reconcile pass runs at most once per 30 min cadence.

5. Move the settled-spender rule into platform-wallet

Whether a coin's spender is "settled" (its stored transaction is IS-locked or later, or it funds an asset lock at InstantSendLocked or later) is a wallet decision, but each host makes it in its persistence handler. Kotlin has one helper, isSettledSpender, used by hasSettledSpender (the re-delivery clear) and releaseVetoed (the sweep-release veto). The link guard (keepSettledSpenderLink) reads only the spender row's context, as Swift's settledSpenderLinkIsKept does; ranking spenders by asset-lock status is left for the Rust rule. Swift checks settledness in upsertUtxo and releaseIsVetoed. At #5767's head, Swift's releaseIsVetoed does not yet include the asset-lock fallback.

To do:

  • Decide the rule once in rs-platform-wallet, for example as a verdict or flag on the changeset, so both hosts persist it instead of deciding it.
  • Until then, add the asset-lock fallback to Swift's releaseIsVetoed, in line with Kotlin (Swift SDK owner).

6. Non-blocking recommendations from the #5767 review

From the approving review at 2efb3143c5, not addressed in #5767:

  • One fail-closed behaviour for the asset-lock lookup. When the lookup fails, Kotlin fails the whole round, while Swift logs, keeps the row spent and commits the rest of the round. Both are safe for funds; make them agree.
  • Tests: the asset-lock lookup-failure path on both hosts, and Swift coverage of lock statuses 3–5. Kotlin covers statuses 1, 2 and 4 in the release veto, with asymmetric txids, since fix(kotlin-sdk): port #4638 to Android on the wallet's TXO reconcile policy #5767.
  • A standalone sweep doesn't move the stale fence. The no-round sweep branch in PlatformWalletPersistenceHandler.kt commits without bumping committedRoundGeneration.
  • Healed rows are always written isConfirmed = true, while the EngineUtxoRow doc says the engine's flags are stored as given.
  • Finish outcome vs Swift. ran = !isCancelled() sends NOT_RUN for a run that applied pages before another wallet was removed, and Kotlin never sends DEFERRED. Both push the next run out by the full 30-minute cadence, where Swift resumes on the next poll.
  • Parity manifest: add the Android TXO reconcile capability to docs/sdk/sdk-parity-manifest.json.

Related

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions