From 3e400576e6bf085a7d01b02bd77a4b611ee6a285 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 13 Oct 2026 16:56:27 +0000 Subject: [PATCH 01/21] fix(swift-sdk): reconcile SwiftData transaction accounting per wallet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A stored Core transaction row kept the amount and direction of its last writer, computed without the wallet's own spent inputs: a self-spend with change showed as a receipt, and an asset lock as a zero transfer. The SwiftData host now recomputes amount and direction from its own TXO rows at load and at the end of each persistence round, keeps contact-only outputs out of the sum, and answers per wallet for a transaction that several local wallets share. Schema V4 records an amount that cannot be determined, and the example app's transaction views show wallet-relative amounts and direction. These changes were developed on the branch of #5150 and are carried over unchanged. 🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PSi8cX229kW2fmLxKW6ir4 --- packages/swift-sdk/SCHEMA_RELEASES.md | 36 +- .../KeyWallet/TransactionDecoder.swift | 3 + .../Persistence/DashModelContainer.swift | 38 +- .../Models/PersistentTransaction.swift | 164 ++++- .../PlatformWalletPersistenceHandler.swift | 162 +++- .../Core/Views/TransactionDetailView.swift | 55 +- .../Views/TransactionDirectionStyle.swift | 25 + .../Core/Views/TransactionListView.swift | 59 +- .../Views/StorageModelListViews.swift | 8 +- .../AssetLockSpendVisibilityTests.swift | 9 +- .../DashLegacySchemaMigrationTests.swift | 19 +- .../DashModelMigrationTests.swift | 16 +- .../DashReleasedSchemaTests.swift | 31 + .../PersistentTransactionDisplayTests.swift | 7 +- .../TransactionAccountingTests.swift | 695 ++++++++++++++++++ 15 files changed, 1184 insertions(+), 143 deletions(-) create mode 100644 packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift create mode 100644 packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index 2183bfbe76d..2e568c859da 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -5,7 +5,8 @@ distribution. TestFlight uploads capture provenance and a synthetic SQLite fixture, but do not by themselves register a released schema. The accepted frozen V1 remains unchanged. Historical V2 is now reconstructed from `52e8d4ec68f0c772313fa1bbef223fb1eabbf1cc`; all 35 entity hashes and the model checksum match the observed App Store 9.0.2 database. Active models are -V3. Other intermediate development shapes remain unsupported. +V4. The released V3 graph is preserved by `DashSchemaSnapshotV3`. Other +intermediate development shapes remain unsupported. The old V2 fixture was generated from September 8 sources containing 13 properties added on August 28, after the August 27 App Store release. The @@ -15,25 +16,28 @@ separate from archive-captured releases. This identifies a matching model source, not the confirmed build commit of Apple's binary. V2 is reserved: automated release capture must not register another shape under that number. -The main migration plan is historical V2 → V3. Accepted V1 has a separate -V1 → V3 plan: V1 already contains the 13 properties missing from historical -V2, so a V1 → V2 → V3 chain could discard values. Routing uses model metadata +The main migration plan is historical V2 → V3 → V4. Accepted V1 has a separate +V1 → V4 plan: V1 already contains the 13 properties missing from historical +V2, so a chain through V2 could discard values. Routing uses model metadata and runs after recovery; a version label alone never selects an unknown beta schema. The former live V2 is accepted only when its complete graph matches -current V3 exactly, entity hashes and checksum alike. That alias therefore -lasts only until the next live-graph change: after it, every store still -labelled `2.0.0` with the former live shape becomes `unsupported-v2` and fails -closed. Check internal devices still carrying that label before the next -shape change and migrate or deliberately reset them then, rather than -discovering them afterwards as failed opens. +frozen V3 exactly, entity hashes and checksum alike. The alias remains supported +when the live graph changes; other stores labelled `2.0.0` fail closed. + +V4 adds the optional `PersistentTransaction.netAmountUnavailable` marker through +a lightweight migration. Existing rows receive `nil`, preserving their stored +accounting. When deleting one participant of a shared transaction, the remaining +wallet's amount and direction are saved before the ownership links disappear. +If its amount was unresolved, the marker keeps it unavailable across restart; +an authoritative wallet record or complete accounting reconciliation clears it. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, -`accepted-v1-to-v3`, `historical-v2-to-v3`, `previous-live-v2-current-shape`, -`unsupported-v2`, `labelled-current-v3`, `ordinary-current-plan` or, from the +`accepted-v1-to-v4`, `historical-v2-to-v4`, `previous-live-v2-v3-shape`, +`published-v3-to-v4`, `unsupported-v2`, `labelled-current-v4`, `ordinary-current-plan` or, from the bridge, `legacy-v1-bridge-to-v3`. Resolving a frozen schema's identity builds a temporary store, so it is memoized per process and consulted only where a -label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0` label takes the +label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0` or `4.0.0` label takes the default plan without any probe, and a probe failure on the undecidable labels refuses the open with the probe's own error while leaving the store untouched. @@ -137,9 +141,9 @@ The run failed before upload, so this provenance establishes a tested source layout rather than proof of publication. Regression tests exercise the public factory, data/default preservation, writes, reopen, and failure recovery. -Keep the bridge for installations that skip the V3 app release. When advancing -to V4, bind `DashSchemaV3` to its released snapshot and retain the legacy-to-V3 -step before the normal V3-to-current plan. The bridge must never automatically +Keep the bridge for installations that skip the V3 app release. `DashSchemaV3` +stays bound to its released snapshot, retaining the legacy-to-V3 step before +the normal V3-to-current plan. The bridge must never automatically follow the latest live model graph. The release observer's one-time `bootstrap` only records its observation baseline; it neither runs this migration nor proves V1's App Store provenance. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift b/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift index 380deeeeff7..7224d3dc282 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift @@ -39,6 +39,9 @@ public struct DecodedTransaction: Sendable, Equatable { public let valueDuffs: UInt64 /// Raw scriptPubKey bytes. public let scriptPubkey: Data + + /// `true` for an `OP_RETURN` (0x6a) data-carrier / burn output. + var isOpReturn: Bool { scriptPubkey.first == 0x6a } } /// Transaction id in consensus (internal) byte order — reverse for diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index ea878415c36..22fbdb63ffc 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -100,7 +100,7 @@ public enum DashModelContainer { /// Create the schema for all Dash Platform models public static var schema: Schema { - Schema(versionedSchema: DashSchemaV3.self) + Schema(versionedSchema: DashSchemaV4.self) } /// Create a persistent model container for storing data. @@ -172,7 +172,7 @@ public enum DashModelContainer { bridgeLegacyStore: Bool = true ) throws -> ModelContainer { SDKLogger.event("store_open_started", category: .persistence, - fields: ["target_version": .publicText("3.0.0")]) + fields: ["target_version": .publicText("4.0.0")]) do { let container: ModelContainer if bridgeLegacyStore { @@ -185,7 +185,7 @@ public enum DashModelContainer { configurations: [configuration]) } SDKLogger.event("store_open_succeeded", category: .persistence, - fields: ["target_version": .publicText("3.0.0")]) + fields: ["target_version": .publicText("4.0.0")]) return container } catch { logMigrationFailure(error) @@ -201,7 +201,7 @@ public enum DashModelContainer { let domain = systemDomains.contains(nsError.domain) ? nsError.domain : String(reflecting: type(of: error)) SDKLogger.event("store_open_failed", category: .persistence, severity: .error, fields: ["error_domain": .publicText(domain), "error_code": .integer(Int64(nsError.code)), - "target_version": .publicText("3.0.0")]) + "target_version": .publicText("4.0.0")]) } /// Select by the complete stored model identity, after journal recovery. @@ -218,7 +218,7 @@ public enum DashModelContainer { guard ObjectIdentifier(defaultPlan) == ObjectIdentifier(DashMigrationPlan.self) else { return defaultPlan } guard FileManager.default.fileExists(atPath: url.path) else { SDKLogger.event("store_migration_route", category: .persistence, fields: [ - "route": .publicText("new-store"), "target_version": .publicText("3.0.0")]) + "route": .publicText("new-store"), "target_version": .publicText("4.0.0")]) return defaultPlan } let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(type: .sqlite, at: url) @@ -236,7 +236,7 @@ public enum DashModelContainer { func logRoute(_ route: String) { SDKLogger.event("store_migration_route", category: .persistence, fields: [ "source_version": .publicText(safeVersions), "source_checksum": .publicText(safeChecksum), - "route": .publicText(route), "target_version": .publicText("3.0.0")]) + "route": .publicText(route), "target_version": .publicText("4.0.0")]) } func matches(_ type: any VersionedSchema.Type) throws -> Bool { let expected = try identity(type) @@ -247,7 +247,7 @@ public enum DashModelContainer { // probe stays fatal here: the open fails with the probe's own error // instead of an inapplicable plan, and the store is untouched. if versions == ["1.0.0"], try matches(DashSchemaV1.self) { - logRoute("accepted-v1-to-v3") + logRoute("accepted-v1-to-v4") return DashAcceptedV1MigrationPlan.self } if versions == ["2.0.0"] { @@ -261,12 +261,14 @@ public enum DashModelContainer { throw DashLegacyStoreSQLite.Failure.unsupported( "The database identifies itself as schema 2.0.0 but its model does not match the supported historical or current schema. The original database has not been replaced. Contact support; do not delete the app.") } - logRoute(historical ? "historical-v2-to-v3" : "previous-live-v2-current-shape") + logRoute(historical ? "historical-v2-to-v4" : "previous-live-v2-v3-shape") } else if versions == ["3.0.0"] { + logRoute("published-v3-to-v4") + } else if versions == ["4.0.0"] { // Same plan either way. Opening a current store must neither wait // on nor fail with a schema probe that could only refine this line; // `source_checksum` above already identifies the exact graph. - logRoute("labelled-current-v3") + logRoute("labelled-current-v4") } else { logRoute("ordinary-current-plan") } @@ -287,12 +289,13 @@ public enum DashModelContainer { /// SwiftData migration plan for Dash Platform model updates public enum DashMigrationPlan: SchemaMigrationPlan { public static var schemas: [any VersionedSchema.Type] { - [DashSchemaV2.self, DashSchemaV3.self] + [DashSchemaV2.self, DashSchemaV3.self, DashSchemaV4.self] } public static var stages: [MigrationStage] { [ - .lightweight(fromVersion: DashSchemaV2.self, toVersion: DashSchemaV3.self) + .lightweight(fromVersion: DashSchemaV2.self, toVersion: DashSchemaV3.self), + .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self) ] } } @@ -300,9 +303,9 @@ public enum DashMigrationPlan: SchemaMigrationPlan { /// Separate compatibility route: V1 has fields missing from historical V2. /// Never insert V2 between this baseline and the current schema. enum DashAcceptedV1MigrationPlan: SchemaMigrationPlan { - static var schemas: [any VersionedSchema.Type] { [DashSchemaV1.self, DashSchemaV3.self] } + static var schemas: [any VersionedSchema.Type] { [DashSchemaV1.self, DashSchemaV4.self] } static var stages: [MigrationStage] { - [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV3.self)] + [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV4.self)] } } @@ -372,9 +375,14 @@ public enum DashSchemaV2: VersionedSchema { } } -/// Current working schema. A later shape change must preserve this graph as -/// the fixed legacy-bridge target before introducing another live version. +/// Frozen released graph and fixed target of the legacy-store bridge. public enum DashSchemaV3: VersionedSchema { public static var versionIdentifier: Schema.Version { Schema.Version(3, 0, 0) } + public static var models: [any PersistentModel.Type] { DashSchemaSnapshotV3.models } +} + +/// Live schema adds an optional accounting-availability marker; existing rows keep nil. +public enum DashSchemaV4: VersionedSchema { + public static var versionIdentifier: Schema.Version { Schema.Version(4, 0, 0) } public static var models: [any PersistentModel.Type] { DashModelContainer.modelTypes } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index f0ecd0fce34..a27fb766d11 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -84,8 +84,11 @@ public final class PersistentTransaction { /// replaces it with the real discriminant on touch. Accessors /// treat the sentinel as unknown (no branch fires). public var transactionTypeKind: UInt8 = 0xFF - /// Net amount in duffs (signed: positive=received, negative=sent). + /// Net Core amount in duffs across locally owned TXOs (positive=received, negative=sent). public var netAmount: Int64 + /// Set when removing a wallet left no authoritative amount for the survivor. + /// `nil` preserves the accounting of rows migrated from V3. + public var netAmountUnavailable: Bool? = nil /// Fee in duffs (nil if unknown). public var fee: UInt64? /// User-assigned label. @@ -228,12 +231,150 @@ public final class PersistentTransaction { } } + /// Core value movement for one wallet; `nil` when it cannot be derived yet. + /// + /// The stored scalar is the last recording wallet's (Rust `net_amount`, + /// or the reconciliation over its own TXOs), so it answers only when + /// `walletId` is the sole participant. Pending inputs do not veto that + /// answer: `upsertTransaction` writes one for every input whose prevout + /// has no local TXO — every foreign input of an incoming payment — and + /// never prunes them, so they cannot tell a late input of ours apart. + public func netAmount(for walletId: Data) -> Int64? { + func owned(_ rows: [PersistentTxo]) -> [PersistentTxo] { + var seen = Set() + return rows.filter { + PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) + && PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId + && seen.insert($0.outpoint).inserted + } + } + let hasUnownedTxos = (inputs + outputs).contains { !PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) } + if participatingWalletIds == [walletId], !hasUnownedTxos { + return netAmountUnavailable == true ? nil : netAmount + } + // Computed from TXOs alone: a pending input this wallet recorded may be + // one of its own still-unlinked coins, so the sum is only provisional. + // TODO(wallet-scoped-accounting-from-rust): a foreign payment to two + // local wallets leaves an unresolvable pending input per wallet, so both + // show "Amount unavailable". Store Rust's per-wallet net amount instead + // (tracked in #5226). + guard !pendingInputs.contains(where: { $0.walletId == walletId }) else { return nil } + let walletInputs = owned(inputs) + let walletOutputs = owned(outputs) + guard !walletInputs.isEmpty || !walletOutputs.isEmpty else { return nil } + return Self.reconciledAccounting( + inputs: walletInputs, ownedOutputAmounts: walletOutputs.map(\.amount), + allOutputsOwned: false, previousDirection: direction, isAssetLock: isAssetLock + )?.netAmount + } + + /// Direction relative to one wallet for transactions shared by multiple local wallets. + public func direction(for walletId: Data) -> UInt32 { + guard participatingWalletIds.count > 1, direction != CoreDirectionCode.coinJoin, + typedKind != .coinJoin else { return direction } + let spendsOurs = inputs.contains { + PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) + && PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId + } + if isAssetLock { + let ownedOutputs = outputs.filter(PlatformWalletPersistenceHandler.isWalletOwnedTxo) + if spendsOurs { + return ownedOutputs.contains { + PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) != walletId + } ? CoreDirectionCode.outgoing : direction + } + return ownedOutputs.contains { + PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId + } ? CoreDirectionCode.incoming : direction + } + return spendsOurs ? CoreDirectionCode.outgoing : CoreDirectionCode.incoming + } + + /// Retain the sole surviving wallet's accounting before ownership links disappear. + func preserveAccounting(removingWallet walletId: Data) { + let participants = participatingWalletIds + guard participants.count == 2, participants.contains(walletId), + let survivor = participants.first(where: { $0 != walletId }) else { return } + let amount = netAmount(for: survivor) + let survivorDirection = direction(for: survivor) + if let amount { netAmount = amount } + netAmountUnavailable = amount == nil + direction = survivorDirection + } + + /// Format the wallet's Core value movement in DASH. + public func formattedAmount(for walletId: Data) -> String { + guard let amount = netAmount(for: walletId) else { return "Amount unavailable" } + return Self.format(duffs: amount) + } + + /// Net amount for `walletId`, or the stored scalar when no wallet scope is given. + public func displayNetAmount(for walletId: Data?) -> Int64? { + walletId.map { netAmount(for: $0) } ?? (netAmountUnavailable == true ? nil : netAmount) + } + + /// `CoreDirectionCode` for `walletId`, or the stored direction when no wallet scope is given. + public func displayDirectionCode(for walletId: Data?) -> UInt32 { + walletId.map { direction(for: $0) } ?? direction + } + + /// Formatted amount for `walletId`, or the stored scalar's when no wallet scope is given. + public func displayFormattedAmount(for walletId: Data?) -> String { + walletId.map { formattedAmount(for: $0) } ?? formattedAmount + } + + /// Signed DASH text for a duff amount; `magnitude` cannot trap on `Int64.min`. + static func format(duffs: Int64) -> String { + String(format: "%@%.8f DASH", duffs >= 0 ? "+" : "-", Double(duffs.magnitude) / 100_000_000) + } + + /// Local wallets owning one of this transaction's TXOs or having recorded it + /// (`involvedAccounts`), whose last writer's accounting is the stored scalar. + private var participatingWalletIds: Set { + let owning = (inputs + outputs).filter(PlatformWalletPersistenceHandler.isWalletOwnedTxo) + .compactMap { PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) } + let recording = involvedAccounts.compactMap { account -> Data? in + let wallet: PersistentWallet? = account.wallet + return wallet?.walletId + } + return Set(owning + recording) + } + + static func reconciledAccounting( + inputs: [PersistentTxo], ownedOutputAmounts: [UInt64], + allOutputsOwned: Bool, previousDirection: UInt32, isAssetLock: Bool + ) -> (netAmount: Int64, direction: UInt32)? { + func total(_ amounts: [UInt64]) -> Int64? { + var sum: Int64 = 0 + for amount in amounts { + guard let value = Int64(exactly: amount) else { return nil } + let addition = sum.addingReportingOverflow(value) + guard !addition.overflow else { return nil } + sum = addition.partialValue + } + return sum + } + guard let received = total(ownedOutputAmounts), let spent = total(inputs.map(\.amount)) else { + return nil + } + // Same rule as Rust (`platform_wallet::changeset::wallet_direction`): + // internal only when nothing leaves the wallet and something stays in + // it, or an asset lock burns into Platform. Both sides test one table. + let direction: UInt32 + if previousDirection == CoreDirectionCode.coinJoin { direction = CoreDirectionCode.coinJoin } + else if inputs.isEmpty { direction = CoreDirectionCode.incoming } + else if allOutputsOwned && (!ownedOutputAmounts.isEmpty || isAssetLock) { + direction = CoreDirectionCode.internalTransfer + } else { direction = CoreDirectionCode.outgoing } + return (received - spent, direction) + } + public var directionName: String { switch direction { - case 0: return "Incoming" - case 1: return "Outgoing" - case 2: return "Internal" - case 3: return "CoinJoin" + case CoreDirectionCode.incoming: return "Incoming" + case CoreDirectionCode.outgoing: return "Outgoing" + case CoreDirectionCode.internalTransfer: return "Internal" + case CoreDirectionCode.coinJoin: return "CoinJoin" default: return "Unknown" } } @@ -337,12 +478,19 @@ public final class PersistentTransaction { } public var formattedAmount: String { - let dash = Double(abs(netAmount)) / 100_000_000.0 - let sign = netAmount >= 0 ? "+" : "-" - return String(format: "%@%.8f DASH", sign, dash) + netAmountUnavailable == true ? "Amount unavailable" : Self.format(duffs: netAmount) } } +/// Wire values of `PersistentTransaction.direction`, matching `directionName` +/// and the FFI's `TransactionDirection` discriminants. +public enum CoreDirectionCode { + public static let incoming: UInt32 = 0 + public static let outgoing: UInt32 = 1 + public static let internalTransfer: UInt32 = 2 + public static let coinJoin: UInt32 = 3 +} + /// Typed mirror of Rust's /// `key_wallet::transaction_checking::transaction_router::TransactionType`, /// pinned to the `u8` discriminants emitted by diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 0693c9e7366..f48f1614e63 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -105,6 +105,20 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { return wallet?.walletId } + /// Contact watch-only TXOs do not belong to the wallet tracking their addresses. + static func isWalletOwnedTxo(_ txo: PersistentTxo) -> Bool { + resolvedWalletId(of: txo) != nil + && txo.account?.accountType != dashpayExternalAccountTypeTag + && txo.coreAddress?.account?.accountType != dashpayExternalAccountTypeTag + } + + /// Network of the account's wallet, read through the same Optional cast + /// as `resolvedWalletId(of:)`. + static func walletNetwork(of account: PersistentAccount?) -> Network? { + let wallet: PersistentWallet? = account?.wallet + return wallet?.network + } + static func walletOwnsTransaction( walletId: Data, transaction: PersistentTransaction @@ -246,6 +260,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { var coreAddressesByAddress: [String: PersistentCoreAddress] = [:] } private var roundIndex: ChangesetRoundIndex? + private var accountingDirty: [Data: PersistentTransaction] = [:] /// Number of persistence rounds committed by this handler, read and /// compared on `serialQueue`. The store reconcile classifies rows off /// this queue and applies the verdicts on it; a round committed in @@ -411,6 +426,8 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { error: error ) backgroundContext.rollback() + // Rows staged by the failed save are gone; never reconcile them. + accountingDirty.removeAll() } } @@ -2538,7 +2555,11 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { record.hasBlockPosition = tx.has_block_position let blockHashBytes = hashData(tx.block_hash) record.blockHash = blockHashBytes.allSatisfy { $0 == 0 } ? nil : blockHashBytes - record.direction = tx.direction + // Empty asset-lock recovery records cannot replace a funded debit or an unavailable amount. + let preserveLockAccounting = tx.transaction_type_kind == TransactionTypeKind.assetLock.rawValue + && tx.net_amount == 0 && !tx.has_fee + && (record.netAmount < 0 || record.netAmountUnavailable == true) + if !preserveLockAccounting { record.direction = tx.direction } if let typeName = tx.transaction_type { record.transactionType = String(cString: typeName) } @@ -2560,8 +2581,12 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { record.providerVotingKeyHash = tx.has_provider_voting_key_hash ? withUnsafeBytes(of: tx.provider_voting_key_hash) { Data($0) } : nil - record.netAmount = tx.net_amount - record.fee = tx.has_fee ? tx.fee : nil + if !preserveLockAccounting { + record.netAmount = tx.net_amount + record.netAmountUnavailable = false + record.fee = tx.has_fee ? tx.fee : nil + } + accountingDirty[record.txid] = record if let labelPtr = tx.label { record.label = String(cString: labelPtr) } @@ -2905,6 +2930,8 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// (`reconcileHealMissingTxos`), so both writers honour the same /// tombstone precedence and spender-adoption rules. private func drainPendingInputs(into record: PersistentTxo, resolvedWalletId: Data) { + if let parent = record.transaction { accountingDirty[parent.txid] = parent } + if let spender = record.spendingTransaction { accountingDirty[spender.txid] = spender } let pendingRows = pendingInputRows(outpoint: record.outpoint) if !pendingRows.isEmpty { // A tombstone is not an observation — it is a sweep's settled @@ -3037,7 +3064,9 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { spender: PersistentTransaction, inputIndex: UInt32? ) { + accountingDirty[spender.txid] = spender let currentSpender = txo.spendingTransaction + if let currentSpender { accountingDirty[currentSpender.txid] = currentSpender } let verdict = Self.reconcileSpendObservation( currentSpenderTxid: currentSpender?.txid, currentSpenderContext: currentSpender?.context, @@ -3280,6 +3309,11 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { self.inChangeset = true self.roundUtxoCreditVerdicts = [:] self.roundUtxoCreditTally = UtxoCreditVerdictTally() + // Out-of-round writers (heal paths, deferred backfills) stage + // entries too; they are not this round's to reconcile and may + // point at rows a failed save rolled back. Load-time accounting + // repairs whatever they touched. + self.accountingDirty.removeAll() SDKLogger.event( "persistence_changeset_started", category: .persistence, @@ -3366,6 +3400,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { self.roundUtxoCreditVerdicts = [:] self.roundUtxoCreditTally = UtxoCreditVerdictTally() self.roundIndex = nil + self.accountingDirty.removeAll() self.roundAdvancedFinalityBoundary = false self.inChangeset = false self.drainDeferredBackfills() @@ -3408,6 +3443,20 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { if roundAdvancedFinalityBoundary { collectFinalizedSweptTombstones(walletId: walletId) } + // Display-only accounting: a failure here must not fail the + // round, and is reported under its own event, not save_failed. + // Recomputed values that did land are consistent on their own. + // TODO(test-round-accounting-failure): cover this catch with a + // FetchFaultInjector test; pinning which read faults needs a Swift run. + do { + try reconcileTransactionAccounting(Array(accountingDirty.values)) + } catch { + SDKLogger.event( + "persistence_transaction_accounting_failed", category: .persistence, severity: .error, + fields: ["phase": .publicText("round"), "wallet_reference": .reference(walletId)], + error: error + ) + } do { try backgroundContext.save() committedRoundGeneration &+= 1 @@ -6503,6 +6552,10 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } if let walletRow = walletRow { + // Shared scalars must be scoped while the departing wallet's TXOs still exist. + for transaction in try backgroundContext.fetch(FetchDescriptor()) { + transaction.preserveAccounting(removingWallet: walletId) + } // Wallet → identities is `.nullify`; this delete // path cascades them explicitly. let identitiesToDelete = Array(walletRow.identities) @@ -6924,6 +6977,73 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { return txids } + /// Repair only fully resolved spends; missing prevouts are not evidence of external ownership. + func reconcileTransactionAccounting( + _ transactions: [PersistentTransaction], txos: [Data: PersistentTxo]? = nil, + addresses: [String: PersistentCoreAddress]? = nil + ) throws { + for transaction in transactions where !transaction.isDeleted { + guard let transactionNetwork = network + ?? Self.walletNetwork(of: transaction.involvedAccounts.first) + ?? Self.walletNetwork(of: transaction.inputs.first?.account) + ?? Self.walletNetwork(of: transaction.outputs.first?.account), + let decoded = try? TransactionDecoder.decode( + transaction.transactionData, network: transactionNetwork + ), !decoded.inputs.isEmpty else { continue } + var inputs: [PersistentTxo] = [] + var complete = true + for input in decoded.inputs { + let key = PersistentTxo.makeOutpoint(txid: input.prevTxid, vout: input.prevVout) + let row: PersistentTxo? + if let txos { row = txos[key] } + else { row = try fetchTxoRowChecked(outpoint: key) } + guard let row, !row.isDeleted, Self.isWalletOwnedTxo(row) else { + complete = false + break + } + inputs.append(row) + } + guard complete else { continue } + var amounts: [UInt64] = [] + var allOutputsOwned = true + let ownedVouts = Set(transaction.outputs.filter { !$0.isDeleted && Self.isWalletOwnedTxo($0) }.map(\.vout)) + // An address-matched output with no linked TXO carries no wallet of + // its own, so it counts only for the spending wallets: another local + // wallet's credit must not hide inside the sender's scalar. + let spendingWallets = Set(inputs.compactMap { Self.resolvedWalletId(of: $0) }) + for (index, output) in decoded.outputs.enumerated() { + // OP_RETURN burns (including asset locks) are not spendable Core outputs. + if output.isOpReturn { continue } + var belongs = ownedVouts.contains(UInt32(index)) + if !belongs, let address = output.address { + let descriptor = FetchDescriptor(predicate: #Predicate { $0.address == address }) + let owner: PersistentCoreAddress? + if let cached = roundIndex?.coreAddressesByAddress[address] { owner = cached } + else if let addresses { owner = addresses[address] } + else { owner = try modelFetcher.fetch(descriptor, in: backgroundContext).first } + if let account = owner?.account, account.accountType != Self.dashpayExternalAccountTypeTag { + let ownerWallet: PersistentWallet? = account.wallet + if let ownerWallet, spendingWallets.contains(ownerWallet.walletId) { + belongs = true + } + } + } + if belongs { amounts.append(output.valueDuffs) } + else { allOutputsOwned = false } + } + if let accounting = PersistentTransaction.reconciledAccounting( + inputs: inputs, ownedOutputAmounts: amounts, allOutputsOwned: allOutputsOwned, + previousDirection: transaction.typedKind == .coinJoin + ? CoreDirectionCode.coinJoin : transaction.direction, + isAssetLock: transaction.isAssetLock + ) { + transaction.netAmount = accounting.netAmount + transaction.netAmountUnavailable = false + transaction.direction = accounting.direction + } + } + } + /// Returns `(nil, 0)` if nothing is restorable. func loadWalletList() -> (entries: UnsafePointer?, count: Int, errored: Bool) { SDKLogger.event( @@ -6969,6 +7089,42 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { ) return (nil, 0, true) } + // Mid-round the context holds another round's staged writes: saving + // would commit half of it and rolling back would silently drop it. + // That round reconciles its own dirty rows; the next load repairs the rest. + // TODO(persist-accounting-backfill-marker): this pass re-reads all + // history on every launch; a persisted completion marker needs a + // SwiftData shape change (a new live schema version) or a side + // channel, which is a product decision. + if !inChangeset { + do { + let walletIds = Set(wallets.map(\.walletId)) + let transactions = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) + .filter { row in + walletIds.contains { Self.walletOwnsTransaction(walletId: $0, transaction: row) } + } + let txos = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) + // One read instead of one per unlinked output. + let addresses = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) + try reconcileTransactionAccounting( + transactions, + // Never trap on a duplicate outpoint; a live row wins over a deleted one. + txos: Dictionary(txos.map { ($0.outpoint, $0) }, uniquingKeysWith: { kept, other in + kept.isDeleted ? other : kept + }), + addresses: Dictionary(addresses.map { ($0.address, $0) }, uniquingKeysWith: { first, _ in first }) + ) + try backgroundContext.save() + } catch { + // Display-only accounting must never block restoring wallets: + // drop this pass's edits and restore on the stored values. + backgroundContext.rollback() + SDKLogger.event( + "persistence_transaction_accounting_failed", category: .persistence, severity: .error, + fields: ["phase": .publicText("load")], error: error + ) + } + } let restorable = wallets.filter { wallet in wallet.accounts.contains { ($0.accountExtendedPubKeyBytes?.isEmpty == false) } } diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift index c222a532b70..ba2092b1f68 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift @@ -3,36 +3,29 @@ import SwiftDashSDK struct TransactionDetailView: View { let transaction: PersistentTransaction - /// Override amount for asset-lock txs. The wallet's `netAmount` - /// shows ~0 for these (credit output is structurally self-owned), - /// so the list view passes the linked - /// `PersistentAssetLock.amountDuffs`. `nil` for non-asset-lock - /// rows OR consumed asset locks whose tracking row was cleaned - /// up after successful identity registration. + var walletId: Data? = nil + /// `nil` while this wallet's amount is unresolved — the same state the + /// amount label shows as "Amount unavailable", so fee and amount agree. + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } + /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil @Environment(\.dismiss) private var dismiss @State private var showCopiedAlert = false - /// Amount label rendered prominently at the top of the sheet. - /// Same precedence rule as the row: asset-lock duffs when we - /// have them, else an explicit "amount unknown" label for the - /// historical-asset-lock case (rather than the misleading - /// `+0.00000000 DASH` from `transaction.formattedAmount`). - /// `nil` for a payload-only provider special tx — a ProRegTx - /// observed via the owner/voting keys moves no wallet balance, - /// and `+0.00000000 DASH` reads as a broken zero-value receive. + /// Show the lock's funding amount, or the wallet's Core value movement for ordinary transactions. private var displayAmount: String? { if transaction.isAssetLock { if let duffs = assetLockAmountDuffs { let dash = Double(duffs) / 100_000_000.0 return String(format: "-%.8f DASH", dash) } - return "Asset Lock (amount unknown)" + return "Asset Lock (amount unavailable)" } - if transaction.isProviderSpecial && transaction.netAmount == 0 { + if transaction.isProviderSpecial && netAmount == 0 { return nil } - return transaction.formattedAmount + return transaction.displayFormattedAmount(for: walletId) } private var typeDescription: String { @@ -42,11 +35,13 @@ struct TransactionDetailView: View { || transaction.isProviderSpecial { return transaction.displayDirection } - switch transaction.netAmount { - case let amount where amount > 0: + switch direction { + case CoreDirectionCode.incoming: return "Received" - case let amount where amount < 0: + case CoreDirectionCode.outgoing: return "Sent" + case CoreDirectionCode.coinJoin: + return "CoinJoin" default: return "Self-Transfer" } @@ -56,14 +51,7 @@ struct TransactionDetailView: View { if transaction.isAssetLock { return "lock.fill" } if transaction.isAssetUnlock { return "lock.open.fill" } if transaction.isProviderSpecial { return "server.rack" } - switch transaction.netAmount { - case let amount where amount > 0: - return "arrow.down.circle.fill" - case let amount where amount < 0: - return "arrow.up.circle.fill" - default: - return "arrow.triangle.2.circlepath" - } + return TransactionDirectionStyle.icon(for: direction) } private var typeColor: Color { @@ -73,14 +61,7 @@ struct TransactionDetailView: View { if transaction.isProviderSpecial { return .orange } - switch transaction.netAmount { - case let amount where amount > 0: - return .green - case let amount where amount < 0: - return .red - default: - return .blue - } + return TransactionDirectionStyle.color(for: direction) } private var isConfirmed: Bool { @@ -208,7 +189,7 @@ struct TransactionDetailView: View { ) } - if let fee = formattedFee, transaction.netAmount < 0 { + if let fee = formattedFee, let amount = netAmount, amount < 0 { TransactionDetailRow( label: "Network Fee", value: fee diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift new file mode 100644 index 00000000000..589aa6bb7f0 --- /dev/null +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift @@ -0,0 +1,25 @@ +import SwiftUI +import SwiftDashSDK + +/// Direction icon and colour shared by the transaction list and detail views. +enum TransactionDirectionStyle { + static func icon(for direction: UInt32) -> String { + switch direction { + case CoreDirectionCode.incoming: return "arrow.down.circle.fill" + case CoreDirectionCode.outgoing: return "arrow.up.circle.fill" + case CoreDirectionCode.internalTransfer: return "arrow.triangle.2.circlepath" + case CoreDirectionCode.coinJoin: return "shuffle.circle.fill" + default: return "questionmark.circle" + } + } + + /// Internal transfers share the outgoing colour: they still pay a fee. + static func color(for direction: UInt32) -> Color { + switch direction { + case CoreDirectionCode.incoming: return .green + case CoreDirectionCode.outgoing, CoreDirectionCode.internalTransfer: return .red + case CoreDirectionCode.coinJoin: return .blue + default: return .secondary + } + } +} diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift index f185f78c20f..5d84aaca7e4 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift @@ -29,10 +29,9 @@ struct TransactionListView: View { @Query private var walletAccounts: [PersistentAccount] @Query private var transactionObservation: [PersistentTransaction] /// Per-wallet asset-lock rows. Used to look up the *locked* amount - /// for each asset-lock tx — `PersistentTransaction.netAmount` is - /// the wallet's input-vs-output diff, which sees the credit - /// output as "to-self" and reports ~0 for asset locks. The - /// `amountDuffs` on the asset-lock row is the actual L1 burn. + /// for each asset-lock tx: `amountDuffs` on the asset-lock row is the + /// payload funding amount, while `PersistentTransaction.netAmount` is + /// the Core debit, which includes the fee. @Query private var assetLocks: [PersistentAssetLock] /// This wallet's owning identities. The DashPay payment / contact /// join below must be scoped to these — two identities in one store @@ -169,6 +168,7 @@ struct TransactionListView: View { .sheet(item: $selectedTransaction) { transaction in TransactionDetailView( transaction: transaction, + walletId: walletId, assetLockAmountDuffs: assetLockAmountByTxid[transaction.txidHex] ) } @@ -202,6 +202,7 @@ struct TransactionListView: View { } label: { TransactionRowView( transaction: transaction, + walletId: walletId, assetLockAmountDuffs: assetLockAmounts[transaction.txidHex], dashpayPayment: payment, dashpayCounterpartyName: payment.map { @@ -219,12 +220,12 @@ struct TransactionListView: View { struct TransactionRowView: View { let transaction: PersistentTransaction - /// Override amount displayed for asset-lock rows. The wallet's - /// `netAmount` shows ~0 for these (credit output is structurally - /// self-owned), so the list view passes the linked - /// `PersistentAssetLock.amountDuffs` — the actual L1 DASH burned - /// to mint platform credits. `nil` for non-asset-lock rows or - /// when no matching row was found. + var walletId: Data? = nil + /// `nil` while this wallet's amount is unresolved — the same state the + /// amount label shows as "Amount unavailable", so fee and amount agree. + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } + /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil /// The DashPay payment this tx belongs to, if any — joined by `txid` in /// `TransactionListView`. When set, the row shows the contact context @@ -253,14 +254,7 @@ struct TransactionRowView: View { // `Internal` — the wallet just sees its own owner/voting/payout // keys in the payload — so the self-transfer arrows would lie. if transaction.isProviderSpecial { return "server.rack" } - // direction: 0=incoming, 1=outgoing, 2=internal, 3=coinJoin - switch transaction.direction { - case 0: return "arrow.down.circle.fill" - case 1: return "arrow.up.circle.fill" - case 2: return "arrow.triangle.2.circlepath" - case 3: return "shuffle.circle.fill" - default: return "questionmark.circle" - } + return TransactionDirectionStyle.icon(for: direction) } private var typeColor: Color { @@ -278,12 +272,7 @@ struct TransactionRowView: View { if transaction.isProviderSpecial { return .orange } - switch transaction.direction { - case 0: return .green - case 1, 2: return .red - case 3: return .blue - default: return .secondary - } + return TransactionDirectionStyle.color(for: direction) } /// Primary label: the contact context for a DashPay payment, else the @@ -400,7 +389,7 @@ struct TransactionRowView: View { .font(.headline) .foregroundColor(typeColor) - if let fee = transaction.fee, transaction.netAmount < 0 { + if let fee = transaction.fee, let amount = netAmount, amount < 0 { Text("Fee: \(formatFee(fee))") .font(.caption2) .foregroundColor(.secondary) @@ -417,35 +406,23 @@ struct TransactionRowView: View { return String(format: "%.8f DASH", dash) } - /// Amount label for the row. For asset-lock txs we substitute - /// the linked `PersistentAssetLock.amountDuffs` (the L1 DASH - /// actually burned to mint platform credits); the wallet's - /// `netAmount` is ~0 for these because the credit output is a - /// self-owned address. Rendered as a negative (DASH leaving L1). - /// - /// If we know the row is an asset lock but the linked - /// `PersistentAssetLock` is missing (e.g. a historical record - /// from before the `Consumed`-status retention change shipped), - /// we render "Asset Lock (amount unknown)" instead of falling - /// through to `transaction.formattedAmount` — that would say - /// `+0.00000000 DASH`, which is misleading for a row the user - /// can see was a funding tx. + /// Keep asset-lock funding amounts distinct from the Core debit, which includes fees. private var displayAmount: String { if transaction.isAssetLock { if let duffs = assetLockAmountDuffs { let dash = Double(duffs) / 100_000_000.0 return String(format: "-%.8f DASH", dash) } - return "Asset Lock (amount unknown)" + return "Asset Lock (amount unavailable)" } // A payload-only provider special tx moves no wallet balance; // `+0.00000000 DASH` reads as a broken zero-value receive, so // put the tx kind in the amount slot instead. A provider tx // that DOES move value (e.g. this wallet funded the collateral) // falls through and shows the real signed amount. - if transaction.isProviderSpecial && transaction.netAmount == 0 { + if transaction.isProviderSpecial && netAmount == 0 { return transaction.providerSpecialName ?? transaction.transactionType } - return transaction.formattedAmount + return transaction.displayFormattedAmount(for: walletId) } } diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift index b4627318cf0..ec38b7dae0f 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift @@ -1162,10 +1162,10 @@ struct TransactionStorageListView: View { // Direction. switch directionFilter { case .all: break - case .incoming where record.direction != 0: return false - case .outgoing where record.direction != 1: return false - case .internalTx where record.direction != 2: return false - case .coinjoin where record.direction != 3: return false + case .incoming where record.direction != CoreDirectionCode.incoming: return false + case .outgoing where record.direction != CoreDirectionCode.outgoing: return false + case .internalTx where record.direction != CoreDirectionCode.internalTransfer: return false + case .coinjoin where record.direction != CoreDirectionCode.coinJoin: return false default: break } // Type. Treat the legacy `"Standard"` placeholder (the diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift index e54ed241b93..c4a793f0ceb 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift @@ -368,8 +368,13 @@ final class AssetLockSpendVisibilityTests: XCTestCase { XCTAssertEqual( injector.observedReads, - ["PersistentWallet", "PersistentTxo", "PersistentAssetLock"], - "the wallet and unspent-TXO reads must have been served — only the lock read failed" + [ + // Wallet list, then the load-time accounting reconcile. + "PersistentWallet", "PersistentTransaction", "PersistentTxo", "PersistentCoreAddress", + // Unspent-TXO restore, then the faulted lock read. + "PersistentTxo", "PersistentAssetLock", + ], + "every read before the lock read must have been served — only the lock read failed" ) XCTAssertTrue( errored, diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift index 982d9ab0afd..26a2fd54943 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift @@ -11,15 +11,18 @@ private final class BridgeFutureMarker { var value: String = "future" init() {} } -private enum BridgeFutureV4: VersionedSchema { - static var versionIdentifier: Schema.Version { Schema.Version(4, 0, 0) } - static var models: [any PersistentModel.Type] { DashSchemaV3.models + [BridgeFutureMarker.self] } +private enum BridgeFutureV5: VersionedSchema { + static var versionIdentifier: Schema.Version { Schema.Version(5, 0, 0) } + static var models: [any PersistentModel.Type] { DashSchemaV4.models + [BridgeFutureMarker.self] } } private enum BridgeFuturePlan: SchemaMigrationPlan { - static var schemas: [any VersionedSchema.Type] { [DashSchemaV1.self, DashSchemaV3.self, BridgeFutureV4.self] } + static var schemas: [any VersionedSchema.Type] { + [DashSchemaV1.self, DashSchemaV3.self, DashSchemaV4.self, BridgeFutureV5.self] + } static var stages: [MigrationStage] { [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV3.self), - .custom(fromVersion: DashSchemaV3.self, toVersion: BridgeFutureV4.self, + .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self), + .custom(fromVersion: DashSchemaV4.self, toVersion: BridgeFutureV5.self, willMigrate: { context in for wallet in try context.fetch(FetchDescriptor()) { wallet.name = "explicit future transformation" @@ -770,7 +773,7 @@ final class DashLegacySchemaMigrationTests: XCTestCase { func testSkippingV3UsesFixedBridgeThenRegisteredCustomFutureStage() throws { try withStore { url in - let schema = Schema(versionedSchema: BridgeFutureV4.self) + let schema = Schema(versionedSchema: BridgeFutureV5.self) var checkedV3 = false let configuration = ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none) XCTAssertThrowsError(try DashLegacySchemaBridge.open( @@ -887,7 +890,9 @@ final class DashLegacySchemaMigrationTests: XCTestCase { let container = try ModelContainer(for: schema, configurations: [ ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none) ]) - try verifyRows(container.mainContext) + let wallet = try XCTUnwrap(container.mainContext.fetch( + FetchDescriptor()).first) + XCTAssertEqual(wallet.name, "historical audit wallet") } XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["2.0.0"]) let container = try open(url, hooks: .init(visit: { _, _ in diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 4beee9638fa..0f27acf4063 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -75,11 +75,11 @@ final class DashModelMigrationTests: XCTestCase { SDKLogger.flush() let log = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(log.contains("event=store_open_started")) - XCTAssertTrue(log.contains("route=\"historical-v2-to-v3\"")) + XCTAssertTrue(log.contains("route=\"historical-v2-to-v4\"")) XCTAssertTrue(log.contains("source_version=\"2.0.0\"")) XCTAssertTrue(log.contains("source_checksum=\"\(sourceChecksum)\"")) - XCTAssertTrue(log.contains("target_version=\"3.0.0\"")) - XCTAssertTrue(log.contains("route=\"labelled-current-v3\"")) + XCTAssertTrue(log.contains("target_version=\"4.0.0\"")) + XCTAssertTrue(log.contains("route=\"labelled-current-v4\"")) XCTAssertEqual(log.components(separatedBy: "event=store_open_succeeded").count - 1, 2) XCTAssertFalse(log.contains("event=store_open_failed")) XCTAssertFalse(log.contains(directory.path)) @@ -95,7 +95,7 @@ final class DashModelMigrationTests: XCTestCase { let updatedLog = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(updatedLog.contains("route=\"new-store\"")) let failure = try XCTUnwrap(updatedLog.split(separator: "\n").first { $0.contains("event=store_open_failed") }) - XCTAssertTrue(failure.contains("target_version=\"3.0.0\"")) + XCTAssertTrue(failure.contains("target_version=\"4.0.0\"")) XCTAssertTrue(failure.contains("error_code=")) XCTAssertFalse(updatedLog.contains(directory.path)) XCTAssertFalse(updatedLog.contains("private-invalid-store-content")) @@ -106,7 +106,7 @@ final class DashModelMigrationTests: XCTestCase { /// either way. Labels whose route is undecidable without that probe /// (accepted V1 versus the bridge, historical V2 versus a beta layout) /// keep failing closed, leaving the store untouched. - func testCurrentV3RouteNeverDependsOnTheSchemaIdentityProbe() throws { + func testCurrentV4RouteNeverDependsOnTheSchemaIdentityProbe() throws { struct ProbeUnavailable: Error {} let failingProbe: (any VersionedSchema.Type) throws -> DashLegacySchemaBridge.Identity = { _ in throw ProbeUnavailable() @@ -117,7 +117,7 @@ final class DashModelMigrationTests: XCTestCase { defer { try? FileManager.default.removeItem(at: directory) } let current = directory.appendingPathComponent("current.store") try autoreleasepool { _ = try DashModelContainer.create(url: current) } - XCTAssertEqual(try DashLegacySchemaBridge.identity(at: current).versions, ["3.0.0"]) + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: current).versions, ["4.0.0"]) let plan = try DashModelContainer.migrationPlan( at: current, defaultPlan: DashMigrationPlan.self, identity: failingProbe) XCTAssertTrue(ObjectIdentifier(plan) == ObjectIdentifier(DashMigrationPlan.self)) @@ -513,7 +513,7 @@ final class DashModelMigrationTests: XCTestCase { try v1Container?.mainContext.save() v1Container = nil - let v2Schema = Schema(versionedSchema: DashSchemaV3.self) + let v2Schema = DashModelContainer.schema let v2Configuration = ModelConfiguration( "DashKeyLimitsMigrationTest", schema: v2Schema, @@ -590,7 +590,7 @@ final class DashModelMigrationTests: XCTestCase { try v1Container?.mainContext.save() v1Container = nil - let v2Schema = Schema(versionedSchema: DashSchemaV3.self) + let v2Schema = DashModelContainer.schema let v2Configuration = ModelConfiguration( "DashContractBoundsKindMigrationTest", schema: v2Schema, diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift index d283b5a20c0..7e268061af8 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift @@ -99,6 +99,37 @@ final class DashReleasedSchemaTests: XCTestCase { } } + @MainActor + func testShouldMigrateV3AccountingAvailabilityAndPersistAnUnavailableAmount() throws { + let fixture = try XCTUnwrap(DashReleasedSchemaRegistry.fixtures.first { + $0.version.versionIdentifier == Schema.Version(3, 0, 0) + }) + let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let url = directory.appendingPathComponent("accounting.store") + try FileManager.default.copyItem(at: source(fixture), to: url) + let txid = Data(repeating: 0x32, count: 32) + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) + .first { $0.txid == txid }) + XCTAssertNil(row.netAmountUnavailable, "V3 rows retain their stored accounting by default") + XCTAssertEqual(row.displayNetAmount(for: nil), row.netAmount) + row.netAmountUnavailable = true + try container.mainContext.save() + } + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) + .first { $0.txid == txid }) + XCTAssertEqual(row.netAmountUnavailable, true) + XCTAssertNil(row.displayNetAmount(for: nil)) + XCTAssertEqual(row.formattedAmount, "Amount unavailable") + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["4.0.0"]) + } + } + @MainActor func testRuntimePlanHasNoDuplicateModelChecksums() throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift index 02b0bc78db8..f3f6d5deb40 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift @@ -12,7 +12,10 @@ final class PersistentTransactionDisplayTests: XCTestCase { /// Direction 2 = internal — the raw classification every special /// tx gets (asset locks, provider txs), which the display helpers /// exist to override. - private func makeTransaction(kind: UInt8, direction: UInt32 = 2) -> PersistentTransaction { + private func makeTransaction( + kind: UInt8, + direction: UInt32 = CoreDirectionCode.internalTransfer + ) -> PersistentTransaction { let tx = PersistentTransaction( txid: Data(repeating: 0xAB, count: 32), transactionData: Data(), @@ -53,7 +56,7 @@ final class PersistentTransactionDisplayTests: XCTestCase { XCTAssertEqual(makeTransaction(kind: 7).displayDirection, "Asset Unlock") // …and non-special kinds fall through to the raw direction. XCTAssertEqual(makeTransaction(kind: 0).displayDirection, "Internal") - XCTAssertEqual(makeTransaction(kind: 0, direction: 0).displayDirection, "Incoming") + XCTAssertEqual(makeTransaction(kind: 0, direction: CoreDirectionCode.incoming).displayDirection, "Incoming") XCTAssertEqual(makeTransaction(kind: 0xFF).displayDirection, "Internal") } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift new file mode 100644 index 00000000000..6fc27f08526 --- /dev/null +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -0,0 +1,695 @@ +import XCTest +import SwiftData +import DashSDKFFI +@testable import SwiftDashSDK + +@MainActor +final class TransactionAccountingTests: XCTestCase { + private func input(_ value: UInt64, wallet: UInt8 = 1) -> PersistentTxo { + let parent = PersistentTransaction(txid: Data(repeating: wallet, count: 32), transactionData: Data()) + let txo = PersistentTxo(transaction: parent, vout: 0, amount: value, address: "", height: 1) + txo.walletId = Data(repeating: wallet, count: 32) + return txo + } + + func testShouldRepairSpentInputsWithoutChangingBalanceOrConsumption() { + let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) + let spent = input(100) + spent.isSpent = true + let result = PersistentTransaction.reconciledAccounting( + inputs: [spent], ownedOutputAmounts: [40], allOutputsOwned: false, previousDirection: 0, isAssetLock: false + ) + XCTAssertEqual(result?.netAmount, -60) + XCTAssertEqual(result?.direction, 1) + XCTAssertTrue(spent.isSpent) + XCTAssertEqual(tx.netAmount, 40) // Computing accounting has no storage side effects. + } + + func testShouldKeepInternalAndCoinJoinSemanticsWithNegativeNet() { + let spent = input(100) + XCTAssertEqual(PersistentTransaction.reconciledAccounting( + inputs: [spent], ownedOutputAmounts: [99], allOutputsOwned: true, previousDirection: 0, isAssetLock: false + )?.direction, 2) + let lock = PersistentTransaction.reconciledAccounting( + inputs: [spent], ownedOutputAmounts: [], allOutputsOwned: true, previousDirection: 2, isAssetLock: true + ) + XCTAssertEqual(lock?.netAmount, -100) + XCTAssertEqual(lock?.direction, 2) + XCTAssertEqual(PersistentTransaction.reconciledAccounting( + inputs: [spent], ownedOutputAmounts: [99], allOutputsOwned: false, previousDirection: 3, isAssetLock: false + )?.direction, 3) + } + + /// Same case table as the Rust repair's + /// `should_classify_repaired_direction_like_the_swift_sdk`. + func testShouldClassifyDirectionLikeTheRustRepair() { + let spent = input(100) + // (spends ours, owned output amounts, all outputs owned, asset lock, previous direction, expected) + let (incoming, outgoing, internalTransfer, coinJoin) = ( + CoreDirectionCode.incoming, CoreDirectionCode.outgoing, + CoreDirectionCode.internalTransfer, CoreDirectionCode.coinJoin + ) + let cases: [(Bool, [UInt64], Bool, Bool, UInt32, UInt32)] = [ + (true, [99], true, false, incoming, internalTransfer), + (true, [40], false, false, incoming, outgoing), + (true, [], true, false, incoming, outgoing), + (true, [], true, true, incoming, internalTransfer), + (true, [40], true, true, incoming, internalTransfer), + (true, [], false, true, incoming, outgoing), + (false, [40], true, false, incoming, incoming), + (true, [99], true, false, coinJoin, coinJoin), + ] + for (index, (spendsOurs, owned, allOwned, isLock, previous, expected)) in cases.enumerated() { + let result = PersistentTransaction.reconciledAccounting( + inputs: spendsOurs ? [spent] : [], ownedOutputAmounts: owned, + allOutputsOwned: allOwned, previousDirection: previous, isAssetLock: isLock + ) + XCTAssertEqual(result?.direction, expected, "case \(index)") + } + } + + func testShouldFormatSignedDuffsWithoutTrappingOnInt64Min() { + XCTAssertEqual(PersistentTransaction.format(duffs: 150_000_000), "+1.50000000 DASH") + XCTAssertEqual(PersistentTransaction.format(duffs: -100), "-0.00000100 DASH") + XCTAssertTrue(PersistentTransaction.format(duffs: .min).hasPrefix("-92233720368.")) + } + + func testShouldRejectOverflowInsteadOfWrappingHistory() { + XCTAssertNil(PersistentTransaction.reconciledAccounting( + inputs: [input(UInt64.max)], ownedOutputAmounts: [], allOutputsOwned: false, previousDirection: 0, isAssetLock: false + )) + } + + func testShouldScopeNetToWalletAndDeduplicateOutpoints() { + let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data()) + let first = input(100) + let other = input(200, wallet: 2) + tx.inputs = [first, first, other] + XCTAssertEqual(tx.netAmount(for: first.walletId), -100) + XCTAssertEqual(tx.netAmount(for: other.walletId), -200) + } + /// `burn` makes the single output an OP_RETURN, as an asset lock's is. + private func serializedSpend(inputs: [Data], outputValue: UInt64 = 40, burn: Bool = false) -> Data { + var bytes = Data([2, 0, 0, 0, UInt8(inputs.count)]) + for txid in inputs { + bytes.append(txid) + bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255]) + } + bytes.append(1) + withUnsafeBytes(of: outputValue.littleEndian) { bytes.append(contentsOf: $0) } + bytes.append(contentsOf: burn ? [1, 0x6a] : [0]) + bytes.append(contentsOf: [0, 0, 0, 0]) + return bytes + } + + func testShouldBackfillExistingHistoryOnLoadAndRemainIdempotent() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let funding = PersistentTransaction(txid: walletId, transactionData: Data()) + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), + direction: CoreDirectionCode.incoming, netAmount: 40 + ) + let coin = PersistentTxo(transaction: funding, vout: 0, amount: 100, address: "", height: 1) + coin.walletId = walletId + coin.isSpent = true + coin.spendingTransaction = spender + context.insert(funding) + context.insert(spender) + context.insert(coin) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + XCTAssertFalse(handler.loadWalletList().errored) + let fresh = ModelContext(container) + let repaired = try XCTUnwrap(fresh.fetch(FetchDescriptor()).first { $0.txid == spender.txid }) + XCTAssertEqual(repaired.netAmount, -100) + XCTAssertEqual(repaired.direction, 1) + XCTAssertTrue(try XCTUnwrap(fresh.fetch(FetchDescriptor()).first).isSpent) + } + + func testShouldNotCommitOrDropOpenRoundWhenLoadRunsMidChangeset() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let funding = PersistentTransaction(txid: walletId, transactionData: Data()) + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), + direction: CoreDirectionCode.incoming, netAmount: 40 + ) + let coin = PersistentTxo(transaction: funding, vout: 0, amount: 100, address: "", height: 1) + coin.walletId = walletId + coin.isSpent = true + coin.spendingTransaction = spender + context.insert(funding) + context.insert(spender) + context.insert(coin) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let identityId = Data(repeating: 9, count: 32) + let fetchIdentity = { + try ModelContext(container).fetch(FetchDescriptor()).first { $0.identityId == identityId } + } + + handler.beginChangeset(walletId: walletId) + handler.persistIdentities( + walletId: walletId, + upserts: [.init( + identityId: identityId, balance: 100, revision: 1, identityIndex: 0, label: nil, + status: 0, walletId: walletId, dpnsNames: [], dashpayProfile: nil, contactProfiles: [] + )], + removed: [] + ) + XCTAssertFalse(handler.loadWalletList().errored) + XCTAssertNil(try fetchIdentity(), "load must not commit half of an open round") + XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) + XCTAssertNotNil(try fetchIdentity(), "load must not roll back an open round") + + // The skipped full pass runs on the next load outside any round. + XCTAssertFalse(handler.loadWalletList().errored) + let repaired = try ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == spender.txid } + XCTAssertEqual(repaired?.netAmount, -100) + } + + func testShouldRestoreWalletsWhenLoadTimeAccountingFails() throws { + let container = try DashModelContainer.createInMemory() + container.mainContext.insert(PersistentWallet(walletId: Data(repeating: 1, count: 32), network: .testnet)) + try container.mainContext.save() + let injector = FetchFaultInjector(faulting: PersistentCoreAddress.self) + let handler = PlatformWalletPersistenceHandler( + modelContainer: container, network: .testnet, modelFetcher: injector + ) + XCTAssertFalse(handler.loadWalletList().errored, "display accounting must not block restore") + XCTAssertTrue(injector.observedReads.contains("PersistentCoreAddress")) + } + + func testShouldPreserveAccountingWhenSomePrevoutsAreMissing() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let coin = input(100) + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), + transactionData: serializedSpend(inputs: [walletId, Data(repeating: 2, count: 32)]), + direction: CoreDirectionCode.outgoing, netAmount: -200 + ) + coin.spendingTransaction = spender + context.insert(coin) + context.insert(spender) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let rows = try ModelContext(container).fetch(FetchDescriptor()) + XCTAssertEqual(rows.first { $0.txid == spender.txid }?.netAmount, -200) + } + + private func persist( + _ handler: PlatformWalletPersistenceHandler, walletId: Data, txid: Data, + bytes: Data? = nil, net: Int64 = 0, kind: UInt8 = 0, + inputTxids: [Data] = [], outputs: [(Data, UInt64)] = [] + ) { + let name = strdup("Standard { index: 0 }") + let address = strdup("") + defer { free(name); free(address) } + var record = TransactionRecordFFI() + withUnsafeMutableBytes(of: &record.txid) { $0.copyBytes(from: txid) } + record.context = 2 + record.net_amount = net + record.transaction_type_kind = kind + var inputs = inputTxids.map { txid -> OutPointFFI in + var result = OutPointFFI() + withUnsafeMutableBytes(of: &result.txid) { $0.copyBytes(from: txid) } + return result + } + var txos = outputs.map { txid, amount -> UtxoEntryFFI in + var result = UtxoEntryFFI() + withUnsafeMutableBytes(of: &result.outpoint.txid) { $0.copyBytes(from: txid) } + result.amount = amount + result.address = address + return result + } + var raw = Array(bytes ?? Data()) + handler.beginChangeset(walletId: walletId) + raw.withUnsafeMutableBufferPointer { rawPtr in + inputs.withUnsafeMutableBufferPointer { inputPtr in + txos.withUnsafeMutableBufferPointer { txoPtr in + record.tx_data = rawPtr.baseAddress + record.tx_data_len = UInt(rawPtr.count) + record.input_outpoints = inputPtr.baseAddress + record.input_outpoints_count = UInt(inputPtr.count) + withUnsafeMutablePointer(to: &record) { recordPtr in + var account = AccountChangeSetFFI() + account.account_type_name = name + account.transactions = recordPtr + account.transactions_count = bytes == nil ? 0 : 1 + account.utxos_added = txoPtr.baseAddress + account.utxos_added_count = UInt(txoPtr.count) + withUnsafeMutablePointer(to: &account) { accountPtr in + var changeset = WalletChangeSetFFI() + changeset.accounts = accountPtr + changeset.accounts_count = 1 + withUnsafePointer(to: &changeset) { ptr in + XCTAssertTrue(handler.persistWalletChangeset(walletId: walletId, changeset: ptr)) + } + } + } + } + } + } + XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) + } + + func testShouldRepairLateInputAndLateOutputInSeparateAtomicRounds() throws { + let container = try DashModelContainer.createInMemory() + let walletId = Data(repeating: 1, count: 32) + container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) + try container.mainContext.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let spenderId = Data(repeating: 3, count: 32) + persist(handler, walletId: walletId, txid: spenderId, + bytes: serializedSpend(inputs: [walletId]), net: 40, inputTxids: [walletId]) + persist(handler, walletId: walletId, txid: walletId, outputs: [(walletId, 100)]) + persist(handler, walletId: walletId, txid: spenderId, outputs: [(spenderId, 40)]) + let context = ModelContext(container) + let row = try XCTUnwrap(context.fetch(FetchDescriptor()).first { $0.txid == spenderId }) + XCTAssertEqual(row.netAmount, -60) + XCTAssertEqual(row.direction, 2) + XCTAssertEqual(row.inputs.count, 1) + XCTAssertEqual(row.outputs.count, 1) + XCTAssertTrue(try XCTUnwrap(row.inputs.first).isSpent) + } + + func testShouldPreserveFundedAssetLockAccountingDuringSyntheticReplayWithMissingPrevout() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let spenderId = Data(repeating: 3, count: 32) + let bytes = serializedSpend(inputs: [walletId, Data(repeating: 2, count: 32)], outputValue: 0, burn: true) + let spender = PersistentTransaction( + txid: spenderId, transactionData: bytes, + direction: CoreDirectionCode.internalTransfer, netAmount: -200 + ) + spender.transactionTypeKind = 6 + let coin = input(100) + coin.spendingTransaction = spender + context.insert(coin) + context.insert(spender) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + persist(handler, walletId: walletId, txid: spenderId, bytes: bytes, kind: 6, inputTxids: [walletId]) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) + XCTAssertEqual(row.netAmount, -200) + XCTAssertEqual(row.direction, 2) + XCTAssertEqual(row.context, 2) + } + + func testShouldPreserveAssetLockDebitWhenNoInputIsLinkedYet() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let spenderId = Data(repeating: 3, count: 32) + let bytes = serializedSpend(inputs: [Data(repeating: 2, count: 32)], outputValue: 0, burn: true) + let lock = PersistentTransaction( + txid: spenderId, transactionData: bytes, + direction: CoreDirectionCode.internalTransfer, netAmount: -200 + ) + lock.transactionTypeKind = 6 + lock.fee = 7 + context.insert(lock) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + persist(handler, walletId: walletId, txid: spenderId, bytes: bytes, kind: 6) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) + XCTAssertEqual(row.netAmount, -200, "a synthetic zero update must not erase the debit") + XCTAssertEqual(row.fee, 7) + XCTAssertEqual(row.direction, 2) + } + + func testShouldRepairNoChangeAssetLockToFullCoreDebit() throws { + let container = try DashModelContainer.createInMemory() + let walletId = Data(repeating: 1, count: 32) + container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) + try container.mainContext.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let spenderId = Data(repeating: 3, count: 32) + persist(handler, walletId: walletId, txid: walletId, outputs: [(walletId, 100)]) + persist(handler, walletId: walletId, txid: spenderId, + bytes: serializedSpend(inputs: [walletId], outputValue: 0, burn: true), kind: 6, inputTxids: [walletId]) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) + XCTAssertEqual(row.netAmount, -100) + XCTAssertEqual(row.direction, 2) + XCTAssertTrue(row.isAssetLock) + } + + func testShouldReportSoleWalletsStoredAmountDespitePendingInputs() { + let walletId = Data(repeating: 1, count: 32) + let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) + let change = PersistentTxo(transaction: tx, vout: 0, amount: 40, address: "", height: 1) + change.walletId = walletId + tx.outputs = [change] + tx.pendingInputs = [PersistentPendingInput( + outpoint: Data(repeating: 9, count: 36), inputIndex: 0, + spendingTxid: tx.txid, spendingTransaction: tx, walletId: walletId + )] + XCTAssertEqual(tx.netAmount(for: walletId), 40, "the sole wallet's stored amount is Rust's net_amount") + } + + func testShouldNotComputeSharedAmountWhileOwnInputsArePending() { + let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) + let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) + let toA = PersistentTxo(transaction: tx, vout: 0, amount: 40, address: "", height: 1) + toA.walletId = walletA + let toB = PersistentTxo(transaction: tx, vout: 1, amount: 60, address: "", height: 1) + toB.walletId = walletB + tx.outputs = [toA, toB] + tx.pendingInputs = [PersistentPendingInput( + outpoint: Data(repeating: 9, count: 36), inputIndex: 0, + spendingTxid: tx.txid, spendingTransaction: tx, walletId: walletA + )] + XCTAssertNil(tx.netAmount(for: walletA), "an unlinked input A recorded may be A's own coin") + XCTAssertEqual(tx.netAmount(for: walletB), 60) + } + + func testShouldKeepIncomingAmountWhenInputsAreForeign() throws { + let container = try DashModelContainer.createInMemory() + let walletId = Data(repeating: 1, count: 32) + container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) + try container.mainContext.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let paymentId = Data(repeating: 3, count: 32) + let foreign = Data(repeating: 2, count: 32) + persist(handler, walletId: walletId, txid: paymentId, + bytes: serializedSpend(inputs: [foreign]), net: 40, + inputTxids: [foreign], outputs: [(paymentId, 40)]) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == paymentId }) + XCTAssertEqual(row.netAmount, 40) + XCTAssertEqual(row.netAmount(for: walletId), 40, "a foreign input is not an unresolved input of ours") + } + + private func localTransfer(in context: ModelContext, assetLock: Bool = false) throws -> PersistentTransaction { + let walletA = PersistentWallet(walletId: Data(repeating: 1, count: 32), network: .testnet) + let walletB = PersistentWallet(walletId: Data(repeating: 2, count: 32), network: .testnet) + let accountA = PersistentAccount(wallet: walletA, accountType: 0, accountIndex: 0, accountTypeName: "Standard") + let accountB = PersistentAccount(wallet: walletB, accountType: 0, accountIndex: 0, accountTypeName: "Standard") + context.insert(walletA) + context.insert(walletB) + context.insert(accountA) + context.insert(accountB) + let tx = PersistentTransaction( + txid: Data(repeating: 3, count: 32), + transactionData: serializedSpend(inputs: [walletA.walletId], outputValue: 90), + direction: CoreDirectionCode.internalTransfer, netAmount: -10 + ) + tx.transactionTypeKind = assetLock ? TransactionTypeKind.assetLock.rawValue : TransactionTypeKind.standard.rawValue + let coin = input(100) + coin.account = accountA + coin.isSpent = true + coin.spendingTransaction = tx + let credit = PersistentTxo(transaction: tx, vout: 0, amount: 90, address: "", height: 1) + credit.walletId = walletB.walletId + credit.account = accountB + context.insert(tx) + context.insert(coin) + context.insert(credit) + tx.involvedAccounts = [accountA, accountB] + try context.save() + return tx + } + + func testShouldKeepSurvivingWalletAccountingAfterDeletingEitherTransferWallet() throws { + for deletedWallet in [UInt8(1), UInt8(2)] { + let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let url = directory.appendingPathComponent("wallet.store") + let survivor = Data(repeating: deletedWallet == 1 ? 2 : 1, count: 32) + let amount: Int64 = deletedWallet == 1 ? 90 : -100 + let direction = deletedWallet == 1 ? CoreDirectionCode.incoming : CoreDirectionCode.outgoing + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + _ = try localTransfer(in: container.mainContext) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + try handler.deleteWalletData(walletId: Data(repeating: deletedWallet, count: 32)) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == Data(repeating: 3, count: 32) }) + XCTAssertEqual(row.netAmount(for: survivor), amount) + XCTAssertEqual(row.direction(for: survivor), direction) + } + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == Data(repeating: 3, count: 32) }) + XCTAssertEqual(row.netAmount(for: survivor), amount) + XCTAssertEqual(row.direction(for: survivor), direction) + } + } + } + + func testShouldKeepUnavailableAmountAfterDeletingAnotherParticipant() throws { + let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let url = directory.appendingPathComponent("wallet.store") + let survivor = Data(repeating: 2, count: 32) + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let tx = try localTransfer(in: container.mainContext, assetLock: true) + let foreign = Data(repeating: 9, count: 32) + tx.transactionData = serializedSpend(inputs: [Data(repeating: 1, count: 32), foreign], outputValue: 90) + tx.netAmount = 0 + let pending = PersistentPendingInput( + outpoint: PersistentTxo.makeOutpoint(txid: foreign, vout: 0), inputIndex: 1, + spendingTxid: tx.txid, spendingTransaction: tx, walletId: survivor + ) + container.mainContext.insert(pending) + try container.mainContext.save() + XCTAssertNil(tx.netAmount(for: survivor)) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + try handler.deleteWalletData(walletId: Data(repeating: 1, count: 32)) + // An empty asset-lock recovery update must not invent accounting. + persist(handler, walletId: survivor, txid: tx.txid, bytes: tx.transactionData, kind: 6) + } + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == Data(repeating: 3, count: 32) }) + XCTAssertNil(row.netAmount(for: survivor)) + XCTAssertEqual(row.formattedAmount(for: survivor), "Amount unavailable") + // A new authoritative wallet record can make the amount available. + persist(handler, walletId: survivor, txid: row.txid, bytes: row.transactionData, net: 90, kind: 6) + let updated = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == row.txid }) + XCTAssertEqual(updated.netAmount(for: survivor), 90) + } + } + + func testShouldScopeSharedAssetLockDirectionBeforeAndAfterReconciliation() throws { + let container = try DashModelContainer.createInMemory() + let tx = try localTransfer(in: container.mainContext, assetLock: true) + let walletA = Data(repeating: 1, count: 32) + let walletB = Data(repeating: 2, count: 32) + // A's Rust record is outgoing, while reconciliation sees both local wallets. + tx.direction = CoreDirectionCode.outgoing + XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.outgoing) + XCTAssertEqual(tx.direction(for: walletB), CoreDirectionCode.incoming) + try container.mainContext.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == tx.txid }) + XCTAssertEqual(row.direction, CoreDirectionCode.internalTransfer) + XCTAssertEqual(row.direction(for: walletA), CoreDirectionCode.outgoing) + XCTAssertEqual(row.direction(for: walletB), CoreDirectionCode.incoming) + } + + func testShouldKeepOwnAssetLockConversionInternalWithAnotherKeysOnlyParticipant() throws { + let container = try DashModelContainer.createInMemory() + let tx = try localTransfer(in: container.mainContext, assetLock: true) + let walletA = Data(repeating: 1, count: 32) + let change = try XCTUnwrap(tx.outputs.first) + change.walletId = walletA + change.account = tx.inputs.first?.account + XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.internalTransfer) + XCTAssertEqual(tx.direction(for: Data(repeating: 2, count: 32)), CoreDirectionCode.internalTransfer) + // A no-change conversion remains internal too. + container.mainContext.delete(change) + tx.transactionData = serializedSpend(inputs: [walletA], outputValue: 90, burn: true) + try container.mainContext.save() + XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.internalTransfer) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == tx.txid }) + XCTAssertEqual(row.direction(for: walletA), CoreDirectionCode.internalTransfer) + } + + func testShouldScopeDirectionAndAmountToEachWalletOfALocalTransfer() { + let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) + let (amount, fee): (UInt64, UInt64) = (90, 10) + let coin = input(amount + fee, wallet: 1) + // Whichever wallet recorded last owns the stored scalars; here the sender. + let tx = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: Data(), + direction: CoreDirectionCode.outgoing, netAmount: -Int64(amount + fee) + ) + let credit = PersistentTxo(transaction: tx, vout: 0, amount: amount, address: "", height: 1) + credit.walletId = walletB + tx.inputs = [coin] + tx.outputs = [credit] + XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.outgoing) + XCTAssertEqual(tx.direction(for: walletB), CoreDirectionCode.incoming) + XCTAssertEqual(tx.netAmount(for: walletA), -Int64(amount + fee)) + XCTAssertEqual(tx.netAmount(for: walletB), Int64(amount)) + XCTAssertEqual(tx.formattedAmount(for: walletB), "+0.00000090 DASH") + } + + func testShouldReportAmountUnavailableOnlyWhenNoWalletAccountingApplies() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) + func makeAccount(of walletId: Data) -> PersistentAccount { + let wallet = PersistentWallet(walletId: walletId, network: .testnet) + let account = PersistentAccount( + wallet: wallet, accountType: 0, accountIndex: 0, accountTypeName: "Standard BIP44 Account" + ) + context.insert(wallet) + context.insert(account) + return account + } + let (accountA, accountB) = (makeAccount(of: walletA), makeAccount(of: walletB)) + // Payload-only (no linked TXO), e.g. a provider tx matched by key. + let payloadOnly = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data()) + let shared = PersistentTransaction(txid: Data(repeating: 4, count: 32), transactionData: Data(), netAmount: 40) + let unrecorded = PersistentTransaction(txid: Data(repeating: 5, count: 32), transactionData: Data(), netAmount: 40) + context.insert(payloadOnly) + context.insert(shared) + context.insert(unrecorded) + payloadOnly.involvedAccounts = [accountA] + shared.involvedAccounts = [accountA, accountB] + try context.save() + + XCTAssertEqual(payloadOnly.netAmount(for: walletA), 0, "the sole recorder's stored amount stands") + XCTAssertEqual(payloadOnly.formattedAmount(for: walletA), "+0.00000000 DASH") + XCTAssertNil(shared.netAmount(for: walletA), "the stored scalar may be the other recorder's") + XCTAssertNil(shared.netAmount(for: walletB)) + XCTAssertEqual(shared.formattedAmount(for: walletA), "Amount unavailable") + XCTAssertNil(unrecorded.netAmount(for: walletA), "a wallet with no stake has no amount") + } + + func testShouldNotCountAnotherLocalWalletsUnlinkedOutputForTheSender() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let senderId = Data(repeating: 1, count: 32) + let receiver = PersistentWallet(walletId: Data(repeating: 2, count: 32), network: .testnet) + let receiverAccount = PersistentAccount( + wallet: receiver, accountType: 0, accountIndex: 0, accountTypeName: "Standard BIP44 Account" + ) + // P2PKH to pubkey hash 0x05 x 20 on testnet: B's address with no TXO row yet. + let receiverAddress = PersistentCoreAddress( + address: "yLmzEvw3frCPS4cyRmFFeKbt64fUPzMwFh", poolTypeTag: 0, addressIndex: 0, derivationPath: "" + ) + receiverAddress.account = receiverAccount + context.insert(PersistentWallet(walletId: senderId, network: .testnet)) + context.insert(receiver) + context.insert(receiverAccount) + context.insert(receiverAddress) + var bytes = Data([2, 0, 0, 0, 1]) + bytes.append(senderId) + bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255, 1]) + withUnsafeBytes(of: UInt64(40).littleEndian) { bytes.append(contentsOf: $0) } + bytes.append(contentsOf: [25, 0x76, 0xa9, 0x14] + [UInt8](repeating: 5, count: 20) + [0x88, 0xac]) + bytes.append(contentsOf: [0, 0, 0, 0]) + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: bytes, direction: CoreDirectionCode.outgoing, netAmount: -100 + ) + let coin = input(100) + coin.spendingTransaction = spender + context.insert(coin) + context.insert(spender) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) + XCTAssertEqual(row.netAmount, -100, "the receiving wallet's credit is not the sender's") + XCTAssertEqual(row.netAmount(for: senderId), -100) + } + + func testShouldExcludePersistedContactOutputsFromOwnedAccounting() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + let wallet = PersistentWallet(walletId: walletId, network: .testnet) + let contactAccount = PersistentAccount( + wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, + accountIndex: 0, accountTypeName: "DashPay External Account" + ) + context.insert(wallet) + context.insert(contactAccount) + let coin = input(100) + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), + direction: CoreDirectionCode.internalTransfer, netAmount: -60 + ) + coin.spendingTransaction = spender + let contactOutput = PersistentTxo(transaction: spender, vout: 0, amount: 40, address: "", height: 1) + contactOutput.walletId = walletId + contactOutput.account = contactAccount + context.insert(coin) + context.insert(spender) + context.insert(contactOutput) + try context.save() + XCTAssertEqual(spender.netAmount(for: walletId), -100) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) + XCTAssertEqual(row.netAmount, -100) + XCTAssertEqual(row.direction, 1) + XCTAssertEqual(row.netAmount(for: walletId), -100) + } + + func testShouldNotTreatPersistedContactInputAsOurFunding() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + let wallet = PersistentWallet(walletId: walletId, network: .testnet) + let contactAccount = PersistentAccount( + wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, + accountIndex: 0, accountTypeName: "DashPay External Account" + ) + context.insert(wallet) + context.insert(contactAccount) + let coin = input(100) + coin.account = contactAccount + let spender = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), + direction: CoreDirectionCode.incoming, netAmount: 40 + ) + coin.spendingTransaction = spender + let received = PersistentTxo(transaction: spender, vout: 0, amount: 40, address: "", height: 1) + received.walletId = walletId + context.insert(coin) + context.insert(spender) + context.insert(received) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) + XCTAssertEqual(row.netAmount, 40) + XCTAssertEqual(row.direction, 0) + XCTAssertEqual(row.netAmount(for: walletId), 40) + } + +} From 4413d3246211c11a36a89122567558ff8ad66007 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:53:54 +0000 Subject: [PATCH 02/21] fix(swift-sdk): report a received amount despite a contact's watch-only output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `netAmount(for:)` treated a DashPay contact's watch-only TXO as a reason to distrust the sole participant's stored amount, fell through to the TXO-computed path, and there a pending foreign input made it answer `nil` ("Amount unavailable") although the wallet's own output gave the amount. The stored amount of such a row is still not trusted on its own: rows with a contact TXO predate the Rust filter that keeps a contact's record from defining the transaction row. The computed amount now stands despite a pending input when the wallet is the sole participant, every unowned TXO is a contact's, and the stored amount agrees with it. A stored amount the wallet's outputs do not confirm keeps answering `nil`, since the pending input may then be an unlinked coin of the wallet. `isContactTxo` names the contact predicate `isWalletOwnedTxo` already applied; its result is unchanged. 🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PSi8cX229kW2fmLxKW6ir4 --- .../Models/PersistentTransaction.swift | 27 +++++++++----- .../PlatformWalletPersistenceHandler.swift | 10 +++-- .../TransactionAccountingTests.swift | 37 +++++++++++++++++++ 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index a27fb766d11..cc84271d68f 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -248,24 +248,31 @@ public final class PersistentTransaction { && seen.insert($0.outpoint).inserted } } - let hasUnownedTxos = (inputs + outputs).contains { !PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) } - if participatingWalletIds == [walletId], !hasUnownedTxos { + let unownedTxos = (inputs + outputs).filter { !PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) } + let isSoleParticipant = participatingWalletIds == [walletId] + if isSoleParticipant, unownedTxos.isEmpty { return netAmountUnavailable == true ? nil : netAmount } - // Computed from TXOs alone: a pending input this wallet recorded may be - // one of its own still-unlinked coins, so the sum is only provisional. - // TODO(wallet-scoped-accounting-from-rust): a foreign payment to two - // local wallets leaves an unresolvable pending input per wallet, so both - // show "Amount unavailable". Store Rust's per-wallet net amount instead - // (tracked in #5226). - guard !pendingInputs.contains(where: { $0.walletId == walletId }) else { return nil } let walletInputs = owned(inputs) let walletOutputs = owned(outputs) guard !walletInputs.isEmpty || !walletOutputs.isEmpty else { return nil } - return Self.reconciledAccounting( + let computed = Self.reconciledAccounting( inputs: walletInputs, ownedOutputAmounts: walletOutputs.map(\.amount), allOutputsOwned: false, previousDirection: direction, isAssetLock: isAssetLock )?.netAmount + // Computed from TXOs alone: a pending input this wallet recorded may be + // one of its own still-unlinked coins, so the sum is only provisional. + // It stands when only a contact's watch-only TXOs kept the sole + // participant off its stored amount and that amount agrees with it. + // TODO(wallet-scoped-accounting-from-rust): a foreign payment to two + // local wallets leaves an unresolvable pending input per wallet, so both + // show "Amount unavailable". Store Rust's per-wallet net amount instead + // (tracked in #5226). + if pendingInputs.contains(where: { $0.walletId == walletId }) { + guard isSoleParticipant, netAmountUnavailable != true, computed == netAmount, + unownedTxos.allSatisfy(PlatformWalletPersistenceHandler.isContactTxo) else { return nil } + } + return computed } /// Direction relative to one wallet for transactions shared by multiple local wallets. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index f48f1614e63..efddb7e5f59 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -107,9 +107,13 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// Contact watch-only TXOs do not belong to the wallet tracking their addresses. static func isWalletOwnedTxo(_ txo: PersistentTxo) -> Bool { - resolvedWalletId(of: txo) != nil - && txo.account?.accountType != dashpayExternalAccountTypeTag - && txo.coreAddress?.account?.accountType != dashpayExternalAccountTypeTag + resolvedWalletId(of: txo) != nil && !isContactTxo(txo) + } + + /// A TXO on a contact's watch-only chain, by its account or its address's. + static func isContactTxo(_ txo: PersistentTxo) -> Bool { + txo.account?.accountType == dashpayExternalAccountTypeTag + || txo.coreAddress?.account?.accountType == dashpayExternalAccountTypeTag } /// Network of the account's wallet, read through the same Optional cast diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift index 6fc27f08526..358235b6f7e 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -692,4 +692,41 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertEqual(row.netAmount(for: walletId), 40) } + func testShouldReportReceivedAmountDespiteContactOutputWhenStoredAmountAgrees() throws { + let container = try DashModelContainer.createInMemory() + let context = container.mainContext + let walletId = Data(repeating: 1, count: 32) + let wallet = PersistentWallet(walletId: walletId, network: .testnet) + let contactAccount = PersistentAccount( + wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, + accountIndex: 0, accountTypeName: "DashPay External Account" + ) + context.insert(wallet) + context.insert(contactAccount) + let payment = PersistentTransaction( + txid: Data(repeating: 3, count: 32), transactionData: Data(), + direction: CoreDirectionCode.incoming, netAmount: 40 + ) + let received = PersistentTxo(transaction: payment, vout: 0, amount: 40, address: "", height: 1) + received.walletId = walletId + let contactOutput = PersistentTxo(transaction: payment, vout: 1, amount: 60, address: "", height: 1) + contactOutput.walletId = walletId + contactOutput.account = contactAccount + context.insert(payment) + context.insert(received) + context.insert(contactOutput) + // The foreign input: every prevout with no local TXO is recorded as pending. + context.insert(PersistentPendingInput( + outpoint: Data(repeating: 9, count: 36), inputIndex: 0, + spendingTxid: payment.txid, spendingTransaction: payment, walletId: walletId + )) + try context.save() + XCTAssertEqual( + payment.netAmount(for: walletId), 40, "a contact's output does not make the received amount unknown" + ) + // A stored amount the wallet's own outputs do not confirm may hide an unlinked coin of ours. + payment.netAmount = -60 + XCTAssertNil(payment.netAmount(for: walletId)) + } + } From 7a282c927319075458cf02167471e568747a7f9b Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:54:20 +0000 Subject: [PATCH 03/21] fix(swift-sdk): scope linked outputs to the spending wallets in reconciliation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `reconcileTransactionAccounting` counted every locally owned linked output as received, whichever wallet owned it, while the address fallback below it counted an unlinked output only for the spending wallets. When local wallet A spent 100 and local wallet B received 90 in one transaction, B's credit entered the stored scalar: -10 and "internal" instead of A's -100 and "outgoing". Linked outputs are now scoped by their wallet the same way. The stored scalar of a transfer between two local wallets is therefore the sender's; `testShouldScopeSharedAssetLockDirectionBeforeAndAfterReconciliation` pinned the old "internal" and is updated. Per-wallet answers (`netAmount(for:)`, `direction(for:)`) are unchanged: with two participants they never read the stored scalar. 🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PSi8cX229kW2fmLxKW6ir4 --- .../PlatformWalletPersistenceHandler.swift | 10 +++++++--- .../TransactionAccountingTests.swift | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index efddb7e5f59..ed99dee0327 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -7010,11 +7010,15 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { guard complete else { continue } var amounts: [UInt64] = [] var allOutputsOwned = true - let ownedVouts = Set(transaction.outputs.filter { !$0.isDeleted && Self.isWalletOwnedTxo($0) }.map(\.vout)) - // An address-matched output with no linked TXO carries no wallet of - // its own, so it counts only for the spending wallets: another local + // An output counts only for the spending wallets — a linked TXO by + // its wallet, an address-matched one by its address's: another local // wallet's credit must not hide inside the sender's scalar. let spendingWallets = Set(inputs.compactMap { Self.resolvedWalletId(of: $0) }) + let ownedVouts = Set(transaction.outputs.filter { output -> Bool in + guard !output.isDeleted, Self.isWalletOwnedTxo(output), + let owner = Self.resolvedWalletId(of: output) else { return false } + return spendingWallets.contains(owner) + }.map(\.vout)) for (index, output) in decoded.outputs.enumerated() { // OP_RETURN burns (including asset locks) are not spendable Core outputs. if output.isOpReturn { continue } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift index 358235b6f7e..4686f2abc39 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -501,7 +501,7 @@ final class TransactionAccountingTests: XCTestCase { let tx = try localTransfer(in: container.mainContext, assetLock: true) let walletA = Data(repeating: 1, count: 32) let walletB = Data(repeating: 2, count: 32) - // A's Rust record is outgoing, while reconciliation sees both local wallets. + // A's Rust record is outgoing, and reconciliation keeps the stored scalar the sender's. tx.direction = CoreDirectionCode.outgoing XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.outgoing) XCTAssertEqual(tx.direction(for: walletB), CoreDirectionCode.incoming) @@ -510,7 +510,7 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertFalse(handler.loadWalletList().errored) let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) .first { $0.txid == tx.txid }) - XCTAssertEqual(row.direction, CoreDirectionCode.internalTransfer) + XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) XCTAssertEqual(row.direction(for: walletA), CoreDirectionCode.outgoing) XCTAssertEqual(row.direction(for: walletB), CoreDirectionCode.incoming) } @@ -627,6 +627,19 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertEqual(row.netAmount(for: senderId), -100) } + func testShouldNotCountAnotherLocalWalletsLinkedOutputForTheSender() throws { + let container = try DashModelContainer.createInMemory() + let tx = try localTransfer(in: container.mainContext) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertFalse(handler.loadWalletList().errored) + let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) + .first { $0.txid == tx.txid }) + XCTAssertEqual(row.netAmount, -100, "the receiving wallet's linked credit is not the sender's") + XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) + XCTAssertEqual(row.netAmount(for: Data(repeating: 1, count: 32)), -100) + XCTAssertEqual(row.netAmount(for: Data(repeating: 2, count: 32)), 90) + } + func testShouldExcludePersistedContactOutputsFromOwnedAccounting() throws { let container = try DashModelContainer.createInMemory() let context = container.mainContext From 70a1b30c3adf44d093300c7d3cf18ccd85dca8ce Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:55:21 +0000 Subject: [PATCH 04/21] fix(swift-sdk): reconcile accounting when a TXO heal completes a spend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `drainPendingInputs` marks the healed coin's parent and its adopted spender in `accountingDirty`, but only `endChangeset` consumed that dictionary. `reconcileHealMissingTxos` runs outside a round and commits through `reconcileSave`, so a healed prevout repaired the TXO graph and left the spender's `netAmount` and `direction` stale until the next load-time pass. The heal step now reconciles the transactions it marked before its save, so the accounting commits with the coin and a failed save rolls both back. The step clears `accountingDirty` when it starts, because entries an earlier out-of-round writer left may point at rolled-back rows, and again once it has consumed them. Accounting is display-only: a failure is logged as `persistence_transaction_accounting_failed` with `phase=txo_reconcile_heal` and does not fail the heal. That catch has no fault-injection test yet (`TODO(test-heal-accounting-failure)`). 🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PSi8cX229kW2fmLxKW6ir4 --- .../PlatformWalletPersistenceHandler.swift | 26 +++++++++-- .../CoreTxoReconcileTests.swift | 43 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index ed99dee0327..2f50466e4a2 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -3313,10 +3313,10 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { self.inChangeset = true self.roundUtxoCreditVerdicts = [:] self.roundUtxoCreditTally = UtxoCreditVerdictTally() - // Out-of-round writers (heal paths, deferred backfills) stage - // entries too; they are not this round's to reconcile and may - // point at rows a failed save rolled back. Load-time accounting - // repairs whatever they touched. + // Out-of-round writers stage entries too (deferred backfills, a + // heal step that stopped before reconciling its own); they are + // not this round's to reconcile and may point at rows a failed + // save rolled back. Load-time accounting repairs whatever is left. self.accountingDirty.removeAll() SDKLogger.event( "persistence_changeset_started", @@ -11523,6 +11523,9 @@ extension PlatformWalletPersistenceHandler { ) -> CoreTxoReconcileStep { onQueue { guard !inChangeset else { return .retryLater } + // This step reconciles only what it stages: entries left by an + // earlier out-of-round writer may point at rolled-back rows. + accountingDirty.removeAll() var counts = CoreTxoHealCounts() for row in rows { guard row.txid.count == 32, !row.scriptPubKey.isEmpty, !row.address.isEmpty else { @@ -11622,6 +11625,21 @@ extension PlatformWalletPersistenceHandler { ] ) } + // A healed prevout can complete a spend, so the accounting it + // resolves commits in the same save. Display-only: a failure is + // reported under its own event and the heal still lands. + // TODO(test-heal-accounting-failure): cover this catch with a + // FetchFaultInjector test; pinning which read faults needs a Swift run. + do { + try reconcileTransactionAccounting(Array(accountingDirty.values)) + } catch { + SDKLogger.event( + "persistence_transaction_accounting_failed", category: .persistence, severity: .error, + fields: ["phase": .publicText("txo_reconcile_heal"), "wallet_reference": .reference(walletId)], + error: error + ) + } + accountingDirty.removeAll() guard counts.inserted + counts.healedSpent == 0 || reconcileSave(operation: "txo_reconcile_heal", walletId: walletId) else { diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift index eac6a57af07..d8688d49aba 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift @@ -467,6 +467,49 @@ final class CoreTxoReconcileTests: XCTestCase { XCTAssertEqual(try restoredUtxoCount(handler), 0) } + /// A healed coin can be the prevout a stored spender was missing. The + /// heal's own save then repairs that spender's amount and direction — + /// they do not stay stale until the next launch's load-time pass. + func testAHealedPrevoutRepairsItsSpendersAccounting() throws { + let (handler, container) = try makeHandler() + try seedWallet(in: container) + let coinTxid = txid(0x86) + let spenderTxid = txid(0x87) + // One input spending the missing coin, one output the wallet does not own. + var bytes = Data([2, 0, 0, 0, 1]) + bytes.append(coinTxid) + bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255, 1]) + withUnsafeBytes(of: UInt64(40).littleEndian) { bytes.append(contentsOf: $0) } + bytes.append(contentsOf: [0, 0, 0, 0, 0]) + let context = ModelContext(container) + // Stored as an incoming payment: what history showed while the coin was missing. + let spender = PersistentTransaction( + txid: spenderTxid, transactionData: bytes, context: 3, blockHeight: 2_391_800, + direction: CoreDirectionCode.incoming, netAmount: 40 + ) + context.insert(spender) + context.insert(PersistentPendingInput( + outpoint: PersistentTxo.makeOutpoint(txid: coinTxid, vout: 0), + inputIndex: 0, + spendingTxid: spenderTxid, + spendingTransaction: spender, + walletId: walletId + )) + try context.save() + let engine = FakeCoreTxoEngine(inventory: [engineUtxo(txid: coinTxid)]) + + let report = run(handler, engine: engine) + + XCTAssertTrue(report.completed) + XCTAssertEqual(report.healedSpent, 1) + // Read without a load in between: the load-time pass would repair it too. + let row = try XCTUnwrap( + ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderTxid } + ) + XCTAssertEqual(row.netAmount, -19_549, "the healed coin is the spender's only input") + XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) + } + /// A store read that fails inside the heal pass is a failure, not a /// skip: the account lookup cannot tell "no such account" from "could /// not read", so it must not answer the former when the latter From 018f00fdb3bc3acf70abb07dd338e61dde94238f Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:16:46 +0000 Subject: [PATCH 05/21] fix(swift-sdk): preserve base V4 schema before accounting migration Freeze the complete base V4 graph and migrate accounting availability through V5. Keep historical routes and the legacy bridge intact. Co-Authored-By: OpenAI Codex --- packages/swift-sdk/SCHEMA_RELEASES.md | 14 +- .../Persistence/DashModelContainer.swift | 30 +- ...shSchemaSnapshotV4+PersistentAccount.swift | 78 ++++ ...SchemaSnapshotV4+PersistentAssetLock.swift | 106 +++++ ...hemaSnapshotV4+PersistentCoreAddress.swift | 68 +++ ...hSchemaSnapshotV4+PersistentDPNSName.swift | 132 ++++++ ...otV4+PersistentDashpayContactProfile.swift | 97 +++++ ...otV4+PersistentDashpayContactRequest.swift | 118 +++++ ...hotV4+PersistentDashpayIgnoredSender.swift | 67 +++ ...aSnapshotV4+PersistentDashpayPayment.swift | 99 +++++ ...aSnapshotV4+PersistentDashpayProfile.swift | 70 +++ ...emaSnapshotV4+PersistentDataContract.swift | 286 +++++++++++++ ...hSchemaSnapshotV4+PersistentDocument.swift | 181 ++++++++ ...emaSnapshotV4+PersistentDocumentType.swift | 154 +++++++ ...hSchemaSnapshotV4+PersistentIdentity.swift | 304 +++++++++++++ ...V4+PersistentIdentityBalanceMetadata.swift | 32 ++ ...DashSchemaSnapshotV4+PersistentIndex.swift | 113 +++++ ...chemaSnapshotV4+PersistentInvitation.swift | 73 ++++ ...shSchemaSnapshotV4+PersistentKeyword.swift | 40 ++ ...chemaSnapshotV4+PersistentMasternode.swift | 185 ++++++++ ...emaSnapshotV4+PersistentPendingInput.swift | 46 ++ ...SnapshotV4+PersistentPlatformAddress.swift | 78 ++++ ...PersistentPlatformAddressesSyncState.swift | 41 ++ ...hSchemaSnapshotV4+PersistentProperty.swift | 55 +++ ...SchemaSnapshotV4+PersistentPublicKey.swift | 218 ++++++++++ ...napshotV4+PersistentShieldedActivity.swift | 89 ++++ ...emaSnapshotV4+PersistentShieldedNote.swift | 66 +++ ...hotV4+PersistentShieldedOutgoingNote.swift | 49 +++ ...apshotV4+PersistentShieldedSyncState.swift | 34 ++ ...pshotV4+PersistentShieldedViewingKey.swift | 34 ++ ...DashSchemaSnapshotV4+PersistentToken.swift | 403 ++++++++++++++++++ ...emaSnapshotV4+PersistentTokenBalance.swift | 198 +++++++++ ...apshotV4+PersistentTokenHistoryEvent.swift | 112 +++++ ...apshotV4+PersistentTrackedMasternode.swift | 42 ++ ...hemaSnapshotV4+PersistentTransaction.swift | 167 ++++++++ .../DashSchemaSnapshotV4+PersistentTxo.swift | 99 +++++ ...ashSchemaSnapshotV4+PersistentWallet.swift | 95 +++++ ...otV4+PersistentWalletManagerMetadata.swift | 34 ++ .../DashSchemaSnapshotV4+Schema.swift | 47 ++ .../DashSchemaSnapshotV4+TokenTypes.swift | 165 +++++++ .../DashLegacySchemaMigrationTests.swift | 13 +- .../DashModelMigrationTests.swift | 12 +- .../DashReleasedSchemaTests.swift | 20 +- .../swift-sdk/scripts/freeze_schema_models.py | 16 +- .../scripts/test_freeze_schema_models.py | 2 +- 45 files changed, 4345 insertions(+), 37 deletions(-) create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAccount.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAssetLock.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentCoreAddress.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDPNSName.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactProfile.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactRequest.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayIgnoredSender.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayPayment.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayProfile.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDataContract.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocument.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocumentType.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentity.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentityBalanceMetadata.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIndex.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentInvitation.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentKeyword.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentMasternode.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPendingInput.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddress.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddressesSyncState.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentProperty.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPublicKey.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedActivity.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedNote.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedOutgoingNote.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedSyncState.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedViewingKey.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentToken.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenBalance.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenHistoryEvent.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTrackedMasternode.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTransaction.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTxo.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWallet.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWalletManagerMetadata.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+Schema.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+TokenTypes.swift diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index 5d3fd2e4dc8..6a8aef72f51 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -5,7 +5,7 @@ distribution. TestFlight uploads capture provenance and a synthetic SQLite fixture, but do not by themselves register a released schema. The accepted frozen V1 remains unchanged. Historical V2 is now reconstructed from `52e8d4ec68f0c772313fa1bbef223fb1eabbf1cc`; all 35 entity hashes and the model checksum match the observed App Store 9.0.2 database. Active models are -V4. The released V3 graph is preserved by `DashSchemaSnapshotV3`. Other +V5. The merged V4 graph is preserved by `DashSchemaSnapshotV4`. The released V3 graph is preserved by `DashSchemaSnapshotV3`. Other intermediate development shapes remain unsupported. The old V2 fixture was generated from September 8 sources containing 13 @@ -16,15 +16,15 @@ separate from archive-captured releases. This identifies a matching model source, not the confirmed build commit of Apple's binary. V2 is reserved: automated release capture must not register another shape under that number. -The main migration plan is historical V2 → V3 → V4. Accepted V1 has a separate -V1 → V4 plan: V1 already contains the 13 properties missing from historical +The main migration plan is historical V2 → V3 → V4 → V5. Accepted V1 has a separate +V1 → V3 → V4 → V5 plan: V1 already contains the 13 properties missing from historical V2, so a chain through V2 could discard values. Routing uses model metadata and runs after recovery; a version label alone never selects an unknown beta schema. The former live V2 is accepted only when its complete graph matches frozen V3 exactly, entity hashes and checksum alike. The alias remains supported when the live graph changes; other stores labelled `2.0.0` fail closed. -V4 adds the optional `PersistentTransaction.netAmountUnavailable` marker through +V5 adds the optional `PersistentTransaction.netAmountUnavailable` marker through a lightweight migration. Existing rows receive `nil`, preserving their stored accounting. When deleting one participant of a shared transaction, the remaining wallet's amount and direction are saved before the ownership links disappear. @@ -33,11 +33,11 @@ an authoritative wallet record or complete accounting reconciliation clears it. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, -`accepted-v1-to-v4`, `historical-v2-to-v4`, `previous-live-v2-v3-shape`, -`published-v3-to-v4`, `unsupported-v2`, `labelled-current-v4`, `ordinary-current-plan` or, from the +`accepted-v1-to-v5`, `historical-v2-to-v5`, `previous-live-v2-v3-shape`, +`published-v3-to-v5`, `published-v4-to-v5`, `unsupported-v2`, `labelled-current-v5`, `ordinary-current-plan` or, from the bridge, `legacy-v1-bridge-to-v3`. Resolving a frozen schema's identity builds a temporary store, so it is memoized per process and consulted only where a -label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0` or `4.0.0` label takes the +label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0`, `4.0.0` or `5.0.0` label takes the default plan without any probe, and a probe failure on the undecidable labels refuses the open with the probe's own error while leaving the store untouched. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index 58be6921bd4..3614c6633cd 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -100,7 +100,7 @@ public enum DashModelContainer { /// Create the schema for all Dash Platform models public static var schema: Schema { - Schema(versionedSchema: DashSchemaV4.self) + Schema(versionedSchema: DashSchemaV5.self) } /// Create a persistent model container for storing data. @@ -247,7 +247,7 @@ public enum DashModelContainer { // probe stays fatal here: the open fails with the probe's own error // instead of an inapplicable plan, and the store is untouched. if versions == ["1.0.0"], try matches(DashSchemaV1.self) { - logRoute("accepted-v1-to-v4") + logRoute("accepted-v1-to-v5") return DashAcceptedV1MigrationPlan.self } if versions == ["2.0.0"] { @@ -261,14 +261,16 @@ public enum DashModelContainer { throw DashLegacyStoreSQLite.Failure.unsupported( "The database identifies itself as schema 2.0.0 but its model does not match the supported historical or current schema. The original database has not been replaced. Contact support; do not delete the app.") } - logRoute(historical ? "historical-v2-to-v4" : "previous-live-v2-v3-shape") + logRoute(historical ? "historical-v2-to-v5" : "previous-live-v2-v3-shape") } else if versions == ["3.0.0"] { - logRoute("published-v3-to-v4") + logRoute("published-v3-to-v5") } else if versions == ["4.0.0"] { + logRoute("published-v4-to-v5") + } else if versions == ["5.0.0"] { // Same plan either way. Opening a current store must neither wait // on nor fail with a schema probe that could only refine this line; // `source_checksum` above already identifies the exact graph. - logRoute("labelled-current-v4") + logRoute("labelled-current-v5") } else { logRoute("ordinary-current-plan") } @@ -289,13 +291,14 @@ public enum DashModelContainer { /// SwiftData migration plan for Dash Platform model updates public enum DashMigrationPlan: SchemaMigrationPlan { public static var schemas: [any VersionedSchema.Type] { - [DashSchemaV2.self, DashSchemaV3.self, DashSchemaV4.self] + [DashSchemaV2.self, DashSchemaV3.self, DashSchemaV4.self, DashSchemaV5.self] } public static var stages: [MigrationStage] { [ .lightweight(fromVersion: DashSchemaV2.self, toVersion: DashSchemaV3.self), - .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self) + .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self), + .lightweight(fromVersion: DashSchemaV4.self, toVersion: DashSchemaV5.self) ] } } @@ -303,10 +306,11 @@ public enum DashMigrationPlan: SchemaMigrationPlan { /// Separate compatibility route: V1 has fields missing from historical V2. /// Never insert V2 between this baseline and the current schema. enum DashAcceptedV1MigrationPlan: SchemaMigrationPlan { - static var schemas: [any VersionedSchema.Type] { [DashSchemaV1.self, DashSchemaV3.self, DashSchemaV4.self] } + static var schemas: [any VersionedSchema.Type] { [DashSchemaV1.self, DashSchemaV3.self, DashSchemaV4.self, DashSchemaV5.self] } static var stages: [MigrationStage] { [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV3.self), - .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self)] + .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self), + .lightweight(fromVersion: DashSchemaV4.self, toVersion: DashSchemaV5.self)] } } @@ -382,8 +386,14 @@ public enum DashSchemaV3: VersionedSchema { public static var models: [any PersistentModel.Type] { DashSchemaSnapshotV3.models } } -/// Current schema: optional drain operation IDs and transaction accounting availability. +/// Frozen base schema with optional drain operation IDs. public enum DashSchemaV4: VersionedSchema { public static var versionIdentifier: Schema.Version { Schema.Version(4, 0, 0) } + public static var models: [any PersistentModel.Type] { DashSchemaSnapshotV4.models } +} + +/// Current schema: optional transaction accounting availability. +public enum DashSchemaV5: VersionedSchema { + public static var versionIdentifier: Schema.Version { Schema.Version(5, 0, 0) } public static var models: [any PersistentModel.Type] { DashModelContainer.modelTypes } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAccount.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAccount.swift new file mode 100644 index 00000000000..986e433ce60 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAccount.swift @@ -0,0 +1,78 @@ +import Foundation +import SwiftData + +// `PersistentAccount` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentAccount { + #Unique([ + \.wallet, + \.accountType, + \.accountIndex, + \.standardTag, + \.registrationIndex, + \.keyClass, + \.userIdentityId, + \.friendIdentityId, + ]) + + var accountType: UInt32 + var accountIndex: UInt32 + var accountTypeName: String + var balanceConfirmed: UInt64 + var balanceUnconfirmed: UInt64 + var externalHighestUsed: Int32 + var internalHighestUsed: Int32 + var standardTag: UInt8 + var registrationIndex: UInt32 + var keyClass: UInt32 + var userIdentityId: Data + var friendIdentityId: Data + @Attribute(.unique) var accountExtendedPubKeyBytes: Data? + var createdAt: Date + var lastUpdated: Date + + var wallet: PersistentWallet + + @Relationship(deleteRule: .cascade, inverse: \PersistentCoreAddress.account) + var coreAddresses: [PersistentCoreAddress] + + @Relationship(deleteRule: .cascade, inverse: \PersistentPlatformAddress.account) + var platformAddresses: [PersistentPlatformAddress] + + var involvedTransactions: [PersistentTransaction] = [] + + init( + wallet: PersistentWallet, + accountType: UInt32, + accountIndex: UInt32, + accountTypeName: String + ) { + self.wallet = wallet + self.accountType = accountType + self.accountIndex = accountIndex + self.accountTypeName = accountTypeName + self.balanceConfirmed = 0 + self.balanceUnconfirmed = 0 + self.externalHighestUsed = -1 + self.internalHighestUsed = -1 + self.standardTag = 0 + self.registrationIndex = 0 + self.keyClass = 0 + self.userIdentityId = Data() + self.friendIdentityId = Data() + self.accountExtendedPubKeyBytes = nil + self.createdAt = Date() + self.lastUpdated = Date() + self.coreAddresses = [] + self.platformAddresses = [] + self.involvedTransactions = [] + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAssetLock.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAssetLock.swift new file mode 100644 index 00000000000..d5a66f33295 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentAssetLock.swift @@ -0,0 +1,106 @@ +import Foundation +import SwiftData + +// `PersistentAssetLock` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentAssetLock { + #Index([\.walletId]) + + @Attribute(.unique) var outPointHex: String + + var walletId: Data + + var transactionBytes: Data + + var fundingTypeRaw: Int + + var identityIndexRaw: Int32 + + var accountIndexRaw: Int32 = 0 + + var amountDuffs: Int64 + + var statusRaw: Int + + var proofBytes: Data? + + var recipientPlatformAddressHash: Data? + + var recipientPlatformAddressType: UInt8? + + var recipientIsExternal: Bool? + + var operationId: Data? + + var createdAt: Date + var updatedAt: Date + + init( + outPointHex: String, + walletId: Data, + transactionBytes: Data, + fundingTypeRaw: Int, + identityIndexRaw: Int32, + accountIndexRaw: Int32 = 0, + amountDuffs: Int64, + statusRaw: Int, + proofBytes: Data? = nil, + operationId: Data? = nil + ) { + self.outPointHex = outPointHex + self.walletId = walletId + self.transactionBytes = transactionBytes + self.fundingTypeRaw = fundingTypeRaw + self.identityIndexRaw = identityIndexRaw + self.accountIndexRaw = accountIndexRaw + self.amountDuffs = amountDuffs + self.statusRaw = statusRaw + self.proofBytes = proofBytes + self.operationId = operationId + self.createdAt = Date() + self.updatedAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentAssetLock { + static func predicate(walletId: Data) -> Predicate { + #Predicate { entry in + entry.walletId == walletId + } + } + + static func predicate( + walletId: Data, + identityIndex: UInt32 + ) -> Predicate { + let identityIndexRaw = Int32(bitPattern: identityIndex) + return #Predicate { entry in + entry.walletId == walletId && entry.identityIndexRaw == identityIndexRaw + } + } +} + +extension DashSchemaSnapshotV4.PersistentAssetLock { + static func encodeOutPoint(rawBytes: Data) -> String { + precondition(rawBytes.count == 36, "outpoint must be 36 bytes") + let txid = rawBytes.prefix(32) + let voutBytes = rawBytes.suffix(4) + let vout = voutBytes.withUnsafeBytes { raw -> UInt32 in + var value: UInt32 = 0 + withUnsafeMutableBytes(of: &value) { dst in + dst.copyBytes(from: raw.prefix(MemoryLayout.size)) + } + return UInt32(littleEndian: value) + } + let txidHex = txid.reversed().map { String(format: "%02x", $0) }.joined() + return "\(txidHex):\(vout)" + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentCoreAddress.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentCoreAddress.swift new file mode 100644 index 00000000000..3deef36953b --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentCoreAddress.swift @@ -0,0 +1,68 @@ +import Foundation +import SwiftData + +// `PersistentCoreAddress` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentCoreAddress { + @Attribute(.unique) var address: String + var publicKey: Data + var keyType: UInt8 = 0 + var poolTypeTag: UInt8 + var addressIndex: UInt32 + var derivationPath: String + var isUsed: Bool + var firstSeenHeight: UInt32 + var lastSeenHeight: UInt32 + var balance: UInt64 + var createdAt: Date + var lastUpdated: Date + + var account: PersistentAccount? + + @Relationship(deleteRule: .cascade, inverse: \PersistentTxo.coreAddress) + var txos: [PersistentTxo] = [] + + init( + address: String, + publicKey: Data = Data(), + keyType: UInt8 = 0, + poolTypeTag: UInt8, + addressIndex: UInt32, + derivationPath: String, + isUsed: Bool = false, + balance: UInt64 = 0 + ) { + self.address = address + self.publicKey = publicKey + self.keyType = keyType + self.poolTypeTag = poolTypeTag + self.addressIndex = addressIndex + self.derivationPath = derivationPath + self.isUsed = isUsed + self.firstSeenHeight = 0 + self.lastSeenHeight = 0 + self.balance = balance + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentCoreAddress { + var poolTypeName: String { + switch poolTypeTag { + case 0: return "External" + case 1: return "Internal" + case 2: return "Additional" + case 3: return "Additional (Hardened)" + default: return "Unknown(\(poolTypeTag))" + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDPNSName.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDPNSName.swift new file mode 100644 index 00000000000..8e3deed2f65 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDPNSName.swift @@ -0,0 +1,132 @@ +import Foundation +import SwiftData + +// `PersistentDPNSName` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDPNSName { + #Unique([\.networkRaw, \.normalizedParentDomainName, \.normalizedLabel]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var label: String + + var normalizedLabel: String + + var parentDomainName: String + + var normalizedParentDomainName: String + + var acquiredAt: UInt64 + + var isOwned: Bool = true + + var documentIdBase58: String? + + var priceCredits: Int64? + + var saleStatusRaw: Int16 = 0 + + var counterpartyIdBase58: String? + + var documentCreatedAtMs: UInt64? + + var documentUpdatedAtMs: UInt64? + + var documentTransferredAtMs: UInt64? + + var marketplaceUpdatedAt: UInt64 = 0 + + var identity: PersistentIdentity + + var createdAt: Date + var lastUpdated: Date + + init( + identity: PersistentIdentity, + label: String, + parentDomainName: String = "dash", + acquiredAt: UInt64 = 0, + isOwned: Bool = true + ) { + self.identity = identity + self.networkRaw = identity.networkRaw + self.label = label + self.normalizedLabel = Self.normalize(label) + self.parentDomainName = parentDomainName + self.normalizedParentDomainName = Self.normalize(parentDomainName) + self.acquiredAt = acquiredAt + self.isOwned = isOwned + self.documentIdBase58 = nil + self.priceCredits = nil + self.saleStatusRaw = 0 + self.counterpartyIdBase58 = nil + self.documentCreatedAtMs = nil + self.documentUpdatedAtMs = nil + self.documentTransferredAtMs = nil + self.marketplaceUpdatedAt = 0 + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDPNSName { + var saleStatus: DpnsNameSaleStatus? { + guard documentIdBase58 != nil else { return nil } + switch saleStatusRaw { + case 0: + return .owned + case 1: + guard let to = counterpartyId else { return nil } + return .sold(to: to) + case 2: + guard let to = counterpartyId else { return nil } + return .transferred(to: to) + default: + return nil + } + } + + var counterpartyId: Data? { + counterpartyIdBase58.flatMap { Data.identifier(fromBase58: $0) } + } + + var listedPriceCredits: UInt64? { + guard documentIdBase58 != nil, let priceCredits else { return nil } + return UInt64(bitPattern: priceCredits) + } +} + +extension DashSchemaSnapshotV4.PersistentDPNSName { + static func normalize(_ input: String) -> String { + String(input.map { c -> Character in + switch c { + case "o", "O": return "0" + case "i", "I": return "1" + case "l", "L": return "1" + default: return Character(c.lowercased()) + } + }) + } +} + +extension DashSchemaSnapshotV4.PersistentDPNSName { + static func predicate(identityId: Data) -> Predicate { + let target = identityId + return #Predicate { name in + name.identity.identityId == target && name.isOwned == true + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactProfile.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactProfile.swift new file mode 100644 index 00000000000..37df99d2187 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactProfile.swift @@ -0,0 +1,97 @@ +import Foundation +import SwiftData + +// `PersistentDashpayContactProfile` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDashpayContactProfile { + #Unique([ + \.networkRaw, \.ownerIdentityId, \.contactIdentityId + ]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var ownerIdentityId: Data + + var contactIdentityId: Data + + var displayName: String? + + var publicMessage: String? + + var bio: String? + + var avatarUrl: String? + + var avatarHash: Data? + + var avatarFingerprint: Data? + + var checkedAtMs: UInt64 + + var owner: PersistentIdentity + + var createdAt: Date + var lastUpdated: Date + + init( + owner: PersistentIdentity, + contactIdentityId: Data, + checkedAtMs: UInt64, + displayName: String? = nil, + publicMessage: String? = nil, + bio: String? = nil, + avatarUrl: String? = nil, + avatarHash: Data? = nil, + avatarFingerprint: Data? = nil + ) { + self.owner = owner + self.networkRaw = owner.networkRaw + self.ownerIdentityId = owner.identityId + self.contactIdentityId = contactIdentityId + self.checkedAtMs = checkedAtMs + self.displayName = displayName + self.publicMessage = publicMessage + self.bio = bio + self.avatarUrl = avatarUrl + self.avatarHash = avatarHash + self.avatarFingerprint = avatarFingerprint + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDashpayContactProfile { + static func predicate( + ownerIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + return #Predicate { row in + row.ownerIdentityId == target + } + } + + static func predicate( + ownerIdentityId: Data, + contactIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + let contact = contactIdentityId + return #Predicate { row in + row.ownerIdentityId == target + && row.contactIdentityId == contact + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactRequest.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactRequest.swift new file mode 100644 index 00000000000..83d17bfafad --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayContactRequest.swift @@ -0,0 +1,118 @@ +import Foundation +import SwiftData + +// `PersistentDashpayContactRequest` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDashpayContactRequest { + #Unique([ + \.networkRaw, \.ownerIdentityId, \.contactIdentityId, \.isOutgoing + ]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var ownerIdentityId: Data + + var contactIdentityId: Data + + var isOutgoing: Bool + + var senderKeyIndex: UInt32 + + var recipientKeyIndex: UInt32 + + var accountReference: UInt32 + + var encryptedPublicKey: Data + + var encryptedAccountLabel: Data? + + var autoAcceptProof: Data? + + var coreHeightCreatedAt: UInt32 + + var createdAtMillis: UInt64 + + var paymentChannelBroken: Bool = false + + var contactAlias: String? + + var contactNote: String? + + var contactHidden: Bool = false + + var contactAccountLabel: String? + + var contactAcceptedAccounts: [UInt32] = [] + + var owner: PersistentIdentity + + var createdAt: Date + var lastUpdated: Date + + init( + owner: PersistentIdentity, + contactIdentityId: Data, + isOutgoing: Bool, + senderKeyIndex: UInt32, + recipientKeyIndex: UInt32, + accountReference: UInt32, + encryptedPublicKey: Data, + encryptedAccountLabel: Data? = nil, + autoAcceptProof: Data? = nil, + coreHeightCreatedAt: UInt32, + createdAtMillis: UInt64, + paymentChannelBroken: Bool = false + ) { + self.owner = owner + self.networkRaw = owner.networkRaw + self.ownerIdentityId = owner.identityId + self.contactIdentityId = contactIdentityId + self.isOutgoing = isOutgoing + self.senderKeyIndex = senderKeyIndex + self.recipientKeyIndex = recipientKeyIndex + self.accountReference = accountReference + self.encryptedPublicKey = encryptedPublicKey + self.encryptedAccountLabel = encryptedAccountLabel + self.autoAcceptProof = autoAcceptProof + self.coreHeightCreatedAt = coreHeightCreatedAt + self.createdAtMillis = createdAtMillis + self.paymentChannelBroken = paymentChannelBroken + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDashpayContactRequest { + static func predicate( + ownerIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + return #Predicate { row in + row.ownerIdentityId == target + } + } + + static func predicate( + ownerIdentityId: Data, + isOutgoing: Bool + ) -> Predicate { + let target = ownerIdentityId + let direction = isOutgoing + return #Predicate { row in + row.ownerIdentityId == target && row.isOutgoing == direction + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayIgnoredSender.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayIgnoredSender.swift new file mode 100644 index 00000000000..fd8e91d8177 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayIgnoredSender.swift @@ -0,0 +1,67 @@ +import Foundation +import SwiftData + +// `PersistentDashpayIgnoredSender` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDashpayIgnoredSender { + #Unique([ + \.networkRaw, \.ownerIdentityId, \.ignoredSenderId + ]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var ownerIdentityId: Data + + var ignoredSenderId: Data + + var owner: PersistentIdentity + + var ignoredAt: Date + + init( + owner: PersistentIdentity, + ignoredSenderId: Data + ) { + self.owner = owner + self.networkRaw = owner.networkRaw + self.ownerIdentityId = owner.identityId + self.ignoredSenderId = ignoredSenderId + self.ignoredAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDashpayIgnoredSender { + static func predicate( + ownerIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + return #Predicate { row in + row.ownerIdentityId == target + } + } + + static func predicate( + ownerIdentityId: Data, + ignoredSenderId: Data + ) -> Predicate { + let target = ownerIdentityId + let sender = ignoredSenderId + return #Predicate { row in + row.ownerIdentityId == target + && row.ignoredSenderId == sender + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayPayment.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayPayment.swift new file mode 100644 index 00000000000..6783a127296 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayPayment.swift @@ -0,0 +1,99 @@ +import Foundation +import SwiftData + +// `PersistentDashpayPayment` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDashpayPayment { + #Unique([ + \.networkRaw, \.ownerIdentityId, \.txid + ]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var ownerIdentityId: Data + + var counterpartyIdentityId: Data + + var amountDuffs: UInt64 + + var directionRaw: UInt8 + + var direction: DashPayPaymentDirection { + get { DashPayPaymentDirection(rawValue: directionRaw) ?? .sent } + set { directionRaw = newValue.rawValue } + } + + var statusRaw: UInt8 + + var status: DashPayPaymentStatus { + get { DashPayPaymentStatus(rawValue: statusRaw) ?? .pending } + set { statusRaw = newValue.rawValue } + } + + var txid: String + + var memo: String? + + var owner: PersistentIdentity + + var createdAt: Date + var lastUpdated: Date + + init( + owner: PersistentIdentity, + counterpartyIdentityId: Data, + amountDuffs: UInt64, + direction: DashPayPaymentDirection, + status: DashPayPaymentStatus, + txid: String, + memo: String? = nil + ) { + self.owner = owner + self.networkRaw = owner.networkRaw + self.ownerIdentityId = owner.identityId + self.counterpartyIdentityId = counterpartyIdentityId + self.amountDuffs = amountDuffs + self.directionRaw = direction.rawValue + self.statusRaw = status.rawValue + self.txid = txid + self.memo = memo + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDashpayPayment { + static func predicate( + ownerIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + return #Predicate { row in + row.ownerIdentityId == target + } + } + + static func predicate( + ownerIdentityId: Data, + counterpartyIdentityId: Data + ) -> Predicate { + let target = ownerIdentityId + let counterparty = counterpartyIdentityId + return #Predicate { row in + row.ownerIdentityId == target + && row.counterpartyIdentityId == counterparty + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayProfile.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayProfile.swift new file mode 100644 index 00000000000..a5f835fe3af --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDashpayProfile.swift @@ -0,0 +1,70 @@ +import Foundation +import SwiftData + +// `PersistentDashpayProfile` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDashpayProfile { + #Unique([\.networkRaw, \.identity]) + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var displayName: String? + + var publicMessage: String? + + var bio: String? + + var avatarUrl: String? + + var avatarHash: Data? + + var avatarFingerprint: Data? + + var identity: PersistentIdentity + + var createdAt: Date + var lastUpdated: Date + + init( + identity: PersistentIdentity, + displayName: String? = nil, + publicMessage: String? = nil, + bio: String? = nil, + avatarUrl: String? = nil, + avatarHash: Data? = nil, + avatarFingerprint: Data? = nil + ) { + self.identity = identity + self.networkRaw = identity.networkRaw + self.displayName = displayName + self.publicMessage = publicMessage + self.bio = bio + self.avatarUrl = avatarUrl + self.avatarHash = avatarHash + self.avatarFingerprint = avatarFingerprint + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDashpayProfile { + static func predicate(identityId: Data) -> Predicate { + let target = identityId + return #Predicate { profile in + profile.identity.identityId == target + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDataContract.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDataContract.swift new file mode 100644 index 00000000000..2aec109f10e --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDataContract.swift @@ -0,0 +1,286 @@ +import Foundation +import SwiftData + +// `PersistentDataContract` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDataContract { + #Index([\.networkRaw]) + + @Attribute(.unique) var id: Data + var name: String + var serializedContract: Data + var createdAt: Date + var lastAccessedAt: Date + + var binarySerialization: Data? + + var version: Int? + var ownerId: Data? + + @Relationship(deleteRule: .cascade, inverse: \PersistentKeyword.dataContract) + var keywordRelations: [PersistentKeyword] + var contractDescription: String? + + var schemaData: Data + var documentTypesData: Data + + var groupsData: Data? + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var lastUpdated: Date + var lastSyncedAt: Date? + + var canBeDeleted: Bool + var readonly: Bool + var keepsHistory: Bool + var schemaDefs: Int? + + var documentsKeepHistoryContractDefault: Bool + var documentsMutableContractDefault: Bool + var documentsCanBeDeletedContractDefault: Bool + + @Relationship(deleteRule: .cascade, inverse: \PersistentToken.dataContract) + var tokens: [PersistentToken]? + + @Relationship(deleteRule: .cascade, inverse: \PersistentDocumentType.dataContract) + var documentTypes: [PersistentDocumentType]? + + @Relationship(deleteRule: .cascade, inverse: \PersistentDocument.dataContract) + var documents: [PersistentDocument] + + @Relationship(deleteRule: .nullify, inverse: \PersistentIdentity.ownedDataContracts) + var ownerIdentity: PersistentIdentity? + + var hasTokens: Bool + var tokensData: Data? + + var idBase58: String { + id.toBase58String() + } + + var ownerIdBase58: String? { + ownerId?.toBase58String() + } + + var parsedContract: [String: Any]? { + try? JSONSerialization.jsonObject(with: serializedContract, options: []) as? [String: Any] + } + + var binarySerializationHex: String? { + binarySerialization?.toHexString() + } + + var keywords: [String] { + keywordRelations.map { $0.keyword } + } + + var schema: [String: Any] { + get { + guard let json = try? JSONSerialization.jsonObject(with: schemaData), + let dict = json as? [String: Any] else { + return [:] + } + return dict + } + set { + schemaData = (try? JSONSerialization.data(withJSONObject: newValue)) ?? Data() + lastUpdated = Date() + } + } + + var documentTypesList: [String] { + get { + guard let json = try? JSONSerialization.jsonObject(with: documentTypesData), + let array = json as? [String] else { + return [] + } + return array + } + set { + documentTypesData = (try? JSONSerialization.data(withJSONObject: newValue)) ?? Data() + lastUpdated = Date() + } + } + + var tokenConfigurations: [String: Any]? { + get { + guard let data = tokensData, + let json = try? JSONSerialization.jsonObject(with: data), + let dict = json as? [String: Any] else { + return nil + } + return dict + } + set { + if let newValue = newValue { + tokensData = try? JSONSerialization.data(withJSONObject: newValue) + hasTokens = true + } else { + tokensData = nil + hasTokens = false + } + lastUpdated = Date() + } + } + + var groups: [String: Any]? { + get { + guard let data = groupsData, + let json = try? JSONSerialization.jsonObject(with: data), + let dict = json as? [String: Any] else { + return nil + } + return dict + } + set { + if let newValue = newValue { + groupsData = try? JSONSerialization.data(withJSONObject: newValue) + } else { + groupsData = nil + } + lastUpdated = Date() + } + } + + init( + id: Data, + name: String, + serializedContract: Data, + version: Int? = 1, + ownerId: Data? = nil, + schema: [String: Any] = [:], + documentTypesList: [String] = [], + keywords: [String] = [], + description: String? = nil, + hasTokens: Bool = false, + network: Network + ) { + self.id = id + self.name = name + self.serializedContract = serializedContract + self.createdAt = Date() + self.lastAccessedAt = Date() + self.version = version + self.ownerId = ownerId + + self.schemaData = (try? JSONSerialization.data(withJSONObject: schema)) ?? Data() + self.documentTypesData = (try? JSONSerialization.data(withJSONObject: documentTypesList)) ?? Data() + + self.keywordRelations = keywords.map { PersistentKeyword(keyword: $0, contractId: id.toBase58String()) } + self.contractDescription = description + + self.hasTokens = hasTokens + self.tokensData = nil + + self.groupsData = nil + + self.documents = [] + + self.ownerIdentity = nil + + self.networkRaw = network.rawValue + self.lastUpdated = Date() + self.lastSyncedAt = nil + + self.canBeDeleted = false + self.readonly = false + self.keepsHistory = false + self.documentsKeepHistoryContractDefault = false + self.documentsMutableContractDefault = true + self.documentsCanBeDeletedContractDefault = true + } + + func updateLastAccessed() { + self.lastAccessedAt = Date() + } + + func updateVersion(_ newVersion: Int) { + self.version = newVersion + self.lastUpdated = Date() + } + + func markAsSynced() { + self.lastSyncedAt = Date() + } + + func addDocument(_ document: PersistentDocument) { + documents.append(document) + lastUpdated = Date() + } + + func removeDocument(withId documentId: String) { + if let docIdData = Data.identifier(fromBase58: documentId) { + documents.removeAll { $0.id == docIdData } + } + lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDataContract { + static func predicate(contractId: String) -> Predicate { + guard let idData = Data.identifier(fromBase58: contractId) else { + return #Predicate { _ in false } + } + return #Predicate { contract in + contract.id == idData + } + } + + static func predicate(ownerId: Data) -> Predicate { + #Predicate { contract in + contract.ownerId == ownerId + } + } + + static func predicate(name: String) -> Predicate { + #Predicate { contract in + contract.name.localizedStandardContains(name) + } + } + + static var contractsWithTokensPredicate: Predicate { + #Predicate { contract in + contract.hasTokens == true + } + } + + static func predicate(keyword: String) -> Predicate { + #Predicate { contract in + contract.keywordRelations.contains { $0.keyword == keyword } + } + } + + static func needsSyncPredicate(olderThan date: Date) -> Predicate { + #Predicate { contract in + contract.lastSyncedAt == nil || contract.lastSyncedAt! < date + } + } + + static func predicate(network: Network) -> Predicate { + let target = network.rawValue + return #Predicate { contract in + contract.networkRaw == target + } + } + + static func contractsWithTokensPredicate(network: Network) -> Predicate { + let target = network.rawValue + return #Predicate { contract in + contract.hasTokens == true && contract.networkRaw == target + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocument.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocument.swift new file mode 100644 index 00000000000..adddc393f53 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocument.swift @@ -0,0 +1,181 @@ +import Foundation +import SwiftData + +// `PersistentDocument` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDocument { + #Index([\.networkRaw]) + + @Attribute(.unique) var documentId: String + + var documentType: String + var revision: Int32 + var data: Data + + var contractId: String + var ownerId: String + + var contractIdData: Data + var ownerIdData: Data + + var createdAt: Date + var updatedAt: Date + var transferredAt: Date? + + var createdAtBlockHeight: Int64? + var updatedAtBlockHeight: Int64? + var transferredAtBlockHeight: Int64? + + var createdAtCoreBlockHeight: Int64? + var updatedAtCoreBlockHeight: Int64? + var transferredAtCoreBlockHeight: Int64? + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var isDeleted: Bool = false + + var localCreatedAt: Date + var localUpdatedAt: Date + + var documentType_relation: PersistentDocumentType? + var dataContract: PersistentDataContract? + + var ownerIdentity: PersistentIdentity? + + var id: Data { + Data.identifier(fromBase58: documentId) ?? Data() + } + + var idBase58: String { + documentId + } + + var ownerIdBase58: String { + ownerId + } + + var contractIdBase58: String { + contractId + } + + var properties: [String: Any]? { + try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] + } + + var displayTitle: String { + guard let props = properties else { return "Document" } + + if let title = props["title"] as? String { return title } + if let name = props["name"] as? String { return name } + if let label = props["label"] as? String { return label } + if let normalizedLabel = props["normalizedLabel"] as? String { return normalizedLabel } + + return documentType + } + + var summary: String { + var parts: [String] = [] + + parts.append("Type: \(documentType)") + parts.append("Rev: \(revision)") + + let formatter = DateFormatter() + formatter.calendar = Calendar(identifier: .gregorian) + formatter.dateStyle = .short + parts.append("Created: \(formatter.string(from: createdAt))") + + return parts.joined(separator: " • ") + } + + init( + documentId: String, + documentType: String, + revision: Int32, + data: Data, + contractId: String, + ownerId: String, + network: Network + ) { + self.documentId = documentId + self.documentType = documentType + self.revision = revision + self.data = data + self.contractId = contractId + self.ownerId = ownerId + self.contractIdData = Data.identifier(fromBase58: contractId) ?? Data() + self.ownerIdData = Data.identifier(fromBase58: ownerId) ?? Data() + self.networkRaw = network.rawValue + self.createdAt = Date() + self.updatedAt = Date() + self.localCreatedAt = Date() + self.localUpdatedAt = Date() + } + + func updateProperties(_ newData: Data) { + self.data = newData + self.updatedAt = Date() + } + + func updateRevision(_ newRevision: Int64) { + self.revision = Int32(newRevision) + self.updatedAt = Date() + } + + func markAsDeleted() { + self.isDeleted = true + self.updatedAt = Date() + } + + static func predicate(documentId: String) -> Predicate { + #Predicate { doc in + doc.documentId == documentId && doc.isDeleted == false + } + } + + static func predicate(contractId: String, network: Network) -> Predicate { + let target = network.rawValue + return #Predicate { doc in + doc.contractId == contractId && doc.networkRaw == target && doc.isDeleted == false + } + } + + static func predicate(ownerId: Data) -> Predicate { + let ownerIdString = ownerId.toBase58String() + return #Predicate { doc in + doc.ownerId == ownerIdString && doc.isDeleted == false + } + } + + func linkToLocalIdentityIfNeeded(in modelContext: ModelContext) { + guard ownerIdentity == nil else { return } + + let ownerIdToMatch = self.ownerIdData + let identityPredicate = #Predicate { identity in + identity.identityId == ownerIdToMatch && identity.isLocal == true + } + + let descriptor = FetchDescriptor(predicate: identityPredicate) + + do { + if let localIdentity = try modelContext.fetch(descriptor).first { + self.ownerIdentity = localIdentity + self.localUpdatedAt = Date() + } + } catch { + print("Failed to link document to local identity: \(error)") + } + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocumentType.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocumentType.swift new file mode 100644 index 00000000000..8e590b57b92 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentDocumentType.swift @@ -0,0 +1,154 @@ +import Foundation +import SwiftData + +// `PersistentDocumentType` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentDocumentType { + @Attribute(.unique) var id: Data + var contractId: Data + var name: String + + var schemaJSON: Data + var propertiesJSON: Data + + var documentsKeepHistory: Bool + var documentsMutable: Bool + var documentsCanBeDeleted: Bool + var documentsTransferable: Bool + + var indexOnly: Bool = false + + var requiredFieldsJSON: Data? + + var securityLevel: Int + + var tradeMode: Int + var creationRestrictionMode: Int + + var requiresIdentityEncryptionBoundedKey: Bool + var requiresIdentityDecryptionBoundedKey: Bool + + var createdAt: Date + var lastAccessedAt: Date + + var dataContract: PersistentDataContract? + + @Relationship(deleteRule: .cascade, inverse: \PersistentDocument.documentType_relation) + var documents: [PersistentDocument]? + + @Relationship(deleteRule: .cascade, inverse: \PersistentIndex.documentType) + var indices: [PersistentIndex]? + + @Relationship(deleteRule: .cascade, inverse: \PersistentProperty.documentType) + var propertiesList: [PersistentProperty]? + + init(contractId: Data, name: String, schemaJSON: Data, propertiesJSON: Data) { + var idData = contractId + idData.append(name.data(using: .utf8) ?? Data()) + self.id = idData + + self.contractId = contractId + self.name = name + self.schemaJSON = schemaJSON + self.propertiesJSON = propertiesJSON + self.documentsKeepHistory = false + self.documentsMutable = true + self.documentsCanBeDeleted = true + self.documentsTransferable = false + self.securityLevel = 0 + self.tradeMode = 0 + self.creationRestrictionMode = 0 + self.requiresIdentityEncryptionBoundedKey = false + self.requiresIdentityDecryptionBoundedKey = false + self.createdAt = Date() + self.lastAccessedAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentDocumentType { + var contractIdBase58: String { + contractId.toBase58String() + } + + var schema: [String: Any]? { + try? JSONSerialization.jsonObject(with: schemaJSON, options: []) as? [String: Any] + } + + var properties: [String: Any]? { + try? JSONSerialization.jsonObject(with: propertiesJSON, options: []) as? [String: Any] + } + + var persistentProperties: [DashSchemaSnapshotV4.PersistentProperty]? { + return propertiesList + } + + var requiredFields: [String]? { + guard let data = requiredFieldsJSON else { return nil } + return try? JSONSerialization.jsonObject(with: data, options: []) as? [String] + } + + var immutability: DocumentTypeImmutability { + DocumentTypeImmutability(documentTypeSchema: schema) + } + + var immutableProperties: [String] { + immutability.immutableProperties + } + + var immutableAllowSetting: [String] { + immutability.immutableAllowSetting + } + + var typedArrays: [DocumentTypedArray] { + DocumentTypedArray.all(inDocumentTypeSchema: schema) + } + + func typedArray(named name: String) -> DocumentTypedArray? { + DocumentTypedArray.named(name, inDocumentTypeSchema: schema) + } + + var declaresPropertyConstraints: Bool { + schema?["propertyConstraints"] != nil + } + + func propertyConstraints(using sdk: SDK) throws -> [DocumentPropertyConstraint] { + try sdk.documentPropertyConstraints( + serializedContract: storedContractSerialization(), + documentType: name + ) + } + + func propertyConstraintViolation( + propertiesJSON: String, + ownerId: Identifier, + using sdk: SDK + ) throws -> PropertyConstraintViolation? { + try sdk.checkDocumentPropertyConstraints( + serializedContract: storedContractSerialization(), + documentType: name, + propertiesJSON: propertiesJSON, + ownerId: ownerId + ) + } + + private func storedContractSerialization() throws -> Data { + guard let serialization = dataContract?.binarySerialization, !serialization.isEmpty else { + throw SDKError.invalidState( + "The data contract \(contractIdBase58) has no stored serialization; download it again" + ) + } + return serialization + } + + var documentCount: Int { + documents?.count ?? 0 + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentity.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentity.swift new file mode 100644 index 00000000000..d25bcb5997a --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentity.swift @@ -0,0 +1,304 @@ +import Foundation +import SwiftData + +// `PersistentIdentity` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentIdentity { + #Index([\.networkRaw]) + + @Attribute(.unique) var identityId: Data + var balance: Int64 + var revision: Int64 + var isLocal: Bool + var alias: String? + var dpnsName: String? + var mainDpnsName: String? + var identityType: String + + var votingPrivateKeyIdentifier: String? + var ownerPrivateKeyIdentifier: String? + var payoutPrivateKeyIdentifier: String? + + @Relationship(deleteRule: .cascade) var publicKeys: [PersistentPublicKey] + + var createdAt: Date + var lastUpdated: Date + var lastSyncedAt: Date? + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + var wallet: PersistentWallet? + var identityIndex: UInt32 = 0 + + @Relationship(deleteRule: .cascade, inverse: \PersistentDocument.ownerIdentity) var documents: [PersistentDocument] + @Relationship(deleteRule: .nullify) var tokenBalances: [PersistentTokenBalance] + + @Relationship(deleteRule: .cascade, inverse: \PersistentDPNSName.identity) + var dpnsNames: [PersistentDPNSName] = [] + + @Relationship(deleteRule: .cascade, inverse: \PersistentDashpayProfile.identity) + var dashpayProfile: PersistentDashpayProfile? + + @Relationship(deleteRule: .cascade, inverse: \PersistentDashpayContactRequest.owner) + var contactRequests: [PersistentDashpayContactRequest] = [] + + @Relationship(deleteRule: .cascade, inverse: \PersistentDashpayPayment.owner) + var dashpayPayments: [PersistentDashpayPayment] = [] + + @Relationship(deleteRule: .cascade, inverse: \PersistentDashpayIgnoredSender.owner) + var dashpayIgnoredSenders: [PersistentDashpayIgnoredSender] = [] + + @Relationship(deleteRule: .cascade, inverse: \PersistentDashpayContactProfile.owner) + var contactProfiles: [PersistentDashpayContactProfile] = [] + + var ownedDataContracts: [PersistentDataContract] + + init( + identityId: Data, + balance: Int64 = 0, + revision: Int64 = 0, + isLocal: Bool = true, + alias: String? = nil, + dpnsName: String? = nil, + mainDpnsName: String? = nil, + identityType: IdentityType = .user, + votingPrivateKeyIdentifier: String? = nil, + ownerPrivateKeyIdentifier: String? = nil, + payoutPrivateKeyIdentifier: String? = nil, + network: Network, + identityIndex: UInt32 = 0 + ) { + self.identityId = identityId + self.balance = balance + self.revision = revision + self.isLocal = isLocal + self.alias = alias + self.dpnsName = dpnsName + self.mainDpnsName = mainDpnsName + self.identityType = identityType.rawValue + self.votingPrivateKeyIdentifier = votingPrivateKeyIdentifier + self.ownerPrivateKeyIdentifier = ownerPrivateKeyIdentifier + self.payoutPrivateKeyIdentifier = payoutPrivateKeyIdentifier + self.networkRaw = network.rawValue + self.identityIndex = identityIndex + self.publicKeys = [] + self.documents = [] + self.tokenBalances = [] + self.dpnsNames = [] + self.dashpayProfile = nil + self.contactRequests = [] + self.dashpayPayments = [] + self.dashpayIgnoredSenders = [] + self.contactProfiles = [] + self.ownedDataContracts = [] + self.createdAt = Date() + self.lastUpdated = Date() + self.lastSyncedAt = nil + } + + var identityIdString: String { + identityId.toHexString() + } + + var identityIdBase58: String { + identityId.toBase58String() + } + + var formattedBalance: String { + let dashAmount = Double(balance) / 100_000_000_000 + return String(format: "%.8f DASH", dashAmount) + } + + var identityPublicKeys: [IdentityPublicKey] { + publicKeys.compactMap { $0.toIdentityPublicKey() } + } + + var displayName: String { + if let alias = alias, !alias.isEmpty { + return alias + } + if let mainDpnsName = ownedMainDpnsName { + return mainDpnsName + } + if let dpnsName = dpnsName, !dpnsName.isEmpty { + return dpnsName + } + return String(identityIdString.prefix(12)) + "..." + } + + var ownedMainDpnsName: String? { + guard let mainDpnsName, !mainDpnsName.isEmpty else { return nil } + let normalized = PersistentDPNSName.normalize(mainDpnsName) + let names = dpnsNames + guard names.isEmpty else { + return names.contains { $0.isOwned && $0.normalizedLabel == normalized } ? mainDpnsName : nil + } + guard let context = modelContext else { return mainDpnsName } + let networkRaw = networkRaw + let parent = PersistentDPNSName.normalize("dash") + var descriptor = FetchDescriptor( + predicate: #Predicate { + $0.networkRaw == networkRaw + && $0.normalizedParentDomainName == parent + && $0.normalizedLabel == normalized + } + ) + descriptor.fetchLimit = 1 + let row: PersistentDPNSName? + do { + row = try context.fetch(descriptor).first + } catch { + return nil + } + if let row, row.identity.identityId != identityId { + return nil + } + return mainDpnsName + } + + var identityTypeEnum: IdentityType { + IdentityType(rawValue: identityType) ?? .user + } + + func updateBalance(_ newBalance: Int64) { + self.balance = newBalance + self.lastUpdated = Date() + } + + func updateRevision(_ newRevision: Int64) { + self.revision = newRevision + self.lastUpdated = Date() + } + + func markAsSynced() { + self.lastSyncedAt = Date() + } + + func updateDPNSName(_ name: String?) { + self.dpnsName = name + self.lastUpdated = Date() + } + + func addPublicKey(_ key: PersistentPublicKey) { + publicKeys.append(key) + lastUpdated = Date() + } + + func removePublicKey(withId keyId: Int32) { + publicKeys.removeAll { $0.keyId == keyId } + lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentIdentity { + static func predicate(identityId: Data) -> Predicate { + #Predicate { identity in + identity.identityId == identityId + } + } + + static var walletOwnedIdentitiesPredicate: Predicate { + #Predicate { identity in + identity.wallet != nil + } + } + + static func predicate(type: IdentityType) -> Predicate { + let typeString = type.rawValue + return #Predicate { identity in + identity.identityType == typeString + } + } + + static func needsSyncPredicate(olderThan date: Date) -> Predicate { + #Predicate { identity in + identity.lastSyncedAt == nil || identity.lastSyncedAt! < date + } + } + + static func predicate(network: Network) -> Predicate { + let target = network.rawValue + return #Predicate { identity in + identity.networkRaw == target + } + } + + static func walletOwnedIdentitiesPredicate(network: Network) -> Predicate { + let target = network.rawValue + return #Predicate { identity in + identity.wallet != nil && identity.networkRaw == target + } + } + + static func fetch( + in context: ModelContext, + identityId: Data + ) -> DashSchemaSnapshotV4.PersistentIdentity? { + let target = identityId + let descriptor = FetchDescriptor( + predicate: #Predicate { $0.identityId == target } + ) + return try? context.fetch(descriptor).first + } +} + +extension DashSchemaSnapshotV4.PersistentIdentity { + @discardableResult + static func updateBalance( + in context: ModelContext, + identityId: Data, + balance: UInt64 + ) -> Bool { + guard let row = fetch(in: context, identityId: identityId) else { return false } + row.balance = Int64(bitPattern: balance) + row.lastUpdated = Date() + return true + } + + @discardableResult + static func updateDpnsName( + in context: ModelContext, + identityId: Data, + dpnsName: String? + ) -> Bool { + guard let row = fetch(in: context, identityId: identityId) else { return false } + row.dpnsName = dpnsName + row.lastUpdated = Date() + return true + } + + @discardableResult + static func updateMainDpnsName( + in context: ModelContext, + identityId: Data, + mainDpnsName: String? + ) -> Bool { + guard let row = fetch(in: context, identityId: identityId) else { return false } + row.mainDpnsName = mainDpnsName + row.lastUpdated = Date() + return true + } + + @discardableResult + static func remove( + in context: ModelContext, + identityId: Data + ) -> Bool { + guard let row = fetch(in: context, identityId: identityId) else { return false } + context.delete(row) + return true + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentityBalanceMetadata.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentityBalanceMetadata.swift new file mode 100644 index 00000000000..4a1de59943b --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIdentityBalanceMetadata.swift @@ -0,0 +1,32 @@ +import Foundation +import SwiftData + +// `PersistentIdentityBalanceMetadata` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentIdentityBalanceMetadata { + #Unique([\.networkRaw, \.walletId, \.identityId]) + var networkRaw: UInt32 + var walletId: Data + var identityId: Data + var platformHeight: Int64 + var coreHeight: UInt32 + var timestampMillis: Int64 + + init(networkRaw: UInt32, walletId: Data, identityId: Data, + platformHeight: UInt64, coreHeight: UInt32, timestampMillis: UInt64) { + self.networkRaw = networkRaw + self.walletId = walletId + self.identityId = identityId + self.platformHeight = Int64(bitPattern: platformHeight) + self.coreHeight = coreHeight + self.timestampMillis = Int64(bitPattern: timestampMillis) + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIndex.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIndex.swift new file mode 100644 index 00000000000..ea10dbfbcb4 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentIndex.swift @@ -0,0 +1,113 @@ +import Foundation +import SwiftData + +// `PersistentIndex` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentIndex { + @Attribute(.unique) var id: Data + var contractId: Data + var documentTypeName: String + var name: String + + var unique: Bool + var nullSearchable: Bool + var contested: Bool + + var countable: String? + var rangeCountable: Bool = false + var summable: String? + var rangeSummable: Bool = false + var averageable: String? + var rangeAverageable: Bool = false + + var rankedCountable: Bool = false + var rankedSummable: Bool = false + var rankedAverageable: Bool = false + + var terminal: String? + + var preallocated: Bool = false + + var timeRangeJSON: Data? + + var propertiesJSON: Data + + var contestedDetailsJSON: Data? + + var createdAt: Date + + var documentType: PersistentDocumentType? + + init(contractId: Data, documentTypeName: String, name: String, properties: [String]) { + var idData = contractId + idData.append(documentTypeName.data(using: .utf8) ?? Data()) + idData.append(name.data(using: .utf8) ?? Data()) + self.id = idData + + self.contractId = contractId + self.documentTypeName = documentTypeName + self.name = name + self.unique = false + self.nullSearchable = false + self.contested = false + + if let jsonData = try? JSONSerialization.data(withJSONObject: properties, options: []) { + self.propertiesJSON = jsonData + } else { + self.propertiesJSON = Data() + } + + self.createdAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentIndex { + var properties: [String]? { + try? JSONSerialization.jsonObject(with: propertiesJSON, options: []) as? [String] + } + + var contestedDetails: [String: Any]? { + guard let data = contestedDetailsJSON else { return nil } + return try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] + } + + var timeRange: [String: Any]? { + guard let data = timeRangeJSON else { return nil } + return try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] + } + + var authoredDefinition: [String: Any]? { + guard let indices = documentType?.schema?["indices"] as? [[String: Any]] else { + return nil + } + return indices.first { $0["name"] as? String == name } + } + + var authoredKeywords: AuthoredIndexKeywords { + AuthoredIndexKeywords(authoredDefinition: authoredDefinition) + } + + var summableOffCountIndex: String? { + authoredKeywords.summableOffCountIndex + } + + var rankedCountableAt: [String] { + authoredKeywords.rankedCountableAt + } + + var rankedSummableAt: [String] { + authoredKeywords.rankedSummableAt + } + + var rankedAverageableAt: [String] { + authoredKeywords.rankedAverageableAt + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentInvitation.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentInvitation.swift new file mode 100644 index 00000000000..fa8fb65d042 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentInvitation.swift @@ -0,0 +1,73 @@ +import Foundation +import SwiftData + +// `PersistentInvitation` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentInvitation { + #Index([\.walletId]) + + @Attribute(.unique) var outPointHex: String + + var rawOutPoint: Data + + var walletId: Data + + var fundingIndexRaw: Int + + var amountDuffs: Int64 + + var expiryUnix: Int + + var createdAtSecs: Int + + var hasInviter: Bool + + var statusRaw: Int + + var reclaimInFlight: Bool = false + + var createdAt: Date + var updatedAt: Date + + init( + outPointHex: String, + rawOutPoint: Data, + walletId: Data, + fundingIndexRaw: Int, + amountDuffs: Int64, + expiryUnix: Int, + createdAtSecs: Int, + hasInviter: Bool, + statusRaw: Int, + reclaimInFlight: Bool = false + ) { + self.outPointHex = outPointHex + self.rawOutPoint = rawOutPoint + self.walletId = walletId + self.fundingIndexRaw = fundingIndexRaw + self.amountDuffs = amountDuffs + self.expiryUnix = expiryUnix + self.createdAtSecs = createdAtSecs + self.hasInviter = hasInviter + self.statusRaw = statusRaw + self.reclaimInFlight = reclaimInFlight + self.createdAt = Date() + self.updatedAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentInvitation { + static func predicate(walletId: Data) -> Predicate { + #Predicate { entry in + entry.walletId == walletId + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentKeyword.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentKeyword.swift new file mode 100644 index 00000000000..ba5e5e39e71 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentKeyword.swift @@ -0,0 +1,40 @@ +import Foundation +import SwiftData + +// `PersistentKeyword` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentKeyword { + @Attribute(.unique) var id: String + var keyword: String + var contractId: String + + var dataContract: PersistentDataContract? + + init(keyword: String, contractId: String) { + self.id = "\(contractId)_\(keyword)" + self.keyword = keyword + self.contractId = contractId + } + } +} + +extension DashSchemaSnapshotV4.PersistentKeyword { + static func predicate(keyword: String) -> Predicate { + #Predicate { item in + item.keyword.localizedStandardContains(keyword) + } + } + + static func predicate(contractId: String) -> Predicate { + #Predicate { item in + item.contractId == contractId + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentMasternode.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentMasternode.swift new file mode 100644 index 00000000000..8399203bde7 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentMasternode.swift @@ -0,0 +1,185 @@ +import Foundation +import SwiftData + +// `PersistentMasternode` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentMasternode { + #Unique([\.walletId, \.proTxHash]) + + var walletId: Data + var proTxHash: Data + var registrationTxid: Data + + var serviceAddress: String? + var isEvonode: Bool + + var ownerKeyHash: Data? + var votingKeyHash: Data? + var ownerAddress: String? + var votingAddress: String? + var operatorPublicKey: Data? + var platformNodeId: Data? + var payoutAddress: String? + var operatorPseudoAddress: String? + var platformNodeAddress: String? + + var ownerInWallet: Bool = false + var ownerAccountType: UInt8 = 0 + var ownerKeyIndex: UInt32 = 0 + var votingInWallet: Bool = false + var votingAccountType: UInt8 = 0 + var votingKeyIndex: UInt32 = 0 + var operatorInWallet: Bool = false + var operatorAccountType: UInt8 = 0 + var operatorKeyIndex: UInt32 = 0 + var platformInWallet: Bool = false + var platformAccountType: UInt8 = 0 + var platformKeyIndex: UInt32 = 0 + + var collateralTxid: Data? + var collateralVout: UInt32 + + var revoked: Bool + var revocationReason: UInt16 + var statusRaw: UInt8 = 3 + + var registrationHeight: UInt32 + var hasRegistration: Bool + var txCount: UInt32 + + var orderIndex: UInt32 + var typeIndex: UInt32 = 0 + + var createdAt: Date + var lastUpdated: Date + + init( + walletId: Data, + proTxHash: Data, + registrationTxid: Data, + serviceAddress: String? = nil, + isEvonode: Bool = false, + ownerKeyHash: Data? = nil, + votingKeyHash: Data? = nil, + ownerAddress: String? = nil, + votingAddress: String? = nil, + operatorPublicKey: Data? = nil, + platformNodeId: Data? = nil, + payoutAddress: String? = nil, + collateralTxid: Data? = nil, + collateralVout: UInt32 = 0, + revoked: Bool = false, + revocationReason: UInt16 = 0, + statusRaw: UInt8 = 3, + registrationHeight: UInt32 = 0, + hasRegistration: Bool = false, + txCount: UInt32 = 0, + orderIndex: UInt32 = 0, + typeIndex: UInt32 = 0 + ) { + self.walletId = walletId + self.proTxHash = proTxHash + self.registrationTxid = registrationTxid + self.serviceAddress = serviceAddress + self.isEvonode = isEvonode + self.ownerKeyHash = ownerKeyHash + self.votingKeyHash = votingKeyHash + self.ownerAddress = ownerAddress + self.votingAddress = votingAddress + self.operatorPublicKey = operatorPublicKey + self.platformNodeId = platformNodeId + self.payoutAddress = payoutAddress + self.collateralTxid = collateralTxid + self.collateralVout = collateralVout + self.revoked = revoked + self.revocationReason = revocationReason + self.statusRaw = statusRaw + self.registrationHeight = registrationHeight + self.hasRegistration = hasRegistration + self.txCount = txCount + self.orderIndex = orderIndex + self.typeIndex = typeIndex + self.createdAt = Date() + self.lastUpdated = Date() + } + + var proTxHashHex: String { + proTxHash.reversed().map { String(format: "%02x", $0) }.joined() + } + + var proTxHashShort: String { + let hex = proTxHashHex + guard hex.count >= 12 else { return hex } + return "\(String(hex.prefix(6)))…\(String(hex.suffix(6)))" + } + + var ownerKeyHashHex: String? { + ownerKeyHash.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + var votingKeyHashHex: String? { + votingKeyHash.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + static func providerAccountTypeName(_ tag: UInt8) -> String { + switch tag { + case 8: return "ProviderVotingKeys" + case 9: return "ProviderOwnerKeys" + case 10: return "ProviderOperatorKeys" + case 11: return "ProviderPlatformKeys" + default: return "Unknown(\(tag))" + } + } + + static func keyOwnershipLabel( + inWallet: Bool, + accountType: UInt8, + index: UInt32 + ) -> String { + inWallet + ? "\(providerAccountTypeName(accountType)) #\(index)" + : "not in this wallet" + } + + var operatorPublicKeyHex: String? { + operatorPublicKey.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + var platformNodeIdHex: String? { + platformNodeId.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + var collateralDisplay: String? { + guard let txid = collateralTxid else { return nil } + let hex = txid.reversed().map { String(format: "%02x", $0) }.joined() + return "\(hex):\(collateralVout)" + } + + var displayNumber: Int { + Int(typeIndex) + } + + var typeName: String { + isEvonode ? "Evonode" : "Masternode" + } + + var displayTitle: String { + "\(typeName) \(displayNumber)" + } + + var status: MasternodeStatus { + MasternodeStatus(rawValue: statusRaw) ?? .unknown + } + + var statusName: String { + status.displayName + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPendingInput.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPendingInput.swift new file mode 100644 index 00000000000..43a2f6a2fb9 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPendingInput.swift @@ -0,0 +1,46 @@ +import Foundation +import SwiftData + +// `PersistentPendingInput` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentPendingInput { + #Index([\.outpoint], [\.walletId], [\.walletId, \.isSweptTombstone]) + var outpoint: Data + + var inputIndex: UInt32 + + var spendingTxid: Data + + var spendingTransaction: PersistentTransaction? + + var walletId: Data + + var createdAt: Date + + var isSweptTombstone: Bool = false + + var winnerMinedHeight: UInt32? + + init( + outpoint: Data, + inputIndex: UInt32, + spendingTxid: Data, + spendingTransaction: PersistentTransaction?, + walletId: Data + ) { + self.outpoint = outpoint + self.inputIndex = inputIndex + self.spendingTxid = spendingTxid + self.spendingTransaction = spendingTransaction + self.walletId = walletId + self.createdAt = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddress.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddress.swift new file mode 100644 index 00000000000..f621099c8d2 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddress.swift @@ -0,0 +1,78 @@ +import Foundation +import SwiftData + +// `PersistentPlatformAddress` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentPlatformAddress { + #Index([\.walletId]) + + @Attribute(.unique) var address: String + var addressType: UInt8 + @Attribute(.unique) var addressHash: Data + var publicKey: Data + var accountIndex: UInt32 + var addressIndex: UInt32 + var derivationPath: String + var isUsed: Bool + var balance: UInt64 + var nonce: UInt32 + var firstSeenHeight: UInt32 + var lastSeenHeight: UInt64 + var walletId: Data + var createdAt: Date + var lastUpdated: Date + + var account: PersistentAccount? + + init( + address: String, + addressType: UInt8, + addressHash: Data, + publicKey: Data = Data(), + accountIndex: UInt32, + addressIndex: UInt32, + derivationPath: String, + isUsed: Bool = false, + balance: UInt64 = 0, + nonce: UInt32 = 0, + walletId: Data + ) { + self.address = address + self.addressType = addressType + self.addressHash = addressHash + self.publicKey = publicKey + self.accountIndex = accountIndex + self.addressIndex = addressIndex + self.derivationPath = derivationPath + self.isUsed = isUsed + self.balance = balance + self.nonce = nonce + self.firstSeenHeight = 0 + self.lastSeenHeight = 0 + self.walletId = walletId + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentPlatformAddress { + static func predicate(walletId: Data) -> Predicate { + #Predicate { entry in + entry.walletId == walletId + } + } + + static var nonZeroBalancesPredicate: Predicate { + #Predicate { entry in + entry.balance > 0 + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddressesSyncState.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddressesSyncState.swift new file mode 100644 index 00000000000..7677e0c6a2d --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPlatformAddressesSyncState.swift @@ -0,0 +1,41 @@ +import Foundation +import SwiftData + +// `PersistentPlatformAddressesSyncState` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentPlatformAddressesSyncState { + @Attribute(.unique) var walletId: Data + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + var syncHeight: UInt64 + var syncTimestamp: UInt64 + var lastKnownRecentBlock: UInt64 + var lastUpdated: Date + + init( + walletId: Data, + network: Network, + syncHeight: UInt64, + syncTimestamp: UInt64, + lastKnownRecentBlock: UInt64 + ) { + self.walletId = walletId + self.networkRaw = network.rawValue + self.syncHeight = syncHeight + self.syncTimestamp = syncTimestamp + self.lastKnownRecentBlock = lastKnownRecentBlock + self.lastUpdated = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentProperty.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentProperty.swift new file mode 100644 index 00000000000..2253d76b134 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentProperty.swift @@ -0,0 +1,55 @@ +import Foundation +import SwiftData + +// `PersistentProperty` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentProperty { + @Attribute(.unique) var id: Data + var contractId: Data + var documentTypeName: String + var name: String + + var type: String + var format: String? + var contentMediaType: String? + var byteArray: Bool + var minItems: Int? + var maxItems: Int? + var pattern: String? + var minLength: Int? + var maxLength: Int? + var minValue: Int? + var maxValue: Int? + var fieldDescription: String? + + var transient: Bool + var isRequired: Bool + + var createdAt: Date + + var documentType: PersistentDocumentType? + + init(contractId: Data, documentTypeName: String, name: String, type: String) { + var idData = contractId + idData.append(documentTypeName.data(using: .utf8) ?? Data()) + idData.append(name.data(using: .utf8) ?? Data()) + self.id = idData + + self.contractId = contractId + self.documentTypeName = documentTypeName + self.name = name + self.type = type + self.byteArray = false + self.transient = false + self.isRequired = false + self.createdAt = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPublicKey.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPublicKey.swift new file mode 100644 index 00000000000..981a7f07985 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentPublicKey.swift @@ -0,0 +1,218 @@ +import Foundation +import SwiftData + +// `PersistentPublicKey` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentPublicKey { + var keyId: Int32 + var purpose: String + var securityLevel: String + var keyType: String + var readOnly: Bool + var disabledAt: Int64? + + var totalBudget: Int64? + + var expiresAt: Int64? + + var publicKeyData: Data + + var contractBoundsData: Data? + + var contractBoundsDocumentTypeName: String? + + var contractBoundsKind: Int? + + var privateKeyKeychainIdentifier: String? + + var walletId: Data? + + var identityDerivationPath: String? + + var identityId: String + var createdAt: Date + var lastAccessed: Date? + + @Relationship(inverse: \PersistentIdentity.publicKeys) + var identity: PersistentIdentity? + + init( + keyId: Int32, + purpose: KeyPurpose, + securityLevel: SecurityLevel, + keyType: KeyType, + publicKeyData: Data, + readOnly: Bool = false, + disabledAt: Int64? = nil, + contractBounds: [Data]? = nil, + contractBoundsDocumentTypeName: String? = nil, + totalBudget: Int64? = nil, + expiresAt: Int64? = nil, + contractBoundsKind: Int? = nil, + identityId: String + ) { + self.keyId = keyId + self.purpose = String(purpose.rawValue) + self.securityLevel = String(securityLevel.rawValue) + self.keyType = String(keyType.rawValue) + self.publicKeyData = publicKeyData + self.readOnly = readOnly + self.disabledAt = disabledAt + self.totalBudget = totalBudget + self.expiresAt = expiresAt + if let contractBounds = contractBounds { + self.contractBoundsData = try? JSONSerialization.data(withJSONObject: contractBounds.map { $0.base64EncodedString() }) + } else { + self.contractBoundsData = nil + } + self.contractBoundsDocumentTypeName = contractBoundsDocumentTypeName + self.contractBoundsKind = contractBoundsKind + self.identityId = identityId + self.createdAt = Date() + } + + var contractBounds: [Data]? { + get { + guard let data = contractBoundsData, + let json = try? JSONSerialization.jsonObject(with: data), + let strings = json as? [String] else { + return nil + } + return strings.compactMap { Data(base64Encoded: $0) } + } + set { + contractBoundsDocumentTypeName = nil + contractBoundsKind = newValue == nil ? 0 : 1 + if let newValue = newValue { + contractBoundsData = try? JSONSerialization.data(withJSONObject: newValue.map { $0.base64EncodedString() }) + } else { + contractBoundsData = nil + } + } + } + + var effectiveContractBoundsKind: Int { + if let contractBoundsKind = contractBoundsKind { + return contractBoundsKind + } + guard contractBoundsData != nil else { return 0 } + if let name = contractBoundsDocumentTypeName, !name.isEmpty { return 2 } + return 1 + } + + var purposeEnum: KeyPurpose? { + guard let purposeInt = UInt8(purpose) else { return nil } + return KeyPurpose(rawValue: purposeInt) + } + + var securityLevelEnum: SecurityLevel? { + guard let levelInt = UInt8(securityLevel) else { return nil } + return SecurityLevel(rawValue: levelInt) + } + + var keyTypeEnum: KeyType? { + guard let typeInt = UInt8(keyType) else { return nil } + return KeyType(rawValue: typeInt) + } + + var isDisabled: Bool { + disabledAt != nil + } + + var totalBudgetCredits: UInt64? { + get { totalBudget.map { UInt64(bitPattern: $0) } } + set { totalBudget = newValue.map { Int64(bitPattern: $0) } } + } + + var expiresAtMillis: UInt64? { + get { expiresAt.map { UInt64(bitPattern: $0) } } + set { expiresAt = newValue.map { Int64(bitPattern: $0) } } + } + + var hasLimits: Bool { + totalBudget != nil || expiresAt != nil + } + + var hasPrivateKeyIdentifier: Bool { + privateKeyKeychainIdentifier != nil + } + } +} + +extension DashSchemaSnapshotV4.PersistentPublicKey { + func toIdentityPublicKey() -> IdentityPublicKey? { + guard let purpose = purposeEnum, + let securityLevel = securityLevelEnum, + let keyType = keyTypeEnum else { + return nil + } + + let bounds: ContractBounds? + let boundsKind = effectiveContractBoundsKind + if (1...2).contains(boundsKind), let id = contractBounds?.first, id.count == 32 { + if boundsKind == 2, + let docTypeName = contractBoundsDocumentTypeName, !docTypeName.isEmpty { + bounds = .singleContractDocumentType(id: id, documentTypeName: docTypeName) + } else { + bounds = .singleContract(id: id) + } + } else { + bounds = nil + } + + return IdentityPublicKey( + id: KeyID(keyId), + purpose: purpose, + securityLevel: securityLevel, + contractBounds: bounds, + keyType: keyType, + readOnly: readOnly, + data: publicKeyData, + disabledAt: disabledAt.map { TimestampMillis($0) }, + totalBudget: totalBudgetCredits, + expiresAt: expiresAtMillis + ) + } + + static func from(_ publicKey: IdentityPublicKey, identityId: String) -> DashSchemaSnapshotV4.PersistentPublicKey? { + let boundsIds: [Data]? + let docTypeName: String? + let kind: Int + switch publicKey.contractBounds { + case .singleContract(let id): + boundsIds = [id] + docTypeName = nil + kind = 1 + case .singleContractDocumentType(let id, let name): + boundsIds = [id] + docTypeName = name + kind = 2 + case .none: + boundsIds = nil + docTypeName = nil + kind = 0 + } + return DashSchemaSnapshotV4.PersistentPublicKey( + keyId: Int32(publicKey.id), + purpose: publicKey.purpose, + securityLevel: publicKey.securityLevel, + keyType: publicKey.keyType, + publicKeyData: publicKey.data, + readOnly: publicKey.readOnly, + disabledAt: publicKey.disabledAt.map { Int64($0) }, + contractBounds: boundsIds, + contractBoundsDocumentTypeName: docTypeName, + totalBudget: publicKey.totalBudget.map { Int64(bitPattern: $0) }, + expiresAt: publicKey.expiresAt.map { Int64(bitPattern: $0) }, + contractBoundsKind: kind, + identityId: identityId + ) + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedActivity.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedActivity.swift new file mode 100644 index 00000000000..ea27a8572b8 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedActivity.swift @@ -0,0 +1,89 @@ +import Foundation +import SwiftData + +// `PersistentShieldedActivity` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentShieldedActivity { + #Unique([\.walletId, \.accountIndex, \.entryId]) + #Index([\.walletId, \.accountIndex]) + + var walletId: Data + var accountIndex: UInt32 + var entryId: Data + + var kindTag: Int + var direction: Int + var status: Int + + var amount: UInt64 + var fee: UInt64 + var hasFee: Bool + var blockHeight: UInt64 + var hasBlockHeight: Bool + var createdAtMs: UInt64 + + var minNotePosition: UInt64 = 0 + var hasMinNotePosition: Bool = false + + var identityId: Data + var counterparty: Data + var memo: Data + var noteCmxs: Data + var spentNullifiers: Data + + var createdAt: Date + var lastUpdated: Date + + init( + walletId: Data, + accountIndex: UInt32, + entryId: Data, + kindTag: Int, + direction: Int, + status: Int, + amount: UInt64, + fee: UInt64, + hasFee: Bool, + blockHeight: UInt64, + hasBlockHeight: Bool, + createdAtMs: UInt64, + minNotePosition: UInt64 = 0, + hasMinNotePosition: Bool = false, + identityId: Data, + counterparty: Data, + memo: Data, + noteCmxs: Data, + spentNullifiers: Data + ) { + self.walletId = walletId + self.accountIndex = accountIndex + self.entryId = entryId + self.kindTag = kindTag + self.direction = direction + self.status = status + self.amount = amount + self.fee = fee + self.hasFee = hasFee + self.blockHeight = blockHeight + self.hasBlockHeight = hasBlockHeight + self.createdAtMs = createdAtMs + self.minNotePosition = minNotePosition + self.hasMinNotePosition = hasMinNotePosition + self.identityId = identityId + self.counterparty = counterparty + self.memo = memo + self.noteCmxs = noteCmxs + self.spentNullifiers = spentNullifiers + let now = Date() + self.createdAt = now + self.lastUpdated = now + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedNote.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedNote.swift new file mode 100644 index 00000000000..beedd9b7268 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedNote.swift @@ -0,0 +1,66 @@ +import Foundation +import SwiftData + +// `PersistentShieldedNote` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentShieldedNote { + #Index([\.walletId, \.accountIndex]) + + var walletId: Data + var accountIndex: UInt32 + var position: UInt64 + var cmx: Data + @Attribute(.unique) var nullifier: Data + var blockHeight: UInt64 + var isSpent: Bool + var value: UInt64 + var noteData: Data + + var createdAt: Date + var lastUpdated: Date + + init( + walletId: Data, + accountIndex: UInt32, + position: UInt64, + cmx: Data, + nullifier: Data, + blockHeight: UInt64, + isSpent: Bool, + value: UInt64, + noteData: Data + ) { + self.walletId = walletId + self.accountIndex = accountIndex + self.position = position + self.cmx = cmx + self.nullifier = nullifier + self.blockHeight = blockHeight + self.isSpent = isSpent + self.value = value + self.noteData = noteData + let now = Date() + self.createdAt = now + self.lastUpdated = now + } + } +} + +extension DashSchemaSnapshotV4.PersistentShieldedNote { + static func unspentPredicate(walletId: Data) -> Predicate { + #Predicate { + $0.walletId == walletId && $0.isSpent == false + } + } + + static var unspentPredicate: Predicate { + #Predicate { $0.isSpent == false } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedOutgoingNote.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedOutgoingNote.swift new file mode 100644 index 00000000000..6831b25d1ef --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedOutgoingNote.swift @@ -0,0 +1,49 @@ +import Foundation +import SwiftData + +// `PersistentShieldedOutgoingNote` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentShieldedOutgoingNote { + #Unique([\.walletId, \.accountIndex, \.cmx]) + #Index([\.walletId, \.accountIndex]) + + var walletId: Data + var accountIndex: UInt32 + var cmx: Data + var recipient: Data + var value: UInt64 + var memo: Data + var blockHeight: UInt64 + + var createdAt: Date + var lastUpdated: Date + + init( + walletId: Data, + accountIndex: UInt32, + cmx: Data, + recipient: Data, + value: UInt64, + memo: Data, + blockHeight: UInt64 + ) { + self.walletId = walletId + self.accountIndex = accountIndex + self.cmx = cmx + self.recipient = recipient + self.value = value + self.memo = memo + self.blockHeight = blockHeight + let now = Date() + self.createdAt = now + self.lastUpdated = now + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedSyncState.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedSyncState.swift new file mode 100644 index 00000000000..8b92ec5c9da --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedSyncState.swift @@ -0,0 +1,34 @@ +import Foundation +import SwiftData + +// `PersistentShieldedSyncState` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentShieldedSyncState { + #Unique([\.walletId, \.accountIndex]) + #Index([\.walletId]) + + var walletId: Data + var accountIndex: UInt32 + var lastSyncedIndex: UInt64 + + var lastUpdated: Date + + init( + walletId: Data, + accountIndex: UInt32, + lastSyncedIndex: UInt64 = 0 + ) { + self.walletId = walletId + self.accountIndex = accountIndex + self.lastSyncedIndex = lastSyncedIndex + self.lastUpdated = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedViewingKey.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedViewingKey.swift new file mode 100644 index 00000000000..bc2ccb9d82e --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentShieldedViewingKey.swift @@ -0,0 +1,34 @@ +import Foundation +import SwiftData + +// `PersistentShieldedViewingKey` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentShieldedViewingKey { + #Unique([\.walletId, \.accountIndex]) + #Index([\.walletId]) + + var walletId: Data + var accountIndex: UInt32 + var fvkBytes: Data + + var lastUpdated: Date + + init( + walletId: Data, + accountIndex: UInt32, + fvkBytes: Data + ) { + self.walletId = walletId + self.accountIndex = accountIndex + self.fvkBytes = fvkBytes + self.lastUpdated = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentToken.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentToken.swift new file mode 100644 index 00000000000..cf706af35aa --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentToken.swift @@ -0,0 +1,403 @@ +import Foundation +import SwiftData + +// `PersistentToken` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentToken { + @Attribute(.unique) var id: Data + var contractId: Data + var position: Int + var name: String + + var baseSupply: String + var maxSupply: String? + var decimals: Int + + var localizations: [String: TokenLocalization]? + + var isPaused: Bool + var allowTransferToFrozenBalance: Bool + + var keepsTransferHistory: Bool + var keepsFreezingHistory: Bool + var keepsMintingHistory: Bool + var keepsBurningHistory: Bool + var keepsDirectPricingHistory: Bool + var keepsDirectPurchaseHistory: Bool + + var conventionsChangeRules: ChangeControlRules? + var maxSupplyChangeRules: ChangeControlRules? + var manualMintingRules: ChangeControlRules? + var manualBurningRules: ChangeControlRules? + var freezeRules: ChangeControlRules? + var unfreezeRules: ChangeControlRules? + var destroyFrozenFundsRules: ChangeControlRules? + var emergencyActionRules: ChangeControlRules? + + var perpetualDistribution: TokenPerpetualDistribution? + var preProgrammedDistribution: TokenPreProgrammedDistribution? + var newTokensDestinationIdentity: Data? + var mintingAllowChoosingDestination: Bool + var distributionChangeRules: TokenDistributionChangeRules? + + var tradeMode: TokenTradeMode + var tradeModeChangeRules: ChangeControlRules? + + var mainControlGroupPosition: Int? + var mainControlGroupCanBeModified: String? + + var tokenDescription: String? + + var createdAt: Date + var lastUpdatedAt: Date + + var dataContract: PersistentDataContract? + + @Relationship(deleteRule: .cascade) + var balances: [PersistentTokenBalance]? + + @Relationship(deleteRule: .cascade) + var historyEvents: [PersistentTokenHistoryEvent]? + + init(contractId: Data, position: Int, name: String, baseSupply: String, decimals: Int = 8) { + var idData = contractId + withUnsafeBytes(of: position.bigEndian) { bytes in + idData.append(contentsOf: bytes) + } + self.id = idData + + self.contractId = contractId + self.position = position + self.name = name + self.baseSupply = baseSupply + self.decimals = decimals + + self.isPaused = false + self.allowTransferToFrozenBalance = true + self.keepsTransferHistory = true + self.keepsFreezingHistory = true + self.keepsMintingHistory = true + self.keepsBurningHistory = true + self.keepsDirectPricingHistory = true + self.keepsDirectPurchaseHistory = true + self.mintingAllowChoosingDestination = true + self.tradeMode = TokenTradeMode.notTradeable + + self.createdAt = Date() + self.lastUpdatedAt = Date() + } + } +} + +extension DashSchemaSnapshotV4.PersistentToken { + var displayName: String { + if let desc = tokenDescription, !desc.isEmpty { + return desc + } + return getSingularForm() ?? name + } + + var formattedBaseSupply: String { + Self.formatSupply(baseSupply, decimals: decimals) + } + + static func formatSupply(_ raw: String, decimals: Int) -> String { + guard !raw.isEmpty, raw.allSatisfy({ $0.isASCII && $0.isNumber }) else { + return raw + } + let normalized = String(raw.drop(while: { $0 == "0" })) + let digits = normalized.isEmpty ? "0" : normalized + let scale = max(0, decimals) + let integer: String + var fraction = "" + if scale == 0 { + integer = digits + } else if digits.count <= scale { + integer = "0" + fraction = String(repeating: "0", count: scale - digits.count) + digits + } else { + let split = digits.index(digits.endIndex, offsetBy: -scale) + integer = String(digits[.. 0 && offset.isMultiple(of: 3) { grouped.append(",") } + grouped.append(character) + } + grouped = String(grouped.reversed()) + return fraction.isEmpty ? grouped : "\(grouped).\(fraction)" + } + + var contractIdBase58: String { + contractId.toBase58String() + } + + var canManuallyMint: Bool { + manualMintingRules != nil + } + + var canManuallyBurn: Bool { + manualBurningRules != nil + } + + var canFreeze: Bool { + freezeRules != nil + } + + var canUnfreeze: Bool { + unfreezeRules != nil + } + + var canDestroyFrozenFunds: Bool { + destroyFrozenFundsRules != nil + } + + var hasEmergencyActions: Bool { + emergencyActionRules != nil + } + + var canChangeMaxSupply: Bool { + maxSupplyChangeRules != nil + } + + var canChangeConventions: Bool { + conventionsChangeRules != nil + } + + var oncePerIdentityDistribution: TokenOncePerIdentityDistribution? { + guard let contract = dataContract else { return nil } + return TokenOncePerIdentityDistributionCache.shared.distribution( + contractId: contract.id, + serializedContract: contract.serializedContract, + lastUpdated: contract.lastUpdated, + position: position + ) + } + + var hasDistribution: Bool { + perpetualDistribution != nil + || preProgrammedDistribution != nil + || oncePerIdentityDistribution != nil + } + + var canChangeTradeMode: Bool { + tradeModeChangeRules != nil + } + + var keepsAnyHistory: Bool { + keepsTransferHistory || + keepsFreezingHistory || + keepsMintingHistory || + keepsBurningHistory || + keepsDirectPricingHistory || + keepsDirectPurchaseHistory + } + + var totalSupply: String { + guard let balances = balances, !balances.isEmpty else { return baseSupply } + return Self.sumUnsignedBalances(balances.map(\.unsignedBalance)) + } + + var totalFrozenBalance: String { + guard let balances = balances else { return "0" } + return Self.sumUnsignedBalances( + balances.lazy.filter(\.frozen).map(\.unsignedBalance) + ) + } + + var activeHolders: Int { + balances?.filter { $0.unsignedBalance > 0 }.count ?? 0 + } + + private static func sumUnsignedBalances(_ values: S) -> String + where S.Element == UInt64 { + var digits: [UInt8] = [0] // little-endian decimal digits + + for value in values { + var carry = 0 + let addend = String(value).utf8.reversed().map { Int($0 - 48) } + let width = max(digits.count, addend.count) + if digits.count < width { + digits.append(contentsOf: repeatElement(0, count: width - digits.count)) + } + + for index in 0.. 0 { + digits.append(UInt8(carry % 10)) + carry /= 10 + } + } + + return String(digits.reversed().map { Character(String($0)) }) + } + + var hasMaxSupply: Bool { + maxSupply != nil + } + + var isTradeable: Bool { + tradeMode != .notTradeable + } + + var newTokensDestinationIdentityBase58: String? { + newTokensDestinationIdentity?.toBase58String() + } +} + +extension DashSchemaSnapshotV4.PersistentToken { + func setLocalization(languageCode: String, singularForm: String, pluralForm: String, description: String? = nil) { + if localizations == nil { + localizations = [:] + } + localizations?[languageCode] = DashSchemaSnapshotV4.TokenLocalization( + singularForm: singularForm, + pluralForm: pluralForm, + description: description + ) + lastUpdatedAt = Date() + } + + func getSingularForm(languageCode: String = "en") -> String? { + return localizations?[languageCode]?.singularForm ?? localizations?["en"]?.singularForm + } + + func getPluralForm(languageCode: String = "en") -> String? { + return localizations?[languageCode]?.pluralForm ?? localizations?["en"]?.pluralForm + } +} + +extension DashSchemaSnapshotV4.PersistentToken { + func getChangeControlRules(for type: ChangeControlRuleType) -> DashSchemaSnapshotV4.ChangeControlRules? { + switch type { + case .conventions: return conventionsChangeRules + case .maxSupply: return maxSupplyChangeRules + case .manualMinting: return manualMintingRules + case .manualBurning: return manualBurningRules + case .freeze: return freezeRules + case .unfreeze: return unfreezeRules + case .destroyFrozenFunds: return destroyFrozenFundsRules + case .emergencyAction: return emergencyActionRules + case .tradeMode: return tradeModeChangeRules + } + } + + func setChangeControlRules(_ rules: DashSchemaSnapshotV4.ChangeControlRules, for type: ChangeControlRuleType) { + switch type { + case .conventions: conventionsChangeRules = rules + case .maxSupply: maxSupplyChangeRules = rules + case .manualMinting: manualMintingRules = rules + case .manualBurning: manualBurningRules = rules + case .freeze: freezeRules = rules + case .unfreeze: unfreezeRules = rules + case .destroyFrozenFunds: destroyFrozenFundsRules = rules + case .emergencyAction: emergencyActionRules = rules + case .tradeMode: tradeModeChangeRules = rules + } + + lastUpdatedAt = Date() + } +} + +extension DashSchemaSnapshotV4.PersistentToken { + static func mintableTokensPredicate() -> Predicate { + #Predicate { token in + token.manualMintingRules != nil + } + } + + static func burnableTokensPredicate() -> Predicate { + #Predicate { token in + token.manualBurningRules != nil + } + } + + static func freezableTokensPredicate() -> Predicate { + #Predicate { token in + token.freezeRules != nil + } + } + + static func columnBackedDistributionTokensPredicate() -> Predicate { + #Predicate { token in + token.perpetualDistribution != nil || token.preProgrammedDistribution != nil + } + } + + @available( + *, + deprecated, + renamed: "columnBackedDistributionTokensPredicate()", + message: """ + This predicate never matched the once-per-identity kind, which is derived from the \ + contract JSON rather than stored on the token row. Fetch without it and filter on \ + `hasDistribution`, or call `columnBackedDistributionTokensPredicate()` when the two \ + column-backed kinds really are all you want. + """ + ) + static func distributionTokensPredicate() -> Predicate { + columnBackedDistributionTokensPredicate() + } + + static func pausedTokensPredicate() -> Predicate { + #Predicate { token in + token.isPaused == true + } + } + + static func tokensByContractPredicate(contractId: Data) -> Predicate { + #Predicate { token in + token.contractId == contractId + } + } + + static func tokensWithControlRulePredicate(rule: ControlRuleType) -> Predicate { + switch rule { + case .manualMinting: + return #Predicate { token in + token.manualMintingRules != nil + } + case .manualBurning: + return #Predicate { token in + token.manualBurningRules != nil + } + case .freeze: + return #Predicate { token in + token.freezeRules != nil + } + case .unfreeze: + return #Predicate { token in + token.unfreezeRules != nil + } + case .destroyFrozenFunds: + return #Predicate { token in + token.destroyFrozenFundsRules != nil + } + case .emergencyAction: + return #Predicate { token in + token.emergencyActionRules != nil + } + case .conventions: + return #Predicate { token in + token.conventionsChangeRules != nil + } + case .maxSupply: + return #Predicate { token in + token.maxSupplyChangeRules != nil + } + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenBalance.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenBalance.swift new file mode 100644 index 00000000000..da61f3ba641 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenBalance.swift @@ -0,0 +1,198 @@ +import Foundation +import SwiftData + +// `PersistentTokenBalance` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentTokenBalance { + #Index([\.networkRaw]) + + var tokenId: String + var identityId: Data + var balance: Int64 + var frozen: Bool + + var createdAt: Date + var lastUpdated: Date + var lastSyncedAt: Date? + + var tokenName: String? + var tokenSymbol: String? + var tokenDecimals: Int32? + + var networkRaw: UInt32 + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + @Relationship(deleteRule: .nullify) var identity: PersistentIdentity? + @Relationship(inverse: \PersistentToken.balances) var token: PersistentToken? + + init( + tokenId: String, + identityId: Data, + balance: Int64 = 0, + frozen: Bool = false, + tokenName: String? = nil, + tokenSymbol: String? = nil, + tokenDecimals: Int32? = nil, + network: Network + ) { + self.tokenId = tokenId + self.identityId = identityId + self.balance = balance + self.frozen = frozen + self.tokenName = tokenName + self.tokenSymbol = tokenSymbol + self.tokenDecimals = tokenDecimals + self.createdAt = Date() + self.lastUpdated = Date() + self.lastSyncedAt = nil + self.networkRaw = network.rawValue + } + + convenience init( + tokenId: String, + identityId: Data, + unsignedBalance: UInt64, + frozen: Bool = false, + tokenName: String? = nil, + tokenSymbol: String? = nil, + tokenDecimals: Int32? = nil, + network: Network + ) { + self.init( + tokenId: tokenId, + identityId: identityId, + balance: Int64(bitPattern: unsignedBalance), + frozen: frozen, + tokenName: tokenName, + tokenSymbol: tokenSymbol, + tokenDecimals: tokenDecimals, + network: network + ) + } + + var unsignedBalance: UInt64 { + get { UInt64(bitPattern: balance) } + set { balance = Int64(bitPattern: newValue) } + } + + var formattedBalance: String { + let decimals: Int + if let tokenDecimals { + decimals = Int(tokenDecimals) + } else if let tokenDecimals = token?.decimals { + decimals = tokenDecimals + } else { + return "\(unsignedBalance)" + } + + guard decimals > 0 else { return String(unsignedBalance) } + + let digits = String(unsignedBalance) + let scale = decimals + if digits.count <= scale { + return "0." + String(repeating: "0", count: scale - digits.count) + digits + } + let split = digits.index(digits.endIndex, offsetBy: -scale) + return String(digits[.. (tokenId: String, balance: UInt64, frozen: Bool) { + return (tokenId: tokenId, balance: unsignedBalance, frozen: frozen) + } +} + +extension DashSchemaSnapshotV4.PersistentTokenBalance { + static func predicate(tokenId: String, identityId: Data) -> Predicate { + #Predicate { balance in + balance.tokenId == tokenId && balance.identityId == identityId + } + } + + static func predicate(identityId: Data) -> Predicate { + #Predicate { balance in + balance.identityId == identityId + } + } + + static func predicate(tokenId: String) -> Predicate { + #Predicate { balance in + balance.tokenId == tokenId + } + } + + static var nonZeroBalancesPredicate: Predicate { + #Predicate { balance in + balance.balance != 0 + } + } + + static var frozenBalancesPredicate: Predicate { + #Predicate { balance in + balance.frozen == true + } + } + + static func needsSyncPredicate(olderThan date: Date) -> Predicate { + #Predicate { balance in + balance.lastSyncedAt == nil || balance.lastSyncedAt! < date + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenHistoryEvent.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenHistoryEvent.swift new file mode 100644 index 00000000000..af354fd6d32 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTokenHistoryEvent.swift @@ -0,0 +1,112 @@ +import Foundation +import SwiftData + +// `PersistentTokenHistoryEvent` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentTokenHistoryEvent { + @Attribute(.unique) var id: UUID + + var eventType: String + var transactionId: Data? + var blockHeight: Int64? + var coreBlockHeight: Int64? + + var fromIdentity: Data? + var toIdentity: Data? + var performedByIdentity: Data + + var amount: String? + var balanceBefore: String? + var balanceAfter: String? + + var additionalDataJSON: Data? + + var eventDescription: String? + + var createdAt: Date + var eventTimestamp: Date + + @Relationship(inverse: \PersistentToken.historyEvents) + var token: PersistentToken? + + init( + eventType: TokenEventType, + performedByIdentity: Data, + eventTimestamp: Date = Date() + ) { + self.id = UUID() + self.eventType = eventType.rawValue + self.performedByIdentity = performedByIdentity + self.eventTimestamp = eventTimestamp + self.createdAt = Date() + } + + var eventTypeEnum: TokenEventType { + TokenEventType(rawValue: eventType) ?? .unknown + } + + var fromIdentityBase58: String? { + fromIdentity?.toBase58String() + } + + var toIdentityBase58: String? { + toIdentity?.toBase58String() + } + + var performedByIdentityBase58: String { + performedByIdentity.toBase58String() + } + + var displayTitle: String { + switch eventTypeEnum { + case .mint: + return "Minted \(formattedAmount)" + case .burn: + return "Burned \(formattedAmount)" + case .transfer: + return "Transfer \(formattedAmount)" + case .freeze: + return "Frozen \(formattedAmount)" + case .unfreeze: + return "Unfrozen \(formattedAmount)" + case .destroyFrozenFunds: + return "Destroyed Frozen Funds \(formattedAmount)" + case .configUpdate: + return "Configuration Updated" + case .emergencyAction: + return "Emergency Action" + case .perpetualDistribution: + return "Perpetual Distribution \(formattedAmount)" + case .preProgrammedRelease: + return "Pre-programmed Release \(formattedAmount)" + case .directPricing: + return "Direct Pricing Updated" + case .directPurchase: + return "Direct Purchase \(formattedAmount)" + case .unknown: + return "Unknown Event" + } + } + + private var formattedAmount: String { + guard let amount = amount else { return "" } + return amount + } + + func setAdditionalData(_ data: [String: Any]) { + additionalDataJSON = try? JSONSerialization.data(withJSONObject: data) + } + + func getAdditionalData() -> [String: Any]? { + guard let data = additionalDataJSON else { return nil } + return try? JSONSerialization.jsonObject(with: data) as? [String: Any] + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTrackedMasternode.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTrackedMasternode.swift new file mode 100644 index 00000000000..a673ac09cfe --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTrackedMasternode.swift @@ -0,0 +1,42 @@ +import Foundation +import SwiftData + +// `PersistentTrackedMasternode` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentTrackedMasternode { + #Unique([\.networkRaw, \.proTxHash]) + #Index([\.networkRaw]) + + var networkRaw: UInt32 + var proTxHash: Data + var label: String? + var addedAt: UInt64 + var snapshotJSON: String + + var network: Network? { + get { Network(rawValue: networkRaw) } + set { networkRaw = newValue?.rawValue ?? networkRaw } + } + + init( + networkRaw: UInt32, + proTxHash: Data, + label: String?, + addedAt: UInt64, + snapshotJSON: String + ) { + self.networkRaw = networkRaw + self.proTxHash = proTxHash + self.label = label + self.addedAt = addedAt + self.snapshotJSON = snapshotJSON + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTransaction.swift new file mode 100644 index 00000000000..3ec3cf0fae4 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTransaction.swift @@ -0,0 +1,167 @@ +import Foundation +import SwiftData + +// `PersistentTransaction` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentTransaction { + #Index([\.firstSeen]) + + @Attribute(.unique) var txid: Data + var transactionData: Data + var context: UInt32 + var blockHeight: UInt32 + var blockHash: Data? + var blockTimestamp: UInt32 + var blockPosition: UInt32 = 0 + var hasBlockPosition: Bool = false + var direction: UInt32 + var transactionType: String + var transactionTypeKind: UInt8 = 0xFF + var netAmount: Int64 + var fee: UInt64? + var label: String + var firstSeen: UInt64 + + var providerServiceAddress: String? = nil + var providerProTxHash: Data? = nil + var providerCollateralTxid: Data? = nil + var providerCollateralVout: UInt32 = 0 + var providerOwnerKeyHash: Data? = nil + var providerVotingKeyHash: Data? = nil + + var createdAt: Date + var lastUpdated: Date + + @Relationship(deleteRule: .cascade, inverse: \PersistentTxo.transaction) + var outputs: [PersistentTxo] = [] + + @Relationship(inverse: \PersistentTxo.spendingTransaction) + var inputs: [PersistentTxo] = [] + + @Relationship(deleteRule: .cascade, inverse: \PersistentPendingInput.spendingTransaction) + var pendingInputs: [PersistentPendingInput] = [] + + @Relationship(inverse: \PersistentAccount.involvedTransactions) + var involvedAccounts: [PersistentAccount] = [] + + init( + txid: Data, + transactionData: Data, + context: UInt32 = 0, + blockHeight: UInt32 = 0, + direction: UInt32 = 0, + transactionType: String = "Standard", + netAmount: Int64 = 0, + firstSeen: UInt64 = 0 + ) { + self.txid = txid + self.transactionData = transactionData + self.context = context + self.blockHeight = blockHeight + self.blockTimestamp = 0 + self.direction = direction + self.transactionType = transactionType + self.netAmount = netAmount + self.firstSeen = firstSeen + self.label = "" + self.createdAt = Date() + self.lastUpdated = Date() + } + + var txidHex: String { + txid.reversed().map { String(format: "%02x", $0) }.joined() + } + + var contextName: String { + switch context { + case 0: return "Mempool" + case 1: return "InstantSend" + case 2: return "In Block" + case 3: return "Chain Locked" + default: return "Unknown" + } + } + + var directionName: String { + switch direction { + case 0: return "Incoming" + case 1: return "Outgoing" + case 2: return "Internal" + case 3: return "CoinJoin" + default: return "Unknown" + } + } + + var typedKind: TransactionTypeKind? { + TransactionTypeKind(rawValue: transactionTypeKind) + } + + var isAssetLock: Bool { + typedKind == .assetLock + } + + var isAssetUnlock: Bool { + typedKind == .assetUnlock + } + + var isProviderRegistration: Bool { + typedKind == .providerRegistration + } + + var isProviderUpdateService: Bool { + typedKind == .providerUpdateService + } + + var providerProTxHashHex: String? { + providerProTxHash.map { $0.reversed().map { String(format: "%02x", $0) }.joined() } + } + + var providerCollateralDisplay: String? { + guard let txid = providerCollateralTxid else { return nil } + let hex = txid.reversed().map { String(format: "%02x", $0) }.joined() + return "\(hex):\(providerCollateralVout)" + } + + var providerOwnerKeyHashHex: String? { + providerOwnerKeyHash.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + var providerVotingKeyHashHex: String? { + providerVotingKeyHash.map { $0.map { String(format: "%02x", $0) }.joined() } + } + + var isProviderSpecial: Bool { + providerSpecialName != nil + } + + var providerSpecialName: String? { + switch typedKind { + case .providerRegistration: return "Provider Registration" + case .providerUpdateRegistrar: return "Provider Update Registrar" + case .providerUpdateService: return "Provider Update Service" + case .providerUpdateRevocation: return "Provider Update Revocation" + default: return nil + } + } + + var displayDirection: String { + if isAssetLock { return "Asset Lock" } + if isAssetUnlock { return "Asset Unlock" } + if let name = providerSpecialName { return name } + return directionName + } + + var formattedAmount: String { + let dash = Double(abs(netAmount)) / 100_000_000.0 + let sign = netAmount >= 0 ? "+" : "-" + return String(format: "%@%.8f DASH", sign, dash) + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTxo.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTxo.swift new file mode 100644 index 00000000000..b730772443c --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentTxo.swift @@ -0,0 +1,99 @@ +import Foundation +import SwiftData + +// `PersistentTxo` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentTxo { + #Index([\.walletId]) + + @Attribute(.unique) var outpoint: Data + var vout: UInt32 + var amount: UInt64 + var address: String + var scriptPubKey: Data + var height: UInt32 + var isCoinbase: Bool + var isConfirmed: Bool + var isInstantLocked: Bool + var isLocked: Bool + var isSpent: Bool + var createdAt: Date + var lastUpdated: Date + + var walletId: Data = Data() + + var transaction: PersistentTransaction? + + var spendingTransaction: PersistentTransaction? + + var supersededByTxid: Data? + + var spendingInputIndex: UInt32? = nil + + var account: PersistentAccount? + + var coreAddress: PersistentCoreAddress? + + init( + transaction: PersistentTransaction, + vout: UInt32, + amount: UInt64, + address: String, + scriptPubKey: Data = Data(), + height: UInt32 = 0 + ) { + self.outpoint = Self.makeOutpoint(txid: transaction.txid, vout: vout) + self.vout = vout + self.amount = amount + self.address = address + self.scriptPubKey = scriptPubKey + self.height = height + self.isCoinbase = false + self.isConfirmed = false + self.isInstantLocked = false + self.isLocked = false + self.isSpent = false + self.createdAt = Date() + self.lastUpdated = Date() + self.transaction = transaction + } + + static func makeOutpoint(txid: Data, vout: UInt32) -> Data { + var data = Data(capacity: 36) + data.append(txid) + var v = vout.littleEndian + withUnsafeBytes(of: &v) { data.append(contentsOf: $0) } + return data + } + + var txid: Data { + if let transaction { + return transaction.txid + } + return outpoint.count >= 32 ? Data(outpoint.prefix(32)) : Data() + } + + var txidHex: String { + let rawTxid = txid + guard rawTxid.count == 32 else { return "" } + return rawTxid.reversed().map { String(format: "%02x", $0) }.joined() + } + + var outpointHex: String { + let hex = txidHex + return hex.isEmpty ? "" : "\(hex):\(vout)" + } + + var formattedAmount: String { + let dash = Double(amount) / 100_000_000.0 + return String(format: "%.8f DASH", dash) + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWallet.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWallet.swift new file mode 100644 index 00000000000..d085cd9cf01 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWallet.swift @@ -0,0 +1,95 @@ +import Foundation +import SwiftData + +// `PersistentWallet` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentWallet { + #Index([\.networkRaw], [\.walletGroupId]) + #Unique([\.walletId]) + + var walletId: Data + var walletGroupId: Data = Data() + var networkRaw: UInt32? + + var network: Network? { + get { + guard let raw = networkRaw else { return nil } + return Network(rawValue: raw) ?? .testnet + } + set { networkRaw = newValue?.rawValue } + } + var name: String? + var walletDescription: String? + var birthHeight: UInt32 + var syncedHeight: UInt32 + var lastSynced: UInt64 + var lastAppliedChainLockBytes: Data? + var lastAppliedChainLockHeight: UInt32? + var isImported: Bool = false + var seedBindingVerifiedMarker: String? + var createdAt: Date + var lastUpdated: Date + + @Relationship(deleteRule: .cascade, inverse: \PersistentAccount.wallet) + var accounts: [PersistentAccount] + + @Relationship(deleteRule: .nullify, inverse: \PersistentIdentity.wallet) + var identities: [PersistentIdentity] + + init( + walletId: Data, + walletGroupId: Data = Data(), + network: Network? = nil, + name: String? = nil, + walletDescription: String? = nil, + birthHeight: UInt32 = 0, + syncedHeight: UInt32 = 0, + isImported: Bool = false + ) { + self.walletId = walletId + self.walletGroupId = walletGroupId + self.networkRaw = network?.rawValue + self.name = name + self.walletDescription = walletDescription + self.birthHeight = birthHeight + self.syncedHeight = syncedHeight + self.lastSynced = 0 + self.isImported = isImported + self.createdAt = Date() + self.lastUpdated = Date() + self.accounts = [] + self.identities = [] + } + } +} + +extension DashSchemaSnapshotV4.PersistentWallet { + var label: String { + if let name = name, !name.isEmpty { + return name + } + let hex = walletId.prefix(4) + .map { String(format: "%02x", $0) } + .joined() + return hex.isEmpty ? "Wallet" : "Wallet \(hex)…" + } +} + +extension DashSchemaSnapshotV4.PersistentWallet { + static func predicate(walletId: Data) -> Predicate { + #Predicate { $0.walletId == walletId } + } + + static func predicate( + walletGroupId: Data + ) -> Predicate { + #Predicate { $0.walletGroupId == walletGroupId } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWalletManagerMetadata.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWalletManagerMetadata.swift new file mode 100644 index 00000000000..1a08dd6ab62 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+PersistentWalletManagerMetadata.swift @@ -0,0 +1,34 @@ +import Foundation +import SwiftData + +// `PersistentWalletManagerMetadata` exactly as schema DashSchemaSnapshotV4 registered it, generated by +// scripts/freeze_schema_models.py from the live model at commit 292b5101f3. +// Do not edit: every stored property, its optionality and default, and +// every @Attribute / @Relationship / #Unique here is an input to that +// version's checksum, and every #Index to the store's SQLite indexes; +// changing any of them re-breaks the stores this copy exists to keep +// openable. See the live model for what each column means. +extension DashSchemaSnapshotV4 { + @Model + final class PersistentWalletManagerMetadata { + @Attribute(.unique) var networkRaw: UInt32 + var combinedSyncHeight: UInt32 + var combinedSyncBlockHash: Data? + var walletCount: Int + var createdAt: Date + var lastUpdated: Date + + var network: Network { + get { Network(rawValue: networkRaw) ?? .testnet } + set { networkRaw = newValue.rawValue } + } + + init(network: Network) { + self.networkRaw = network.rawValue + self.combinedSyncHeight = 0 + self.walletCount = 0 + self.createdAt = Date() + self.lastUpdated = Date() + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+Schema.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+Schema.swift new file mode 100644 index 00000000000..1362c23bf5e --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+Schema.swift @@ -0,0 +1,47 @@ +import Foundation +import SwiftData + +// Generated release snapshot; never add alongside its live version in the migration plan. +enum DashSchemaSnapshotV4: VersionedSchema { + static var versionIdentifier: Schema.Version { Schema.Version(4, 0, 0) } + static var models: [any PersistentModel.Type] { + [ + PersistentIdentity.self, + PersistentDPNSName.self, + PersistentDashpayProfile.self, + PersistentDashpayContactProfile.self, + PersistentDashpayContactRequest.self, + PersistentDashpayPayment.self, + PersistentDashpayIgnoredSender.self, + PersistentDocument.self, + PersistentDataContract.self, + PersistentPublicKey.self, + PersistentTokenBalance.self, + PersistentKeyword.self, + PersistentToken.self, + PersistentDocumentType.self, + PersistentIndex.self, + PersistentProperty.self, + PersistentTokenHistoryEvent.self, + PersistentPlatformAddress.self, + PersistentPlatformAddressesSyncState.self, + PersistentWallet.self, + PersistentAccount.self, + PersistentCoreAddress.self, + PersistentTransaction.self, + PersistentTxo.self, + PersistentPendingInput.self, + PersistentWalletManagerMetadata.self, + PersistentShieldedNote.self, + PersistentShieldedOutgoingNote.self, + PersistentShieldedSyncState.self, + PersistentShieldedActivity.self, + PersistentShieldedViewingKey.self, + PersistentAssetLock.self, + PersistentInvitation.self, + PersistentMasternode.self, + PersistentTrackedMasternode.self, + PersistentIdentityBalanceMetadata.self + ] + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+TokenTypes.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+TokenTypes.swift new file mode 100644 index 00000000000..568f869cf26 --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/FrozenSchemas/DashSchemaSnapshotV4+TokenTypes.swift @@ -0,0 +1,165 @@ +import Foundation +import SwiftData + +// Inline value types exactly as schema DashSchemaSnapshotV4 stored them, generated +// by scripts/freeze_schema_models.py from TokenTypes.swift +// at commit 292b5101f3. SwiftData expands a stored Codable struct into composite +// attributes of the owning entity, so these shapes are inputs to that +// version's checksum just like the model's own properties. Do not edit. +extension DashSchemaSnapshotV4 { + struct ChangeControlRules: Codable, Equatable, Sendable { + var authorizedToMakeChange: String + var adminActionTakers: String + var changingAuthorizedActionTakersToNoOneAllowed: Bool + var changingAdminActionTakersToNoOneAllowed: Bool + var selfChangingAdminActionTakersAllowed: Bool + + init( + authorizedToMakeChange: String = AuthorizedActionTakers.noOne.rawValue, + adminActionTakers: String = AuthorizedActionTakers.noOne.rawValue, + changingAuthorizedActionTakersToNoOneAllowed: Bool = false, + changingAdminActionTakersToNoOneAllowed: Bool = false, + selfChangingAdminActionTakersAllowed: Bool = false + ) { + self.authorizedToMakeChange = authorizedToMakeChange + self.adminActionTakers = adminActionTakers + self.changingAuthorizedActionTakersToNoOneAllowed = changingAuthorizedActionTakersToNoOneAllowed + self.changingAdminActionTakersToNoOneAllowed = changingAdminActionTakersToNoOneAllowed + self.selfChangingAdminActionTakersAllowed = selfChangingAdminActionTakersAllowed + } + + static func mostRestrictive() -> ChangeControlRules { + return ChangeControlRules() + } + + static func contractOwnerControlled() -> ChangeControlRules { + return ChangeControlRules( + authorizedToMakeChange: AuthorizedActionTakers.contractOwner.rawValue, + adminActionTakers: AuthorizedActionTakers.noOne.rawValue, + selfChangingAdminActionTakersAllowed: true + ) + } + } + + enum AuthorizedActionTakers: String, CaseIterable, Codable, Sendable { + case noOne = "NoOne" + case contractOwner = "ContractOwner" + case mainGroup = "MainGroup" + + enum WireType { + static let noOne = "noOne" + static let contractOwner = "contractOwner" + static let identity = "identity" + static let mainGroup = "mainGroup" + static let group = "group" + } + + static func identity(_ id: Data) -> String { + return "Identity:\(id.toBase58String())" + } + + static func identity(_ base58Id: String) -> String { + return "Identity:\(base58Id)" + } + + static func group(_ position: Int) -> String { + return "Group:\(position)" + } + } + + struct TokenPerpetualDistribution: Codable, Equatable, Sendable { + var distributionType: String + var distributionRecipient: String + var enabled: Bool + var lastDistributionTime: Date? + var nextDistributionTime: Date? + + init(distributionRecipient: String = "AllEqualShare", enabled: Bool = true) { + self.distributionType = "{}" + self.distributionRecipient = distributionRecipient + self.enabled = enabled + } + } + + struct TokenPreProgrammedDistribution: Codable, Equatable, Sendable { + var distributionSchedule: [DistributionEvent] + var currentEventIndex: Int + var totalDistributed: String + var remainingToDistribute: String + var isActive: Bool + var isPaused: Bool + var isCompleted: Bool + + init() { + self.distributionSchedule = [] + self.currentEventIndex = 0 + self.totalDistributed = "0" + self.remainingToDistribute = "0" + self.isActive = true + self.isPaused = false + self.isCompleted = false + } + } + + struct DistributionEvent: Codable, Equatable, Sendable { + var id: UUID + var triggerType: String + var triggerTime: Date? + var triggerBlock: Int64? + var triggerCondition: String? + var amount: String + var recipient: String + var description: String? + + init(triggerTime: Date, amount: String, recipient: String = "AllHolders", description: String? = nil) { + self.id = UUID() + self.triggerType = "Time" + self.triggerTime = triggerTime + self.amount = amount + self.recipient = recipient + self.description = description + } + } + + struct TokenDistributionChangeRules: Codable, Equatable, Sendable { + var perpetualDistributionRules: ChangeControlRules? + var newTokensDestinationIdentityRules: ChangeControlRules? + var mintingAllowChoosingDestinationRules: ChangeControlRules? + var changeDirectPurchasePricingRules: ChangeControlRules? + + init( + perpetualDistributionRules: ChangeControlRules? = nil, + newTokensDestinationIdentityRules: ChangeControlRules? = nil, + mintingAllowChoosingDestinationRules: ChangeControlRules? = nil, + changeDirectPurchasePricingRules: ChangeControlRules? = nil + ) { + self.perpetualDistributionRules = perpetualDistributionRules + self.newTokensDestinationIdentityRules = newTokensDestinationIdentityRules + self.mintingAllowChoosingDestinationRules = mintingAllowChoosingDestinationRules + self.changeDirectPurchasePricingRules = changeDirectPurchasePricingRules + } + } + + enum TokenTradeMode: String, CaseIterable, Codable, Sendable { + case notTradeable = "NotTradeable" + + var displayName: String { + switch self { + case .notTradeable: + return "Not Tradeable" + } + } + } + + struct TokenLocalization: Codable, Equatable, Sendable { + let singularForm: String + let pluralForm: String + let description: String? + + init(singularForm: String, pluralForm: String, description: String? = nil) { + self.singularForm = singularForm + self.pluralForm = pluralForm + self.description = description + } + } +} diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift index 26a2fd54943..8a494a0a09c 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift @@ -11,18 +11,19 @@ private final class BridgeFutureMarker { var value: String = "future" init() {} } -private enum BridgeFutureV5: VersionedSchema { - static var versionIdentifier: Schema.Version { Schema.Version(5, 0, 0) } - static var models: [any PersistentModel.Type] { DashSchemaV4.models + [BridgeFutureMarker.self] } +private enum BridgeFutureV6: VersionedSchema { + static var versionIdentifier: Schema.Version { Schema.Version(6, 0, 0) } + static var models: [any PersistentModel.Type] { DashSchemaV5.models + [BridgeFutureMarker.self] } } private enum BridgeFuturePlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] { - [DashSchemaV1.self, DashSchemaV3.self, DashSchemaV4.self, BridgeFutureV5.self] + [DashSchemaV1.self, DashSchemaV3.self, DashSchemaV4.self, DashSchemaV5.self, BridgeFutureV6.self] } static var stages: [MigrationStage] { [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV3.self), .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self), - .custom(fromVersion: DashSchemaV4.self, toVersion: BridgeFutureV5.self, + .lightweight(fromVersion: DashSchemaV4.self, toVersion: DashSchemaV5.self), + .custom(fromVersion: DashSchemaV5.self, toVersion: BridgeFutureV6.self, willMigrate: { context in for wallet in try context.fetch(FetchDescriptor()) { wallet.name = "explicit future transformation" @@ -773,7 +774,7 @@ final class DashLegacySchemaMigrationTests: XCTestCase { func testSkippingV3UsesFixedBridgeThenRegisteredCustomFutureStage() throws { try withStore { url in - let schema = Schema(versionedSchema: BridgeFutureV5.self) + let schema = Schema(versionedSchema: BridgeFutureV6.self) var checkedV3 = false let configuration = ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none) XCTAssertThrowsError(try DashLegacySchemaBridge.open( diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 8e5344b165f..0cb85079011 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -75,11 +75,11 @@ final class DashModelMigrationTests: XCTestCase { SDKLogger.flush() let log = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(log.contains("event=store_open_started")) - XCTAssertTrue(log.contains("route=\"historical-v2-to-v4\"")) + XCTAssertTrue(log.contains("route=\"historical-v2-to-v5\"")) XCTAssertTrue(log.contains("source_version=\"2.0.0\"")) XCTAssertTrue(log.contains("source_checksum=\"\(sourceChecksum)\"")) - XCTAssertTrue(log.contains("target_version=\"4.0.0\"")) - XCTAssertTrue(log.contains("route=\"labelled-current-v4\"")) + XCTAssertTrue(log.contains("target_version=\"5.0.0\"")) + XCTAssertTrue(log.contains("route=\"labelled-current-v5\"")) XCTAssertEqual(log.components(separatedBy: "event=store_open_succeeded").count - 1, 2) XCTAssertFalse(log.contains("event=store_open_failed")) XCTAssertFalse(log.contains(directory.path)) @@ -95,7 +95,7 @@ final class DashModelMigrationTests: XCTestCase { let updatedLog = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(updatedLog.contains("route=\"new-store\"")) let failure = try XCTUnwrap(updatedLog.split(separator: "\n").first { $0.contains("event=store_open_failed") }) - XCTAssertTrue(failure.contains("target_version=\"4.0.0\"")) + XCTAssertTrue(failure.contains("target_version=\"5.0.0\"")) XCTAssertTrue(failure.contains("error_code=")) XCTAssertFalse(updatedLog.contains(directory.path)) XCTAssertFalse(updatedLog.contains("private-invalid-store-content")) @@ -106,7 +106,7 @@ final class DashModelMigrationTests: XCTestCase { /// either way. Labels whose route is undecidable without that probe /// (accepted V1 versus the bridge, historical V2 versus a beta layout) /// keep failing closed, leaving the store untouched. - func testCurrentV4RouteNeverDependsOnTheSchemaIdentityProbe() throws { + func testCurrentV5RouteNeverDependsOnTheSchemaIdentityProbe() throws { struct ProbeUnavailable: Error {} let failingProbe: (any VersionedSchema.Type) throws -> DashLegacySchemaBridge.Identity = { _ in throw ProbeUnavailable() @@ -117,7 +117,7 @@ final class DashModelMigrationTests: XCTestCase { defer { try? FileManager.default.removeItem(at: directory) } let current = directory.appendingPathComponent("current.store") try autoreleasepool { _ = try DashModelContainer.create(url: current) } - XCTAssertEqual(try DashLegacySchemaBridge.identity(at: current).versions, ["4.0.0"]) + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: current).versions, ["5.0.0"]) let plan = try DashModelContainer.migrationPlan( at: current, defaultPlan: DashMigrationPlan.self, identity: failingProbe) XCTAssertTrue(ObjectIdentifier(plan) == ObjectIdentifier(DashMigrationPlan.self)) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift index 7e268061af8..6b5482a4b67 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift @@ -99,8 +99,15 @@ final class DashReleasedSchemaTests: XCTestCase { } } + private enum BaseV4Plan: SchemaMigrationPlan { + static var schemas: [any VersionedSchema.Type] { [DashSchemaV3.self, DashSchemaV4.self] } + static var stages: [MigrationStage] { + [.lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self)] + } + } + @MainActor - func testShouldMigrateV3AccountingAvailabilityAndPersistAnUnavailableAmount() throws { + func testShouldMigrateV4AccountingAvailabilityAndPersistAnUnavailableAmount() throws { let fixture = try XCTUnwrap(DashReleasedSchemaRegistry.fixtures.first { $0.version.versionIdentifier == Schema.Version(3, 0, 0) }) @@ -109,12 +116,19 @@ final class DashReleasedSchemaTests: XCTestCase { defer { try? FileManager.default.removeItem(at: directory) } let url = directory.appendingPathComponent("accounting.store") try FileManager.default.copyItem(at: source(fixture), to: url) + try autoreleasepool { + let schema = Schema(versionedSchema: DashSchemaV4.self) + let container = try ModelContainer(for: schema, migrationPlan: BaseV4Plan.self, + configurations: [ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none)]) + try container.mainContext.save() + } + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["4.0.0"]) let txid = Data(repeating: 0x32, count: 32) try autoreleasepool { let container = try DashModelContainer.create(url: url) let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) .first { $0.txid == txid }) - XCTAssertNil(row.netAmountUnavailable, "V3 rows retain their stored accounting by default") + XCTAssertNil(row.netAmountUnavailable, "V4 rows retain their stored accounting by default") XCTAssertEqual(row.displayNetAmount(for: nil), row.netAmount) row.netAmountUnavailable = true try container.mainContext.save() @@ -126,7 +140,7 @@ final class DashReleasedSchemaTests: XCTestCase { XCTAssertEqual(row.netAmountUnavailable, true) XCTAssertNil(row.displayNetAmount(for: nil)) XCTAssertEqual(row.formattedAmount, "Amount unavailable") - XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["4.0.0"]) + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["5.0.0"]) } } diff --git a/packages/swift-sdk/scripts/freeze_schema_models.py b/packages/swift-sdk/scripts/freeze_schema_models.py index ef4cf8b08d2..d5c59b05b57 100755 --- a/packages/swift-sdk/scripts/freeze_schema_models.py +++ b/packages/swift-sdk/scripts/freeze_schema_models.py @@ -333,6 +333,8 @@ def emit(path, text): f"{OUT_DIR}/{freeze.schema}+{model}.swift", render_model(freeze, sha, source, model, sibling), ) + # Preserve the merged V4 model graph independently of release capture. + files.update(render_source_snapshot(root, "4.0.0", "292b5101f33b7b7a68f7ffd97df1520b2ff9ef67")) return files @@ -662,13 +664,19 @@ def render_snapshot(root, version, entry, *, inventory=None): commit = entry["platform_sha"] if not re.fullmatch(r"[0-9a-f]{40}", commit): raise SystemExit("release source must be a full Git SHA") - namespace = "DashSchemaSnapshotV" + version.split(".")[0] - if entry["namespace"] != namespace: + inventory = read_inventory(root, commit) if inventory is None else validate_inventory(inventory) + if set(inventory["models"]) != set(entry["schema"]["entity_hashes"]): + raise SystemExit("historical inventory differs from captured model membership") + if entry["namespace"] != "DashSchemaSnapshotV" + version.split(".")[0]: raise SystemExit("unexpected snapshot namespace") + return render_source_snapshot(root, version, commit, inventory=inventory) + + +def render_source_snapshot(root, version, commit, *, inventory=None): + """Freeze a source graph without asserting App Store release provenance.""" + namespace = "DashSchemaSnapshotV" + version.split(".")[0] inventory = read_inventory(root, commit) if inventory is None else validate_inventory(inventory) models = inventory["models"] - if set(models) != set(entry["schema"]["entity_hashes"]): - raise SystemExit("historical inventory differs from captured model membership") sources = inventory_sources(root, inventory, commit) validate_storage_graph(inventory, sources) names = set(models) | {name for group in inventory["value_types"] for name in group["names"]} diff --git a/packages/swift-sdk/scripts/test_freeze_schema_models.py b/packages/swift-sdk/scripts/test_freeze_schema_models.py index 128f501bd42..4d2ef3be8c6 100644 --- a/packages/swift-sdk/scripts/test_freeze_schema_models.py +++ b/packages/swift-sdk/scripts/test_freeze_schema_models.py @@ -51,7 +51,7 @@ def setUp(self): shutil.copyfile(os.path.join(ROOT, gen.TEST_REGISTRY_FILE), target) def test_should_find_the_committed_files_are_the_generators_output(self): - self.assertEqual(len(gen.render_baseline(ROOT)), 71) + self.assertEqual(len(gen.render_baseline(ROOT)), 109) self.assertIn(gen.TEST_REGISTRY_FILE, self.files) self.assertEqual(gen.check_problems(ROOT, self.files), []) From a22e3246a4b45387b6871aa26f9059ac70173d16 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:17:54 +0000 Subject: [PATCH 06/21] style(swift-sdk): align migration stage indentation Co-Authored-By: OpenAI Codex --- .../Sources/SwiftDashSDK/Persistence/DashModelContainer.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index 3614c6633cd..c45fadd05b8 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -310,7 +310,7 @@ enum DashAcceptedV1MigrationPlan: SchemaMigrationPlan { static var stages: [MigrationStage] { [.lightweight(fromVersion: DashSchemaV1.self, toVersion: DashSchemaV3.self), .lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self), - .lightweight(fromVersion: DashSchemaV4.self, toVersion: DashSchemaV5.self)] + .lightweight(fromVersion: DashSchemaV4.self, toVersion: DashSchemaV5.self)] } } From accca0d9eb6e42989c40dc260eae6d14ce5a5ed8 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:45:15 +0000 Subject: [PATCH 07/21] feat(platform-wallet): emit per-wallet transaction accounting to persistence hosts Hosts kept one transaction row per txid and re-derived each wallet's net amount and direction from their own TXO links. That accounting belongs to rs-platform-wallet, so it now hands it over ready to store: - `changeset::record_accounting` turns a record into a `WalletAccounting` (txid, net, direction, fee), or `None` for a detail-less record such as an asset lock rebuilt from its bytes at load and re-emitted on chain-lock promotion, whose net of 0 is only a placeholder. - `changeset::history_accounting` computes the same rows for a host's stored history from the restored wallet's own scripts (contact watch-only chains excluded), so rows stored before hosts kept per-wallet accounting are backfilled with the live rule. - `PlatformWalletPersistence` gains `load_core_history_for_accounting` and `store_transaction_accounting` (no-op defaults); `load_from_persistor` runs the backfill per loaded wallet and never fails the load on it. - FFI: `TransactionAccountingFFI`, `CoreHistoryTransactionFFI` and three size-gated `PersistenceCallbacksExtension` slots. The persist slot fires after each round's changeset callback with the round's rows; the frozen `TransactionRecordFFI` and `WalletChangeSetFFI` layouts are unchanged. `PLATFORM_WALLET_TRANSACTION_DIRECTION_*` export the direction values. Co-Authored-By: Claude Opus 5.5 --- .../src/core_wallet_types.rs | 75 ++- .../rs-platform-wallet-ffi/src/manager.rs | 19 +- .../rs-platform-wallet-ffi/src/persistence.rs | 512 +++++++++++++++++- .../src/sqlite/schema/core_history.rs | 6 +- .../rs-platform-wallet/src/changeset/mod.rs | 5 +- .../src/changeset/traits.rs | 34 ++ .../src/changeset/wallet_accounting.rs | 410 +++++++++++++- .../rs-platform-wallet/src/manager/load.rs | 164 +++++- 8 files changed, 1178 insertions(+), 47 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index dd56522df75..be4896c895a 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -364,6 +364,40 @@ pub struct AccountChangeSetFFI { pub has_internal_highest_used: bool, } +/// One transaction's Core accounting from ONE wallet's point of view, as +/// `platform_wallet::changeset::WalletAccounting` computes it. The host +/// persists it keyed by `(txid, wallet_id)` (the wallet is the callback's +/// `wallet_id`) and displays it; it never derives or corrects it. +/// +/// [`TransactionRecordFFI`]'s own `net_amount` / `direction` / `fee` stay +/// for hosts that predate this row. A record with no accounting evidence +/// yields NO row here, so the host keeps what it stored. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TransactionAccountingFFI { + pub txid: [u8; 32], + /// `Σ owned outputs − Σ owned inputs` in duffs (positive = received). + pub net_amount: i64, + /// 0=incoming, 1=outgoing, 2=internal, 3=coinJoin. + pub direction: u32, + /// Meaningful only when `has_fee`. + pub fee: u64, + pub has_fee: bool, +} + +/// One stored Core transaction the host hands back for the load-time +/// accounting backfill (see `on_load_core_history_for_accounting_fn`). +/// Host-owned until the matching free callback. +#[repr(C)] +pub struct CoreHistoryTransactionFFI { + /// Consensus-encoded transaction body. + pub tx_bytes: *const u8, + pub tx_bytes_len: usize, + /// The host holds no `TransactionAccountingFFI` row for this + /// transaction and wallet yet. + pub needs_accounting: bool, +} + // --------------------------------------------------------------------------- // Top-level wallet changeset // --------------------------------------------------------------------------- @@ -1525,6 +1559,40 @@ pub(crate) fn masternode_entry_v2_ffi( } } +/// `direction` wire values of [`TransactionRecordFFI`] and +/// [`TransactionAccountingFFI`], exported so hosts name them from the +/// generated header instead of re-declaring them. +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING: u32 = 0; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING: u32 = 1; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL: u32 = 2; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN: u32 = 3; + +fn direction_to_u32( + direction: key_wallet::managed_account::transaction_record::TransactionDirection, +) -> u32 { + use key_wallet::managed_account::transaction_record::TransactionDirection; + match direction { + TransactionDirection::Incoming => PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING, + TransactionDirection::Outgoing => PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING, + TransactionDirection::Internal => PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL, + TransactionDirection::CoinJoin => PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN, + } +} + +impl From<&platform_wallet::changeset::WalletAccounting> for TransactionAccountingFFI { + fn from(row: &platform_wallet::changeset::WalletAccounting) -> Self { + let mut txid = [0u8; 32]; + txid.copy_from_slice(row.txid.as_ref()); + Self { + txid, + net_amount: row.net_amount, + direction: direction_to_u32(row.direction), + fee: row.fee.unwrap_or(0), + has_fee: row.fee.is_some(), + } + } +} + fn tx_record_to_ffi( tr: &key_wallet::managed_account::transaction_record::TransactionRecord, ) -> TransactionRecordFFI { @@ -1571,12 +1639,7 @@ fn tx_record_to_ffi( } }; - let dir_val = match tr.direction { - key_wallet::managed_account::transaction_record::TransactionDirection::Incoming => 0u32, - key_wallet::managed_account::transaction_record::TransactionDirection::Outgoing => 1, - key_wallet::managed_account::transaction_record::TransactionDirection::Internal => 2, - key_wallet::managed_account::transaction_record::TransactionDirection::CoinJoin => 3, - }; + let dir_val = direction_to_u32(tr.direction); let type_str = CString::new(format!("{:?}", tr.transaction_type)) .unwrap_or_else(|_| CString::new("Unknown").unwrap()); diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 4d1adad6356..e5767267301 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -8,9 +8,10 @@ use crate::event_handler::{ }; use crate::handle::*; use crate::persistence::{ - FFIPersister, FreeTrackedMasternodesFn, LoadAssetLockOperationIdFn, - LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn, - PersistDpnsNameStatesFn, PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, + FFIPersister, FreeCoreHistoryForAccountingFn, FreeTrackedMasternodesFn, + LoadAssetLockOperationIdFn, LoadCoreHistoryForAccountingFn, LoadIdentityBalanceBlockTimeFn, + LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn, PersistDpnsNameStatesFn, + PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistTransactionAccountingFn, PersistWalletChangesetChainLockHeightFn, PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, @@ -290,6 +291,18 @@ unsafe fn persistence_extension_callbacks( on_load_asset_lock_operation_id_fn, LoadAssetLockOperationIdFn ), + persist_transaction_accounting: slot!( + on_persist_transaction_accounting_fn, + PersistTransactionAccountingFn + ), + load_core_history_for_accounting: slot!( + on_load_core_history_for_accounting_fn, + LoadCoreHistoryForAccountingFn + ), + load_core_history_for_accounting_free: slot!( + on_load_core_history_for_accounting_free_fn, + FreeCoreHistoryForAccountingFn + ), } } diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 09e06b1fe14..1846eaa2b23 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -30,11 +30,13 @@ use crate::types::{FFINetwork, Network}; use platform_wallet::changeset::provider_key_account::{ rebuild_provider_key_account, ProviderAccountRebuildError, }; +use platform_wallet::changeset::record_accounting; use platform_wallet::changeset::{ AccountAddressPoolEntry, AccountRegistrationEntry, ClientStartState, ClientWalletStartState, ListedCoreTxid, PersistenceCapabilities, PersistenceError, PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, ProviderKeyAccountEntry, - ProviderKeyExtendedPubKey, PERSISTENCE_CAPABILITIES_VERSION, + ProviderKeyExtendedPubKey, StoredCoreTransaction, WalletAccounting, + PERSISTENCE_CAPABILITIES_VERSION, }; use platform_wallet::wallet::platform_wallet::WalletId; #[cfg(feature = "shielded")] @@ -56,8 +58,9 @@ use crate::contact_persistence::{ use crate::core_address_types::{AddressPoolTypeTagFFI, CoreAddressEntryFFI, KeyTypeTagFFI}; use crate::core_wallet_types::{ build_dashpay_backfill_covered_for_callback, build_sweep_batches_for_callback, - build_utxo_credit_verdicts_for_callback, free_wallet_changeset_ffi, - DashPayBackfillCoveredContactFFI, SweepBatchFFI, UtxoCreditVerdictFFI, WalletChangeSetFFI, + build_utxo_credit_verdicts_for_callback, free_wallet_changeset_ffi, CoreHistoryTransactionFFI, + DashPayBackfillCoveredContactFFI, SweepBatchFFI, TransactionAccountingFFI, + UtxoCreditVerdictFFI, WalletChangeSetFFI, }; use crate::dashpay_payment::{build_payment_persist_entries, DashpayPaymentPersistEntryFFI}; use crate::dpns_name_state_persistence::{ @@ -319,6 +322,40 @@ pub type LoadAssetLockOperationIdFn = unsafe extern "C" fn( out_operation_id: *mut u8, ) -> i32; +/// Carries one wallet's accounting rows (see [`TransactionAccountingFFI`]). +/// The host upserts each keyed by `(txid, wallet_id)`. Fired inside a round +/// right after `on_persist_wallet_changeset_fn`, so the transaction rows the +/// accounting belongs to already exist, and only when the round has a +/// record with accounting evidence. Also fired OUTSIDE any round, once per +/// wallet at load, with the backfill computed from +/// `on_load_core_history_for_accounting_fn`; the host then commits on its +/// own. A non-zero return fails the round like any other per-kind callback. +pub type PersistTransactionAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, +) -> i32; + +/// Return the wallet's stored Core transactions for the accounting backfill: +/// an EMPTY array when every one already has an accounting row for this +/// wallet, otherwise ALL of them with `needs_accounting` set on the ones +/// missing it (the others resolve the flagged ones' inputs). The host keeps +/// the array valid until [`FreeCoreHistoryForAccountingFn`]. +pub type LoadCoreHistoryForAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, +) -> i32; + +/// Release an array returned by [`LoadCoreHistoryForAccountingFn`]. +pub type FreeCoreHistoryForAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + rows: *const CoreHistoryTransactionFFI, + count: usize, +); + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -485,6 +522,35 @@ pub struct PersistenceCallbacksExtension { out_operation_id: *mut u8, ) -> i32, >, + /// Per-wallet transaction accounting (see + /// [`PersistTransactionAccountingFn`]). Additive like the slots above: a + /// host without it keeps reading `TransactionRecordFFI`'s own values. + pub on_persist_transaction_accounting_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32, + >, + /// Stored history for the load-time accounting backfill (see + /// [`LoadCoreHistoryForAccountingFn`]). Read only together with the free + /// slot below and the persist slot above. + pub on_load_core_history_for_accounting_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, + ) -> i32, + >, + pub on_load_core_history_for_accounting_free_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + rows: *const CoreHistoryTransactionFFI, + count: usize, + ), + >, } impl Default for PersistenceCallbacksExtension { @@ -505,6 +571,9 @@ impl Default for PersistenceCallbacksExtension { on_persist_wallet_dashpay_backfill_fn: None, on_persist_asset_lock_operation_id_fn: None, on_load_asset_lock_operation_id_fn: None, + on_persist_transaction_accounting_fn: None, + on_load_core_history_for_accounting_fn: None, + on_load_core_history_for_accounting_free_fn: None, } } } @@ -526,6 +595,9 @@ pub struct PersistenceExtensionCallbacks { pub wallet_dashpay_backfill: Option, pub persist_asset_lock_operation_id: Option, pub load_asset_lock_operation_id: Option, + pub persist_transaction_accounting: Option, + pub load_core_history_for_accounting: Option, + pub load_core_history_for_accounting_free: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -1467,6 +1539,14 @@ pub struct FFIPersister { tracked_masternodes_callbacks: PersistenceExtensionCallbacks, persist_identity_balance_block_time_callback: Option, load_identity_balance_block_time_callback: Option, + /// Per-wallet accounting rows, fired after each round's changeset and by + /// the load-time backfill. `None` keeps a host on `TransactionRecordFFI`'s + /// values, as before the slot existed. + persist_transaction_accounting_callback: Option, + /// Stored-history read for the backfill; used only when the free slot is + /// wired too. + load_core_history_for_accounting_callback: Option, + load_core_history_for_accounting_free_callback: Option, /// Semantic capability declaration supplied separately from the callback /// vtable by the additive manager-create API. Keeping this out of /// `PersistenceCallbacks` preserves that established C struct's size. @@ -1592,6 +1672,10 @@ impl FFIPersister { persist_identity_balance_block_time_callback: extensions .persist_identity_balance_block_time, load_identity_balance_block_time_callback: extensions.load_identity_balance_block_time, + persist_transaction_accounting_callback: extensions.persist_transaction_accounting, + load_core_history_for_accounting_callback: extensions.load_core_history_for_accounting, + load_core_history_for_accounting_free_callback: extensions + .load_core_history_for_accounting_free, declared_capabilities, round_lock: Mutex::new(RoundGuardState::default()), } @@ -1825,6 +1909,87 @@ impl PlatformWalletPersistence for FFIPersister { Ok(()) } + fn load_core_history_for_accounting( + &self, + wallet_id: WalletId, + ) -> Result, PersistenceError> { + // Rows nobody could persist are not worth reading. + let (Some(load), Some(free), Some(_)) = ( + self.load_core_history_for_accounting_callback, + self.load_core_history_for_accounting_free_callback, + self.persist_transaction_accounting_callback, + ) else { + return Ok(Vec::new()); + }; + let mut rows_ptr: *const CoreHistoryTransactionFFI = std::ptr::null(); + let mut count: usize = 0; + let rc = unsafe { + load( + self.callbacks.context, + wallet_id.as_ptr(), + &mut rows_ptr, + &mut count, + ) + }; + if rc != 0 { + return Err(persist_callback_error( + rc, + format!("on_load_core_history_for_accounting_fn returned error code {rc}"), + )); + } + let mut out = Vec::with_capacity(count); + if !rows_ptr.is_null() && count > 0 { + let rows = unsafe { slice::from_raw_parts(rows_ptr, count) }; + for row in rows { + let bytes = unsafe { slice_from_raw(row.tx_bytes, row.tx_bytes_len) }; + match dashcore::consensus::encode::deserialize::(bytes) { + Ok(transaction) => out.push(StoredCoreTransaction { + transaction, + needs_accounting: row.needs_accounting, + }), + // One undecodable row only loses its own accounting (or a + // prevout another row would have resolved). + Err(e) => tracing::warn!( + error = %e, + "load: skipping an undecodable stored transaction in the accounting backfill" + ), + } + } + } + unsafe { free(self.callbacks.context, rows_ptr, count) }; + Ok(out) + } + + fn store_transaction_accounting( + &self, + wallet_id: WalletId, + rows: &[WalletAccounting], + ) -> Result<(), PersistenceError> { + let Some(cb) = self.persist_transaction_accounting_callback else { + return Ok(()); + }; + if rows.is_empty() { + return Ok(()); + } + let rows: Vec = + rows.iter().map(TransactionAccountingFFI::from).collect(); + let rc = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + rows.as_ptr(), + rows.len(), + ) + }; + if rc != 0 { + return Err(persist_callback_error( + rc, + format!("on_persist_transaction_accounting_fn returned error code {rc}"), + )); + } + Ok(()) + } + fn load_tracked_masternodes( &self, network: dashcore::Network, @@ -2269,6 +2434,37 @@ impl PlatformWalletPersistence for FFIPersister { } } + // Per-wallet accounting rides its own size-negotiated slot: the + // frozen `TransactionRecordFFI` stride cannot say "this record + // carries no accounting", and this row can, by being absent. + // Fired after the changeset callback so the host already holds + // the transaction rows these belong to. + if let Some(cb) = self.persist_transaction_accounting_callback { + let rows: Vec = core_cs + .records + .iter() + .filter_map(record_accounting) + .map(|row| TransactionAccountingFFI::from(&row)) + .collect(); + if !rows.is_empty() { + let result = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + rows.as_ptr(), + rows.len(), + ) + }; + if result != 0 { + eprintln!( + "Transaction accounting persistence callback returned error code {}", + result + ); + outcome.record(result); + } + } + } + // The numeric chainlock height rides its own size-negotiated // extension slot for the same layout reason the sweeps below do: // the bincode blob on the changeset struct is opaque to a @@ -9757,6 +9953,286 @@ mod tests { drop(persister); } + /// Per-wallet accounting crosses right after the round's changeset, one + /// row per record that carries details. A detail-less record (an asset + /// lock's funding record rebuilt at load and re-emitted when a chain + /// lock promotes it) crosses as NO row, so the host keeps what it stored + /// instead of overwriting it with the placeholder 0. A host without the + /// slot still succeeds. + #[test] + fn store_delivers_transaction_accounting_after_the_changeset_skipping_detailless_records() { + use dashcore::hashes::Hash as _; + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::managed_account::transaction_record::{ + OutputDetail, OutputRole, TransactionDirection, TransactionRecord, + }; + use key_wallet::transaction_checking::TransactionType; + use platform_wallet::changeset::CoreChangeSet; + use platform_wallet::test_support::core_fixtures::{ + block, foreign_address, outside, pay, spend_of, + }; + + #[derive(Default)] + struct Sink { + events: std::sync::Mutex>, + } + unsafe extern "C" fn record_changeset( + ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + let sink = &*(ctx as *const Sink); + sink.events.lock().unwrap().push("changeset".into()); + 0 + } + unsafe extern "C" fn record_accounting_rows( + ctx: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32 { + let sink = &*(ctx as *const Sink); + let wallet = *wallet_id; + let mut events = sink.events.lock().unwrap(); + for row in slice::from_raw_parts(rows, rows_count) { + events.push(format!( + "accounting wallet={wallet:02x} txid={} net={} direction={} has_fee={}", + hex::encode(row.txid), + row.net_amount, + row.direction, + row.has_fee + )); + } + 0 + } + let bip44 = AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }; + let received_tx = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let received = TransactionRecord::new( + received_tx.clone(), + bip44, + block(10), + TransactionType::Standard, + TransactionDirection::Incoming, + Vec::new(), + vec![OutputDetail { + index: 0, + role: OutputRole::Received, + address: Some(foreign_address(2)), + value: 5_000, + }], + 5_000, + ); + let restored_lock = TransactionRecord::new( + spend_of(&[outside(3)], vec![pay(9_000, &foreign_address(4))]), + bip44, + block(11), + TransactionType::AssetLock, + TransactionDirection::Internal, + Vec::new(), + Vec::new(), + 0, + ); + let round = || PlatformWalletChangeSet { + core: Some(CoreChangeSet { + records: vec![received.clone(), restored_lock.clone()], + ..Default::default() + }), + ..Default::default() + }; + + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + callbacks, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + persist_transaction_accounting: Some(record_accounting_rows), + ..Default::default() + }, + ); + persister + .store([0xAA; 32], round()) + .expect("accounting round must succeed"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec![ + "changeset".to_string(), + format!( + "accounting wallet=aa txid={} net=5000 direction=0 has_fee=false", + hex::encode(received_tx.txid().as_byte_array()) + ), + ], + ); + drop(persister); + + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let persister = FFIPersister::new_with_persistence_capabilities( + callbacks, + PersistenceCapabilities::NONE, + ); + persister + .store([0xAA; 32], round()) + .expect("slotless-host round must still succeed"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["changeset".to_string()] + ); + } + + /// The load-time backfill reads the host's stored history through the + /// load/free pair, decoding each body and keeping its flag (an + /// undecodable body is skipped, not fatal), and hands the computed rows + /// back through the persist slot. Without the persist slot nothing is + /// read: rows nobody can store are not worth computing. + #[test] + fn should_read_stored_history_and_persist_backfilled_accounting_through_the_extension() { + use key_wallet::managed_account::transaction_record::TransactionDirection; + use platform_wallet::test_support::core_fixtures::{ + foreign_address, outside, pay, spend_of, + }; + + struct Host { + bodies: Vec>, + rows: Vec, + loads: std::sync::Mutex, + frees: std::sync::Mutex, + persisted: std::sync::Mutex>, + } + unsafe extern "C" fn load( + ctx: *mut c_void, + _wallet_id: *const u8, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, + ) -> i32 { + let host = &*(ctx as *const Host); + *host.loads.lock().unwrap() += 1; + *out_rows = host.rows.as_ptr(); + *out_count = host.rows.len(); + 0 + } + unsafe extern "C" fn free( + ctx: *mut c_void, + _rows: *const CoreHistoryTransactionFFI, + _count: usize, + ) { + let host = &*(ctx as *const Host); + *host.frees.lock().unwrap() += 1; + } + unsafe extern "C" fn persist( + ctx: *mut c_void, + _wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32 { + let host = &*(ctx as *const Host); + host.persisted + .lock() + .unwrap() + .extend_from_slice(slice::from_raw_parts(rows, rows_count)); + 0 + } + + let first = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let second = spend_of(&[outside(3)], vec![pay(7_000, &foreign_address(4))]); + let mut host = Host { + bodies: vec![ + dashcore::consensus::encode::serialize(&first), + vec![0xFF], + dashcore::consensus::encode::serialize(&second), + ], + rows: Vec::new(), + loads: std::sync::Mutex::new(0), + frees: std::sync::Mutex::new(0), + persisted: std::sync::Mutex::new(Vec::new()), + }; + host.rows = host + .bodies + .iter() + .zip([true, true, false]) + .map(|(body, needs_accounting)| CoreHistoryTransactionFFI { + tx_bytes: body.as_ptr(), + tx_bytes_len: body.len(), + needs_accounting, + }) + .collect(); + let context = &host as *const Host as *mut c_void; + let extensions = PersistenceExtensionCallbacks { + persist_transaction_accounting: Some(persist), + load_core_history_for_accounting: Some(load), + load_core_history_for_accounting_free: Some(free), + ..Default::default() + }; + + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context, + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + extensions, + ); + let history = persister + .load_core_history_for_accounting([1; 32]) + .expect("history read must succeed"); + assert_eq!( + history + .iter() + .map(|stored| (stored.transaction.txid(), stored.needs_accounting)) + .collect::>(), + vec![(first.txid(), true), (second.txid(), false)] + ); + assert_eq!(*host.frees.lock().unwrap(), 1, "the host array is returned"); + + let row = WalletAccounting { + txid: first.txid(), + net_amount: -5_000, + direction: TransactionDirection::Outgoing, + fee: Some(250), + }; + persister + .store_transaction_accounting([1; 32], &[row]) + .expect("accounting persist must succeed"); + assert_eq!( + host.persisted.lock().unwrap().clone(), + vec![TransactionAccountingFFI::from(&row)] + ); + let persisted = host.persisted.lock().unwrap()[0]; + assert_eq!( + (persisted.direction, persisted.fee, persisted.has_fee), + (1, 250, true) + ); + drop(persister); + + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context, + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + persist_transaction_accounting: None, + ..extensions + }, + ); + assert!(persister + .load_core_history_for_accounting([1; 32]) + .expect("a host that cannot store rows is not read") + .is_empty()); + assert_eq!(*host.loads.lock().unwrap(), 1); + } + #[test] fn should_roundtrip_drain_operation_ids_including_consumed_tombstones() { use dashcore::hashes::Hash; @@ -10256,6 +10732,36 @@ mod tests { PersistenceCallbacksExtension, on_load_asset_lock_operation_id_fn ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ) + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs index 82544eb0b0b..d1223ca4334 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs @@ -523,10 +523,10 @@ mod tests { ); } - /// Shared with the Swift SDK's `TransactionAccountingTests` direction - /// table; pins the rule repair takes from `platform_wallet`. + /// Direction case table; pins the rule repair takes from `platform_wallet`, + /// the same one the live projection and the host-history backfill use. #[test] - fn should_classify_repaired_direction_like_the_swift_sdk() { + fn should_classify_repaired_direction_like_the_live_projection() { use TransactionDirection::{CoinJoin, Incoming, Internal, Outgoing}; use TransactionType::{AssetLock, Standard}; // (type, spends ours, has owned output, has external output, expected) diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 90550f78c8f..cb9fef3d027 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -69,4 +69,7 @@ pub use traits::{ persists_outbound_account_markers, ListedCoreTxid, PersistenceError, PersistenceErrorKind, PlatformWalletPersistence, }; -pub use wallet_accounting::{is_owned, wallet_accounting, wallet_direction}; +pub use wallet_accounting::{ + history_accounting, is_owned, record_accounting, wallet_accounting, wallet_direction, + StoredCoreTransaction, WalletAccounting, +}; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index 11e5d2c58a7..9960e0d52dd 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -8,6 +8,7 @@ use std::error::Error as StdError; use crate::changeset::changeset::{DpnsNameStateEntry, PlatformWalletChangeSet}; use crate::changeset::client_start_state::ClientStartState; use crate::changeset::persistence_capabilities::PersistenceCapabilities; +use crate::changeset::wallet_accounting::{StoredCoreTransaction, WalletAccounting}; use crate::wallet::platform_wallet::WalletId; use dashcore::Txid; use dpp::prelude::Identifier; @@ -528,6 +529,39 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(None) } + /// The wallet's persisted Core transaction history, each entry flagged + /// when the backend holds no [`WalletAccounting`] for it yet. + /// + /// Read once per wallet at load: when any entry is flagged, the manager + /// computes the missing rows with + /// [`history_accounting`](crate::changeset::history_accounting) and + /// hands them to [`Self::store_transaction_accounting`]. A backend + /// returns an EMPTY vector when nothing is flagged, so a backfilled + /// store costs one cheap read per launch. When something is flagged it + /// must return the whole history, flagged or not, because a flagged + /// spend resolves its owned inputs from the outputs of the others. + /// + /// The default (no history) suits backends that never stored + /// accounting-less rows, such as the SQLite store, whose own migration + /// repairs history. + fn load_core_history_for_accounting( + &self, + _wallet_id: WalletId, + ) -> Result, PersistenceError> { + Ok(Vec::new()) + } + + /// Persist per-wallet accounting computed outside a changeset round (the + /// load-time backfill, see [`Self::load_core_history_for_accounting`]). + /// Upsert keyed by `(txid, wallet_id)`. The default drops them. + fn store_transaction_accounting( + &self, + _wallet_id: WalletId, + _rows: &[WalletAccounting], + ) -> Result<(), PersistenceError> { + Ok(()) + } + /// Look up the persisted DPNS marketplace row for one /// `(wallet_id, wallet_identity_id, normalized_label)` triple. /// diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 48ae44a914a..9feb6a67283 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -1,11 +1,16 @@ //! Wallet-level accounting of a Core transaction record: the ownership //! predicate ([`is_owned`]), the direction rule ([`wallet_direction`]) and //! the net formula ([`wallet_accounting`]). The live projection -//! (`fold_same_txid_records`) and the SQLite repair -//! (`platform-wallet-storage`'s `core_history`) both call them, so a row +//! (`fold_same_txid_records`), the per-wallet rows handed to hosts +//! ([`record_accounting`]), the host-history backfill +//! ([`history_accounting`]) and the SQLite repair +//! (`platform-wallet-storage`'s `core_history`) all call them, so a row //! cannot change accounting when storage repairs it; they differ only on //! an `i64` overflow (live saturates, repair errors). //! +//! Hosts (the Swift and Kotlin SDKs) persist [`WalletAccounting`] keyed by +//! `(txid, wallet id)` and display it; they never derive it themselves. +//! //! Upstream `key-wallet` classifies each matched account on its own, and //! its account-local views disagree for an asset lock: the funding //! account reads a lock with no change as `Outgoing` (the OP_RETURN burn @@ -14,12 +19,38 @@ //! moves Core duffs into its own Platform credits, so only the fee //! leaves the wallet. -use std::collections::BTreeSet; +use std::collections::{BTreeMap, BTreeSet}; +use dashcore::{OutPoint, ScriptBuf, Transaction, Txid}; +use key_wallet::managed_account::managed_account_ref::ManagedAccountRef; use key_wallet::managed_account::transaction_record::{ OutputRole, TransactionDirection, TransactionRecord, }; -use key_wallet::transaction_checking::transaction_router::TransactionType; +use key_wallet::transaction_checking::transaction_router::{TransactionRouter, TransactionType}; +use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; + +/// One transaction's Core accounting from one wallet's point of view: the +/// value a host persists keyed by `(txid, wallet id)`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct WalletAccounting { + pub txid: Txid, + /// `Σ owned outputs − Σ owned inputs` in duffs, saturated to `i64`. + pub net_amount: i64, + pub direction: TransactionDirection, + /// The transaction fee, when the wallet funded every input. + pub fee: Option, +} + +/// One persisted Core transaction a host hands back for +/// [`history_accounting`]. +#[derive(Debug, Clone)] +pub struct StoredCoreTransaction { + pub transaction: Transaction, + /// The host holds no [`WalletAccounting`] for this transaction and + /// wallet yet. Transactions without the flag still resolve the + /// prevouts of the flagged ones. + pub needs_accounting: bool, +} /// Classify a transaction from the wallet's point of view. /// @@ -30,11 +61,8 @@ use key_wallet::transaction_checking::transaction_router::TransactionType; /// /// An asset lock is `Internal` whenever nothing leaves the wallet, even /// with no change output: its OP_RETURN value becomes the wallet's own -/// Platform credits. The Swift SDK's -/// `PersistentTransaction.reconciledAccounting` applies the same rule; -/// keep them in step. The case table -/// shared with Swift lives in `platform-wallet-storage` -/// (`should_classify_repaired_direction_like_the_swift_sdk`). +/// Platform credits. The case table lives in `platform-wallet-storage` +/// (`should_classify_repaired_direction_like_the_live_projection`). pub fn wallet_direction( transaction_type: TransactionType, spends_ours: bool, @@ -63,12 +91,141 @@ pub(crate) fn apply_wallet_accounting(record: &mut TransactionRecord) { return; } let (net, direction) = wallet_accounting(record); - // Unreachable for real amounts (the whole supply fits i64 many times - // over); saturate rather than panic on a corrupt record. - record.net_amount = i64::try_from(net).unwrap_or(if net < 0 { i64::MIN } else { i64::MAX }); + record.net_amount = saturating_net(net); record.direction = direction; } +/// The wallet-level accounting `record` carries, or `None` when it carries +/// none. +/// +/// A record with no input or output details is no evidence of what the +/// transaction did to the wallet: a keys-account marker, or a record the +/// load path rebuilt from persisted bytes alone (an asset lock's funding +/// transaction, a spender of its input) and re-emits when a chain lock +/// promotes it. Such a record's `net_amount` is a placeholder, so the host +/// keeps whatever accounting it already stored. +pub fn record_accounting(record: &TransactionRecord) -> Option { + if record.input_details.is_empty() && record.output_details.is_empty() { + return None; + } + let (net, direction) = wallet_accounting(record); + Some(WalletAccounting { + txid: record.txid, + net_amount: saturating_net(net), + direction, + fee: record.fee, + }) +} + +/// Accounting for each flagged transaction of a host's stored history, from +/// the wallet's own view of which scripts it owns. +/// +/// Rows a host stored before it kept per-wallet accounting carry no +/// trustworthy amount, and the restored wallet holds no records to re-emit. +/// So the host hands back the raw history and this recomputes what the live +/// projection would have emitted: +/// - an output is owned when its script belongs to one of the wallet's funds +/// accounts other than a contact's watch-only chain +/// ([`AccountType::is_contact_owned`](key_wallet::account::AccountType::is_contact_owned)); +/// - an input is owned when its prevout is an owned output of the supplied +/// history or one of the wallet's restored UTXOs; +/// - type, net and direction come from [`TransactionRouter::classify_transaction`] +/// and [`wallet_accounting`]'s rule. +/// +/// A flagged transaction that touches no owned script yields no row, like a +/// detail-less record in [`record_accounting`]. An owned input whose funding +/// transaction is missing from the history and already spent reads as +/// foreign, exactly as it would to the live projection. +pub fn history_accounting( + wallet: &ManagedWalletInfo, + history: &[StoredCoreTransaction], +) -> Vec { + let mut owned_scripts: BTreeSet = BTreeSet::new(); + let mut prevouts: BTreeMap = BTreeMap::new(); + for account in wallet.accounts.all_accounts() { + let ManagedAccountRef::Funds(funds) = account else { + continue; + }; + if account + .managed_account_type() + .to_account_type() + .is_contact_owned() + { + continue; + } + owned_scripts.extend(account.all_script_pubkeys()); + for (outpoint, utxo) in &funds.utxos { + prevouts.insert(*outpoint, utxo.txout.value); + } + } + for stored in history { + let txid = stored.transaction.txid(); + for (vout, output) in stored.transaction.output.iter().enumerate() { + if owned_scripts.contains(&output.script_pubkey) { + prevouts.insert( + OutPoint { + txid, + vout: vout as u32, + }, + output.value, + ); + } + } + } + + let mut rows = Vec::new(); + for stored in history.iter().filter(|stored| stored.needs_accounting) { + let transaction = &stored.transaction; + let spent: Vec = transaction + .input + .iter() + .filter_map(|input| prevouts.get(&input.previous_output).copied()) + .collect(); + let owned: Vec<(usize, u64)> = transaction + .output + .iter() + .enumerate() + .filter(|(_, output)| owned_scripts.contains(&output.script_pubkey)) + .map(|(index, output)| (index, output.value)) + .collect(); + if spent.is_empty() && owned.is_empty() { + continue; + } + let (net, direction) = accounting( + transaction, + TransactionRouter::classify_transaction(transaction), + &spent, + &owned, + &BTreeSet::new(), + ); + // Only a transaction funded entirely by the wallet has a fee the + // wallet can know. + let fee = (spent.len() == transaction.input.len()) + .then(|| { + let funded = spent.iter().try_fold(0u64, |sum, v| sum.checked_add(*v))?; + let paid = transaction + .output + .iter() + .try_fold(0u64, |sum, o| sum.checked_add(o.value))?; + funded.checked_sub(paid) + }) + .flatten(); + rows.push(WalletAccounting { + txid: transaction.txid(), + net_amount: saturating_net(net), + direction, + fee, + }); + } + rows +} + +/// Unreachable for real amounts (the whole supply fits i64 many times over); +/// saturate rather than panic on a corrupt record. +fn saturating_net(net: i128) -> i64 { + i64::try_from(net).unwrap_or(if net < 0 { i64::MIN } else { i64::MAX }) +} + /// Wallet-level net amount and direction of `record`, from its details. /// /// The net is `Σ owned outputs − Σ owned inputs`: the change in the @@ -77,41 +234,234 @@ pub(crate) fn apply_wallet_accounting(record: &mut TransactionRecord) { /// nets `−(credit + fee)`. The net is returned as `i128` so each caller /// picks its own policy for a value outside `i64`. pub fn wallet_accounting(record: &TransactionRecord) -> (i128, TransactionDirection) { - let owned: i128 = record + let spent: Vec = record.input_details.iter().map(|d| d.value).collect(); + let owned: Vec<(usize, u64)> = record .output_details .iter() .filter(|d| is_owned(d.role)) - .map(|d| i128::from(d.value)) - .sum(); - let spent: i128 = record - .input_details - .iter() - .map(|d| i128::from(d.value)) - .sum(); - let has_ours = record.output_details.iter().any(|d| is_owned(d.role)); - // Indexed once so the per-output check below stays linear. - let accounted: BTreeSet = record + .map(|d| (d.index as usize, d.value)) + .collect(); + let unspendable: BTreeSet = record .output_details .iter() - .filter(|d| is_owned(d.role) || d.role == OutputRole::Unspendable) + .filter(|d| d.role == OutputRole::Unspendable) .map(|d| d.index as usize) .collect(); - let has_external = record - .transaction + accounting( + &record.transaction, + record.transaction_type, + &spent, + &owned, + &unspendable, + ) +} + +/// The net formula and direction rule over a transaction's owned inputs +/// (`spent` values) and owned outputs (`(index, value)`); `unspendable` +/// outputs leave nothing the wallet could lose, like an OP_RETURN. +fn accounting( + transaction: &Transaction, + transaction_type: TransactionType, + spent: &[u64], + owned: &[(usize, u64)], + unspendable: &BTreeSet, +) -> (i128, TransactionDirection) { + let received: i128 = owned.iter().map(|(_, value)| i128::from(*value)).sum(); + let paid: i128 = spent.iter().map(|value| i128::from(*value)).sum(); + // Indexed once so the per-output check below stays linear. + let accounted: BTreeSet = owned + .iter() + .map(|(index, _)| *index) + .chain(unspendable.iter().copied()) + .collect(); + let has_external = transaction .output .iter() .enumerate() .any(|(index, output)| !output.script_pubkey.is_op_return() && !accounted.contains(&index)); let direction = wallet_direction( - record.transaction_type, - !record.input_details.is_empty(), - has_ours, + transaction_type, + !spent.is_empty(), + !owned.is_empty(), has_external, ); - (owned - spent, direction) + (received - paid, direction) } /// Whether an output with this role belongs to the wallet. pub fn is_owned(role: OutputRole) -> bool { matches!(role, OutputRole::Received | OutputRole::Change) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::changeset::changeset::fold_same_txid_records; + use crate::test_support::core_fixtures::{ + block, foreign_address, outside, pay, records_of, spend_of, wallet_with_receive_address, + }; + use key_wallet::account::{Account, AccountType}; + use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use key_wallet::Wallet; + + const FUNDING: u64 = 100_000; + const PAID: u64 = 30_000; + const CHANGE: u64 = 69_000; + const FEE: u64 = FUNDING - PAID - CHANGE; + + fn stored(transaction: &Transaction, needs_accounting: bool) -> StoredCoreTransaction { + StoredCoreTransaction { + transaction: transaction.clone(), + needs_accounting, + } + } + + /// A payment from outside into the wallet, then a spend of it that pays + /// a foreign address and returns change: what the live projection emits + /// for each, and the history the host stored. + async fn receive_then_spend() -> ( + ManagedWalletInfo, + Vec, + Transaction, + Transaction, + ) { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let xpub = wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let change = info + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .next_change_address(Some(&xpub), true) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let spend = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![pay(PAID, &foreign_address(9)), pay(CHANGE, &change)], + ); + let mut live = Vec::new(); + for (tx, height) in [(&funding, 100), (&spend, 101)] { + let mut records = records_of(&mut info, &mut wallet, tx, block(height)).await; + fold_same_txid_records(&mut records); + live.extend(records.iter().filter_map(record_accounting)); + } + (info, live, funding, spend) + } + + #[tokio::test] + async fn should_backfill_stored_history_with_the_live_projections_accounting() { + let (info, live, funding, spend) = receive_then_spend().await; + assert_eq!( + live.iter().map(|row| row.net_amount).collect::>(), + vec![FUNDING as i64, CHANGE as i64 - FUNDING as i64], + "live projection: receive, then a spend netting −(paid + fee)" + ); + + let backfilled = history_accounting(&info, &[stored(&funding, true), stored(&spend, true)]); + + assert_eq!(backfilled.len(), 2); + for (row, expected) in backfilled.iter().zip(&live) { + assert_eq!(row.txid, expected.txid); + assert_eq!(row.net_amount, expected.net_amount); + assert_eq!(row.direction, expected.direction); + } + assert_eq!(backfilled[1].direction, TransactionDirection::Outgoing); + assert_eq!( + backfilled[1].fee, + Some(FEE), + "the wallet funded every input" + ); + assert_eq!(backfilled[0].fee, None, "a foreign input hides the fee"); + } + + /// The other stored transactions resolve a flagged spend's inputs, but + /// only flagged ones get a row. + #[tokio::test] + async fn should_resolve_inputs_from_unflagged_history_and_answer_only_flagged_rows() { + let (info, live, funding, spend) = receive_then_spend().await; + + let backfilled = + history_accounting(&info, &[stored(&funding, false), stored(&spend, true)]); + + assert_eq!(backfilled.len(), 1); + assert_eq!(backfilled[0].txid, spend.txid()); + assert_eq!(backfilled[0].net_amount, live[1].net_amount); + } + + #[tokio::test] + async fn should_yield_no_row_for_a_transaction_touching_no_owned_script() { + let (info, _, _, _) = receive_then_spend().await; + let unrelated = spend_of(&[outside(3)], vec![pay(5_000, &foreign_address(4))]); + + assert!(history_accounting(&info, &[stored(&unrelated, true)]).is_empty()); + } + + /// An output on a contact's watch-only chain is the contact's money, + /// not the wallet's: paying it is outgoing, the same rule the live + /// projection applies by dropping the contact account's slice. + #[tokio::test] + async fn should_not_count_a_contacts_watch_only_output_as_owned() { + let (_, mut info, receive) = wallet_with_receive_address(); + let contact_wallet = Wallet::new_random( + dashcore::Network::Testnet, + WalletAccountCreationOptions::Default, + ) + .unwrap(); + let contact_xpub = contact_wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let account = Account::new( + None, + AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [1; 32], + friend_identity_id: [2; 32], + }, + contact_xpub, + dashcore::Network::Testnet, + ) + .unwrap(); + let contact_account = ManagedCoreFundsAccount::from_account(&account); + let contact_address = ManagedAccountRef::Funds(&contact_account) + .all_addresses() + .into_iter() + .next() + .expect("the contact chain derives addresses"); + info.accounts + .insert_funds_bearing_account(contact_account) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let to_contact = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![pay(FUNDING - FEE, &contact_address)], + ); + + let rows = history_accounting(&info, &[stored(&funding, false), stored(&to_contact, true)]); + + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].net_amount, -(FUNDING as i64)); + assert_eq!(rows[0].direction, TransactionDirection::Outgoing); + } + + /// A record rebuilt at load from bytes alone (an asset lock's funding + /// transaction) is re-emitted on chain-lock promotion with a placeholder + /// net of 0: it must reach the host as "no accounting", not as 0. + #[test] + fn should_report_no_accounting_for_a_record_without_details() { + let tx = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let record = TransactionRecord::new( + tx, + AccountType::Standard { + index: 0, + standard_account_type: key_wallet::account::StandardAccountType::BIP44Account, + }, + block(10), + TransactionType::AssetLock, + TransactionDirection::Internal, + Vec::new(), + Vec::new(), + 0, + ); + + assert_eq!(record_accounting(&record), None); + } +} diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1b612536087..e32c25762fa 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -3,7 +3,9 @@ use std::collections::BTreeMap; use std::sync::Arc; -use crate::changeset::{ClientStartState, ClientWalletStartState, PlatformWalletPersistence}; +use crate::changeset::{ + history_accounting, ClientStartState, ClientWalletStartState, PlatformWalletPersistence, +}; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; @@ -656,6 +658,12 @@ impl PlatformWalletManager

{ return Err(err); } + // Past the rollback point. Accounting is display-only, so a failure + // here is logged and never fails the load. + for (wallet_id, _) in &inserted_in_wallets { + self.backfill_history_accounting(*wallet_id).await; + } + // Past the rollback point: every registration here is one this load // actually committed, so the transactions now have a wallet to belong // to for as long as it stays registered. @@ -735,6 +743,53 @@ impl PlatformWalletManager

{ Ok(()) } + + /// Hand the persister the per-wallet accounting of every stored + /// transaction it flags as lacking one (see + /// [`PlatformWalletPersistence::load_core_history_for_accounting`]). + async fn backfill_history_accounting(&self, wallet_id: WalletId) { + let persister = Arc::clone(&self.persister); + let history = + match run_blocking_load(move || persister.load_core_history_for_accounting(wallet_id)) + .await + { + Ok(history) => history, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = ?e, + "load: reading stored history for the accounting backfill failed" + ); + return; + } + }; + if !history.iter().any(|stored| stored.needs_accounting) { + return; + } + let rows = { + let wm = self.wallet_manager.read().await; + let Some(info) = wm.get_wallet_info(&wallet_id) else { + return; + }; + history_accounting(&info.core_wallet, &history) + }; + if rows.is_empty() { + return; + } + let persister = Arc::clone(&self.persister); + let count = rows.len(); + if let Err(e) = + run_blocking_load(move || persister.store_transaction_accounting(wallet_id, &rows)) + .await + { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + rows = count, + error = ?e, + "load: persisting backfilled transaction accounting failed" + ); + } + } } /// What one re-dispatch attempt did, so a caller — and a test — can tell the @@ -1312,6 +1367,113 @@ mod idempotent_load_tests { ); } + /// Hands back a stored history for the accounting backfill and records + /// what the manager persists from it. + struct HistoryPersister { + wallet: Wallet, + managed: ManagedWalletInfo, + history: Vec, + stored: std::sync::Mutex)>>, + } + + impl PlatformWalletPersistence for HistoryPersister { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + SingleWalletPersister { + wallet: self.wallet.clone(), + managed: self.managed.clone(), + } + .load() + } + + fn load_core_history_for_accounting( + &self, + _wallet_id: WalletId, + ) -> Result, PersistenceError> { + Ok(self.history.clone()) + } + + fn store_transaction_accounting( + &self, + wallet_id: WalletId, + rows: &[crate::changeset::WalletAccounting], + ) -> Result<(), PersistenceError> { + self.stored.lock().unwrap().push((wallet_id, rows.to_vec())); + Ok(()) + } + } + + async fn load_with_history( + flagged: bool, + ) -> ( + WalletId, + dashcore::Transaction, + Vec<(WalletId, Vec)>, + ) { + let (ctx, funding) = TestWalletContext::new_random() + .with_mempool_funding(100_000) + .await; + let wallet_id = ctx.wallet.compute_wallet_id(); + let persister = Arc::new(HistoryPersister { + wallet: ctx.wallet, + managed: ctx.managed_wallet, + history: vec![crate::changeset::StoredCoreTransaction { + transaction: funding.clone(), + needs_accounting: flagged, + }], + stored: std::sync::Mutex::new(Vec::new()), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let event_handler: Arc = Arc::new(NoopTestEventHandler); + let manager = PlatformWalletManager::new(sdk, Arc::clone(&persister), event_handler); + manager + .load_from_persistor() + .await + .expect("the wallet must load"); + let stored = persister.stored.lock().unwrap().clone(); + (wallet_id, funding, stored) + } + + /// Rows the host stored before it kept per-wallet accounting are + /// backfilled at load from the restored wallet's own view of its scripts. + #[tokio::test] + async fn load_backfills_accounting_for_stored_history_the_host_flags() { + let (wallet_id, funding, stored) = load_with_history(true).await; + + assert_eq!(stored.len(), 1, "one backfill write for the wallet"); + let (written_for, rows) = &stored[0]; + assert_eq!(written_for, &wallet_id); + assert_eq!( + rows.as_slice(), + &[crate::changeset::WalletAccounting { + txid: funding.txid(), + net_amount: 100_000, + direction: + key_wallet::managed_account::transaction_record::TransactionDirection::Incoming, + fee: None, + }] + ); + } + + /// A host whose history is all accounted writes nothing. + #[tokio::test] + async fn load_skips_the_backfill_when_nothing_is_flagged() { + let (_, _, stored) = load_with_history(false).await; + + assert!(stored.is_empty()); + } + fn make_pending_manager( persister: PendingSendPersister, ) -> Arc> { From c081e8711333c6e570523c838f62f13e9d5f8783 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:45:33 +0000 Subject: [PATCH 08/21] feat(swift-sdk): persist Rust-emitted per-wallet transaction accounting SwiftData now stores the accounting rs-platform-wallet emits, one `PersistentTransactionAccounting` row per (walletId, txid), and shows it. It no longer recomputes or overwrites it. - Live schema V5 (unreleased) carries the new entity in place of the `netAmountUnavailable` marker; V4 -> V5 stays a lightweight migration. - The handler wires the three new extension slots: rows from each round stage with it, and the load-time backfill commits on its own. History is offered to Rust only when some transaction lacks this wallet's row. - `netAmount(for:)`, `direction(for:)` and `fee(for:)` are lookups that fall back to the shared last-writer scalars when Rust has emitted no row. - `upsertTransaction` writes the record's shared values as delivered. The asset-lock "recovery record" veto is gone; Rust now says "no accounting" by sending no row. - The load, round-end and TXO-heal reconcile passes and `accountingDirty` are removed. Deleting a wallet deletes its accounting rows. - `CoreDirectionCode` binds the exported `PLATFORM_WALLET_TRANSACTION_DIRECTION_*` constants instead of re-declaring the values. - Example app: the account-detail transaction rows and sheet are now scoped to their wallet, and fees come from the wallet's row. Not built here: no Swift toolchain on this host. Co-Authored-By: Claude Opus 5.5 --- packages/swift-sdk/SCHEMA_RELEASES.md | 13 +- .../Persistence/DashModelContainer.swift | 3 +- .../Models/PersistentTransaction.swift | 165 ++------- .../PersistentTransactionAccounting.swift | 48 +++ .../PlatformWallet/CoreDirectionCode.swift | 11 + .../PlatformWalletPersistenceHandler.swift | 346 +++++++++--------- .../Core/Views/AccountDetailView.swift | 4 +- .../Core/Views/TransactionDetailView.swift | 9 +- .../Core/Views/TransactionListView.swift | 7 +- packages/swift-sdk/schema-models.json | 3 +- 10 files changed, 293 insertions(+), 316 deletions(-) create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index 0714ae16766..ee711319892 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -24,12 +24,13 @@ schema. The former live V2 is accepted only when its complete graph matches frozen V3 exactly, entity hashes and checksum alike. The alias remains supported when the live graph changes; other stores labelled `2.0.0` fail closed. -V5 adds the optional `PersistentTransaction.netAmountUnavailable` marker through -a lightweight migration. Existing rows receive `nil`, preserving their stored -accounting. When deleting one participant of a shared transaction, the remaining -wallet's amount and direction are saved before the ownership links disappear. -If its amount was unresolved, the marker keeps it unavailable across restart; -an authoritative wallet record or complete accounting reconciliation clears it. +V5 adds the `PersistentTransactionAccounting` entity through a lightweight +migration: one row per `(walletId, txid)` holding the net amount, direction and +fee `rs-platform-wallet` emitted for that wallet. Migrated stores start with no +rows; at the first load Rust reads the stored history back +(`on_load_core_history_for_accounting_fn`), computes the accounting of every +transaction lacking a row and hands it to `on_persist_transaction_accounting_fn`. +Deleting a wallet deletes its rows; deleting a transaction cascades to them. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index c45fadd05b8..d21ed19427f 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -94,7 +94,8 @@ public enum DashModelContainer { PersistentInvitation.self, PersistentMasternode.self, PersistentTrackedMasternode.self, - PersistentIdentityBalanceMetadata.self + PersistentIdentityBalanceMetadata.self, + PersistentTransactionAccounting.self ] } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index cc84271d68f..bfd3811ba9c 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -84,12 +84,12 @@ public final class PersistentTransaction { /// replaces it with the real discriminant on touch. Accessors /// treat the sentinel as unknown (no branch fires). public var transactionTypeKind: UInt8 = 0xFF - /// Net Core amount in duffs across locally owned TXOs (positive=received, negative=sent). + /// Net amount in duffs (signed: positive=received, negative=sent) as the + /// last recording wallet's `TransactionRecordFFI` carried it. Shared by + /// every wallet in the transaction; per-wallet values live in + /// [`walletAccounting`]; `netAmount(for:)` prefers the wallet's row. public var netAmount: Int64 - /// Set when removing a wallet left no authoritative amount for the survivor. - /// `nil` preserves the accounting of rows migrated from V3. - public var netAmountUnavailable: Bool? = nil - /// Fee in duffs (nil if unknown). + /// Fee in duffs (nil if unknown); same last-writer caveat as `netAmount`. public var fee: UInt64? /// User-assigned label. public var label: String @@ -183,6 +183,11 @@ public final class PersistentTransaction { @Relationship(inverse: \PersistentAccount.involvedTransactions) public var involvedAccounts: [PersistentAccount] = [] + /// Rust-emitted accounting per wallet, one row per `(wallet, txid)`. + /// Cascade-deletes with the transaction. + @Relationship(deleteRule: .cascade, inverse: \PersistentTransactionAccounting.transaction) + public var walletAccounting: [PersistentTransactionAccounting] = [] + public init( txid: Data, transactionData: Data, @@ -231,93 +236,36 @@ public final class PersistentTransaction { } } - /// Core value movement for one wallet; `nil` when it cannot be derived yet. - /// - /// The stored scalar is the last recording wallet's (Rust `net_amount`, - /// or the reconciliation over its own TXOs), so it answers only when - /// `walletId` is the sole participant. Pending inputs do not veto that - /// answer: `upsertTransaction` writes one for every input whose prevout - /// has no local TXO — every foreign input of an incoming payment — and - /// never prunes them, so they cannot tell a late input of ours apart. - public func netAmount(for walletId: Data) -> Int64? { - func owned(_ rows: [PersistentTxo]) -> [PersistentTxo] { - var seen = Set() - return rows.filter { - PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) - && PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId - && seen.insert($0.outpoint).inserted - } - } - let unownedTxos = (inputs + outputs).filter { !PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) } - let isSoleParticipant = participatingWalletIds == [walletId] - if isSoleParticipant, unownedTxos.isEmpty { - return netAmountUnavailable == true ? nil : netAmount - } - let walletInputs = owned(inputs) - let walletOutputs = owned(outputs) - guard !walletInputs.isEmpty || !walletOutputs.isEmpty else { return nil } - let computed = Self.reconciledAccounting( - inputs: walletInputs, ownedOutputAmounts: walletOutputs.map(\.amount), - allOutputsOwned: false, previousDirection: direction, isAssetLock: isAssetLock - )?.netAmount - // Computed from TXOs alone: a pending input this wallet recorded may be - // one of its own still-unlinked coins, so the sum is only provisional. - // It stands when only a contact's watch-only TXOs kept the sole - // participant off its stored amount and that amount agrees with it. - // TODO(wallet-scoped-accounting-from-rust): a foreign payment to two - // local wallets leaves an unresolvable pending input per wallet, so both - // show "Amount unavailable". Store Rust's per-wallet net amount instead - // (tracked in #5226). - if pendingInputs.contains(where: { $0.walletId == walletId }) { - guard isSoleParticipant, netAmountUnavailable != true, computed == netAmount, - unownedTxos.allSatisfy(PlatformWalletPersistenceHandler.isContactTxo) else { return nil } - } - return computed + /// The accounting Rust emitted for `walletId`, if any. + public func accounting(for walletId: Data) -> PersistentTransactionAccounting? { + walletAccounting.first { $0.walletId == walletId } } - /// Direction relative to one wallet for transactions shared by multiple local wallets. + /// Core value movement for one wallet: its Rust-emitted row, else the + /// shared stored amount (a transaction Rust has no accounting for). + public func netAmount(for walletId: Data) -> Int64 { + accounting(for: walletId)?.netAmount ?? netAmount + } + + /// `CoreDirectionCode` for one wallet, with the same fallback as `netAmount(for:)`. public func direction(for walletId: Data) -> UInt32 { - guard participatingWalletIds.count > 1, direction != CoreDirectionCode.coinJoin, - typedKind != .coinJoin else { return direction } - let spendsOurs = inputs.contains { - PlatformWalletPersistenceHandler.isWalletOwnedTxo($0) - && PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId - } - if isAssetLock { - let ownedOutputs = outputs.filter(PlatformWalletPersistenceHandler.isWalletOwnedTxo) - if spendsOurs { - return ownedOutputs.contains { - PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) != walletId - } ? CoreDirectionCode.outgoing : direction - } - return ownedOutputs.contains { - PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) == walletId - } ? CoreDirectionCode.incoming : direction - } - return spendsOurs ? CoreDirectionCode.outgoing : CoreDirectionCode.incoming + accounting(for: walletId)?.direction ?? direction } - /// Retain the sole surviving wallet's accounting before ownership links disappear. - func preserveAccounting(removingWallet walletId: Data) { - let participants = participatingWalletIds - guard participants.count == 2, participants.contains(walletId), - let survivor = participants.first(where: { $0 != walletId }) else { return } - let amount = netAmount(for: survivor) - let survivorDirection = direction(for: survivor) - if let amount { netAmount = amount } - netAmountUnavailable = amount == nil - direction = survivorDirection + /// Fee for one wallet, with the same fallback as `netAmount(for:)`. + public func fee(for walletId: Data) -> UInt64? { + guard let row = accounting(for: walletId) else { return fee } + return row.fee } /// Format the wallet's Core value movement in DASH. public func formattedAmount(for walletId: Data) -> String { - guard let amount = netAmount(for: walletId) else { return "Amount unavailable" } - return Self.format(duffs: amount) + Self.format(duffs: netAmount(for: walletId)) } /// Net amount for `walletId`, or the stored scalar when no wallet scope is given. - public func displayNetAmount(for walletId: Data?) -> Int64? { - walletId.map { netAmount(for: $0) } ?? (netAmountUnavailable == true ? nil : netAmount) + public func displayNetAmount(for walletId: Data?) -> Int64 { + walletId.map { netAmount(for: $0) } ?? netAmount } /// `CoreDirectionCode` for `walletId`, or the stored direction when no wallet scope is given. @@ -325,6 +273,11 @@ public final class PersistentTransaction { walletId.map { direction(for: $0) } ?? direction } + /// Fee for `walletId`, or the stored fee when no wallet scope is given. + public func displayFee(for walletId: Data?) -> UInt64? { + walletId.map { fee(for: $0) } ?? fee + } + /// Formatted amount for `walletId`, or the stored scalar's when no wallet scope is given. public func displayFormattedAmount(for walletId: Data?) -> String { walletId.map { formattedAmount(for: $0) } ?? formattedAmount @@ -335,47 +288,6 @@ public final class PersistentTransaction { String(format: "%@%.8f DASH", duffs >= 0 ? "+" : "-", Double(duffs.magnitude) / 100_000_000) } - /// Local wallets owning one of this transaction's TXOs or having recorded it - /// (`involvedAccounts`), whose last writer's accounting is the stored scalar. - private var participatingWalletIds: Set { - let owning = (inputs + outputs).filter(PlatformWalletPersistenceHandler.isWalletOwnedTxo) - .compactMap { PlatformWalletPersistenceHandler.resolvedWalletId(of: $0) } - let recording = involvedAccounts.compactMap { account -> Data? in - let wallet: PersistentWallet? = account.wallet - return wallet?.walletId - } - return Set(owning + recording) - } - - static func reconciledAccounting( - inputs: [PersistentTxo], ownedOutputAmounts: [UInt64], - allOutputsOwned: Bool, previousDirection: UInt32, isAssetLock: Bool - ) -> (netAmount: Int64, direction: UInt32)? { - func total(_ amounts: [UInt64]) -> Int64? { - var sum: Int64 = 0 - for amount in amounts { - guard let value = Int64(exactly: amount) else { return nil } - let addition = sum.addingReportingOverflow(value) - guard !addition.overflow else { return nil } - sum = addition.partialValue - } - return sum - } - guard let received = total(ownedOutputAmounts), let spent = total(inputs.map(\.amount)) else { - return nil - } - // Same rule as Rust (`platform_wallet::changeset::wallet_direction`): - // internal only when nothing leaves the wallet and something stays in - // it, or an asset lock burns into Platform. Both sides test one table. - let direction: UInt32 - if previousDirection == CoreDirectionCode.coinJoin { direction = CoreDirectionCode.coinJoin } - else if inputs.isEmpty { direction = CoreDirectionCode.incoming } - else if allOutputsOwned && (!ownedOutputAmounts.isEmpty || isAssetLock) { - direction = CoreDirectionCode.internalTransfer - } else { direction = CoreDirectionCode.outgoing } - return (received - spent, direction) - } - public var directionName: String { switch direction { case CoreDirectionCode.incoming: return "Incoming" @@ -485,19 +397,10 @@ public final class PersistentTransaction { } public var formattedAmount: String { - netAmountUnavailable == true ? "Amount unavailable" : Self.format(duffs: netAmount) + Self.format(duffs: netAmount) } } -/// Wire values of `PersistentTransaction.direction`, matching `directionName` -/// and the FFI's `TransactionDirection` discriminants. -public enum CoreDirectionCode { - public static let incoming: UInt32 = 0 - public static let outgoing: UInt32 = 1 - public static let internalTransfer: UInt32 = 2 - public static let coinJoin: UInt32 = 3 -} - /// Typed mirror of Rust's /// `key_wallet::transaction_checking::transaction_router::TransactionType`, /// pinned to the `u8` discriminants emitted by diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift new file mode 100644 index 00000000000..e2da437cbef --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift @@ -0,0 +1,48 @@ +import Foundation +import SwiftData + +/// One transaction's Core accounting for ONE wallet, exactly as +/// `rs-platform-wallet` emitted it (`TransactionAccountingFFI`, through +/// `on_persist_transaction_accounting_fn`). +/// +/// A transaction shared by several local wallets has one row per wallet, +/// so each wallet shows its own side of it. A wallet with no row has no +/// accounting from Rust yet (a payload-only provider transaction, or a +/// stored row the load-time backfill has not reached). Pure storage: +/// nothing on the Swift side derives, corrects or vetoes these values. +@Model +public final class PersistentTransactionAccounting { + #Unique([\.walletId, \.txid]) + #Index([\.walletId], [\.txid]) + + /// The wallet this accounting belongs to (32 bytes). + public var walletId: Data + /// Transaction id, raw wire bytes like `PersistentTransaction.txid`. + public var txid: Data + /// Net Core amount in duffs for this wallet (positive = received). + public var netAmount: Int64 + /// `CoreDirectionCode` wire value for this wallet. + public var direction: UInt32 + /// Fee in duffs; `nil` unless this wallet funded every input. + public var fee: UInt64? + /// Inverse of `PersistentTransaction.walletAccounting`, which cascades. + public var transaction: PersistentTransaction? + public var lastUpdated: Date + + public init( + walletId: Data, + txid: Data, + netAmount: Int64, + direction: UInt32, + fee: UInt64?, + transaction: PersistentTransaction? + ) { + self.walletId = walletId + self.txid = txid + self.netAmount = netAmount + self.direction = direction + self.fee = fee + self.transaction = transaction + self.lastUpdated = Date() + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift new file mode 100644 index 00000000000..27f3c9abf4f --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift @@ -0,0 +1,11 @@ +import DashSDKFFI + +/// Swift names for the `direction` wire values of `PersistentTransaction` +/// and `PersistentTransactionAccounting`, bound to the constants the Rust +/// FFI exports (`PLATFORM_WALLET_TRANSACTION_DIRECTION_*`), never re-declared. +public enum CoreDirectionCode { + public static let incoming = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING) + public static let outgoing = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING) + public static let internalTransfer = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL) + public static let coinJoin = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN) +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 16bfee78e95..c0606d0955a 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -271,7 +271,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { var coreAddressesByAddress: [String: PersistentCoreAddress] = [:] } private var roundIndex: ChangesetRoundIndex? - private var accountingDirty: [Data: PersistentTransaction] = [:] /// Number of persistence rounds committed by this handler, read and /// compared on `serialQueue`. The store reconcile classifies rows off /// this queue and applies the verdicts on it; a round committed in @@ -437,8 +436,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { error: error ) backgroundContext.rollback() - // Rows staged by the failed save are gone; never reconcile them. - accountingDirty.removeAll() } } @@ -2655,11 +2652,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { record.hasBlockPosition = tx.has_block_position let blockHashBytes = hashData(tx.block_hash) record.blockHash = blockHashBytes.allSatisfy { $0 == 0 } ? nil : blockHashBytes - // Empty asset-lock recovery records cannot replace a funded debit or an unavailable amount. - let preserveLockAccounting = tx.transaction_type_kind == TransactionTypeKind.assetLock.rawValue - && tx.net_amount == 0 && !tx.has_fee - && (record.netAmount < 0 || record.netAmountUnavailable == true) - if !preserveLockAccounting { record.direction = tx.direction } + record.direction = tx.direction if let typeName = tx.transaction_type { record.transactionType = String(cString: typeName) } @@ -2681,12 +2674,10 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { record.providerVotingKeyHash = tx.has_provider_voting_key_hash ? withUnsafeBytes(of: tx.provider_voting_key_hash) { Data($0) } : nil - if !preserveLockAccounting { - record.netAmount = tx.net_amount - record.netAmountUnavailable = false - record.fee = tx.has_fee ? tx.fee : nil - } - accountingDirty[record.txid] = record + // The shared last-writer scalars, kept for unscoped display. The + // per-wallet values arrive through `persistTransactionAccounting`. + record.netAmount = tx.net_amount + record.fee = tx.has_fee ? tx.fee : nil if let labelPtr = tx.label { record.label = String(cString: labelPtr) } @@ -3030,8 +3021,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// (`reconcileHealMissingTxos`), so both writers honour the same /// tombstone precedence and spender-adoption rules. private func drainPendingInputs(into record: PersistentTxo, resolvedWalletId: Data) { - if let parent = record.transaction { accountingDirty[parent.txid] = parent } - if let spender = record.spendingTransaction { accountingDirty[spender.txid] = spender } let pendingRows = pendingInputRows(outpoint: record.outpoint) if !pendingRows.isEmpty { // A tombstone is not an observation — it is a sweep's settled @@ -3164,9 +3153,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { spender: PersistentTransaction, inputIndex: UInt32? ) { - accountingDirty[spender.txid] = spender let currentSpender = txo.spendingTransaction - if let currentSpender { accountingDirty[currentSpender.txid] = currentSpender } let verdict = Self.reconcileSpendObservation( currentSpenderTxid: currentSpender?.txid, currentSpenderContext: currentSpender?.context, @@ -3329,6 +3316,14 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { extensionCallbacks.on_load_identity_balance_block_time_fn = loadIdentityBalanceBlockTimeCallback extensionCallbacks.on_persist_asset_lock_operation_id_fn = persistAssetLockOperationIdCallback extensionCallbacks.on_load_asset_lock_operation_id_fn = loadAssetLockOperationIdCallback + // Per-wallet transaction accounting exactly as Rust computes it: + // after each round's changeset, and once at load for stored rows + // that have none yet (Rust reads the history back, computes, and + // hands the rows to the persist slot). + extensionCallbacks.on_persist_transaction_accounting_fn = persistTransactionAccountingCallback + extensionCallbacks.on_load_core_history_for_accounting_fn = loadCoreHistoryForAccountingCallback + extensionCallbacks.on_load_core_history_for_accounting_free_fn = + loadCoreHistoryForAccountingFreeCallback return extensionCallbacks } @@ -3412,11 +3407,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { self.inChangeset = true self.roundUtxoCreditVerdicts = [:] self.roundUtxoCreditTally = UtxoCreditVerdictTally() - // Out-of-round writers stage entries too (deferred backfills, a - // heal step that stopped before reconciling its own); they are - // not this round's to reconcile and may point at rows a failed - // save rolled back. Load-time accounting repairs whatever is left. - self.accountingDirty.removeAll() SDKLogger.event( "persistence_changeset_started", category: .persistence, @@ -3503,7 +3493,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { self.roundUtxoCreditVerdicts = [:] self.roundUtxoCreditTally = UtxoCreditVerdictTally() self.roundIndex = nil - self.accountingDirty.removeAll() self.roundAdvancedFinalityBoundary = false self.inChangeset = false self.drainDeferredBackfills() @@ -3546,20 +3535,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { if roundAdvancedFinalityBoundary { collectFinalizedSweptTombstones(walletId: walletId) } - // Display-only accounting: a failure here must not fail the - // round, and is reported under its own event, not save_failed. - // Recomputed values that did land are consistent on their own. - // TODO(test-round-accounting-failure): cover this catch with a - // FetchFaultInjector test; pinning which read faults needs a Swift run. - do { - try reconcileTransactionAccounting(Array(accountingDirty.values)) - } catch { - SDKLogger.event( - "persistence_transaction_accounting_failed", category: .persistence, severity: .error, - fields: ["phase": .publicText("round"), "wallet_reference": .reference(walletId)], - error: error - ) - } do { try backgroundContext.save() committedRoundGeneration &+= 1 @@ -6708,9 +6683,12 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } if let walletRow = walletRow { - // Shared scalars must be scoped while the departing wallet's TXOs still exist. - for transaction in try backgroundContext.fetch(FetchDescriptor()) { - transaction.preserveAccounting(removingWallet: walletId) + // The departing wallet's accounting rows go with it; a + // shared transaction keeps the other wallets' rows. + let accountingRows = FetchDescriptor( + predicate: #Predicate { $0.walletId == walletId }) + for row in try backgroundContext.fetch(accountingRows) { + backgroundContext.delete(row) } // Wallet → identities is `.nullify`; this delete // path cascades them explicitly. @@ -7133,77 +7111,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { return txids } - /// Repair only fully resolved spends; missing prevouts are not evidence of external ownership. - func reconcileTransactionAccounting( - _ transactions: [PersistentTransaction], txos: [Data: PersistentTxo]? = nil, - addresses: [String: PersistentCoreAddress]? = nil - ) throws { - for transaction in transactions where !transaction.isDeleted { - guard let transactionNetwork = network - ?? Self.walletNetwork(of: transaction.involvedAccounts.first) - ?? Self.walletNetwork(of: transaction.inputs.first?.account) - ?? Self.walletNetwork(of: transaction.outputs.first?.account), - let decoded = try? TransactionDecoder.decode( - transaction.transactionData, network: transactionNetwork - ), !decoded.inputs.isEmpty else { continue } - var inputs: [PersistentTxo] = [] - var complete = true - for input in decoded.inputs { - let key = PersistentTxo.makeOutpoint(txid: input.prevTxid, vout: input.prevVout) - let row: PersistentTxo? - if let txos { row = txos[key] } - else { row = try fetchTxoRowChecked(outpoint: key) } - guard let row, !row.isDeleted, Self.isWalletOwnedTxo(row) else { - complete = false - break - } - inputs.append(row) - } - guard complete else { continue } - var amounts: [UInt64] = [] - var allOutputsOwned = true - // An output counts only for the spending wallets — a linked TXO by - // its wallet, an address-matched one by its address's: another local - // wallet's credit must not hide inside the sender's scalar. - let spendingWallets = Set(inputs.compactMap { Self.resolvedWalletId(of: $0) }) - let ownedVouts = Set(transaction.outputs.filter { output -> Bool in - guard !output.isDeleted, Self.isWalletOwnedTxo(output), - let owner = Self.resolvedWalletId(of: output) else { return false } - return spendingWallets.contains(owner) - }.map(\.vout)) - for (index, output) in decoded.outputs.enumerated() { - // OP_RETURN burns (including asset locks) are not spendable Core outputs. - if output.isOpReturn { continue } - var belongs = ownedVouts.contains(UInt32(index)) - if !belongs, let address = output.address { - let descriptor = FetchDescriptor(predicate: #Predicate { $0.address == address }) - let owner: PersistentCoreAddress? - if let cached = roundIndex?.coreAddressesByAddress[address] { owner = cached } - else if let addresses { owner = addresses[address] } - else { owner = try modelFetcher.fetch(descriptor, in: backgroundContext).first } - if let account = owner?.account, account.accountType != Self.dashpayExternalAccountTypeTag { - let ownerWallet: PersistentWallet? = account.wallet - if let ownerWallet, spendingWallets.contains(ownerWallet.walletId) { - belongs = true - } - } - } - if belongs { amounts.append(output.valueDuffs) } - else { allOutputsOwned = false } - } - if let accounting = PersistentTransaction.reconciledAccounting( - inputs: inputs, ownedOutputAmounts: amounts, allOutputsOwned: allOutputsOwned, - previousDirection: transaction.typedKind == .coinJoin - ? CoreDirectionCode.coinJoin : transaction.direction, - isAssetLock: transaction.isAssetLock - ) { - transaction.netAmount = accounting.netAmount - transaction.netAmountUnavailable = false - transaction.direction = accounting.direction - } - } - } - /// Returns `(nil, 0)` if nothing is restorable. func loadWalletList() -> (entries: UnsafePointer?, count: Int, errored: Bool) { SDKLogger.event( @@ -7249,42 +7156,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { ) return (nil, 0, true) } - // Mid-round the context holds another round's staged writes: saving - // would commit half of it and rolling back would silently drop it. - // That round reconciles its own dirty rows; the next load repairs the rest. - // TODO(persist-accounting-backfill-marker): this pass re-reads all - // history on every launch; a persisted completion marker needs a - // SwiftData shape change (a new live schema version) or a side - // channel, which is a product decision. - if !inChangeset { - do { - let walletIds = Set(wallets.map(\.walletId)) - let transactions = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) - .filter { row in - walletIds.contains { Self.walletOwnsTransaction(walletId: $0, transaction: row) } - } - let txos = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) - // One read instead of one per unlinked output. - let addresses = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) - try reconcileTransactionAccounting( - transactions, - // Never trap on a duplicate outpoint; a live row wins over a deleted one. - txos: Dictionary(txos.map { ($0.outpoint, $0) }, uniquingKeysWith: { kept, other in - kept.isDeleted ? other : kept - }), - addresses: Dictionary(addresses.map { ($0.address, $0) }, uniquingKeysWith: { first, _ in first }) - ) - try backgroundContext.save() - } catch { - // Display-only accounting must never block restoring wallets: - // drop this pass's edits and restore on the stored values. - backgroundContext.rollback() - SDKLogger.event( - "persistence_transaction_accounting_failed", category: .persistence, severity: .error, - fields: ["phase": .publicText("load")], error: error - ) - } - } let restorable = wallets.filter { wallet in wallet.accounts.contains { ($0.accountExtendedPubKeyBytes?.isEmpty == false) } } @@ -9219,6 +9090,108 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// Outstanding tracked-masternode load allocations keyed by the /// entries pointer we handed to Rust. + /// Outstanding stored-history loans handed to Rust for the accounting + /// backfill, keyed by the rows pointer. + private var coreHistoryLoadAllocations: [UnsafeRawPointer: CoreHistoryLoadAllocation] = [:] + + /// Upsert one wallet's Rust-emitted accounting rows, keyed by + /// `(walletId, txid)`. Inside a round they stage with it and commit at + /// `endChangeset`; outside one (the load-time backfill) they commit here. + /// Returns `false` to fail the round, like the other per-kind callbacks. + func persistTransactionAccounting( + walletId: Data, + rows: UnsafePointer?, + count: UInt + ) -> Bool { + onQueue { + switch roundWalletLookup(walletId: walletId, callback: "transaction_accounting") { + case .failed: return false + case .absent: return true + case .found: break + } + guard count > 0, let rows else { return true } + for i in 0.. (rows: UnsafePointer?, count: Int, errored: Bool) { + onQueue { + let history: [PersistentTransaction] + do { + history = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) + .filter { Self.walletOwnsTransaction(walletId: walletId, transaction: $0) } + } catch { + SDKLogger.event( + "persistence_core_history_load_failed", category: .persistence, severity: .error, + fields: ["wallet_reference": .reference(walletId)], error: error + ) + return (nil, 0, true) + } + let flags = history.map { $0.accounting(for: walletId) == nil } + guard flags.contains(true) else { return (nil, 0, false) } + let allocation = CoreHistoryLoadAllocation() + let buf = UnsafeMutablePointer.allocate(capacity: history.count) + allocation.entries = buf + for (index, transaction) in history.enumerated() { + let body = transaction.transactionData + let bytes = UnsafeMutablePointer.allocate(capacity: max(body.count, 1)) + body.copyBytes(to: bytes, count: body.count) + allocation.buffers.append(bytes) + buf[index] = CoreHistoryTransactionFFI( + tx_bytes: UnsafePointer(bytes), + tx_bytes_len: UInt(body.count), + needs_accounting: flags[index] + ) + } + coreHistoryLoadAllocations[UnsafeRawPointer(buf)] = allocation + return (UnsafePointer(buf), history.count, false) + } + } + + /// Release a loan handed out by `loadCoreHistoryForAccounting`. + func loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer?) { + onQueue { + guard let rows, + let allocation = coreHistoryLoadAllocations.removeValue(forKey: rows) else { return } + allocation.release() + } + } + private var trackedMasternodeLoadAllocations: [UnsafeRawPointer: TrackedMasternodeLoadAllocation] = [:] @@ -9461,6 +9434,20 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// Owned allocations behind one `loadTrackedMasternodes` answer: the /// entries buffer plus every strdup'd label / snapshot string. Trivial C /// structs — deallocate only. +private final class CoreHistoryLoadAllocation { + var entries: UnsafeMutablePointer? + var buffers: [UnsafeMutablePointer] = [] + + func release() { + for buffer in buffers { + buffer.deallocate() + } + buffers.removeAll() + entries?.deallocate() + entries = nil + } +} + private final class TrackedMasternodeLoadAllocation { var entries: UnsafeMutablePointer? var count: Int = 0 @@ -11705,9 +11692,6 @@ extension PlatformWalletPersistenceHandler { ) -> CoreTxoReconcileStep { onQueue { guard !inChangeset else { return .retryLater } - // This step reconciles only what it stages: entries left by an - // earlier out-of-round writer may point at rolled-back rows. - accountingDirty.removeAll() var counts = CoreTxoHealCounts() for row in rows { guard row.txid.count == 32, !row.scriptPubKey.isEmpty, !row.address.isEmpty else { @@ -11807,21 +11791,6 @@ extension PlatformWalletPersistenceHandler { ] ) } - // A healed prevout can complete a spend, so the accounting it - // resolves commits in the same save. Display-only: a failure is - // reported under its own event and the heal still lands. - // TODO(test-heal-accounting-failure): cover this catch with a - // FetchFaultInjector test; pinning which read faults needs a Swift run. - do { - try reconcileTransactionAccounting(Array(accountingDirty.values)) - } catch { - SDKLogger.event( - "persistence_transaction_accounting_failed", category: .persistence, severity: .error, - fields: ["phase": .publicText("txo_reconcile_heal"), "wallet_reference": .reference(walletId)], - error: error - ) - } - accountingDirty.removeAll() guard counts.inserted + counts.healedSpent == 0 || reconcileSave(operation: "txo_reconcile_heal", walletId: walletId) else { @@ -12041,6 +12010,51 @@ extension PlatformWalletPersistenceHandler { } } +/// C shim for the extension's `on_persist_transaction_accounting_fn`. +private func persistTransactionAccountingCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer?, + rows: UnsafePointer?, + count: UInt +) -> Int32 { + guard let context, let walletIdPtr else { return 0 } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + return handler.persistTransactionAccounting( + walletId: Data(bytes: walletIdPtr, count: 32), rows: rows, count: count + ) ? 0 : 1 +} + +/// C shim for `on_load_core_history_for_accounting_fn`. Hands Rust a loaned +/// array released by the free shim below. +private func loadCoreHistoryForAccountingCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer?, + outRows: UnsafeMutablePointer?>?, + outCount: UnsafeMutablePointer? +) -> Int32 { + guard let context, let walletIdPtr, let outRows, let outCount else { return 1 } + outRows.pointee = nil + outCount.pointee = 0 + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + let (rows, count, errored) = handler.loadCoreHistoryForAccounting( + walletId: Data(bytes: walletIdPtr, count: 32) + ) + outRows.pointee = rows + outCount.pointee = UInt(count) + return errored ? 1 : 0 +} + +/// C shim for `on_load_core_history_for_accounting_free_fn`. +private func loadCoreHistoryForAccountingFreeCallback( + context: UnsafeMutableRawPointer?, + rows: UnsafePointer?, + _ count: UInt +) { + guard let context else { return } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + handler.loadCoreHistoryForAccountingFree(rows: rows.map(UnsafeRawPointer.init)) +} + private func persistAssetLockOperationIdCallback( context: UnsafeMutableRawPointer?, walletId: UnsafePointer?, outPoint: UnsafePointer?, operationId: UnsafePointer? diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift index e056190e51b..2b5bbc1fe7c 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift @@ -164,7 +164,7 @@ struct AccountDetailView: View { .navigationTitle(account.accountTypeName) .navigationBarTitleDisplayMode(.large) .sheet(item: $selectedTransaction) { transaction in - TransactionDetailView(transaction: transaction) + TransactionDetailView(transaction: transaction, walletId: wallet.walletId) } .sheet(isPresented: $showingPINPrompt) { PINPromptView( @@ -465,7 +465,7 @@ struct AccountDetailView: View { Button { selectedTransaction = tx } label: { - TransactionRowView(transaction: tx) + TransactionRowView(transaction: tx, walletId: wallet.walletId) } .buttonStyle(.plain) if idx < txs.count - 1 { diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift index ba2092b1f68..4f2c5753486 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift @@ -4,9 +4,8 @@ import SwiftDashSDK struct TransactionDetailView: View { let transaction: PersistentTransaction var walletId: Data? = nil - /// `nil` while this wallet's amount is unresolved — the same state the - /// amount label shows as "Amount unavailable", so fee and amount agree. - private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + /// This wallet's Rust-emitted amount (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64 { transaction.displayNetAmount(for: walletId) } private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil @@ -78,7 +77,7 @@ struct TransactionDetailView: View { } private var formattedFee: String? { - guard let fee = transaction.fee else { return nil } + guard let fee = transaction.displayFee(for: walletId) else { return nil } let dash = Double(fee) / 100_000_000.0 return String(format: "%.8f DASH", dash) } @@ -189,7 +188,7 @@ struct TransactionDetailView: View { ) } - if let fee = formattedFee, let amount = netAmount, amount < 0 { + if let fee = formattedFee, netAmount < 0 { TransactionDetailRow( label: "Network Fee", value: fee diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift index 5d84aaca7e4..e38acb46e4a 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift @@ -221,9 +221,8 @@ struct TransactionListView: View { struct TransactionRowView: View { let transaction: PersistentTransaction var walletId: Data? = nil - /// `nil` while this wallet's amount is unresolved — the same state the - /// amount label shows as "Amount unavailable", so fee and amount agree. - private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + /// This wallet's Rust-emitted amount (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64 { transaction.displayNetAmount(for: walletId) } private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil @@ -389,7 +388,7 @@ struct TransactionRowView: View { .font(.headline) .foregroundColor(typeColor) - if let fee = transaction.fee, let amount = netAmount, amount < 0 { + if let fee = transaction.displayFee(for: walletId), netAmount < 0 { Text("Fee: \(formatFee(fee))") .font(.caption2) .foregroundColor(.secondary) diff --git a/packages/swift-sdk/schema-models.json b/packages/swift-sdk/schema-models.json index 439a181f8e3..b712344a688 100644 --- a/packages/swift-sdk/schema-models.json +++ b/packages/swift-sdk/schema-models.json @@ -36,7 +36,8 @@ "PersistentInvitation": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentInvitation.swift", "PersistentMasternode": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentMasternode.swift", "PersistentTrackedMasternode": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTrackedMasternode.swift", - "PersistentIdentityBalanceMetadata": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentIdentityBalanceMetadata.swift" + "PersistentIdentityBalanceMetadata": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentIdentityBalanceMetadata.swift", + "PersistentTransactionAccounting": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift" }, "value_types": [ { From b59aa587a2ec3ad2f6baf893e754818a6fd7fb05 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:46:30 +0000 Subject: [PATCH 09/21] refactor(swift-sdk): drop the Swift-side transaction accounting derivation With accounting coming from rs-platform-wallet, the Swift copies of its rules have no callers left: - `isWalletOwnedTxo`, `isContactTxo` and `walletNetwork(of:)`: TXO ownership and contact-output detection for the deleted reconcile pass. - `DecodedTransaction.Output.isOpReturn`, added only to feed that pass. Tests follow the new contract. `TransactionAccountingTests` now covers storing, updating and looking up the emitted per-wallet rows, the out-of-round backfill commit, the history offered to Rust, a record re-emitted without accounting, and wallet deletion. The direction case table stays in Rust. The V5 schema tests cover the new entity, and the heal-time and load-time accounting expectations are reverted to the base. Not built here: no Swift toolchain on this host. Co-Authored-By: Claude Opus 5.5 --- .../KeyWallet/TransactionDecoder.swift | 3 - .../PlatformWalletPersistenceHandler.swift | 18 - .../AssetLockSpendVisibilityTests.swift | 9 +- .../CoreTxoReconcileTests.swift | 43 - .../DashModelMigrationTests.swift | 7 + .../DashReleasedSchemaTests.swift | 26 +- .../TransactionAccountingTests.swift | 779 +++--------------- 7 files changed, 164 insertions(+), 721 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift b/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift index 7224d3dc282..380deeeeff7 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift @@ -39,9 +39,6 @@ public struct DecodedTransaction: Sendable, Equatable { public let valueDuffs: UInt64 /// Raw scriptPubKey bytes. public let scriptPubkey: Data - - /// `true` for an `OP_RETURN` (0x6a) data-carrier / burn output. - var isOpReturn: Bool { scriptPubkey.first == 0x6a } } /// Transaction id in consensus (internal) byte order — reverse for diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index c0606d0955a..6322f76b003 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -112,24 +112,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { return wallet?.walletId } - /// Contact watch-only TXOs do not belong to the wallet tracking their addresses. - static func isWalletOwnedTxo(_ txo: PersistentTxo) -> Bool { - resolvedWalletId(of: txo) != nil && !isContactTxo(txo) - } - - /// A TXO on a contact's watch-only chain, by its account or its address's. - static func isContactTxo(_ txo: PersistentTxo) -> Bool { - txo.account?.accountType == dashpayExternalAccountTypeTag - || txo.coreAddress?.account?.accountType == dashpayExternalAccountTypeTag - } - - /// Network of the account's wallet, read through the same Optional cast - /// as `resolvedWalletId(of:)`. - static func walletNetwork(of account: PersistentAccount?) -> Network? { - let wallet: PersistentWallet? = account?.wallet - return wallet?.network - } - static func walletOwnsTransaction( walletId: Data, transaction: PersistentTransaction diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift index 8e927d75230..7aaafce9db8 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockSpendVisibilityTests.swift @@ -368,13 +368,8 @@ final class AssetLockSpendVisibilityTests: XCTestCase { XCTAssertEqual( injector.observedReads, - [ - // Wallet list, then the load-time accounting reconcile. - "PersistentWallet", "PersistentTransaction", "PersistentTxo", "PersistentCoreAddress", - // Unspent-TXO restore, then the faulted lock read. - "PersistentTxo", "PersistentAssetLock", - ], - "every read before the lock read must have been served — only the lock read failed" + ["PersistentWallet", "PersistentTxo", "PersistentAssetLock"], + "the wallet and unspent-TXO reads must have been served — only the lock read failed" ) XCTAssertTrue( errored, diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift index d8688d49aba..eac6a57af07 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/CoreTxoReconcileTests.swift @@ -467,49 +467,6 @@ final class CoreTxoReconcileTests: XCTestCase { XCTAssertEqual(try restoredUtxoCount(handler), 0) } - /// A healed coin can be the prevout a stored spender was missing. The - /// heal's own save then repairs that spender's amount and direction — - /// they do not stay stale until the next launch's load-time pass. - func testAHealedPrevoutRepairsItsSpendersAccounting() throws { - let (handler, container) = try makeHandler() - try seedWallet(in: container) - let coinTxid = txid(0x86) - let spenderTxid = txid(0x87) - // One input spending the missing coin, one output the wallet does not own. - var bytes = Data([2, 0, 0, 0, 1]) - bytes.append(coinTxid) - bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255, 1]) - withUnsafeBytes(of: UInt64(40).littleEndian) { bytes.append(contentsOf: $0) } - bytes.append(contentsOf: [0, 0, 0, 0, 0]) - let context = ModelContext(container) - // Stored as an incoming payment: what history showed while the coin was missing. - let spender = PersistentTransaction( - txid: spenderTxid, transactionData: bytes, context: 3, blockHeight: 2_391_800, - direction: CoreDirectionCode.incoming, netAmount: 40 - ) - context.insert(spender) - context.insert(PersistentPendingInput( - outpoint: PersistentTxo.makeOutpoint(txid: coinTxid, vout: 0), - inputIndex: 0, - spendingTxid: spenderTxid, - spendingTransaction: spender, - walletId: walletId - )) - try context.save() - let engine = FakeCoreTxoEngine(inventory: [engineUtxo(txid: coinTxid)]) - - let report = run(handler, engine: engine) - - XCTAssertTrue(report.completed) - XCTAssertEqual(report.healedSpent, 1) - // Read without a load in between: the load-time pass would repair it too. - let row = try XCTUnwrap( - ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderTxid } - ) - XCTAssertEqual(row.netAmount, -19_549, "the healed coin is the spender's only input") - XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) - } - /// A store read that fails inside the heal pass is a failure, not a /// skip: the account lookup cannot tell "no such account" from "could /// not read", so it must not answer the former when the latter diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 0cb85079011..0351eeb6143 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -454,6 +454,13 @@ final class DashModelMigrationTests: XCTestCase { ["PersistentTrackedMasternode", "PersistentIdentityBalanceMetadata"]) } + func testV5AddsPerWalletTransactionAccountingToV4() { + XCTAssertEqual( + Set(Schema(versionedSchema: DashSchemaV5.self).entities.map(\.name)) + .subtracting(Schema(versionedSchema: DashSchemaV4.self).entities.map(\.name)), + ["PersistentTransactionAccounting"]) + } + @MainActor func testV1BalanceGetsNoWatermarkUntilOneIsPersistedInLiveV3() throws { let (directory, url) = try copyFixture(Self.fixtures[0]) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift index 6b5482a4b67..ae7e3e0713f 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift @@ -106,8 +106,11 @@ final class DashReleasedSchemaTests: XCTestCase { } } + /// V4 → V5 adds the per-wallet accounting entity: migrated rows start + /// without one and keep their stored amount, and a row written after the + /// migration survives a reopen and cascades with its transaction. @MainActor - func testShouldMigrateV4AccountingAvailabilityAndPersistAnUnavailableAmount() throws { + func testShouldMigrateV4ToPerWalletAccountingRows() throws { let fixture = try XCTUnwrap(DashReleasedSchemaRegistry.fixtures.first { $0.version.versionIdentifier == Schema.Version(3, 0, 0) }) @@ -124,23 +127,32 @@ final class DashReleasedSchemaTests: XCTestCase { } XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["4.0.0"]) let txid = Data(repeating: 0x32, count: 32) + let walletId = Data(repeating: 0x07, count: 32) try autoreleasepool { let container = try DashModelContainer.create(url: url) let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) .first { $0.txid == txid }) - XCTAssertNil(row.netAmountUnavailable, "V4 rows retain their stored accounting by default") - XCTAssertEqual(row.displayNetAmount(for: nil), row.netAmount) - row.netAmountUnavailable = true + XCTAssertTrue(row.walletAccounting.isEmpty, "V4 rows carry no per-wallet accounting yet") + XCTAssertEqual(row.netAmount(for: walletId), row.netAmount) + let accounting = PersistentTransactionAccounting( + walletId: walletId, txid: txid, netAmount: -1_234, + direction: CoreDirectionCode.outgoing, fee: 34, transaction: nil + ) + container.mainContext.insert(accounting) + row.walletAccounting.append(accounting) try container.mainContext.save() } try autoreleasepool { let container = try DashModelContainer.create(url: url) let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) .first { $0.txid == txid }) - XCTAssertEqual(row.netAmountUnavailable, true) - XCTAssertNil(row.displayNetAmount(for: nil)) - XCTAssertEqual(row.formattedAmount, "Amount unavailable") + XCTAssertEqual(row.netAmount(for: walletId), -1_234) + XCTAssertEqual(row.direction(for: walletId), CoreDirectionCode.outgoing) + XCTAssertEqual(row.fee(for: walletId), 34) XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["5.0.0"]) + container.mainContext.delete(row) + try container.mainContext.save() + XCTAssertTrue(try container.mainContext.fetch(FetchDescriptor()).isEmpty) } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift index 4686f2abc39..246381c1daf 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -3,209 +3,50 @@ import SwiftData import DashSDKFFI @testable import SwiftDashSDK +/// The Swift store persists the per-wallet accounting `rs-platform-wallet` +/// emits and displays it; it never derives it. The rule itself is tested in +/// Rust (`platform_wallet::changeset::wallet_accounting`). @MainActor final class TransactionAccountingTests: XCTestCase { - private func input(_ value: UInt64, wallet: UInt8 = 1) -> PersistentTxo { - let parent = PersistentTransaction(txid: Data(repeating: wallet, count: 32), transactionData: Data()) - let txo = PersistentTxo(transaction: parent, vout: 0, amount: value, address: "", height: 1) - txo.walletId = Data(repeating: wallet, count: 32) - return txo - } - - func testShouldRepairSpentInputsWithoutChangingBalanceOrConsumption() { - let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) - let spent = input(100) - spent.isSpent = true - let result = PersistentTransaction.reconciledAccounting( - inputs: [spent], ownedOutputAmounts: [40], allOutputsOwned: false, previousDirection: 0, isAssetLock: false - ) - XCTAssertEqual(result?.netAmount, -60) - XCTAssertEqual(result?.direction, 1) - XCTAssertTrue(spent.isSpent) - XCTAssertEqual(tx.netAmount, 40) // Computing accounting has no storage side effects. - } - - func testShouldKeepInternalAndCoinJoinSemanticsWithNegativeNet() { - let spent = input(100) - XCTAssertEqual(PersistentTransaction.reconciledAccounting( - inputs: [spent], ownedOutputAmounts: [99], allOutputsOwned: true, previousDirection: 0, isAssetLock: false - )?.direction, 2) - let lock = PersistentTransaction.reconciledAccounting( - inputs: [spent], ownedOutputAmounts: [], allOutputsOwned: true, previousDirection: 2, isAssetLock: true - ) - XCTAssertEqual(lock?.netAmount, -100) - XCTAssertEqual(lock?.direction, 2) - XCTAssertEqual(PersistentTransaction.reconciledAccounting( - inputs: [spent], ownedOutputAmounts: [99], allOutputsOwned: false, previousDirection: 3, isAssetLock: false - )?.direction, 3) - } + private let walletA = Data(repeating: 1, count: 32) + private let walletB = Data(repeating: 2, count: 32) + private let txid = Data(repeating: 3, count: 32) - /// Same case table as the Rust repair's - /// `should_classify_repaired_direction_like_the_swift_sdk`. - func testShouldClassifyDirectionLikeTheRustRepair() { - let spent = input(100) - // (spends ours, owned output amounts, all outputs owned, asset lock, previous direction, expected) - let (incoming, outgoing, internalTransfer, coinJoin) = ( - CoreDirectionCode.incoming, CoreDirectionCode.outgoing, - CoreDirectionCode.internalTransfer, CoreDirectionCode.coinJoin - ) - let cases: [(Bool, [UInt64], Bool, Bool, UInt32, UInt32)] = [ - (true, [99], true, false, incoming, internalTransfer), - (true, [40], false, false, incoming, outgoing), - (true, [], true, false, incoming, outgoing), - (true, [], true, true, incoming, internalTransfer), - (true, [40], true, true, incoming, internalTransfer), - (true, [], false, true, incoming, outgoing), - (false, [40], true, false, incoming, incoming), - (true, [99], true, false, coinJoin, coinJoin), - ] - for (index, (spendsOurs, owned, allOwned, isLock, previous, expected)) in cases.enumerated() { - let result = PersistentTransaction.reconciledAccounting( - inputs: spendsOurs ? [spent] : [], ownedOutputAmounts: owned, - allOutputsOwned: allOwned, previousDirection: previous, isAssetLock: isLock - ) - XCTAssertEqual(result?.direction, expected, "case \(index)") - } - } - - func testShouldFormatSignedDuffsWithoutTrappingOnInt64Min() { - XCTAssertEqual(PersistentTransaction.format(duffs: 150_000_000), "+1.50000000 DASH") - XCTAssertEqual(PersistentTransaction.format(duffs: -100), "-0.00000100 DASH") - XCTAssertTrue(PersistentTransaction.format(duffs: .min).hasPrefix("-92233720368.")) - } - - func testShouldRejectOverflowInsteadOfWrappingHistory() { - XCTAssertNil(PersistentTransaction.reconciledAccounting( - inputs: [input(UInt64.max)], ownedOutputAmounts: [], allOutputsOwned: false, previousDirection: 0, isAssetLock: false - )) - } - - func testShouldScopeNetToWalletAndDeduplicateOutpoints() { - let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data()) - let first = input(100) - let other = input(200, wallet: 2) - tx.inputs = [first, first, other] - XCTAssertEqual(tx.netAmount(for: first.walletId), -100) - XCTAssertEqual(tx.netAmount(for: other.walletId), -200) - } - /// `burn` makes the single output an OP_RETURN, as an asset lock's is. - private func serializedSpend(inputs: [Data], outputValue: UInt64 = 40, burn: Bool = false) -> Data { - var bytes = Data([2, 0, 0, 0, UInt8(inputs.count)]) - for txid in inputs { - bytes.append(txid) - bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255]) - } - bytes.append(1) - withUnsafeBytes(of: outputValue.littleEndian) { bytes.append(contentsOf: $0) } - bytes.append(contentsOf: burn ? [1, 0x6a] : [0]) - bytes.append(contentsOf: [0, 0, 0, 0]) - return bytes - } - - func testShouldBackfillExistingHistoryOnLoadAndRemainIdempotent() throws { + private func makeStore() throws -> (ModelContainer, PlatformWalletPersistenceHandler) { let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let funding = PersistentTransaction(txid: walletId, transactionData: Data()) - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), - direction: CoreDirectionCode.incoming, netAmount: 40 - ) - let coin = PersistentTxo(transaction: funding, vout: 0, amount: 100, address: "", height: 1) - coin.walletId = walletId - coin.isSpent = true - coin.spendingTransaction = spender - context.insert(funding) - context.insert(spender) - context.insert(coin) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - XCTAssertFalse(handler.loadWalletList().errored) - let fresh = ModelContext(container) - let repaired = try XCTUnwrap(fresh.fetch(FetchDescriptor()).first { $0.txid == spender.txid }) - XCTAssertEqual(repaired.netAmount, -100) - XCTAssertEqual(repaired.direction, 1) - XCTAssertTrue(try XCTUnwrap(fresh.fetch(FetchDescriptor()).first).isSpent) - } - - func testShouldNotCommitOrDropOpenRoundWhenLoadRunsMidChangeset() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let funding = PersistentTransaction(txid: walletId, transactionData: Data()) - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), - direction: CoreDirectionCode.incoming, netAmount: 40 - ) - let coin = PersistentTxo(transaction: funding, vout: 0, amount: 100, address: "", height: 1) - coin.walletId = walletId - coin.isSpent = true - coin.spendingTransaction = spender - context.insert(funding) - context.insert(spender) - context.insert(coin) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - let identityId = Data(repeating: 9, count: 32) - let fetchIdentity = { - try ModelContext(container).fetch(FetchDescriptor()).first { $0.identityId == identityId } - } - - handler.beginChangeset(walletId: walletId) - handler.persistIdentities( - walletId: walletId, - upserts: [.init( - identityId: identityId, balance: 100, revision: 1, identityIndex: 0, label: nil, - status: 0, walletId: walletId, dpnsNames: [], dashpayProfile: nil, contactProfiles: [] - )], - removed: [] - ) - XCTAssertFalse(handler.loadWalletList().errored) - XCTAssertNil(try fetchIdentity(), "load must not commit half of an open round") - XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) - XCTAssertNotNil(try fetchIdentity(), "load must not roll back an open round") - - // The skipped full pass runs on the next load outside any round. - XCTAssertFalse(handler.loadWalletList().errored) - let repaired = try ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == spender.txid } - XCTAssertEqual(repaired?.netAmount, -100) - } - - func testShouldRestoreWalletsWhenLoadTimeAccountingFails() throws { - let container = try DashModelContainer.createInMemory() - container.mainContext.insert(PersistentWallet(walletId: Data(repeating: 1, count: 32), network: .testnet)) + container.mainContext.insert(PersistentWallet(walletId: walletA, network: .testnet)) + container.mainContext.insert(PersistentWallet(walletId: walletB, network: .testnet)) try container.mainContext.save() - let injector = FetchFaultInjector(faulting: PersistentCoreAddress.self) - let handler = PlatformWalletPersistenceHandler( - modelContainer: container, network: .testnet, modelFetcher: injector - ) - XCTAssertFalse(handler.loadWalletList().errored, "display accounting must not block restore") - XCTAssertTrue(injector.observedReads.contains("PersistentCoreAddress")) + return (container, PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet)) + } + + private func row(_ txid: Data, net: Int64, direction: UInt32, fee: UInt64? = nil) -> TransactionAccountingFFI { + var row = TransactionAccountingFFI() + withUnsafeMutableBytes(of: &row.txid) { $0.copyBytes(from: txid) } + row.net_amount = net + row.direction = direction + row.fee = fee ?? 0 + row.has_fee = fee != nil + return row + } + + /// Hand the handler `rows` the way Rust's extension slot does: inside a + /// round, or on their own like the load-time backfill. + @discardableResult + private func account( + _ handler: PlatformWalletPersistenceHandler, walletId: Data, + _ rows: [TransactionAccountingFFI], inRound: Bool = true + ) -> Bool { + if inRound { handler.beginChangeset(walletId: walletId) } + let stored = rows.withUnsafeBufferPointer { + handler.persistTransactionAccounting(walletId: walletId, rows: $0.baseAddress, count: UInt($0.count)) + } + if inRound { XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) } + return stored } - func testShouldPreserveAccountingWhenSomePrevoutsAreMissing() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let coin = input(100) - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), - transactionData: serializedSpend(inputs: [walletId, Data(repeating: 2, count: 32)]), - direction: CoreDirectionCode.outgoing, netAmount: -200 - ) - coin.spendingTransaction = spender - context.insert(coin) - context.insert(spender) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let rows = try ModelContext(container).fetch(FetchDescriptor()) - XCTAssertEqual(rows.first { $0.txid == spender.txid }?.netAmount, -200) + private func transaction(_ container: ModelContainer, _ txid: Data) throws -> PersistentTransaction { + try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == txid }) } private func persist( @@ -264,482 +105,134 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) } - func testShouldRepairLateInputAndLateOutputInSeparateAtomicRounds() throws { - let container = try DashModelContainer.createInMemory() - let walletId = Data(repeating: 1, count: 32) - container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) - try container.mainContext.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - let spenderId = Data(repeating: 3, count: 32) - persist(handler, walletId: walletId, txid: spenderId, - bytes: serializedSpend(inputs: [walletId]), net: 40, inputTxids: [walletId]) - persist(handler, walletId: walletId, txid: walletId, outputs: [(walletId, 100)]) - persist(handler, walletId: walletId, txid: spenderId, outputs: [(spenderId, 40)]) - let context = ModelContext(container) - let row = try XCTUnwrap(context.fetch(FetchDescriptor()).first { $0.txid == spenderId }) - XCTAssertEqual(row.netAmount, -60) - XCTAssertEqual(row.direction, 2) - XCTAssertEqual(row.inputs.count, 1) - XCTAssertEqual(row.outputs.count, 1) - XCTAssertTrue(try XCTUnwrap(row.inputs.first).isSpent) - } + func testShouldStoreEachWalletsEmittedAccountingSeparately() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100) + persist(handler, walletId: walletB, txid: txid, bytes: Data([1]), net: 90) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.outgoing, fee: 10)]) + account(handler, walletId: walletB, [row(txid, net: 90, direction: CoreDirectionCode.incoming)]) - func testShouldPreserveFundedAssetLockAccountingDuringSyntheticReplayWithMissingPrevout() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let spenderId = Data(repeating: 3, count: 32) - let bytes = serializedSpend(inputs: [walletId, Data(repeating: 2, count: 32)], outputValue: 0, burn: true) - let spender = PersistentTransaction( - txid: spenderId, transactionData: bytes, - direction: CoreDirectionCode.internalTransfer, netAmount: -200 - ) - spender.transactionTypeKind = 6 - let coin = input(100) - coin.spendingTransaction = spender - context.insert(coin) - context.insert(spender) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - persist(handler, walletId: walletId, txid: spenderId, bytes: bytes, kind: 6, inputTxids: [walletId]) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) - XCTAssertEqual(row.netAmount, -200) - XCTAssertEqual(row.direction, 2) - XCTAssertEqual(row.context, 2) + let stored = try transaction(container, txid) + XCTAssertEqual(stored.walletAccounting.count, 2) + XCTAssertEqual(stored.netAmount(for: walletA), -100) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.outgoing) + XCTAssertEqual(stored.fee(for: walletA), 10) + XCTAssertEqual(stored.netAmount(for: walletB), 90) + XCTAssertEqual(stored.direction(for: walletB), CoreDirectionCode.incoming) + XCTAssertNil(stored.fee(for: walletB), "a wallet that funded nothing knows no fee") + XCTAssertEqual(stored.displayFormattedAmount(for: walletB), "+0.00000090 DASH") } - func testShouldPreserveAssetLockDebitWhenNoInputIsLinkedYet() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let spenderId = Data(repeating: 3, count: 32) - let bytes = serializedSpend(inputs: [Data(repeating: 2, count: 32)], outputValue: 0, burn: true) - let lock = PersistentTransaction( - txid: spenderId, transactionData: bytes, - direction: CoreDirectionCode.internalTransfer, netAmount: -200 - ) - lock.transactionTypeKind = 6 - lock.fee = 7 - context.insert(lock) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - persist(handler, walletId: walletId, txid: spenderId, bytes: bytes, kind: 6) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) - XCTAssertEqual(row.netAmount, -200, "a synthetic zero update must not erase the debit") - XCTAssertEqual(row.fee, 7) - XCTAssertEqual(row.direction, 2) - } + func testShouldUpdateTheWalletsRowInsteadOfAddingAnother() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) + account(handler, walletId: walletA, [row(txid, net: -60, direction: CoreDirectionCode.outgoing)]) - func testShouldRepairNoChangeAssetLockToFullCoreDebit() throws { - let container = try DashModelContainer.createInMemory() - let walletId = Data(repeating: 1, count: 32) - container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) - try container.mainContext.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - let spenderId = Data(repeating: 3, count: 32) - persist(handler, walletId: walletId, txid: walletId, outputs: [(walletId, 100)]) - persist(handler, walletId: walletId, txid: spenderId, - bytes: serializedSpend(inputs: [walletId], outputValue: 0, burn: true), kind: 6, inputTxids: [walletId]) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spenderId }) - XCTAssertEqual(row.netAmount, -100) - XCTAssertEqual(row.direction, 2) - XCTAssertTrue(row.isAssetLock) + let stored = try transaction(container, txid) + XCTAssertEqual(stored.walletAccounting.count, 1) + XCTAssertEqual(stored.netAmount(for: walletA), -60) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.outgoing) } - func testShouldReportSoleWalletsStoredAmountDespitePendingInputs() { - let walletId = Data(repeating: 1, count: 32) - let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) - let change = PersistentTxo(transaction: tx, vout: 0, amount: 40, address: "", height: 1) - change.walletId = walletId - tx.outputs = [change] - tx.pendingInputs = [PersistentPendingInput( - outpoint: Data(repeating: 9, count: 36), inputIndex: 0, - spendingTxid: tx.txid, spendingTransaction: tx, walletId: walletId - )] - XCTAssertEqual(tx.netAmount(for: walletId), 40, "the sole wallet's stored amount is Rust's net_amount") - } + /// The load-time backfill arrives outside any round and commits itself. + func testShouldCommitAccountingDeliveredOutsideARound() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) - func testShouldNotComputeSharedAmountWhileOwnInputsArePending() { - let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) - let tx = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data(), netAmount: 40) - let toA = PersistentTxo(transaction: tx, vout: 0, amount: 40, address: "", height: 1) - toA.walletId = walletA - let toB = PersistentTxo(transaction: tx, vout: 1, amount: 60, address: "", height: 1) - toB.walletId = walletB - tx.outputs = [toA, toB] - tx.pendingInputs = [PersistentPendingInput( - outpoint: Data(repeating: 9, count: 36), inputIndex: 0, - spendingTxid: tx.txid, spendingTransaction: tx, walletId: walletA - )] - XCTAssertNil(tx.netAmount(for: walletA), "an unlinked input A recorded may be A's own coin") - XCTAssertEqual(tx.netAmount(for: walletB), 60) - } + XCTAssertTrue(account(handler, walletId: walletA, + [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], inRound: false)) - func testShouldKeepIncomingAmountWhenInputsAreForeign() throws { - let container = try DashModelContainer.createInMemory() - let walletId = Data(repeating: 1, count: 32) - container.mainContext.insert(PersistentWallet(walletId: walletId, network: .testnet)) - try container.mainContext.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - let paymentId = Data(repeating: 3, count: 32) - let foreign = Data(repeating: 2, count: 32) - persist(handler, walletId: walletId, txid: paymentId, - bytes: serializedSpend(inputs: [foreign]), net: 40, - inputTxids: [foreign], outputs: [(paymentId, 40)]) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == paymentId }) - XCTAssertEqual(row.netAmount, 40) - XCTAssertEqual(row.netAmount(for: walletId), 40, "a foreign input is not an unresolved input of ours") + XCTAssertEqual(try transaction(container, txid).netAmount(for: walletA), -60) } - private func localTransfer(in context: ModelContext, assetLock: Bool = false) throws -> PersistentTransaction { - let walletA = PersistentWallet(walletId: Data(repeating: 1, count: 32), network: .testnet) - let walletB = PersistentWallet(walletId: Data(repeating: 2, count: 32), network: .testnet) - let accountA = PersistentAccount(wallet: walletA, accountType: 0, accountIndex: 0, accountTypeName: "Standard") - let accountB = PersistentAccount(wallet: walletB, accountType: 0, accountIndex: 0, accountTypeName: "Standard") - context.insert(walletA) - context.insert(walletB) - context.insert(accountA) - context.insert(accountB) - let tx = PersistentTransaction( - txid: Data(repeating: 3, count: 32), - transactionData: serializedSpend(inputs: [walletA.walletId], outputValue: 90), - direction: CoreDirectionCode.internalTransfer, netAmount: -10 - ) - tx.transactionTypeKind = assetLock ? TransactionTypeKind.assetLock.rawValue : TransactionTypeKind.standard.rawValue - let coin = input(100) - coin.account = accountA - coin.isSpent = true - coin.spendingTransaction = tx - let credit = PersistentTxo(transaction: tx, vout: 0, amount: 90, address: "", height: 1) - credit.walletId = walletB.walletId - credit.account = accountB - context.insert(tx) - context.insert(coin) - context.insert(credit) - tx.involvedAccounts = [accountA, accountB] - try context.save() - return tx - } + func testShouldIgnoreAccountingForATransactionTheStoreDoesNotHold() throws { + let (container, handler) = try makeStore() - func testShouldKeepSurvivingWalletAccountingAfterDeletingEitherTransferWallet() throws { - for deletedWallet in [UInt8(1), UInt8(2)] { - let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) - try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) - defer { try? FileManager.default.removeItem(at: directory) } - let url = directory.appendingPathComponent("wallet.store") - let survivor = Data(repeating: deletedWallet == 1 ? 2 : 1, count: 32) - let amount: Int64 = deletedWallet == 1 ? 90 : -100 - let direction = deletedWallet == 1 ? CoreDirectionCode.incoming : CoreDirectionCode.outgoing - try autoreleasepool { - let container = try DashModelContainer.create(url: url) - _ = try localTransfer(in: container.mainContext) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - try handler.deleteWalletData(walletId: Data(repeating: deletedWallet, count: 32)) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == Data(repeating: 3, count: 32) }) - XCTAssertEqual(row.netAmount(for: survivor), amount) - XCTAssertEqual(row.direction(for: survivor), direction) - } - try autoreleasepool { - let container = try DashModelContainer.create(url: url) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == Data(repeating: 3, count: 32) }) - XCTAssertEqual(row.netAmount(for: survivor), amount) - XCTAssertEqual(row.direction(for: survivor), direction) - } - } - } + XCTAssertTrue(account(handler, walletId: walletA, [row(txid, net: 5, direction: CoreDirectionCode.incoming)])) - func testShouldKeepUnavailableAmountAfterDeletingAnotherParticipant() throws { - let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) - try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) - defer { try? FileManager.default.removeItem(at: directory) } - let url = directory.appendingPathComponent("wallet.store") - let survivor = Data(repeating: 2, count: 32) - try autoreleasepool { - let container = try DashModelContainer.create(url: url) - let tx = try localTransfer(in: container.mainContext, assetLock: true) - let foreign = Data(repeating: 9, count: 32) - tx.transactionData = serializedSpend(inputs: [Data(repeating: 1, count: 32), foreign], outputValue: 90) - tx.netAmount = 0 - let pending = PersistentPendingInput( - outpoint: PersistentTxo.makeOutpoint(txid: foreign, vout: 0), inputIndex: 1, - spendingTxid: tx.txid, spendingTransaction: tx, walletId: survivor - ) - container.mainContext.insert(pending) - try container.mainContext.save() - XCTAssertNil(tx.netAmount(for: survivor)) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - try handler.deleteWalletData(walletId: Data(repeating: 1, count: 32)) - // An empty asset-lock recovery update must not invent accounting. - persist(handler, walletId: survivor, txid: tx.txid, bytes: tx.transactionData, kind: 6) - } - try autoreleasepool { - let container = try DashModelContainer.create(url: url) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == Data(repeating: 3, count: 32) }) - XCTAssertNil(row.netAmount(for: survivor)) - XCTAssertEqual(row.formattedAmount(for: survivor), "Amount unavailable") - // A new authoritative wallet record can make the amount available. - persist(handler, walletId: survivor, txid: row.txid, bytes: row.transactionData, net: 90, kind: 6) - let updated = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == row.txid }) - XCTAssertEqual(updated.netAmount(for: survivor), 90) - } + XCTAssertTrue(try ModelContext(container).fetch(FetchDescriptor()).isEmpty) } - func testShouldScopeSharedAssetLockDirectionBeforeAndAfterReconciliation() throws { - let container = try DashModelContainer.createInMemory() - let tx = try localTransfer(in: container.mainContext, assetLock: true) - let walletA = Data(repeating: 1, count: 32) - let walletB = Data(repeating: 2, count: 32) - // A's Rust record is outgoing, and reconciliation keeps the stored scalar the sender's. - tx.direction = CoreDirectionCode.outgoing - XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.outgoing) - XCTAssertEqual(tx.direction(for: walletB), CoreDirectionCode.incoming) - try container.mainContext.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == tx.txid }) - XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) - XCTAssertEqual(row.direction(for: walletA), CoreDirectionCode.outgoing) - XCTAssertEqual(row.direction(for: walletB), CoreDirectionCode.incoming) - } + /// A record Rust re-emits without accounting (an asset lock restored from + /// its bytes, promoted by a chain lock) overwrites only the shared + /// scalar; the wallet's row, which Rust did not resend, still answers. + func testShouldKeepTheWalletsRowWhenARecordArrivesWithoutAccounting() throws { + let (container, handler) = try makeStore() + let assetLock = TransactionTypeKind.assetLock.rawValue + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100, kind: assetLock) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.internalTransfer)]) + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 0, kind: assetLock) - func testShouldKeepOwnAssetLockConversionInternalWithAnotherKeysOnlyParticipant() throws { - let container = try DashModelContainer.createInMemory() - let tx = try localTransfer(in: container.mainContext, assetLock: true) - let walletA = Data(repeating: 1, count: 32) - let change = try XCTUnwrap(tx.outputs.first) - change.walletId = walletA - change.account = tx.inputs.first?.account - XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.internalTransfer) - XCTAssertEqual(tx.direction(for: Data(repeating: 2, count: 32)), CoreDirectionCode.internalTransfer) - // A no-change conversion remains internal too. - container.mainContext.delete(change) - tx.transactionData = serializedSpend(inputs: [walletA], outputValue: 90, burn: true) - try container.mainContext.save() - XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.internalTransfer) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == tx.txid }) - XCTAssertEqual(row.direction(for: walletA), CoreDirectionCode.internalTransfer) + let stored = try transaction(container, txid) + XCTAssertEqual(stored.netAmount, 0, "the shared scalar is written as delivered") + XCTAssertEqual(stored.netAmount(for: walletA), -100) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.internalTransfer) } - func testShouldScopeDirectionAndAmountToEachWalletOfALocalTransfer() { - let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) - let (amount, fee): (UInt64, UInt64) = (90, 10) - let coin = input(amount + fee, wallet: 1) - // Whichever wallet recorded last owns the stored scalars; here the sender. + /// Without a row for the wallet, display falls back to the shared scalar. + func testShouldFallBackToTheSharedValuesWithoutAWalletRow() { let tx = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: Data(), - direction: CoreDirectionCode.outgoing, netAmount: -Int64(amount + fee) + txid: txid, transactionData: Data(), direction: CoreDirectionCode.incoming, netAmount: 25 ) - let credit = PersistentTxo(transaction: tx, vout: 0, amount: amount, address: "", height: 1) - credit.walletId = walletB - tx.inputs = [coin] - tx.outputs = [credit] - XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.outgoing) - XCTAssertEqual(tx.direction(for: walletB), CoreDirectionCode.incoming) - XCTAssertEqual(tx.netAmount(for: walletA), -Int64(amount + fee)) - XCTAssertEqual(tx.netAmount(for: walletB), Int64(amount)) - XCTAssertEqual(tx.formattedAmount(for: walletB), "+0.00000090 DASH") + XCTAssertEqual(tx.netAmount(for: walletA), 25) + XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.incoming) + XCTAssertEqual(tx.displayNetAmount(for: nil), 25) } - func testShouldReportAmountUnavailableOnlyWhenNoWalletAccountingApplies() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let (walletA, walletB) = (Data(repeating: 1, count: 32), Data(repeating: 2, count: 32)) - func makeAccount(of walletId: Data) -> PersistentAccount { - let wallet = PersistentWallet(walletId: walletId, network: .testnet) - let account = PersistentAccount( - wallet: wallet, accountType: 0, accountIndex: 0, accountTypeName: "Standard BIP44 Account" - ) - context.insert(wallet) - context.insert(account) - return account - } - let (accountA, accountB) = (makeAccount(of: walletA), makeAccount(of: walletB)) - // Payload-only (no linked TXO), e.g. a provider tx matched by key. - let payloadOnly = PersistentTransaction(txid: Data(repeating: 3, count: 32), transactionData: Data()) - let shared = PersistentTransaction(txid: Data(repeating: 4, count: 32), transactionData: Data(), netAmount: 40) - let unrecorded = PersistentTransaction(txid: Data(repeating: 5, count: 32), transactionData: Data(), netAmount: 40) - context.insert(payloadOnly) - context.insert(shared) - context.insert(unrecorded) - payloadOnly.involvedAccounts = [accountA] - shared.involvedAccounts = [accountA, accountB] - try context.save() + func testShouldOfferNoHistoryWhenEveryTransactionHasTheWalletsRow() throws { + let (_, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) - XCTAssertEqual(payloadOnly.netAmount(for: walletA), 0, "the sole recorder's stored amount stands") - XCTAssertEqual(payloadOnly.formattedAmount(for: walletA), "+0.00000000 DASH") - XCTAssertNil(shared.netAmount(for: walletA), "the stored scalar may be the other recorder's") - XCTAssertNil(shared.netAmount(for: walletB)) - XCTAssertEqual(shared.formattedAmount(for: walletA), "Amount unavailable") - XCTAssertNil(unrecorded.netAmount(for: walletA), "a wallet with no stake has no amount") - } + let history = handler.loadCoreHistoryForAccounting(walletId: walletA) - func testShouldNotCountAnotherLocalWalletsUnlinkedOutputForTheSender() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let senderId = Data(repeating: 1, count: 32) - let receiver = PersistentWallet(walletId: Data(repeating: 2, count: 32), network: .testnet) - let receiverAccount = PersistentAccount( - wallet: receiver, accountType: 0, accountIndex: 0, accountTypeName: "Standard BIP44 Account" - ) - // P2PKH to pubkey hash 0x05 x 20 on testnet: B's address with no TXO row yet. - let receiverAddress = PersistentCoreAddress( - address: "yLmzEvw3frCPS4cyRmFFeKbt64fUPzMwFh", poolTypeTag: 0, addressIndex: 0, derivationPath: "" - ) - receiverAddress.account = receiverAccount - context.insert(PersistentWallet(walletId: senderId, network: .testnet)) - context.insert(receiver) - context.insert(receiverAccount) - context.insert(receiverAddress) - var bytes = Data([2, 0, 0, 0, 1]) - bytes.append(senderId) - bytes.append(contentsOf: [0, 0, 0, 0, 0, 255, 255, 255, 255, 1]) - withUnsafeBytes(of: UInt64(40).littleEndian) { bytes.append(contentsOf: $0) } - bytes.append(contentsOf: [25, 0x76, 0xa9, 0x14] + [UInt8](repeating: 5, count: 20) + [0x88, 0xac]) - bytes.append(contentsOf: [0, 0, 0, 0]) - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: bytes, direction: CoreDirectionCode.outgoing, netAmount: -100 - ) - let coin = input(100) - coin.spendingTransaction = spender - context.insert(coin) - context.insert(spender) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) - XCTAssertEqual(row.netAmount, -100, "the receiving wallet's credit is not the sender's") - XCTAssertEqual(row.netAmount(for: senderId), -100) + XCTAssertFalse(history.errored) + XCTAssertNil(history.rows) + XCTAssertEqual(history.count, 0) } - func testShouldNotCountAnotherLocalWalletsLinkedOutputForTheSender() throws { - let container = try DashModelContainer.createInMemory() - let tx = try localTransfer(in: container.mainContext) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()) - .first { $0.txid == tx.txid }) - XCTAssertEqual(row.netAmount, -100, "the receiving wallet's linked credit is not the sender's") - XCTAssertEqual(row.direction, CoreDirectionCode.outgoing) - XCTAssertEqual(row.netAmount(for: Data(repeating: 1, count: 32)), -100) - XCTAssertEqual(row.netAmount(for: Data(repeating: 2, count: 32)), 90) - } + /// Rust needs the whole history to resolve a flagged spend's inputs, so + /// every transaction crosses, flagged by whether it lacks a row. + func testShouldOfferTheWholeHistoryFlaggingTransactionsWithoutARow() throws { + let (_, handler) = try makeStore() + let other = Data(repeating: 4, count: 32) + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + persist(handler, walletId: walletA, txid: other, bytes: Data([7, 8, 9]), net: -40) + persist(handler, walletId: walletB, txid: Data(repeating: 5, count: 32), bytes: Data([5]), net: 1) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) - func testShouldExcludePersistedContactOutputsFromOwnedAccounting() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - let wallet = PersistentWallet(walletId: walletId, network: .testnet) - let contactAccount = PersistentAccount( - wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, - accountIndex: 0, accountTypeName: "DashPay External Account" - ) - context.insert(wallet) - context.insert(contactAccount) - let coin = input(100) - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), - direction: CoreDirectionCode.internalTransfer, netAmount: -60 - ) - coin.spendingTransaction = spender - let contactOutput = PersistentTxo(transaction: spender, vout: 0, amount: 40, address: "", height: 1) - contactOutput.walletId = walletId - contactOutput.account = contactAccount - context.insert(coin) - context.insert(spender) - context.insert(contactOutput) - try context.save() - XCTAssertEqual(spender.netAmount(for: walletId), -100) - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) - XCTAssertEqual(row.netAmount, -100) - XCTAssertEqual(row.direction, 1) - XCTAssertEqual(row.netAmount(for: walletId), -100) - } + let history = handler.loadCoreHistoryForAccounting(walletId: walletA) - func testShouldNotTreatPersistedContactInputAsOurFunding() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - let wallet = PersistentWallet(walletId: walletId, network: .testnet) - let contactAccount = PersistentAccount( - wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, - accountIndex: 0, accountTypeName: "DashPay External Account" - ) - context.insert(wallet) - context.insert(contactAccount) - let coin = input(100) - coin.account = contactAccount - let spender = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: serializedSpend(inputs: [walletId]), - direction: CoreDirectionCode.incoming, netAmount: 40 - ) - coin.spendingTransaction = spender - let received = PersistentTxo(transaction: spender, vout: 0, amount: 40, address: "", height: 1) - received.walletId = walletId - context.insert(coin) - context.insert(spender) - context.insert(received) - try context.save() - let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) - XCTAssertFalse(handler.loadWalletList().errored) - let row = try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == spender.txid }) - XCTAssertEqual(row.netAmount, 40) - XCTAssertEqual(row.direction, 0) - XCTAssertEqual(row.netAmount(for: walletId), 40) + XCTAssertFalse(history.errored) + let rows = try XCTUnwrap(history.rows) + let offered = (0.. (Data, Bool) in + let entry = rows[index] + return (Data(bytes: entry.tx_bytes!, count: Int(entry.tx_bytes_len)), entry.needs_accounting) + } + XCTAssertEqual(offered.count, 2, "only wallet A's transactions") + XCTAssertEqual(offered.first { $0.0 == Data([1, 2]) }?.1, false) + XCTAssertEqual(offered.first { $0.0 == Data([7, 8, 9]) }?.1, true) + handler.loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer(rows)) } - func testShouldReportReceivedAmountDespiteContactOutputWhenStoredAmountAgrees() throws { - let container = try DashModelContainer.createInMemory() - let context = container.mainContext - let walletId = Data(repeating: 1, count: 32) - let wallet = PersistentWallet(walletId: walletId, network: .testnet) - let contactAccount = PersistentAccount( - wallet: wallet, accountType: PlatformWalletPersistenceHandler.dashpayExternalAccountTypeTag, - accountIndex: 0, accountTypeName: "DashPay External Account" - ) - context.insert(wallet) - context.insert(contactAccount) - let payment = PersistentTransaction( - txid: Data(repeating: 3, count: 32), transactionData: Data(), - direction: CoreDirectionCode.incoming, netAmount: 40 - ) - let received = PersistentTxo(transaction: payment, vout: 0, amount: 40, address: "", height: 1) - received.walletId = walletId - let contactOutput = PersistentTxo(transaction: payment, vout: 1, amount: 60, address: "", height: 1) - contactOutput.walletId = walletId - contactOutput.account = contactAccount - context.insert(payment) - context.insert(received) - context.insert(contactOutput) - // The foreign input: every prevout with no local TXO is recorded as pending. - context.insert(PersistentPendingInput( - outpoint: Data(repeating: 9, count: 36), inputIndex: 0, - spendingTxid: payment.txid, spendingTransaction: payment, walletId: walletId - )) - try context.save() - XCTAssertEqual( - payment.netAmount(for: walletId), 40, "a contact's output does not make the received amount unknown" - ) - // A stored amount the wallet's own outputs do not confirm may hide an unlinked coin of ours. - payment.netAmount = -60 - XCTAssertNil(payment.netAmount(for: walletId)) + func testShouldDeleteOnlyTheDepartingWalletsRows() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100) + persist(handler, walletId: walletB, txid: txid, bytes: Data([1]), net: 90) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.outgoing)]) + account(handler, walletId: walletB, [row(txid, net: 90, direction: CoreDirectionCode.incoming)]) + + try handler.deleteWalletData(walletId: walletA) + + let rows = try ModelContext(container).fetch(FetchDescriptor()) + XCTAssertEqual(rows.map(\.walletId), [walletB]) + XCTAssertEqual(rows.first?.netAmount, 90) } + func testShouldFormatSignedDuffsWithoutTrappingOnInt64Min() { + XCTAssertEqual(PersistentTransaction.format(duffs: 150_000_000), "+1.50000000 DASH") + XCTAssertEqual(PersistentTransaction.format(duffs: -100), "-0.00000100 DASH") + XCTAssertTrue(PersistentTransaction.format(duffs: .min).hasPrefix("-92233720368.")) + } } From aa086b48d2bf766faba5dbebd80efaee72e7dcbf Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:48:30 +0000 Subject: [PATCH 10/21] fix(swift-sdk): log the live schema version as the migration target `makeContainer`, `logMigrationFailure`, the new-store route and `logRoute` still logged `target_version` "4.0.0" after the live schema moved to V5, so `DashModelMigrationTests` (which expect "5.0.0") failed and production logs misnamed every V5 migration. All five sites now use `targetVersion`, which is derived from `LiveSchema`, the same type that builds `schema`. The tests also assert that no "4.0.0" target is logged. Not built here: no Swift toolchain on this host. Co-Authored-By: Claude Opus 5.5 --- .../Persistence/DashModelContainer.swift | 22 ++++++++++++++----- .../DashModelMigrationTests.swift | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index d21ed19427f..de65f8c5466 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -99,9 +99,19 @@ public enum DashModelContainer { ] } + /// The live versioned schema. `schema` and every diagnostic's + /// `target_version` derive from it, so a schema bump is one edit. + typealias LiveSchema = DashSchemaV5 + /// Create the schema for all Dash Platform models public static var schema: Schema { - Schema(versionedSchema: DashSchemaV5.self) + Schema(versionedSchema: LiveSchema.self) + } + + /// `LiveSchema`'s version as logged in `target_version` (`major.minor.patch`). + static var targetVersion: String { + let version = LiveSchema.versionIdentifier + return "\(version.major).\(version.minor).\(version.patch)" } /// Create a persistent model container for storing data. @@ -173,7 +183,7 @@ public enum DashModelContainer { bridgeLegacyStore: Bool = true ) throws -> ModelContainer { SDKLogger.event("store_open_started", category: .persistence, - fields: ["target_version": .publicText("4.0.0")]) + fields: ["target_version": .publicText(targetVersion)]) do { let container: ModelContainer if bridgeLegacyStore { @@ -186,7 +196,7 @@ public enum DashModelContainer { configurations: [configuration]) } SDKLogger.event("store_open_succeeded", category: .persistence, - fields: ["target_version": .publicText("4.0.0")]) + fields: ["target_version": .publicText(targetVersion)]) return container } catch { logMigrationFailure(error) @@ -202,7 +212,7 @@ public enum DashModelContainer { let domain = systemDomains.contains(nsError.domain) ? nsError.domain : String(reflecting: type(of: error)) SDKLogger.event("store_open_failed", category: .persistence, severity: .error, fields: ["error_domain": .publicText(domain), "error_code": .integer(Int64(nsError.code)), - "target_version": .publicText("4.0.0")]) + "target_version": .publicText(targetVersion)]) } /// Select by the complete stored model identity, after journal recovery. @@ -219,7 +229,7 @@ public enum DashModelContainer { guard ObjectIdentifier(defaultPlan) == ObjectIdentifier(DashMigrationPlan.self) else { return defaultPlan } guard FileManager.default.fileExists(atPath: url.path) else { SDKLogger.event("store_migration_route", category: .persistence, fields: [ - "route": .publicText("new-store"), "target_version": .publicText("4.0.0")]) + "route": .publicText("new-store"), "target_version": .publicText(targetVersion)]) return defaultPlan } let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(type: .sqlite, at: url) @@ -237,7 +247,7 @@ public enum DashModelContainer { func logRoute(_ route: String) { SDKLogger.event("store_migration_route", category: .persistence, fields: [ "source_version": .publicText(safeVersions), "source_checksum": .publicText(safeChecksum), - "route": .publicText(route), "target_version": .publicText("4.0.0")]) + "route": .publicText(route), "target_version": .publicText(targetVersion)]) } func matches(_ type: any VersionedSchema.Type) throws -> Bool { let expected = try identity(type) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 0351eeb6143..0b5d6272d1e 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -79,6 +79,7 @@ final class DashModelMigrationTests: XCTestCase { XCTAssertTrue(log.contains("source_version=\"2.0.0\"")) XCTAssertTrue(log.contains("source_checksum=\"\(sourceChecksum)\"")) XCTAssertTrue(log.contains("target_version=\"5.0.0\"")) + XCTAssertFalse(log.contains("target_version=\"4.0.0\""), "every diagnostic names the live schema") XCTAssertTrue(log.contains("route=\"labelled-current-v5\"")) XCTAssertEqual(log.components(separatedBy: "event=store_open_succeeded").count - 1, 2) XCTAssertFalse(log.contains("event=store_open_failed")) @@ -94,6 +95,7 @@ final class DashModelMigrationTests: XCTestCase { SDKLogger.flush() let updatedLog = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(updatedLog.contains("route=\"new-store\"")) + XCTAssertFalse(updatedLog.contains("target_version=\"4.0.0\"")) let failure = try XCTUnwrap(updatedLog.split(separator: "\n").first { $0.contains("event=store_open_failed") }) XCTAssertTrue(failure.contains("target_version=\"5.0.0\"")) XCTAssertTrue(failure.contains("error_code=")) From 98db33e8ed79b53ba0353c8ef44a07b53c5c0f51 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:14:10 +0000 Subject: [PATCH 11/21] fix(platform-wallet): make the accounting backfill converge and match live rows - Compute the fee once in the shared accounting rule (only when the wallet funded every input), so the load-time backfill writes exactly the rows the live projection emits; tests assert whole-row equality for payments, no-change asset locks, CoinJoin and unspendable outputs. - Classify address-less, provably unspendable outputs like key-wallet's `Unspendable` role in the backfill. - Replace the "empty or ALL history" host obligation: the host returns only the wallet's unaccounted transactions (any subset is safe) or reports the wallet backfilled; Rust fetches the funding transactions it needs through `get_core_tx_record`. - `store_history_accounting` persists the rows and a per-wallet completion marker atomically, so later loads skip the read. The FFI persister brackets it in its own serialized begin/end round (new `on_persist_core_history_accounted_fn` slot, appended and size-gated; `on_load_core_history_for_accounting_fn` gains `out_backfilled`). - Use top-of-file imports in the new code. Co-Authored-By: Claude Opus 5.5 --- .../src/core_wallet_types.rs | 21 +- .../rs-platform-wallet-ffi/src/manager.rs | 81 +++- .../rs-platform-wallet-ffi/src/persistence.rs | 388 ++++++++++------ .../rs-platform-wallet/src/changeset/mod.rs | 4 +- .../src/changeset/traits.rs | 47 +- .../src/changeset/wallet_accounting.rs | 424 ++++++++++++------ .../rs-platform-wallet/src/manager/load.rs | 275 +++++++++--- 7 files changed, 882 insertions(+), 358 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index be4896c895a..497fbf0fb90 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -1,5 +1,7 @@ //! C-compatible types for core wallet changeset FFI. +use key_wallet::managed_account::transaction_record::TransactionDirection; +use platform_wallet::changeset::WalletAccounting; use platform_wallet::masternode::{provider_payload_fields, MasternodeRecord}; use std::os::raw::c_char; @@ -385,17 +387,15 @@ pub struct TransactionAccountingFFI { pub has_fee: bool, } -/// One stored Core transaction the host hands back for the load-time -/// accounting backfill (see `on_load_core_history_for_accounting_fn`). -/// Host-owned until the matching free callback. +/// One stored Core transaction without this wallet's accounting row, handed +/// back for the load-time backfill (see +/// `on_load_core_history_for_accounting_fn`). Host-owned until the matching +/// free callback. #[repr(C)] pub struct CoreHistoryTransactionFFI { /// Consensus-encoded transaction body. pub tx_bytes: *const u8, pub tx_bytes_len: usize, - /// The host holds no `TransactionAccountingFFI` row for this - /// transaction and wallet yet. - pub needs_accounting: bool, } // --------------------------------------------------------------------------- @@ -1567,10 +1567,7 @@ pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING: u32 = 1; pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL: u32 = 2; pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN: u32 = 3; -fn direction_to_u32( - direction: key_wallet::managed_account::transaction_record::TransactionDirection, -) -> u32 { - use key_wallet::managed_account::transaction_record::TransactionDirection; +fn direction_to_u32(direction: TransactionDirection) -> u32 { match direction { TransactionDirection::Incoming => PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING, TransactionDirection::Outgoing => PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING, @@ -1579,8 +1576,8 @@ fn direction_to_u32( } } -impl From<&platform_wallet::changeset::WalletAccounting> for TransactionAccountingFFI { - fn from(row: &platform_wallet::changeset::WalletAccounting) -> Self { +impl From<&WalletAccounting> for TransactionAccountingFFI { + fn from(row: &WalletAccounting) -> Self { let mut txid = [0u8; 32]; txid.copy_from_slice(row.txid.as_ref()); Self { diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index e5767267301..4e1424113cc 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -10,11 +10,12 @@ use crate::handle::*; use crate::persistence::{ FFIPersister, FreeCoreHistoryForAccountingFn, FreeTrackedMasternodesFn, LoadAssetLockOperationIdFn, LoadCoreHistoryForAccountingFn, LoadIdentityBalanceBlockTimeFn, - LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn, PersistDpnsNameStatesFn, - PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistTransactionAccountingFn, - PersistWalletChangesetChainLockHeightFn, PersistWalletChangesetSweepsFn, - PersistWalletChangesetUtxoVerdictsFn, PersistWalletDashPayBackfillFn, PersistenceCallbacks, - PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, + LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn, PersistCoreHistoryAccountedFn, + PersistDpnsNameStatesFn, PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, + PersistTransactionAccountingFn, PersistWalletChangesetChainLockHeightFn, + PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, + PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, + PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, PLATFORM_WALLET_PERSISTENCE_CALLBACKS_EXTENSION_VERSION, }; use crate::runtime::runtime; @@ -303,6 +304,10 @@ unsafe fn persistence_extension_callbacks( on_load_core_history_for_accounting_free_fn, FreeCoreHistoryForAccountingFn ), + persist_core_history_accounted: slot!( + on_persist_core_history_accounted_fn, + PersistCoreHistoryAccountedFn + ), } } @@ -1425,6 +1430,72 @@ mod tests { assert!(parsed.load_asset_lock_operation_id.is_none()); } + /// Each transaction-accounting slot is read only when the host's + /// `struct_size` covers it, so a host built before any of them simply + /// never has them read. + #[test] + fn should_size_gate_transaction_accounting_slots_independently() { + use crate::core_wallet_types::{CoreHistoryTransactionFFI, TransactionAccountingFFI}; + unsafe extern "C" fn persist( + _: *mut c_void, + _: *const u8, + _: *const TransactionAccountingFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn load( + _: *mut c_void, + _: *const u8, + _: *mut bool, + _: *mut *const CoreHistoryTransactionFFI, + _: *mut usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn free(_: *mut c_void, _: *const CoreHistoryTransactionFFI, _: usize) {} + unsafe extern "C" fn accounted(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + let mut ext = PersistenceCallbacksExtension { + on_persist_transaction_accounting_fn: Some(persist), + on_load_core_history_for_accounting_fn: Some(load), + on_load_core_history_for_accounting_free_fn: Some(free), + on_persist_core_history_accounted_fn: Some(accounted), + ..Default::default() + }; + let present = |ext: &PersistenceCallbacksExtension| { + let parsed = unsafe { persistence_extension_callbacks(ext) }; + [ + parsed.persist_transaction_accounting.is_some(), + parsed.load_core_history_for_accounting.is_some(), + parsed.load_core_history_for_accounting_free.is_some(), + parsed.persist_core_history_accounted.is_some(), + ] + }; + assert_eq!(present(&ext), [true; 4]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ); + assert_eq!(present(&ext), [true, true, true, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ); + assert_eq!(present(&ext), [true, true, false, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ); + assert_eq!(present(&ext), [true, false, false, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ); + assert_eq!(present(&ext), [false; 4]); + } + /// A host whose `struct_size` stops right after the chainlock-height /// slot (built before the credit-verdict slot existed) keeps every /// earlier slot and simply never has the verdict slot read; a host diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 1846eaa2b23..e0c2f585202 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -35,8 +35,7 @@ use platform_wallet::changeset::{ AccountAddressPoolEntry, AccountRegistrationEntry, ClientStartState, ClientWalletStartState, ListedCoreTxid, PersistenceCapabilities, PersistenceError, PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, ProviderKeyAccountEntry, - ProviderKeyExtendedPubKey, StoredCoreTransaction, WalletAccounting, - PERSISTENCE_CAPABILITIES_VERSION, + ProviderKeyExtendedPubKey, WalletAccounting, PERSISTENCE_CAPABILITIES_VERSION, }; use platform_wallet::wallet::platform_wallet::WalletId; #[cfg(feature = "shielded")] @@ -323,13 +322,12 @@ pub type LoadAssetLockOperationIdFn = unsafe extern "C" fn( ) -> i32; /// Carries one wallet's accounting rows (see [`TransactionAccountingFFI`]). -/// The host upserts each keyed by `(txid, wallet_id)`. Fired inside a round -/// right after `on_persist_wallet_changeset_fn`, so the transaction rows the -/// accounting belongs to already exist, and only when the round has a -/// record with accounting evidence. Also fired OUTSIDE any round, once per -/// wallet at load, with the backfill computed from -/// `on_load_core_history_for_accounting_fn`; the host then commits on its -/// own. A non-zero return fails the round like any other per-kind callback. +/// The host upserts each keyed by `(txid, wallet_id)`. Always fired inside a +/// begin/end round: right after `on_persist_wallet_changeset_fn`, so the +/// transaction rows the accounting belongs to already exist, when the round +/// has a record with accounting evidence; and in the load-time backfill's +/// own round, before [`PersistCoreHistoryAccountedFn`]. A non-zero return +/// fails the round like any other per-kind callback. pub type PersistTransactionAccountingFn = unsafe extern "C" fn( context: *mut c_void, wallet_id: *const u8, @@ -337,14 +335,18 @@ pub type PersistTransactionAccountingFn = unsafe extern "C" fn( rows_count: usize, ) -> i32; -/// Return the wallet's stored Core transactions for the accounting backfill: -/// an EMPTY array when every one already has an accounting row for this -/// wallet, otherwise ALL of them with `needs_accounting` set on the ones -/// missing it (the others resolve the flagged ones' inputs). The host keeps -/// the array valid until [`FreeCoreHistoryForAccountingFn`]. +/// The load-time accounting backfill's read. Sets `*out_backfilled = true` +/// (and returns no rows) once [`PersistCoreHistoryAccountedFn`] has been +/// stored for this wallet. Otherwise returns the wallet's stored +/// transactions that have no accounting row for it; any subset is safe, +/// since Rust fetches the funding transactions it needs itself +/// (`on_get_core_tx_record_fn`) and a transaction left out only stays +/// without a row. The host keeps the array valid until +/// [`FreeCoreHistoryForAccountingFn`]. pub type LoadCoreHistoryForAccountingFn = unsafe extern "C" fn( context: *mut c_void, wallet_id: *const u8, + out_backfilled: *mut bool, out_rows: *mut *const CoreHistoryTransactionFFI, out_count: *mut usize, ) -> i32; @@ -356,6 +358,12 @@ pub type FreeCoreHistoryForAccountingFn = unsafe extern "C" fn( count: usize, ); +/// Record that this wallet's history backfill is complete, so the next +/// [`LoadCoreHistoryForAccountingFn`] reports it backfilled. Fired in the +/// backfill's round after its rows, and committed with them. +pub type PersistCoreHistoryAccountedFn = + unsafe extern "C" fn(context: *mut c_void, wallet_id: *const u8) -> i32; + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -535,11 +543,12 @@ pub struct PersistenceCallbacksExtension { >, /// Stored history for the load-time accounting backfill (see /// [`LoadCoreHistoryForAccountingFn`]). Read only together with the free - /// slot below and the persist slot above. + /// slot below, the persist slot above and the completion slot. pub on_load_core_history_for_accounting_fn: Option< unsafe extern "C" fn( context: *mut c_void, wallet_id: *const u8, + out_backfilled: *mut bool, out_rows: *mut *const CoreHistoryTransactionFFI, out_count: *mut usize, ) -> i32, @@ -551,6 +560,10 @@ pub struct PersistenceCallbacksExtension { count: usize, ), >, + /// Backfill completion marker (see [`PersistCoreHistoryAccountedFn`]). + /// The backfill runs only when all four accounting slots are present. + pub on_persist_core_history_accounted_fn: + Option i32>, } impl Default for PersistenceCallbacksExtension { @@ -574,6 +587,7 @@ impl Default for PersistenceCallbacksExtension { on_persist_transaction_accounting_fn: None, on_load_core_history_for_accounting_fn: None, on_load_core_history_for_accounting_free_fn: None, + on_persist_core_history_accounted_fn: None, } } } @@ -598,6 +612,7 @@ pub struct PersistenceExtensionCallbacks { pub persist_transaction_accounting: Option, pub load_core_history_for_accounting: Option, pub load_core_history_for_accounting_free: Option, + pub persist_core_history_accounted: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -1543,10 +1558,11 @@ pub struct FFIPersister { /// the load-time backfill. `None` keeps a host on `TransactionRecordFFI`'s /// values, as before the slot existed. persist_transaction_accounting_callback: Option, - /// Stored-history read for the backfill; used only when the free slot is - /// wired too. + /// The load-time backfill's read, free and completion slots; the + /// backfill runs only when all of them and the persist slot are wired. load_core_history_for_accounting_callback: Option, load_core_history_for_accounting_free_callback: Option, + persist_core_history_accounted_callback: Option, /// Semantic capability declaration supplied separately from the callback /// vtable by the additive manager-create API. Keeping this out of /// `PersistenceCallbacks` preserves that established C struct's size. @@ -1676,6 +1692,7 @@ impl FFIPersister { load_core_history_for_accounting_callback: extensions.load_core_history_for_accounting, load_core_history_for_accounting_free_callback: extensions .load_core_history_for_accounting_free, + persist_core_history_accounted_callback: extensions.persist_core_history_accounted, declared_capabilities, round_lock: Mutex::new(RoundGuardState::default()), } @@ -1912,21 +1929,25 @@ impl PlatformWalletPersistence for FFIPersister { fn load_core_history_for_accounting( &self, wallet_id: WalletId, - ) -> Result, PersistenceError> { - // Rows nobody could persist are not worth reading. - let (Some(load), Some(free), Some(_)) = ( + ) -> Result>, PersistenceError> { + // A backfill that could not be stored or completed is not worth + // reading. + let (Some(load), Some(free), Some(_), Some(_)) = ( self.load_core_history_for_accounting_callback, self.load_core_history_for_accounting_free_callback, self.persist_transaction_accounting_callback, + self.persist_core_history_accounted_callback, ) else { - return Ok(Vec::new()); + return Ok(None); }; + let mut backfilled = false; let mut rows_ptr: *const CoreHistoryTransactionFFI = std::ptr::null(); let mut count: usize = 0; let rc = unsafe { load( self.callbacks.context, wallet_id.as_ptr(), + &mut backfilled, &mut rows_ptr, &mut count, ) @@ -1943,12 +1964,8 @@ impl PlatformWalletPersistence for FFIPersister { for row in rows { let bytes = unsafe { slice_from_raw(row.tx_bytes, row.tx_bytes_len) }; match dashcore::consensus::encode::deserialize::(bytes) { - Ok(transaction) => out.push(StoredCoreTransaction { - transaction, - needs_accounting: row.needs_accounting, - }), - // One undecodable row only loses its own accounting (or a - // prevout another row would have resolved). + Ok(transaction) => out.push(transaction), + // One undecodable row only goes without accounting. Err(e) => tracing::warn!( error = %e, "load: skipping an undecodable stored transaction in the accounting backfill" @@ -1957,37 +1974,84 @@ impl PlatformWalletPersistence for FFIPersister { } } unsafe { free(self.callbacks.context, rows_ptr, count) }; - Ok(out) + Ok((!backfilled).then_some(out)) } - fn store_transaction_accounting( + /// The backfill's rows and its completion marker, as one round under + /// `round_lock`: they commit together or not at all, and never land in + /// another wallet's open round. + fn store_history_accounting( &self, wallet_id: WalletId, rows: &[WalletAccounting], ) -> Result<(), PersistenceError> { - let Some(cb) = self.persist_transaction_accounting_callback else { + let (Some(persist_rows), Some(accounted)) = ( + self.persist_transaction_accounting_callback, + self.persist_core_history_accounted_callback, + ) else { return Ok(()); }; - if rows.is_empty() { - return Ok(()); + let mut round = self.round_lock.lock(); + round.begin_round()?; + if let Some(cb) = self.callbacks.on_changeset_begin_fn { + let result = unsafe { cb(self.callbacks.context, wallet_id.as_ptr()) }; + if result != 0 { + let _ = round.end_round(); + return Err(persist_callback_error( + result, + format!( + "changeset-begin callback returned error code {result}; \ + accounting backfill aborted before any write" + ), + )); + } } - let rows: Vec = - rows.iter().map(TransactionAccountingFFI::from).collect(); - let rc = unsafe { - cb( - self.callbacks.context, - wallet_id.as_ptr(), - rows.as_ptr(), - rows.len(), - ) - }; - if rc != 0 { - return Err(persist_callback_error( - rc, - format!("on_persist_transaction_accounting_fn returned error code {rc}"), - )); + let mut outcome = RoundOutcome::default(); + if !rows.is_empty() { + let rows: Vec = + rows.iter().map(TransactionAccountingFFI::from).collect(); + let result = unsafe { + persist_rows( + self.callbacks.context, + wallet_id.as_ptr(), + rows.as_ptr(), + rows.len(), + ) + }; + if result != 0 { + outcome.record(result); + } + } + if outcome.is_success() { + let result = unsafe { accounted(self.callbacks.context, wallet_id.as_ptr()) }; + if result != 0 { + outcome.record(result); + } + } + if let Some(cb) = self.callbacks.on_changeset_end_fn { + let result = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + outcome.is_success(), + ) + }; + if result != 0 { + if outcome.is_success() { + outcome.record(result); + } else { + outcome.record_fatal(); + } + } + } + round.end_round()?; + match outcome.failure_kind() { + Some(kind) => Err(PersistenceError::backend_with_kind( + self.reportable_round_kind(kind), + "the accounting backfill round failed and was rolled back", + )), + None => Ok(()), } - Ok(()) } fn load_tracked_masternodes( @@ -10091,35 +10155,44 @@ mod tests { ); } - /// The load-time backfill reads the host's stored history through the - /// load/free pair, decoding each body and keeping its flag (an - /// undecodable body is skipped, not fatal), and hands the computed rows - /// back through the persist slot. Without the persist slot nothing is - /// read: rows nobody can store are not worth computing. + /// The load-time backfill reads the host's unaccounted transactions + /// through the load/free pair (an undecodable body is skipped, not + /// fatal) until the host reports the wallet backfilled. It then writes + /// the rows and the completion marker as ONE round: begin, rows, marker, + /// end. A failing row write rolls the round back with no marker, so the + /// backfill runs again. Without every slot nothing is read. #[test] - fn should_read_stored_history_and_persist_backfilled_accounting_through_the_extension() { + fn should_backfill_accounting_through_the_extension_in_one_round() { use key_wallet::managed_account::transaction_record::TransactionDirection; use platform_wallet::test_support::core_fixtures::{ foreign_address, outside, pay, spend_of, }; struct Host { - bodies: Vec>, + backfilled: bool, rows: Vec, + fail_rows: bool, loads: std::sync::Mutex, frees: std::sync::Mutex, - persisted: std::sync::Mutex>, + events: std::sync::Mutex>, + } + unsafe fn host<'a>(ctx: *mut c_void) -> &'a Host { + &*(ctx as *const Host) } unsafe extern "C" fn load( ctx: *mut c_void, _wallet_id: *const u8, + out_backfilled: *mut bool, out_rows: *mut *const CoreHistoryTransactionFFI, out_count: *mut usize, ) -> i32 { - let host = &*(ctx as *const Host); + let host = host(ctx); *host.loads.lock().unwrap() += 1; - *out_rows = host.rows.as_ptr(); - *out_count = host.rows.len(); + *out_backfilled = host.backfilled; + if !host.backfilled { + *out_rows = host.rows.as_ptr(); + *out_count = host.rows.len(); + } 0 } unsafe extern "C" fn free( @@ -10127,8 +10200,7 @@ mod tests { _rows: *const CoreHistoryTransactionFFI, _count: usize, ) { - let host = &*(ctx as *const Host); - *host.frees.lock().unwrap() += 1; + *host(ctx).frees.lock().unwrap() += 1; } unsafe extern "C" fn persist( ctx: *mut c_void, @@ -10136,101 +10208,153 @@ mod tests { rows: *const TransactionAccountingFFI, rows_count: usize, ) -> i32 { - let host = &*(ctx as *const Host); - host.persisted + let host = host(ctx); + for row in slice::from_raw_parts(rows, rows_count) { + host.events.lock().unwrap().push(format!( + "row net={} direction={} fee={}", + row.net_amount, row.direction, row.fee + )); + } + i32::from(host.fail_rows) + } + unsafe extern "C" fn accounted(ctx: *mut c_void, _wallet_id: *const u8) -> i32 { + host(ctx).events.lock().unwrap().push("accounted".into()); + 0 + } + unsafe extern "C" fn begin(ctx: *mut c_void, _wallet_id: *const u8) -> i32 { + host(ctx).events.lock().unwrap().push("begin".into()); + 0 + } + unsafe extern "C" fn end(ctx: *mut c_void, _wallet_id: *const u8, success: bool) -> i32 { + host(ctx) + .events .lock() .unwrap() - .extend_from_slice(slice::from_raw_parts(rows, rows_count)); + .push(format!("end success={success}")); 0 } let first = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); let second = spend_of(&[outside(3)], vec![pay(7_000, &foreign_address(4))]); - let mut host = Host { - bodies: vec![ - dashcore::consensus::encode::serialize(&first), - vec![0xFF], - dashcore::consensus::encode::serialize(&second), - ], - rows: Vec::new(), + let bodies = [ + dashcore::consensus::encode::serialize(&first), + vec![0xFF], + dashcore::consensus::encode::serialize(&second), + ]; + let make_host = |backfilled: bool, fail_rows: bool| Host { + backfilled, + rows: bodies + .iter() + .map(|body| CoreHistoryTransactionFFI { + tx_bytes: body.as_ptr(), + tx_bytes_len: body.len(), + }) + .collect(), + fail_rows, loads: std::sync::Mutex::new(0), frees: std::sync::Mutex::new(0), - persisted: std::sync::Mutex::new(Vec::new()), + events: std::sync::Mutex::new(Vec::new()), }; - host.rows = host - .bodies - .iter() - .zip([true, true, false]) - .map(|(body, needs_accounting)| CoreHistoryTransactionFFI { - tx_bytes: body.as_ptr(), - tx_bytes_len: body.len(), - needs_accounting, - }) - .collect(); - let context = &host as *const Host as *mut c_void; let extensions = PersistenceExtensionCallbacks { persist_transaction_accounting: Some(persist), load_core_history_for_accounting: Some(load), load_core_history_for_accounting_free: Some(free), + persist_core_history_accounted: Some(accounted), ..Default::default() }; - - let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( - PersistenceCallbacks { - context, - ..PersistenceCallbacks::default() - }, - PersistenceCapabilities::NONE, - extensions, - ); - let history = persister - .load_core_history_for_accounting([1; 32]) - .expect("history read must succeed"); - assert_eq!( - history - .iter() - .map(|stored| (stored.transaction.txid(), stored.needs_accounting)) - .collect::>(), - vec![(first.txid(), true), (second.txid(), false)] - ); - assert_eq!(*host.frees.lock().unwrap(), 1, "the host array is returned"); - + let persister_for = |host: &Host, extensions: PersistenceExtensionCallbacks| { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: host as *const Host as *mut c_void, + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + extensions, + ) + }; let row = WalletAccounting { txid: first.txid(), net_amount: -5_000, direction: TransactionDirection::Outgoing, fee: Some(250), }; - persister - .store_transaction_accounting([1; 32], &[row]) - .expect("accounting persist must succeed"); + + // Not yet backfilled: the decodable bodies come back, the loan is returned. + let host = make_host(false, false); + let persister = persister_for(&host, extensions); + let unaccounted = persister + .load_core_history_for_accounting([1; 32]) + .expect("history read must succeed") + .expect("not backfilled yet"); assert_eq!( - host.persisted.lock().unwrap().clone(), - vec![TransactionAccountingFFI::from(&row)] + unaccounted.iter().map(|tx| tx.txid()).collect::>(), + vec![first.txid(), second.txid()] ); - let persisted = host.persisted.lock().unwrap()[0]; + assert_eq!(*host.frees.lock().unwrap(), 1); + + // Rows and marker commit in one round. + persister + .store_history_accounting([1; 32], &[row]) + .expect("backfill round must succeed"); assert_eq!( - (persisted.direction, persisted.fee, persisted.has_fee), - (1, 250, true) + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "accounted", + "end success=true" + ] ); drop(persister); - let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( - PersistenceCallbacks { - context, - ..PersistenceCallbacks::default() - }, - PersistenceCapabilities::NONE, - PersistenceExtensionCallbacks { - persist_transaction_accounting: None, - ..extensions - }, + // Nothing to write still records completion. + let host = make_host(false, false); + persister_for(&host, extensions) + .store_history_accounting([1; 32], &[]) + .expect("empty backfill round must succeed"); + assert_eq!( + *host.events.lock().unwrap(), + vec!["begin", "accounted", "end success=true"] ); - assert!(persister - .load_core_history_for_accounting([1; 32]) - .expect("a host that cannot store rows is not read") - .is_empty()); - assert_eq!(*host.loads.lock().unwrap(), 1); + + // A failing row write rolls back, with no marker. + let host = make_host(false, true); + assert!(persister_for(&host, extensions) + .store_history_accounting([1; 32], &[row]) + .is_err()); + assert_eq!( + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "end success=false" + ] + ); + + // Backfilled: nothing to do. + let host = make_host(true, false); + assert_eq!( + persister_for(&host, extensions) + .load_core_history_for_accounting([1; 32]) + .expect("history read must succeed"), + None + ); + + // Without the completion slot the host is never read. + let host = make_host(false, false); + let without_marker = PersistenceExtensionCallbacks { + persist_core_history_accounted: None, + ..extensions + }; + assert_eq!( + persister_for(&host, without_marker) + .load_core_history_for_accounting([1; 32]) + .expect("a host that cannot complete a backfill is not read"), + None + ); + assert_eq!(*host.loads.lock().unwrap(), 0); } #[test] @@ -10762,6 +10886,16 @@ mod tests { PersistenceCallbacksExtension, on_load_core_history_for_accounting_free_fn ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ) + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index cb9fef3d027..6fcebc19ca3 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -70,6 +70,6 @@ pub use traits::{ PlatformWalletPersistence, }; pub use wallet_accounting::{ - history_accounting, is_owned, record_accounting, wallet_accounting, wallet_direction, - StoredCoreTransaction, WalletAccounting, + funding_txids, history_accounting, is_owned, record_accounting, wallet_accounting, + wallet_direction, WalletAccounting, }; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index 9960e0d52dd..a7867d89510 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -8,9 +8,9 @@ use std::error::Error as StdError; use crate::changeset::changeset::{DpnsNameStateEntry, PlatformWalletChangeSet}; use crate::changeset::client_start_state::ClientStartState; use crate::changeset::persistence_capabilities::PersistenceCapabilities; -use crate::changeset::wallet_accounting::{StoredCoreTransaction, WalletAccounting}; +use crate::changeset::wallet_accounting::WalletAccounting; use crate::wallet::platform_wallet::WalletId; -use dashcore::Txid; +use dashcore::{Transaction, Txid}; use dpp::prelude::Identifier; use key_wallet::managed_account::transaction_record::TransactionRecord; @@ -529,32 +529,33 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(None) } - /// The wallet's persisted Core transaction history, each entry flagged - /// when the backend holds no [`WalletAccounting`] for it yet. - /// - /// Read once per wallet at load: when any entry is flagged, the manager - /// computes the missing rows with - /// [`history_accounting`](crate::changeset::history_accounting) and - /// hands them to [`Self::store_transaction_accounting`]. A backend - /// returns an EMPTY vector when nothing is flagged, so a backfilled - /// store costs one cheap read per launch. When something is flagged it - /// must return the whole history, flagged or not, because a flagged - /// spend resolves its owned inputs from the outputs of the others. - /// - /// The default (no history) suits backends that never stored - /// accounting-less rows, such as the SQLite store, whose own migration - /// repairs history. + /// The wallet's stored Core transactions that have no + /// [`WalletAccounting`] yet, or `None` once this wallet's history + /// backfill has completed (see [`Self::store_history_accounting`]). + /// + /// Read once per wallet at load. The manager computes the missing rows + /// with [`history_accounting`](crate::changeset::history_accounting), + /// fetching the funding transactions it needs through + /// [`Self::get_core_tx_record`] itself, and hands them back through + /// [`Self::store_history_accounting`]. Any subset is safe: a transaction + /// left out only stays without a row. + /// + /// The default (`None`) suits backends that never stored rows without + /// accounting, such as the SQLite store, whose own migration repairs + /// history. fn load_core_history_for_accounting( &self, _wallet_id: WalletId, - ) -> Result, PersistenceError> { - Ok(Vec::new()) + ) -> Result>, PersistenceError> { + Ok(None) } - /// Persist per-wallet accounting computed outside a changeset round (the - /// load-time backfill, see [`Self::load_core_history_for_accounting`]). - /// Upsert keyed by `(txid, wallet_id)`. The default drops them. - fn store_transaction_accounting( + /// Persist the backfilled rows (upsert keyed by `(txid, wallet_id)`) + /// and record that this wallet's history backfill is complete, as one + /// atomic write; `rows` may be empty. After it, + /// [`Self::load_core_history_for_accounting`] returns `None` for the + /// wallet. The default drops both. + fn store_history_accounting( &self, _wallet_id: WalletId, _rows: &[WalletAccounting], diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 9feb6a67283..7f54b88215b 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -21,8 +21,9 @@ use std::collections::{BTreeMap, BTreeSet}; -use dashcore::{OutPoint, ScriptBuf, Transaction, Txid}; +use dashcore::{Address, OutPoint, ScriptBuf, Transaction, Txid}; use key_wallet::managed_account::managed_account_ref::ManagedAccountRef; +use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::managed_account::transaction_record::{ OutputRole, TransactionDirection, TransactionRecord, }; @@ -37,21 +38,11 @@ pub struct WalletAccounting { /// `Σ owned outputs − Σ owned inputs` in duffs, saturated to `i64`. pub net_amount: i64, pub direction: TransactionDirection, - /// The transaction fee, when the wallet funded every input. + /// The transaction fee, when the wallet funded every input; otherwise + /// the wallet cannot know it. pub fee: Option, } -/// One persisted Core transaction a host hands back for -/// [`history_accounting`]. -#[derive(Debug, Clone)] -pub struct StoredCoreTransaction { - pub transaction: Transaction, - /// The host holds no [`WalletAccounting`] for this transaction and - /// wallet yet. Transactions without the flag still resolve the - /// prevouts of the flagged ones. - pub needs_accounting: bool, -} - /// Classify a transaction from the wallet's point of view. /// /// - `spends_ours`: at least one input spends a wallet-owned output. @@ -108,59 +99,71 @@ pub fn record_accounting(record: &TransactionRecord) -> Option if record.input_details.is_empty() && record.output_details.is_empty() { return None; } - let (net, direction) = wallet_accounting(record); + let accounting = record_parts(record); Some(WalletAccounting { txid: record.txid, - net_amount: saturating_net(net), - direction, - fee: record.fee, + net_amount: saturating_net(accounting.net), + direction: accounting.direction, + fee: accounting.fee, }) } -/// Accounting for each flagged transaction of a host's stored history, from -/// the wallet's own view of which scripts it owns. -/// -/// Rows a host stored before it kept per-wallet accounting carry no -/// trustworthy amount, and the restored wallet holds no records to re-emit. -/// So the host hands back the raw history and this recomputes what the live -/// projection would have emitted: +/// The txids whose transactions [`history_accounting`] needs, beyond +/// `unaccounted` itself, to resolve the inputs of `unaccounted`: every +/// prevout that is neither one of those transactions' outputs nor one of the +/// wallet's restored UTXOs. Some of them are foreign and will not be found; +/// that is expected. +pub fn funding_txids(wallet: &ManagedWalletInfo, unaccounted: &[Transaction]) -> BTreeSet { + let own: BTreeSet = unaccounted.iter().map(Transaction::txid).collect(); + let utxos: BTreeSet = wallet_funds(wallet) + .flat_map(|funds| funds.utxos.keys().copied()) + .collect(); + unaccounted + .iter() + .flat_map(|transaction| &transaction.input) + .map(|input| input.previous_output) + .filter(|prevout| { + !prevout.is_null() && !own.contains(&prevout.txid) && !utxos.contains(prevout) + }) + .map(|prevout| prevout.txid) + .collect() +} + +/// Accounting for each of `unaccounted`, a host's stored transactions that +/// have no [`WalletAccounting`] yet, from the wallet's own view of which +/// scripts it owns. The live projection's rule, recomputed for rows a host +/// stored before it kept per-wallet accounting: /// - an output is owned when its script belongs to one of the wallet's funds /// accounts other than a contact's watch-only chain /// ([`AccountType::is_contact_owned`](key_wallet::account::AccountType::is_contact_owned)); -/// - an input is owned when its prevout is an owned output of the supplied -/// history or one of the wallet's restored UTXOs; -/// - type, net and direction come from [`TransactionRouter::classify_transaction`] -/// and [`wallet_accounting`]'s rule. +/// an address-less, provably unspendable output is `Unspendable`, as +/// key-wallet classifies it; +/// - an input is owned when its prevout is an owned output of `unaccounted`, +/// of `funding` (the stored transactions [`funding_txids`] named), or one +/// of the wallet's restored UTXOs; +/// - type, net, direction and fee follow [`record_accounting`]. /// -/// A flagged transaction that touches no owned script yields no row, like a -/// detail-less record in [`record_accounting`]. An owned input whose funding -/// transaction is missing from the history and already spent reads as -/// foreign, exactly as it would to the live projection. +/// A transaction that touches no owned script yields no row, like a +/// detail-less record. An owned input whose funding transaction the host no +/// longer stores, and that is no longer a UTXO, cannot be resolved and reads +/// as foreign. Any subset of a host's unaccounted transactions is therefore +/// safe to pass: omitting one only leaves it without a row. pub fn history_accounting( wallet: &ManagedWalletInfo, - history: &[StoredCoreTransaction], + unaccounted: &[Transaction], + funding: &[Transaction], ) -> Vec { let mut owned_scripts: BTreeSet = BTreeSet::new(); let mut prevouts: BTreeMap = BTreeMap::new(); - for account in wallet.accounts.all_accounts() { - let ManagedAccountRef::Funds(funds) = account else { - continue; - }; - if account - .managed_account_type() - .to_account_type() - .is_contact_owned() - { - continue; - } - owned_scripts.extend(account.all_script_pubkeys()); + for funds in wallet_funds(wallet) { + owned_scripts.extend(ManagedAccountRef::Funds(funds).all_script_pubkeys()); for (outpoint, utxo) in &funds.utxos { prevouts.insert(*outpoint, utxo.txout.value); } } - for stored in history { - let txid = stored.transaction.txid(); - for (vout, output) in stored.transaction.output.iter().enumerate() { + for transaction in unaccounted.iter().chain(funding) { + let txid = transaction.txid(); + for (vout, output) in transaction.output.iter().enumerate() { if owned_scripts.contains(&output.script_pubkey) { prevouts.insert( OutPoint { @@ -174,8 +177,7 @@ pub fn history_accounting( } let mut rows = Vec::new(); - for stored in history.iter().filter(|stored| stored.needs_accounting) { - let transaction = &stored.transaction; + for transaction in unaccounted { let spent: Vec = transaction .input .iter() @@ -191,35 +193,52 @@ pub fn history_accounting( if spent.is_empty() && owned.is_empty() { continue; } - let (net, direction) = accounting( + let unspendable: BTreeSet = transaction + .output + .iter() + .enumerate() + .filter(|(_, output)| { + Address::from_script(&output.script_pubkey, wallet.network).is_err() + && output.script_pubkey.is_provably_unspendable() + }) + .map(|(index, _)| index) + .collect(); + let accounting = accounting( transaction, TransactionRouter::classify_transaction(transaction), &spent, &owned, - &BTreeSet::new(), + &unspendable, ); - // Only a transaction funded entirely by the wallet has a fee the - // wallet can know. - let fee = (spent.len() == transaction.input.len()) - .then(|| { - let funded = spent.iter().try_fold(0u64, |sum, v| sum.checked_add(*v))?; - let paid = transaction - .output - .iter() - .try_fold(0u64, |sum, o| sum.checked_add(o.value))?; - funded.checked_sub(paid) - }) - .flatten(); rows.push(WalletAccounting { txid: transaction.txid(), - net_amount: saturating_net(net), - direction, - fee, + net_amount: saturating_net(accounting.net), + direction: accounting.direction, + fee: accounting.fee, }); } rows } +/// The wallet's funds accounts, without contacts' watch-only chains. +fn wallet_funds(wallet: &ManagedWalletInfo) -> impl Iterator + '_ { + wallet + .accounts + .all_accounts() + .into_iter() + .filter_map(|account| match account { + ManagedAccountRef::Funds(funds) + if !account + .managed_account_type() + .to_account_type() + .is_contact_owned() => + { + Some(funds) + } + _ => None, + }) +} + /// Unreachable for real amounts (the whole supply fits i64 many times over); /// saturate rather than panic on a corrupt record. fn saturating_net(net: i128) -> i64 { @@ -234,6 +253,19 @@ fn saturating_net(net: i128) -> i64 { /// nets `−(credit + fee)`. The net is returned as `i128` so each caller /// picks its own policy for a value outside `i64`. pub fn wallet_accounting(record: &TransactionRecord) -> (i128, TransactionDirection) { + let accounting = record_parts(record); + (accounting.net, accounting.direction) +} + +/// What [`accounting`] computes, before an `i64` policy is applied to `net`. +struct Accounting { + net: i128, + direction: TransactionDirection, + fee: Option, +} + +/// [`accounting`] over a record's details. +fn record_parts(record: &TransactionRecord) -> Accounting { let spent: Vec = record.input_details.iter().map(|d| d.value).collect(); let owned: Vec<(usize, u64)> = record .output_details @@ -256,16 +288,17 @@ pub fn wallet_accounting(record: &TransactionRecord) -> (i128, TransactionDirect ) } -/// The net formula and direction rule over a transaction's owned inputs -/// (`spent` values) and owned outputs (`(index, value)`); `unspendable` -/// outputs leave nothing the wallet could lose, like an OP_RETURN. +/// The net formula, direction rule and fee over a transaction's owned +/// inputs (`spent` values) and owned outputs (`(index, value)`); +/// `unspendable` outputs leave nothing the wallet could lose, like an +/// OP_RETURN. The fee is known only when every input is an owned one. fn accounting( transaction: &Transaction, transaction_type: TransactionType, spent: &[u64], owned: &[(usize, u64)], unspendable: &BTreeSet, -) -> (i128, TransactionDirection) { +) -> Accounting { let received: i128 = owned.iter().map(|(_, value)| i128::from(*value)).sum(); let paid: i128 = spent.iter().map(|value| i128::from(*value)).sum(); // Indexed once so the per-output check below stays linear. @@ -285,7 +318,23 @@ fn accounting( !owned.is_empty(), has_external, ); - (received - paid, direction) + let fee = (!spent.is_empty() && spent.len() == transaction.input.len()) + .then(|| { + let funded = spent + .iter() + .try_fold(0u64, |sum, value| sum.checked_add(*value))?; + let paid_out = transaction + .output + .iter() + .try_fold(0u64, |sum, output| sum.checked_add(output.value))?; + funded.checked_sub(paid_out) + }) + .flatten(); + Accounting { + net: received - paid, + direction, + fee, + } } /// Whether an output with this role belongs to the wallet. @@ -300,8 +349,10 @@ mod tests { use crate::test_support::core_fixtures::{ block, foreign_address, outside, pay, records_of, spend_of, wallet_with_receive_address, }; - use key_wallet::account::{Account, AccountType}; - use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; + use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; + use dashcore::transaction::special_transaction::TransactionPayload; + use dashcore::{Network, TxOut}; + use key_wallet::account::{Account, AccountType, StandardAccountType}; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::Wallet; @@ -309,18 +360,15 @@ mod tests { const PAID: u64 = 30_000; const CHANGE: u64 = 69_000; const FEE: u64 = FUNDING - PAID - CHANGE; + /// A CoinJoin denomination (0.001 DASH + fee). + const DENOMINATION: u64 = 100_001; - fn stored(transaction: &Transaction, needs_accounting: bool) -> StoredCoreTransaction { - StoredCoreTransaction { - transaction: transaction.clone(), - needs_accounting, - } - } - - /// A payment from outside into the wallet, then a spend of it that pays - /// a foreign address and returns change: what the live projection emits - /// for each, and the history the host stored. - async fn receive_then_spend() -> ( + /// A payment from outside into the wallet, then `spend` of that coin, + /// both seen by the live projection. Returns the wallet as it stands + /// afterwards, the live rows, and the two transactions. + async fn live( + spend: impl FnOnce(OutPoint, &Address) -> Transaction, + ) -> ( ManagedWalletInfo, Vec, Transaction, @@ -336,65 +384,186 @@ mod tests { .next_change_address(Some(&xpub), true) .unwrap(); let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); - let spend = spend_of( - &[OutPoint::new(funding.txid(), 0)], - vec![pay(PAID, &foreign_address(9)), pay(CHANGE, &change)], - ); - let mut live = Vec::new(); + let spend = spend(OutPoint::new(funding.txid(), 0), &change); + let mut rows = Vec::new(); for (tx, height) in [(&funding, 100), (&spend, 101)] { let mut records = records_of(&mut info, &mut wallet, tx, block(height)).await; fold_same_txid_records(&mut records); - live.extend(records.iter().filter_map(record_accounting)); + rows.extend(records.iter().filter_map(record_accounting)); } - (info, live, funding, spend) + (info, rows, funding, spend) + } + + fn payment_with_change(coin: OutPoint, change: &Address) -> Transaction { + spend_of( + &[coin], + vec![pay(PAID, &foreign_address(9)), pay(CHANGE, change)], + ) + } + + /// The backfill must write exactly what the live projection emitted for + /// the same transactions: amount, direction AND fee. + async fn assert_backfill_matches_live( + spend: impl FnOnce(OutPoint, &Address) -> Transaction, + ) -> Vec { + let (info, live, funding, spend) = live(spend).await; + assert_eq!(live.len(), 2, "the live projection accounts for both"); + let backfilled = history_accounting(&info, &[funding, spend], &[]); + assert_eq!(backfilled, live); + backfilled } #[tokio::test] - async fn should_backfill_stored_history_with_the_live_projections_accounting() { - let (info, live, funding, spend) = receive_then_spend().await; - assert_eq!( - live.iter().map(|row| row.net_amount).collect::>(), - vec![FUNDING as i64, CHANGE as i64 - FUNDING as i64], - "live projection: receive, then a spend netting −(paid + fee)" + async fn should_backfill_a_payment_with_change_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(payment_with_change).await; + + assert_eq!(rows[0].net_amount, FUNDING as i64); + assert_eq!(rows[0].fee, None, "a foreign input hides the fee"); + assert_eq!(rows[1].net_amount, CHANGE as i64 - FUNDING as i64); + assert_eq!(rows[1].direction, TransactionDirection::Outgoing); + assert_eq!(rows[1].fee, Some(FEE), "the wallet funded every input"); + } + + /// An asset lock with no change: the OP_RETURN burn becomes the wallet's + /// own Platform credits, so it is `Internal`, and the Core net is the + /// whole coin. + #[tokio::test] + async fn should_backfill_a_no_change_asset_lock_exactly_like_the_live_projection() { + let credit = FUNDING - FEE; + let rows = assert_backfill_matches_live(|coin, _| { + let mut lock = spend_of( + &[coin], + vec![TxOut { + value: credit, + script_pubkey: ScriptBuf::new_op_return(&[]), + }], + ); + lock.version = 3; + lock.special_transaction_payload = + Some(TransactionPayload::AssetLockPayloadType(AssetLockPayload { + version: 1, + credit_outputs: vec![pay(credit, &foreign_address(5))], + })); + lock + }) + .await; + + assert_eq!(rows[1].net_amount, -(FUNDING as i64)); + assert_eq!(rows[1].direction, TransactionDirection::Internal); + assert_eq!(rows[1].fee, Some(FEE)); + } + + /// An address-less, provably unspendable output that is not an OP_RETURN + /// takes nothing out of the wallet either: a self-transfer stays + /// `Internal`, as key-wallet's `Unspendable` role makes it live. + #[tokio::test] + async fn should_backfill_an_unspendable_output_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(|coin, change| { + spend_of( + &[coin], + vec![ + pay(CHANGE, change), + TxOut { + value: 0, + // OP_VERIF: an illegal opcode, so provably unspendable. + script_pubkey: ScriptBuf::from_bytes(vec![0x65]), + }, + ], + ) + }) + .await; + + assert_eq!(rows[1].direction, TransactionDirection::Internal); + } + + #[tokio::test] + async fn should_backfill_a_coinjoin_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(|coin, change| { + spend_of( + &[coin, outside(1), outside(2)], + vec![ + pay(DENOMINATION, change), + pay(DENOMINATION, &foreign_address(3)), + pay(DENOMINATION, &foreign_address(4)), + ], + ) + }) + .await; + + assert_eq!(rows[1].direction, TransactionDirection::CoinJoin); + assert_eq!(rows[1].fee, None, "other participants funded inputs"); + } + + /// A spend's coin resolves from a funding transaction passed in + /// `funding`; only `unaccounted` transactions get a row. + #[tokio::test] + async fn should_resolve_inputs_from_funding_transactions_and_answer_only_unaccounted() { + let (info, live, funding, spend) = live(payment_with_change).await; + + let backfilled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]); + + assert_eq!(backfilled, vec![live[1]]); + } + + /// A spend whose funding transaction the host no longer has still + /// resolves while its coin is a restored UTXO. + #[tokio::test] + async fn should_resolve_an_input_from_a_restored_utxo_alone() { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + records_of(&mut info, &mut wallet, &funding, block(100)).await; + let spend = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![pay(FUNDING - FEE, &foreign_address(9))], ); - let backfilled = history_accounting(&info, &[stored(&funding, true), stored(&spend, true)]); + let rows = history_accounting(&info, std::slice::from_ref(&spend), &[]); - assert_eq!(backfilled.len(), 2); - for (row, expected) in backfilled.iter().zip(&live) { - assert_eq!(row.txid, expected.txid); - assert_eq!(row.net_amount, expected.net_amount); - assert_eq!(row.direction, expected.direction); - } - assert_eq!(backfilled[1].direction, TransactionDirection::Outgoing); assert_eq!( - backfilled[1].fee, - Some(FEE), - "the wallet funded every input" + rows, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -(FUNDING as i64), + direction: TransactionDirection::Outgoing, + fee: Some(FEE), + }] ); - assert_eq!(backfilled[0].fee, None, "a foreign input hides the fee"); } - /// The other stored transactions resolve a flagged spend's inputs, but - /// only flagged ones get a row. + /// The manager fetches only prevouts nothing else resolves: not the + /// unaccounted transactions' own outputs, not restored UTXOs, not a + /// coinbase's null prevout. #[tokio::test] - async fn should_resolve_inputs_from_unflagged_history_and_answer_only_flagged_rows() { - let (info, live, funding, spend) = receive_then_spend().await; + async fn should_name_only_the_funding_transactions_nothing_else_resolves() { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let utxo_funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + records_of(&mut info, &mut wallet, &utxo_funding, block(100)).await; + let chained = spend_of(&[outside(8)], vec![pay(FUNDING, &foreign_address(1))]); + let spend = spend_of( + &[ + OutPoint::new(utxo_funding.txid(), 0), + OutPoint::new(chained.txid(), 0), + outside(6), + OutPoint::null(), + ], + vec![pay(1, &foreign_address(2))], + ); - let backfilled = - history_accounting(&info, &[stored(&funding, false), stored(&spend, true)]); + let needed = funding_txids(&info, &[chained.clone(), spend]); - assert_eq!(backfilled.len(), 1); - assert_eq!(backfilled[0].txid, spend.txid()); - assert_eq!(backfilled[0].net_amount, live[1].net_amount); + assert_eq!( + needed, + BTreeSet::from([outside(6).txid, outside(8).txid]), + "`chained` is itself unaccounted and the UTXO is restored" + ); } #[tokio::test] async fn should_yield_no_row_for_a_transaction_touching_no_owned_script() { - let (info, _, _, _) = receive_then_spend().await; + let (info, _, _, _) = live(payment_with_change).await; let unrelated = spend_of(&[outside(3)], vec![pay(5_000, &foreign_address(4))]); - assert!(history_accounting(&info, &[stored(&unrelated, true)]).is_empty()); + assert!(history_accounting(&info, &[unrelated], &[]).is_empty()); } /// An output on a contact's watch-only chain is the contact's money, @@ -403,11 +572,8 @@ mod tests { #[tokio::test] async fn should_not_count_a_contacts_watch_only_output_as_owned() { let (_, mut info, receive) = wallet_with_receive_address(); - let contact_wallet = Wallet::new_random( - dashcore::Network::Testnet, - WalletAccountCreationOptions::Default, - ) - .unwrap(); + let contact_wallet = + Wallet::new_random(Network::Testnet, WalletAccountCreationOptions::Default).unwrap(); let contact_xpub = contact_wallet.accounts.standard_bip44_accounts[&0].account_xpub; let account = Account::new( None, @@ -417,7 +583,7 @@ mod tests { friend_identity_id: [2; 32], }, contact_xpub, - dashcore::Network::Testnet, + Network::Testnet, ) .unwrap(); let contact_account = ManagedCoreFundsAccount::from_account(&account); @@ -435,7 +601,7 @@ mod tests { vec![pay(FUNDING - FEE, &contact_address)], ); - let rows = history_accounting(&info, &[stored(&funding, false), stored(&to_contact, true)]); + let rows = history_accounting(&info, std::slice::from_ref(&to_contact), &[funding]); assert_eq!(rows.len(), 1); assert_eq!(rows[0].net_amount, -(FUNDING as i64)); @@ -452,7 +618,7 @@ mod tests { tx, AccountType::Standard { index: 0, - standard_account_type: key_wallet::account::StandardAccountType::BIP44Account, + standard_account_type: StandardAccountType::BIP44Account, }, block(10), TransactionType::AssetLock, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index e32c25762fa..96f6d277cdf 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -4,7 +4,8 @@ use std::collections::BTreeMap; use std::sync::Arc; use crate::changeset::{ - history_accounting, ClientStartState, ClientWalletStartState, PlatformWalletPersistence, + funding_txids, history_accounting, ClientStartState, ClientWalletStartState, + PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; @@ -744,16 +745,19 @@ impl PlatformWalletManager

{ Ok(()) } - /// Hand the persister the per-wallet accounting of every stored - /// transaction it flags as lacking one (see + /// Backfill per-wallet accounting for the stored transactions the + /// persister holds without one (see /// [`PlatformWalletPersistence::load_core_history_for_accounting`]). + /// Nothing is written unless every step succeeds, so a failed backfill + /// simply runs again at the next load. async fn backfill_history_accounting(&self, wallet_id: WalletId) { let persister = Arc::clone(&self.persister); - let history = + let unaccounted = match run_blocking_load(move || persister.load_core_history_for_accounting(wallet_id)) .await { - Ok(history) => history, + Ok(Some(unaccounted)) => unaccounted, + Ok(None) => return, Err(e) => { tracing::warn!( wallet_id = %hex::encode(wallet_id), @@ -763,24 +767,47 @@ impl PlatformWalletManager

{ return; } }; - if !history.iter().any(|stored| stored.needs_accounting) { - return; - } + let needed = { + let wm = self.wallet_manager.read().await; + let Some(info) = wm.get_wallet_info(&wallet_id) else { + return; + }; + funding_txids(&info.core_wallet, &unaccounted) + }; + // Fetched without the manager lock held: each lookup calls the host. + let persister = Arc::clone(&self.persister); + let funding = match run_blocking_load(move || { + let mut funding = Vec::new(); + for txid in &needed { + if let Some(record) = persister.get_core_tx_record(wallet_id, txid)? { + funding.push(record.transaction); + } + } + Ok(funding) + }) + .await + { + Ok(funding) => funding, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = ?e, + "load: reading funding transactions for the accounting backfill failed" + ); + return; + } + }; let rows = { let wm = self.wallet_manager.read().await; let Some(info) = wm.get_wallet_info(&wallet_id) else { return; }; - history_accounting(&info.core_wallet, &history) + history_accounting(&info.core_wallet, &unaccounted, &funding) }; - if rows.is_empty() { - return; - } let persister = Arc::clone(&self.persister); let count = rows.len(); if let Err(e) = - run_blocking_load(move || persister.store_transaction_accounting(wallet_id, &rows)) - .await + run_blocking_load(move || persister.store_history_accounting(wallet_id, &rows)).await { tracing::warn!( wallet_id = %hex::encode(wallet_id), @@ -911,9 +938,18 @@ mod idempotent_load_tests { use key_wallet::wallet::ManagedWalletInfo; use key_wallet::Wallet; + use std::sync::Mutex; + + use dashcore::{OutPoint, Transaction, Txid}; + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::managed_account::transaction_record::{ + TransactionDirection, TransactionRecord, + }; + use key_wallet::transaction_checking::{TransactionContext, TransactionType}; + use crate::changeset::{ ClientStartState, ClientWalletStartState, IdentityManagerStartState, PersistenceError, - PlatformWalletChangeSet, PlatformWalletPersistence, + PlatformWalletChangeSet, PlatformWalletPersistence, WalletAccounting, }; use crate::events::PlatformEventHandler; use crate::test_support::NoopTestEventHandler; @@ -1367,13 +1403,17 @@ mod idempotent_load_tests { ); } - /// Hands back a stored history for the accounting backfill and records - /// what the manager persists from it. + /// Hands back the stored transactions lacking accounting (`None` once + /// backfilled), serves funding transactions by txid, and records what + /// the manager persists. struct HistoryPersister { wallet: Wallet, managed: ManagedWalletInfo, - history: Vec, - stored: std::sync::Mutex)>>, + unaccounted: Option>, + stored_transactions: BTreeMap, + failing_lookup: bool, + lookups: Mutex>, + stored: Mutex)>>, } impl PlatformWalletPersistence for HistoryPersister { @@ -1397,43 +1437,61 @@ mod idempotent_load_tests { .load() } + fn get_core_tx_record( + &self, + _wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + self.lookups.lock().unwrap().push(*txid); + if self.failing_lookup { + return Err(PersistenceError::backend("store unreadable")); + } + Ok(self.stored_transactions.get(txid).map(|tx| { + TransactionRecord::new( + tx.clone(), + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + TransactionContext::Mempool, + TransactionType::Standard, + TransactionDirection::Incoming, + Vec::new(), + Vec::new(), + 0, + ) + })) + } + fn load_core_history_for_accounting( &self, _wallet_id: WalletId, - ) -> Result, PersistenceError> { - Ok(self.history.clone()) + ) -> Result>, PersistenceError> { + Ok(self.unaccounted.clone()) } - fn store_transaction_accounting( + fn store_history_accounting( &self, wallet_id: WalletId, - rows: &[crate::changeset::WalletAccounting], + rows: &[WalletAccounting], ) -> Result<(), PersistenceError> { self.stored.lock().unwrap().push((wallet_id, rows.to_vec())); Ok(()) } } - async fn load_with_history( - flagged: bool, - ) -> ( - WalletId, - dashcore::Transaction, - Vec<(WalletId, Vec)>, - ) { + /// A wallet funded by `funding` (100_000 duffs, output 0), and a spend of + /// that coin paying 74_000 to a foreign address. + async fn funded_wallet() -> (Wallet, ManagedWalletInfo, Transaction, Transaction) { let (ctx, funding) = TestWalletContext::new_random() .with_mempool_funding(100_000) .await; - let wallet_id = ctx.wallet.compute_wallet_id(); - let persister = Arc::new(HistoryPersister { - wallet: ctx.wallet, - managed: ctx.managed_wallet, - history: vec![crate::changeset::StoredCoreTransaction { - transaction: funding.clone(), - needs_accounting: flagged, - }], - stored: std::sync::Mutex::new(Vec::new()), - }); + let spend = spend_to(OutPoint::new(funding.txid(), 0), 74_000); + (ctx.wallet, ctx.managed_wallet, funding, spend) + } + + async fn backfill(persister: HistoryPersister) -> Arc { + let persister = Arc::new(persister); let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let event_handler: Arc = Arc::new(NoopTestEventHandler); let manager = PlatformWalletManager::new(sdk, Arc::clone(&persister), event_handler); @@ -1441,37 +1499,134 @@ mod idempotent_load_tests { .load_from_persistor() .await .expect("the wallet must load"); - let stored = persister.stored.lock().unwrap().clone(); - (wallet_id, funding, stored) + persister + } + + fn history_persister( + wallet: Wallet, + managed: ManagedWalletInfo, + unaccounted: Option>, + stored: &[&Transaction], + ) -> HistoryPersister { + HistoryPersister { + wallet, + managed, + unaccounted, + stored_transactions: stored.iter().map(|tx| (tx.txid(), (*tx).clone())).collect(), + failing_lookup: false, + lookups: Mutex::new(Vec::new()), + stored: Mutex::new(Vec::new()), + } + } + + fn without_utxos(mut managed: ManagedWalletInfo) -> ManagedWalletInfo { + for account in managed.accounts.standard_bip44_accounts.values_mut() { + account.utxos.clear(); + } + managed } - /// Rows the host stored before it kept per-wallet accounting are - /// backfilled at load from the restored wallet's own view of its scripts. + /// Rows stored before per-wallet accounting are backfilled at load. A + /// spend whose coin is among the unaccounted transactions resolves + /// without a lookup; only the funding transaction's foreign prevout is + /// asked for (and not found). #[tokio::test] - async fn load_backfills_accounting_for_stored_history_the_host_flags() { - let (wallet_id, funding, stored) = load_with_history(true).await; + async fn load_backfills_accounting_for_unaccounted_history() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let persister = backfill(history_persister( + wallet, + without_utxos(managed), + Some(vec![funding.clone(), spend.clone()]), + &[], + )) + .await; - assert_eq!(stored.len(), 1, "one backfill write for the wallet"); - let (written_for, rows) = &stored[0]; - assert_eq!(written_for, &wallet_id); assert_eq!( - rows.as_slice(), - &[crate::changeset::WalletAccounting { - txid: funding.txid(), - net_amount: 100_000, - direction: - key_wallet::managed_account::transaction_record::TransactionDirection::Incoming, - fee: None, + *persister.stored.lock().unwrap(), + vec![( + wallet_id, + vec![ + WalletAccounting { + txid: funding.txid(), + net_amount: 100_000, + direction: TransactionDirection::Incoming, + fee: None, + }, + WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), + }, + ] + )] + ); + assert!( + !persister.lookups.lock().unwrap().contains(&funding.txid()), + "the spend's coin resolves from the unaccounted funding transaction" + ); + } + + /// A spend whose coin is neither among the unaccounted transactions nor + /// a UTXO any more resolves through the funding transaction the manager + /// fetches itself; the host is never asked to send its whole history. + #[tokio::test] + async fn load_fetches_the_funding_transaction_a_backfilled_spend_needs() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let persister = backfill(history_persister( + wallet, + without_utxos(managed), + Some(vec![spend.clone()]), + &[&funding], + )) + .await; + + assert_eq!(*persister.lookups.lock().unwrap(), vec![funding.txid()]); + let stored = persister.stored.lock().unwrap().clone(); + assert_eq!(stored.len(), 1); + assert_eq!( + stored[0].1, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), }] ); } - /// A host whose history is all accounted writes nothing. + /// A failed funding lookup writes nothing, so the backfill (and the + /// completion it records) runs again at the next load. #[tokio::test] - async fn load_skips_the_backfill_when_nothing_is_flagged() { - let (_, _, stored) = load_with_history(false).await; + async fn load_writes_no_backfill_when_a_funding_lookup_fails() { + let (wallet, managed, _, spend) = funded_wallet().await; + let mut persister = + history_persister(wallet, without_utxos(managed), Some(vec![spend]), &[]); + persister.failing_lookup = true; + let persister = backfill(persister).await; + + assert_eq!(persister.lookups.lock().unwrap().len(), 1); + assert!(persister.stored.lock().unwrap().is_empty()); + } + + /// Nothing unaccounted still completes the backfill, so the host stops + /// being asked; an already backfilled wallet is left alone. + #[tokio::test] + async fn load_completes_an_empty_backfill_and_skips_a_finished_one() { + let (wallet, managed, _, _) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let empty = backfill(history_persister( + wallet.clone(), + managed.clone(), + Some(Vec::new()), + &[], + )) + .await; + assert_eq!(*empty.stored.lock().unwrap(), vec![(wallet_id, Vec::new())]); - assert!(stored.is_empty()); + let finished = backfill(history_persister(wallet, managed, None, &[])).await; + assert!(finished.stored.lock().unwrap().is_empty()); } fn make_pending_manager( From 651d1d5476ac9a85917290fabea30eaffb79d213 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:14:30 +0000 Subject: [PATCH 12/21] fix(swift-sdk): mark per-wallet accounting backfills and stop scalar fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `PersistentWallet.transactionAccountingBackfilled` (V5, unreleased) records Rust's per-wallet completion marker; a backfilled wallet's load costs one indexed fetch, and an unfinished one offers only its own transactions lacking a row (wallet relationships and walletId-indexed TXOs / pending inputs, not a whole-store scan). - Accounting rows always arrive inside a round and stage with it; the out-of-round save path is gone. - Wallet-scoped amount, direction and fee are `nil` without the wallet's row and display as "—" instead of another wallet's shared scalar. - Storage Explorer lists `PersistentTransactionAccounting` rows and shows the wallet's backfill marker. - Re-attach detached doc comments and refresh the V5 schema docs. - Tests cover the backfill round commit and rollback, the marker, the wallet-scoped offer, loan release and the C shims end to end. Co-Authored-By: Claude Opus 5.5 --- packages/swift-sdk/SCHEMA_RELEASES.md | 19 +- .../Persistence/DashModelContainer.swift | 3 +- .../Models/PersistentTransaction.swift | 34 ++-- .../Persistence/Models/PersistentWallet.swift | 5 + .../PlatformWalletPersistenceHandler.swift | 171 +++++++++++++----- .../Core/Views/TransactionDetailView.swift | 10 +- .../Views/TransactionDirectionStyle.swift | 7 +- .../Core/Views/TransactionListView.swift | 9 +- .../Views/StorageExplorerView.swift | 8 + .../Views/StorageModelListViews.swift | 47 +++++ .../Views/StorageRecordDetailViews.swift | 38 ++++ .../TransactionAccountingTests.swift | 146 ++++++++++++--- 12 files changed, 386 insertions(+), 111 deletions(-) diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index ee711319892..62882b7cf65 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -24,13 +24,18 @@ schema. The former live V2 is accepted only when its complete graph matches frozen V3 exactly, entity hashes and checksum alike. The alias remains supported when the live graph changes; other stores labelled `2.0.0` fail closed. -V5 adds the `PersistentTransactionAccounting` entity through a lightweight -migration: one row per `(walletId, txid)` holding the net amount, direction and -fee `rs-platform-wallet` emitted for that wallet. Migrated stores start with no -rows; at the first load Rust reads the stored history back -(`on_load_core_history_for_accounting_fn`), computes the accounting of every -transaction lacking a row and hands it to `on_persist_transaction_accounting_fn`. -Deleting a wallet deletes its rows; deleting a transaction cascades to them. +V5 adds the `PersistentTransactionAccounting` entity and the +`PersistentWallet.transactionAccountingBackfilled` attribute (default `false`) +through a lightweight migration. Each accounting row, one per +`(walletId, txid)`, holds the net amount, direction and fee `rs-platform-wallet` +emitted for that wallet. Migrated stores start with no rows; at the first load +of each wallet Rust reads back the wallet's transactions that lack a row +(`on_load_core_history_for_accounting_fn`), fetches any funding transaction it +needs through `on_get_core_tx_record_fn`, and writes the rows +(`on_persist_transaction_accounting_fn`) and the marker +(`on_persist_core_history_accounted_fn`) in one round. Later loads see the +marker and skip the read. Deleting a wallet deletes its rows; deleting a +transaction cascades to them. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index de65f8c5466..0395d0115ac 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -403,7 +403,8 @@ public enum DashSchemaV4: VersionedSchema { public static var models: [any PersistentModel.Type] { DashSchemaSnapshotV4.models } } -/// Current schema: optional transaction accounting availability. +/// Current schema: per-wallet transaction accounting (`PersistentTransactionAccounting`) +/// and the wallet's accounting-backfill marker. public enum DashSchemaV5: VersionedSchema { public static var versionIdentifier: Schema.Version { Schema.Version(5, 0, 0) } public static var models: [any PersistentModel.Type] { DashModelContainer.modelTypes } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index bfd3811ba9c..d097d974b34 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -86,8 +86,8 @@ public final class PersistentTransaction { public var transactionTypeKind: UInt8 = 0xFF /// Net amount in duffs (signed: positive=received, negative=sent) as the /// last recording wallet's `TransactionRecordFFI` carried it. Shared by - /// every wallet in the transaction; per-wallet values live in - /// [`walletAccounting`]; `netAmount(for:)` prefers the wallet's row. + /// every wallet in the transaction, so only unscoped views (the storage + /// explorer) show it; per-wallet values live in [`walletAccounting`]. public var netAmount: Int64 /// Fee in duffs (nil if unknown); same last-writer caveat as `netAmount`. public var fee: UInt64? @@ -241,35 +241,34 @@ public final class PersistentTransaction { walletAccounting.first { $0.walletId == walletId } } - /// Core value movement for one wallet: its Rust-emitted row, else the - /// shared stored amount (a transaction Rust has no accounting for). - public func netAmount(for walletId: Data) -> Int64 { - accounting(for: walletId)?.netAmount ?? netAmount + /// Core value movement for one wallet; `nil` when Rust has emitted no + /// accounting for this wallet and transaction. + public func netAmount(for walletId: Data) -> Int64? { + accounting(for: walletId)?.netAmount } - /// `CoreDirectionCode` for one wallet, with the same fallback as `netAmount(for:)`. - public func direction(for walletId: Data) -> UInt32 { - accounting(for: walletId)?.direction ?? direction + /// `CoreDirectionCode` for one wallet; `nil` like `netAmount(for:)`. + public func direction(for walletId: Data) -> UInt32? { + accounting(for: walletId)?.direction } - /// Fee for one wallet, with the same fallback as `netAmount(for:)`. + /// Fee for one wallet; `nil` without a row, or when it funded only some inputs. public func fee(for walletId: Data) -> UInt64? { - guard let row = accounting(for: walletId) else { return fee } - return row.fee + accounting(for: walletId)?.fee } - /// Format the wallet's Core value movement in DASH. + /// The wallet's Core value movement in DASH, or "—" without accounting. public func formattedAmount(for walletId: Data) -> String { - Self.format(duffs: netAmount(for: walletId)) + netAmount(for: walletId).map(Self.format(duffs:)) ?? Self.noAccounting } /// Net amount for `walletId`, or the stored scalar when no wallet scope is given. - public func displayNetAmount(for walletId: Data?) -> Int64 { + public func displayNetAmount(for walletId: Data?) -> Int64? { walletId.map { netAmount(for: $0) } ?? netAmount } /// `CoreDirectionCode` for `walletId`, or the stored direction when no wallet scope is given. - public func displayDirectionCode(for walletId: Data?) -> UInt32 { + public func displayDirectionCode(for walletId: Data?) -> UInt32? { walletId.map { direction(for: $0) } ?? direction } @@ -283,6 +282,9 @@ public final class PersistentTransaction { walletId.map { formattedAmount(for: $0) } ?? formattedAmount } + /// Shown in place of an amount Rust has not accounted for this wallet. + public static let noAccounting = "—" + /// Signed DASH text for a duff amount; `magnitude` cannot trap on `Int64.min`. static func format(duffs: Int64) -> String { String(format: "%@%.8f DASH", duffs >= 0 ? "+" : "-", Double(duffs.magnitude) / 100_000_000) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift index 52365db3006..2d98dac84be 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift @@ -118,6 +118,11 @@ public final class PersistentWallet { /// column, or never verified) means the full check runs at the next /// unlock. public var seedBindingVerifiedMarker: String? + /// Set once Rust's load-time accounting backfill has evaluated this + /// wallet's stored transactions (`on_persist_core_history_accounted_fn`). + /// Opaque to Swift: it only stops the history from being offered again. + /// `false` for rows predating the column, so they are backfilled once. + public var transactionAccountingBackfilled: Bool = false /// Record timestamps. public var createdAt: Date public var lastUpdated: Date diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 6322f76b003..404897a3f50 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -3299,13 +3299,14 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { extensionCallbacks.on_persist_asset_lock_operation_id_fn = persistAssetLockOperationIdCallback extensionCallbacks.on_load_asset_lock_operation_id_fn = loadAssetLockOperationIdCallback // Per-wallet transaction accounting exactly as Rust computes it: - // after each round's changeset, and once at load for stored rows - // that have none yet (Rust reads the history back, computes, and - // hands the rows to the persist slot). + // after each round's changeset, and once per wallet at load for + // stored rows that have none yet (Rust reads them back, computes, + // and writes the rows plus the completion marker in one round). extensionCallbacks.on_persist_transaction_accounting_fn = persistTransactionAccountingCallback extensionCallbacks.on_load_core_history_for_accounting_fn = loadCoreHistoryForAccountingCallback extensionCallbacks.on_load_core_history_for_accounting_free_fn = loadCoreHistoryForAccountingFreeCallback + extensionCallbacks.on_persist_core_history_accounted_fn = persistCoreHistoryAccountedCallback return extensionCallbacks } @@ -9070,16 +9071,11 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } } - /// Outstanding tracked-masternode load allocations keyed by the - /// entries pointer we handed to Rust. - /// Outstanding stored-history loans handed to Rust for the accounting - /// backfill, keyed by the rows pointer. - private var coreHistoryLoadAllocations: [UnsafeRawPointer: CoreHistoryLoadAllocation] = [:] - /// Upsert one wallet's Rust-emitted accounting rows, keyed by - /// `(walletId, txid)`. Inside a round they stage with it and commit at - /// `endChangeset`; outside one (the load-time backfill) they commit here. - /// Returns `false` to fail the round, like the other per-kind callbacks. + /// `(walletId, txid)`. Always fired inside a round (a sync round, or the + /// load-time backfill's own), so they stage with it and commit at + /// `endChangeset`. Returns `false` to fail the round, like the other + /// per-kind callbacks. func persistTransactionAccounting( walletId: Data, rows: UnsafePointer?, @@ -9095,8 +9091,22 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { for i in 0.. Bool { + onQueue { + switch roundWalletLookup(walletId: walletId, callback: "core_history_accounted") { + case .failed: return false + case .absent: return true + case .found(let wallet): + wallet.transactionAccountingBackfilled = true + return true + } } } @@ -9124,47 +9134,87 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } } - /// The wallet's stored transactions for Rust's accounting backfill: none - /// when each already has this wallet's row, otherwise all of them, with - /// the ones lacking a row flagged (the others resolve their inputs). - /// Payload-only transactions never get a row, so a wallet holding one - /// is offered its history on every load; Rust answers it with no rows. + /// Rust's accounting-backfill read. `backfilled` once the wallet's + /// marker is set (or the wallet row is gone): one indexed fetch, no + /// history scan. Otherwise the wallet's stored transactions lacking its + /// accounting row, gathered from the wallet's own relationships and + /// `walletId`-indexed rows rather than the whole store. Rust fetches any + /// funding transaction it needs itself, so a transaction missed here + /// only stays without a row. func loadCoreHistoryForAccounting( walletId: Data - ) -> (rows: UnsafePointer?, count: Int, errored: Bool) { + ) -> (backfilled: Bool, rows: UnsafePointer?, count: Int, errored: Bool) { onQueue { - let history: [PersistentTransaction] + let unaccounted: [PersistentTransaction] do { - history = try modelFetcher.fetch(FetchDescriptor(), in: backgroundContext) - .filter { Self.walletOwnsTransaction(walletId: walletId, transaction: $0) } + guard let wallet = try fetchWalletRecord(walletId: walletId), + !wallet.transactionAccountingBackfilled else { + return (true, nil, 0, false) + } + unaccounted = try walletTransactions(wallet: wallet) + .filter { $0.accounting(for: walletId) == nil } } catch { SDKLogger.event( "persistence_core_history_load_failed", category: .persistence, severity: .error, fields: ["wallet_reference": .reference(walletId)], error: error ) - return (nil, 0, true) + return (false, nil, 0, true) } - let flags = history.map { $0.accounting(for: walletId) == nil } - guard flags.contains(true) else { return (nil, 0, false) } + guard !unaccounted.isEmpty else { return (false, nil, 0, false) } let allocation = CoreHistoryLoadAllocation() - let buf = UnsafeMutablePointer.allocate(capacity: history.count) + let buf = UnsafeMutablePointer.allocate(capacity: unaccounted.count) allocation.entries = buf - for (index, transaction) in history.enumerated() { + for (index, transaction) in unaccounted.enumerated() { let body = transaction.transactionData let bytes = UnsafeMutablePointer.allocate(capacity: max(body.count, 1)) body.copyBytes(to: bytes, count: body.count) allocation.buffers.append(bytes) buf[index] = CoreHistoryTransactionFFI( tx_bytes: UnsafePointer(bytes), - tx_bytes_len: UInt(body.count), - needs_accounting: flags[index] + tx_bytes_len: UInt(body.count) ) } coreHistoryLoadAllocations[UnsafeRawPointer(buf)] = allocation - return (UnsafePointer(buf), history.count, false) + return (false, UnsafePointer(buf), unaccounted.count, false) } } + /// Every stored transaction touching `wallet`, deduplicated by txid: + /// its accounts' `involvedTransactions`, plus the creating and spending + /// transactions of its TXOs and pending inputs. TXOs migrated with an + /// empty `walletId` are matched through their account, like + /// `walletOwnsTransaction`. + private func walletTransactions(wallet: PersistentWallet) throws -> [PersistentTransaction] { + let walletId = wallet.walletId + var byTxid: [Data: PersistentTransaction] = [:] + func add(_ transaction: PersistentTransaction?) { + guard let transaction else { return } + byTxid[transaction.txid] = transaction + } + for account in wallet.accounts { + account.involvedTransactions.forEach(add) + } + let emptyWalletId = Data() + let ownTxos: [PersistentTxo] = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == walletId }), + in: backgroundContext + ) + let legacyTxos: [PersistentTxo] = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == emptyWalletId }), + in: backgroundContext + ).filter { Self.resolvedWalletId(of: $0) == walletId } + for txo in ownTxos + legacyTxos { + add(txo.transaction) + add(txo.spendingTransaction) + } + let pendingInputs = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == walletId }), + in: backgroundContext + ) + pendingInputs.forEach { add($0.spendingTransaction) } + return Array(byTxid.values) + } + /// Release a loan handed out by `loadCoreHistoryForAccounting`. func loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer?) { onQueue { @@ -9174,9 +9224,20 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } } + /// Outstanding tracked-masternode load allocations keyed by the + /// entries pointer we handed to Rust. private var trackedMasternodeLoadAllocations: [UnsafeRawPointer: TrackedMasternodeLoadAllocation] = [:] + /// Outstanding unaccounted-history loans handed to Rust for the + /// accounting backfill, keyed by the rows pointer. + private var coreHistoryLoadAllocations: [UnsafeRawPointer: CoreHistoryLoadAllocation] = [:] + + /// Loans Rust has not returned yet; zero between backfills. + var outstandingCoreHistoryLoanCount: Int { + onQueue { coreHistoryLoadAllocations.count } + } + /// Human-readable name for a persisted account, mirroring the /// top-level `AccountTypeTagFFI` discriminant plus — for tag 0 /// (Standard) — the `StandardAccountTypeTagFFI` sub-discriminant. @@ -9416,20 +9477,6 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// Owned allocations behind one `loadTrackedMasternodes` answer: the /// entries buffer plus every strdup'd label / snapshot string. Trivial C /// structs — deallocate only. -private final class CoreHistoryLoadAllocation { - var entries: UnsafeMutablePointer? - var buffers: [UnsafeMutablePointer] = [] - - func release() { - for buffer in buffers { - buffer.deallocate() - } - buffers.removeAll() - entries?.deallocate() - entries = nil - } -} - private final class TrackedMasternodeLoadAllocation { var entries: UnsafeMutablePointer? var count: Int = 0 @@ -9446,6 +9493,23 @@ private final class TrackedMasternodeLoadAllocation { } } +/// Owned allocations behind one `loadCoreHistoryForAccounting` answer: the +/// entries buffer plus each copied transaction body. Released by +/// `loadCoreHistoryForAccountingFree`. +private final class CoreHistoryLoadAllocation { + var entries: UnsafeMutablePointer? + var buffers: [UnsafeMutablePointer] = [] + + func release() { + for buffer in buffers { + buffer.deallocate() + } + buffers.removeAll() + entries?.deallocate() + entries = nil + } +} + private final class LoadAllocation { var entries: UnsafeMutablePointer? /// Allocated capacity — equal to `restorable.count`. Used for @@ -12011,21 +12075,34 @@ private func persistTransactionAccountingCallback( private func loadCoreHistoryForAccountingCallback( context: UnsafeMutableRawPointer?, walletIdPtr: UnsafePointer?, + outBackfilled: UnsafeMutablePointer?, outRows: UnsafeMutablePointer?>?, outCount: UnsafeMutablePointer? ) -> Int32 { - guard let context, let walletIdPtr, let outRows, let outCount else { return 1 } + guard let context, let walletIdPtr, let outBackfilled, let outRows, let outCount else { return 1 } + outBackfilled.pointee = false outRows.pointee = nil outCount.pointee = 0 let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() - let (rows, count, errored) = handler.loadCoreHistoryForAccounting( + let (backfilled, rows, count, errored) = handler.loadCoreHistoryForAccounting( walletId: Data(bytes: walletIdPtr, count: 32) ) + outBackfilled.pointee = backfilled outRows.pointee = rows outCount.pointee = UInt(count) return errored ? 1 : 0 } +/// C shim for `on_persist_core_history_accounted_fn`. +private func persistCoreHistoryAccountedCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer? +) -> Int32 { + guard let context, let walletIdPtr else { return 1 } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + return handler.persistCoreHistoryAccounted(walletId: Data(bytes: walletIdPtr, count: 32)) ? 0 : 1 +} + /// C shim for `on_load_core_history_for_accounting_free_fn`. private func loadCoreHistoryForAccountingFreeCallback( context: UnsafeMutableRawPointer?, diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift index 4f2c5753486..0875e244d16 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift @@ -4,9 +4,10 @@ import SwiftDashSDK struct TransactionDetailView: View { let transaction: PersistentTransaction var walletId: Data? = nil - /// This wallet's Rust-emitted amount (see `PersistentTransaction.netAmount(for:)`). - private var netAmount: Int64 { transaction.displayNetAmount(for: walletId) } - private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } + /// This wallet's Rust-emitted amount and direction; `nil` when Rust + /// emitted no accounting for it (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32? { transaction.displayDirectionCode(for: walletId) } /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil @Environment(\.dismiss) private var dismiss @@ -34,6 +35,7 @@ struct TransactionDetailView: View { || transaction.isProviderSpecial { return transaction.displayDirection } + guard let direction else { return "Transaction" } switch direction { case CoreDirectionCode.incoming: return "Received" @@ -188,7 +190,7 @@ struct TransactionDetailView: View { ) } - if let fee = formattedFee, netAmount < 0 { + if let fee = formattedFee, let amount = netAmount, amount < 0 { TransactionDetailRow( label: "Network Fee", value: fee diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift index 589aa6bb7f0..256b7a8ae19 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift @@ -3,7 +3,9 @@ import SwiftDashSDK /// Direction icon and colour shared by the transaction list and detail views. enum TransactionDirectionStyle { - static func icon(for direction: UInt32) -> String { + /// `nil` (no accounting from Rust for this wallet) reads as unknown. + static func icon(for direction: UInt32?) -> String { + guard let direction else { return "questionmark.circle" } switch direction { case CoreDirectionCode.incoming: return "arrow.down.circle.fill" case CoreDirectionCode.outgoing: return "arrow.up.circle.fill" @@ -14,7 +16,8 @@ enum TransactionDirectionStyle { } /// Internal transfers share the outgoing colour: they still pay a fee. - static func color(for direction: UInt32) -> Color { + static func color(for direction: UInt32?) -> Color { + guard let direction else { return .secondary } switch direction { case CoreDirectionCode.incoming: return .green case CoreDirectionCode.outgoing, CoreDirectionCode.internalTransfer: return .red diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift index e38acb46e4a..3d6eaffa633 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift @@ -221,9 +221,10 @@ struct TransactionListView: View { struct TransactionRowView: View { let transaction: PersistentTransaction var walletId: Data? = nil - /// This wallet's Rust-emitted amount (see `PersistentTransaction.netAmount(for:)`). - private var netAmount: Int64 { transaction.displayNetAmount(for: walletId) } - private var direction: UInt32 { transaction.displayDirectionCode(for: walletId) } + /// This wallet's Rust-emitted amount and direction; `nil` when Rust + /// emitted no accounting for it (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32? { transaction.displayDirectionCode(for: walletId) } /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil /// The DashPay payment this tx belongs to, if any — joined by `txid` in @@ -388,7 +389,7 @@ struct TransactionRowView: View { .font(.headline) .foregroundColor(typeColor) - if let fee = transaction.displayFee(for: walletId), netAmount < 0 { + if let fee = transaction.displayFee(for: walletId), let amount = netAmount, amount < 0 { Text("Fee: \(formatFee(fee))") .font(.caption2) .foregroundColor(.secondary) diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift index a567917dedf..d2d429ac86e 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift @@ -145,6 +145,11 @@ struct StorageExplorerView: View { modelRow("Pending Inputs", icon: "hourglass", type: PersistentPendingInput.self) { PendingInputStorageListView(network: network) } + modelRow( + "Transaction Accounting", icon: "plusminus.circle", type: PersistentTransactionAccounting.self + ) { + TransactionAccountingStorageListView(network: network) + } modelRow("Asset Locks", icon: "lock.shield", type: PersistentAssetLock.self) { AssetLockStorageListView(network: network) } @@ -328,6 +333,9 @@ struct StorageExplorerView: View { filteredCount(PersistentPendingInput.self) { walletsOnNetwork.contains($0.walletId) } + filteredCount(PersistentTransactionAccounting.self) { + walletsOnNetwork.contains($0.walletId) + } filteredCount(PersistentShieldedNote.self) { walletsOnNetwork.contains($0.walletId) } diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift index c7a3724fd63..a3433e034fd 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift @@ -1757,6 +1757,53 @@ struct TxoStorageListView: View { } } +// MARK: - PersistentTransactionAccounting + +/// Every per-wallet accounting row `rs-platform-wallet` emitted, newest +/// first: one per `(walletId, txid)`, so a transaction shared by two local +/// wallets appears twice. +struct TransactionAccountingStorageListView: View { + let network: Network + @Query(sort: [SortDescriptor(\PersistentTransactionAccounting.lastUpdated, order: .reverse)]) + private var records: [PersistentTransactionAccounting] + + @Query private var allWallets: [PersistentWallet] + + private var scopedRecords: [PersistentTransactionAccounting] { + let ids = Set(allWallets.lazy + .filter { $0.networkRaw == network.rawValue } + .map(\.walletId)) + return records.filter { ids.contains($0.walletId) } + } + + var body: some View { + let visible = scopedRecords + List(visible) { record in + NavigationLink(destination: TransactionAccountingStorageDetailView(record: record)) { + VStack(alignment: .leading, spacing: 4) { + Text(record.txid.reversed().map { String(format: "%02x", $0) }.joined()) + .font(.system(.caption, design: .monospaced)) + .lineLimit(1).truncationMode(.middle) + HStack(spacing: 8) { + Text(PersistentTransaction.format(duffs: record.netAmount)) + .font(.caption2) + Spacer() + Text(record.walletId.prefix(4).map { String(format: "%02x", $0) }.joined()) + .font(.system(.caption2, design: .monospaced)) + .foregroundColor(.secondary) + } + } + } + } + .navigationTitle("Transaction Accounting (\(visible.count))") + .overlay { + if visible.isEmpty { + ContentUnavailableView("No Accounting Rows", systemImage: "plusminus.circle") + } + } + } +} + // MARK: - PersistentPendingInput /// Diagnostic list of every `PersistentPendingInput` row — one per diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift index 7fe6ed1cd28..d6b06c037a4 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift @@ -1590,6 +1590,10 @@ struct WalletStorageDetailView: View { FieldRow(label: "Birth Height", value: "\(record.birthHeight)") FieldRow(label: "Synced Height", value: "\(record.syncedHeight)") FieldRow(label: "Imported", value: record.isImported ? "Yes" : "No") + FieldRow( + label: "Accounting Backfilled", + value: record.transactionAccountingBackfilled ? "Yes" : "No" + ) } Section("Relationships") { FieldRow(label: "Accounts", value: "\(record.accounts.count)") @@ -2180,6 +2184,40 @@ struct TxoStorageDetailView: View { } } +// MARK: - PersistentTransactionAccounting + +struct TransactionAccountingStorageDetailView: View { + let record: PersistentTransactionAccounting + + var body: some View { + Form { + Section("Accounting") { + FieldRow(label: "Net Amount", value: PersistentTransaction.format(duffs: record.netAmount)) + FieldRow(label: "Direction", value: "\(record.direction)") + FieldRow(label: "Fee", value: record.fee.map { "\($0) duffs" } ?? "—") + } + Section("Keys") { + FieldRow(label: "Wallet ID", value: hexString(record.walletId)) + FieldRow(label: "TXID", value: record.txid.reversed().map { String(format: "%02x", $0) }.joined()) + } + Section("Relationships") { + if let transaction = record.transaction { + NavigationLink(destination: TransactionStorageDetailView(record: transaction)) { + FieldRow(label: "Transaction", value: transaction.txidHex) + } + } else { + FieldRow(label: "Transaction", value: "— (unlinked)") + } + } + Section("Timestamps") { + FieldRow(label: "Last Updated", value: dateString(record.lastUpdated)) + } + } + .navigationTitle("Transaction Accounting") + .navigationBarTitleDisplayMode(.inline) + } +} + // MARK: - PersistentPendingInput struct PendingInputStorageDetailView: View { diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift index 246381c1daf..c2955b0621b 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -31,20 +31,40 @@ final class TransactionAccountingTests: XCTestCase { } /// Hand the handler `rows` the way Rust's extension slot does: inside a - /// round, or on their own like the load-time backfill. + /// round, optionally followed by the backfill's completion marker. @discardableResult private func account( _ handler: PlatformWalletPersistenceHandler, walletId: Data, - _ rows: [TransactionAccountingFFI], inRound: Bool = true + _ rows: [TransactionAccountingFFI], markBackfilled: Bool = false, success: Bool = true ) -> Bool { - if inRound { handler.beginChangeset(walletId: walletId) } - let stored = rows.withUnsafeBufferPointer { + handler.beginChangeset(walletId: walletId) + var stored = rows.withUnsafeBufferPointer { handler.persistTransactionAccounting(walletId: walletId, rows: $0.baseAddress, count: UInt($0.count)) } - if inRound { XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) } + if markBackfilled { + stored = handler.persistCoreHistoryAccounted(walletId: walletId) && stored + } + _ = handler.endChangeset(walletId: walletId, success: success) return stored } + /// The bodies `loadCoreHistoryForAccounting` offers, returning the loan. + private func offered( + _ handler: PlatformWalletPersistenceHandler, walletId: Data + ) throws -> (backfilled: Bool, bodies: [Data]) { + let history = handler.loadCoreHistoryForAccounting(walletId: walletId) + XCTAssertFalse(history.errored) + guard let rows = history.rows else { + XCTAssertEqual(history.count, 0) + return (history.backfilled, []) + } + let bodies = (0.. Data in + Data(bytes: rows[index].tx_bytes!, count: Int(rows[index].tx_bytes_len)) + } + handler.loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer(rows)) + return (history.backfilled, bodies) + } + private func transaction(_ container: ModelContainer, _ txid: Data) throws -> PersistentTransaction { try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == txid }) } @@ -135,15 +155,31 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.outgoing) } - /// The load-time backfill arrives outside any round and commits itself. - func testShouldCommitAccountingDeliveredOutsideARound() throws { + /// The load-time backfill's rows and marker commit with its round. + func testShouldCommitTheBackfillsRowsAndMarkerTogether() throws { let (container, handler) = try makeStore() persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) XCTAssertTrue(account(handler, walletId: walletA, - [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], inRound: false)) + [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], markBackfilled: true)) XCTAssertEqual(try transaction(container, txid).netAmount(for: walletA), -60) + XCTAssertEqual(try offered(handler, walletId: walletA).backfilled, true) + } + + /// A failed backfill round leaves neither rows nor marker, so the next + /// load offers the history again. + func testShouldDropTheBackfillsRowsAndMarkerWhenItsRoundFails() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) + + account(handler, walletId: walletA, + [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], markBackfilled: true, success: false) + + XCTAssertNil(try transaction(container, txid).netAmount(for: walletA)) + let history = try offered(handler, walletId: walletA) + XCTAssertFalse(history.backfilled) + XCTAssertEqual(history.bodies, [Data([1])]) } func testShouldIgnoreAccountingForATransactionTheStoreDoesNotHold() throws { @@ -170,31 +206,33 @@ final class TransactionAccountingTests: XCTestCase { XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.internalTransfer) } - /// Without a row for the wallet, display falls back to the shared scalar. - func testShouldFallBackToTheSharedValuesWithoutAWalletRow() { + /// Without a row for the wallet there is no amount to show for it; the + /// shared scalar answers only unscoped views. + func testShouldShowNoAccountingWithoutAWalletRow() { let tx = PersistentTransaction( txid: txid, transactionData: Data(), direction: CoreDirectionCode.incoming, netAmount: 25 ) - XCTAssertEqual(tx.netAmount(for: walletA), 25) - XCTAssertEqual(tx.direction(for: walletA), CoreDirectionCode.incoming) + XCTAssertNil(tx.netAmount(for: walletA)) + XCTAssertNil(tx.direction(for: walletA)) + XCTAssertEqual(tx.formattedAmount(for: walletA), PersistentTransaction.noAccounting) XCTAssertEqual(tx.displayNetAmount(for: nil), 25) + XCTAssertEqual(tx.displayDirectionCode(for: nil), CoreDirectionCode.incoming) } - func testShouldOfferNoHistoryWhenEveryTransactionHasTheWalletsRow() throws { + func testShouldOfferNothingWhenEveryTransactionHasTheWalletsRow() throws { let (_, handler) = try makeStore() persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) - let history = handler.loadCoreHistoryForAccounting(walletId: walletA) + let history = try offered(handler, walletId: walletA) - XCTAssertFalse(history.errored) - XCTAssertNil(history.rows) - XCTAssertEqual(history.count, 0) + XCTAssertFalse(history.backfilled, "Rust still has to record completion") + XCTAssertEqual(history.bodies, []) } - /// Rust needs the whole history to resolve a flagged spend's inputs, so - /// every transaction crosses, flagged by whether it lacks a row. - func testShouldOfferTheWholeHistoryFlaggingTransactionsWithoutARow() throws { + /// Only this wallet's transactions lacking its row cross; Rust fetches + /// any funding transaction it needs on its own. Every loan is returned. + func testShouldOfferOnlyTheWalletsUnaccountedTransactions() throws { let (_, handler) = try makeStore() let other = Data(repeating: 4, count: 32) persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) @@ -202,18 +240,66 @@ final class TransactionAccountingTests: XCTestCase { persist(handler, walletId: walletB, txid: Data(repeating: 5, count: 32), bytes: Data([5]), net: 1) account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) - let history = handler.loadCoreHistoryForAccounting(walletId: walletA) + let history = try offered(handler, walletId: walletA) - XCTAssertFalse(history.errored) - let rows = try XCTUnwrap(history.rows) - let offered = (0.. (Data, Bool) in - let entry = rows[index] - return (Data(bytes: entry.tx_bytes!, count: Int(entry.tx_bytes_len)), entry.needs_accounting) + XCTAssertFalse(history.backfilled) + XCTAssertEqual(history.bodies, [Data([7, 8, 9])]) + XCTAssertEqual(handler.outstandingCoreHistoryLoanCount, 0) + } + + /// Once the marker is stored, later loads skip the history entirely. + func testShouldReportABackfilledWalletWithoutOfferingHistory() throws { + let (_, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + account(handler, walletId: walletA, [], markBackfilled: true) + + let history = try offered(handler, walletId: walletA) + + XCTAssertTrue(history.backfilled) + XCTAssertEqual(history.bodies, []) + } + + /// The backfill end to end through the C shims Rust calls: read the + /// unaccounted body, return the loan, write the row and the marker in + /// one round, then report the wallet backfilled. + func testShouldDriveTheBackfillThroughTheExtensionShims() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + let callbacks = handler.makePersistenceCallbacksExtension() + let load = try XCTUnwrap(callbacks.on_load_core_history_for_accounting_fn) + let free = try XCTUnwrap(callbacks.on_load_core_history_for_accounting_free_fn) + let persistRows = try XCTUnwrap(callbacks.on_persist_transaction_accounting_fn) + let accounted = try XCTUnwrap(callbacks.on_persist_core_history_accounted_fn) + let context = Unmanaged.passUnretained(handler).toOpaque() + let rows = [row(txid, net: -60, direction: CoreDirectionCode.outgoing, fee: 5)] + + try walletA.withUnsafeBytes { wallet in + let walletBytes = try XCTUnwrap(wallet.bindMemory(to: UInt8.self).baseAddress) + var backfilled = true + var entries: UnsafePointer? + var count: UInt = 0 + XCTAssertEqual(load(context, walletBytes, &backfilled, &entries, &count), 0) + XCTAssertFalse(backfilled) + XCTAssertEqual(count, 1) + let entry = try XCTUnwrap(entries)[0] + XCTAssertEqual(Data(bytes: try XCTUnwrap(entry.tx_bytes), count: Int(entry.tx_bytes_len)), Data([1, 2])) + free(context, entries, count) + XCTAssertEqual(handler.outstandingCoreHistoryLoanCount, 0) + + handler.beginChangeset(walletId: walletA) + XCTAssertEqual(rows.withUnsafeBufferPointer { + persistRows(context, walletBytes, $0.baseAddress, UInt($0.count)) + }, 0) + XCTAssertEqual(accounted(context, walletBytes), 0) + XCTAssertTrue(handler.endChangeset(walletId: walletA, success: true)) + + XCTAssertEqual(load(context, walletBytes, &backfilled, &entries, &count), 0) + XCTAssertTrue(backfilled) + XCTAssertEqual(count, 0) + XCTAssertNil(entries) + XCTAssertEqual(load(nil, walletBytes, &backfilled, &entries, &count), 1) } - XCTAssertEqual(offered.count, 2, "only wallet A's transactions") - XCTAssertEqual(offered.first { $0.0 == Data([1, 2]) }?.1, false) - XCTAssertEqual(offered.first { $0.0 == Data([7, 8, 9]) }?.1, true) - handler.loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer(rows)) + XCTAssertEqual(try transaction(container, txid).fee(for: walletA), 5) } func testShouldDeleteOnlyTheDepartingWalletsRows() throws { From 01ceb6cabe415865a3170cf2cef449419faacb50 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:29:35 +0000 Subject: [PATCH 13/21] fix(platform-wallet): leave a wallet spend with an unresolved coin unaccounted until it resolves The history backfill read an input whose funding transaction it could not get as foreign, so a payment with change was written as an `Incoming` receipt of the change and the completion marker made that permanent. - An unresolved input whose scriptSig redeems a P2PKH script the wallet owns (the wallet signed it) now holds back that transaction's row; genuine receipts, whose inputs are always foreign, still get theirs. `history_accounting` returns `HistoryAccounting { rows, unresolved }`. - `store_history_accounting` takes `complete`: a round that left any transaction unaccounted writes its rows but not the marker, so the next load retries the rest. - Funding bodies are read through the new strict `get_core_transaction`: a stored transaction the host cannot hand over fails the round instead of reading as missing. - The backfill runs for every wallet a load saw, including ones the idempotent reload skips, so an unfinished backfill is retried at the next load, not only at the next launch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- .../rs-platform-wallet-ffi/src/persistence.rs | 36 ++- .../rs-platform-wallet/src/changeset/mod.rs | 2 +- .../src/changeset/traits.rs | 45 +++- .../src/changeset/wallet_accounting.rs | 195 ++++++++++++-- .../rs-platform-wallet/src/manager/load.rs | 247 +++++++++++++++--- 5 files changed, 455 insertions(+), 70 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index e0c2f585202..0309bf18b7a 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -360,7 +360,11 @@ pub type FreeCoreHistoryForAccountingFn = unsafe extern "C" fn( /// Record that this wallet's history backfill is complete, so the next /// [`LoadCoreHistoryForAccountingFn`] reports it backfilled. Fired in the -/// backfill's round after its rows, and committed with them. +/// backfill's round after its rows, and committed with them, only when every +/// transaction got its row. A round that had to leave a transaction +/// unaccounted (a wallet-signed input whose funding transaction is not +/// available yet) commits its rows without it, so the next load reads the +/// rest again. pub type PersistCoreHistoryAccountedFn = unsafe extern "C" fn(context: *mut c_void, wallet_id: *const u8) -> i32; @@ -1977,13 +1981,14 @@ impl PlatformWalletPersistence for FFIPersister { Ok((!backfilled).then_some(out)) } - /// The backfill's rows and its completion marker, as one round under - /// `round_lock`: they commit together or not at all, and never land in - /// another wallet's open round. + /// The backfill's rows and, when `complete`, its completion marker, as + /// one round under `round_lock`: they commit together or not at all, and + /// never land in another wallet's open round. fn store_history_accounting( &self, wallet_id: WalletId, rows: &[WalletAccounting], + complete: bool, ) -> Result<(), PersistenceError> { let (Some(persist_rows), Some(accounted)) = ( self.persist_transaction_accounting_callback, @@ -2022,7 +2027,7 @@ impl PlatformWalletPersistence for FFIPersister { outcome.record(result); } } - if outcome.is_success() { + if complete && outcome.is_success() { let result = unsafe { accounted(self.callbacks.context, wallet_id.as_ptr()) }; if result != 0 { outcome.record(result); @@ -10296,7 +10301,7 @@ mod tests { // Rows and marker commit in one round. persister - .store_history_accounting([1; 32], &[row]) + .store_history_accounting([1; 32], &[row], true) .expect("backfill round must succeed"); assert_eq!( *host.events.lock().unwrap(), @@ -10312,17 +10317,32 @@ mod tests { // Nothing to write still records completion. let host = make_host(false, false); persister_for(&host, extensions) - .store_history_accounting([1; 32], &[]) + .store_history_accounting([1; 32], &[], true) .expect("empty backfill round must succeed"); assert_eq!( *host.events.lock().unwrap(), vec!["begin", "accounted", "end success=true"] ); + // An incomplete backfill writes its rows but no marker, so it stays + // open for the next load. + let host = make_host(false, false); + persister_for(&host, extensions) + .store_history_accounting([1; 32], &[row], false) + .expect("incomplete backfill round must succeed"); + assert_eq!( + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "end success=true" + ] + ); + // A failing row write rolls back, with no marker. let host = make_host(false, true); assert!(persister_for(&host, extensions) - .store_history_accounting([1; 32], &[row]) + .store_history_accounting([1; 32], &[row], true) .is_err()); assert_eq!( *host.events.lock().unwrap(), diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 6fcebc19ca3..1f7a38a3c1a 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -71,5 +71,5 @@ pub use traits::{ }; pub use wallet_accounting::{ funding_txids, history_accounting, is_owned, record_accounting, wallet_accounting, - wallet_direction, WalletAccounting, + wallet_direction, HistoryAccounting, WalletAccounting, }; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index a7867d89510..24aaffe75c0 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -495,6 +495,28 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(None) } + /// The body of a stored Core transaction, for the accounting backfill. + /// + /// Stricter than [`Self::get_core_tx_record`], whose policy suits the + /// asset-lock and DashPay callers that retry a miss anyway: here + /// `Ok(None)` must mean only "this backend does not store `txid`". A + /// stored transaction is returned in any context, InstantSend included, + /// and a stored one that cannot be read (a failing backend, an + /// undecodable body) is an `Err`. The backfill then fails its round and + /// runs again at the next load, rather than reading a wallet-owned input + /// as someone else's. + /// + /// The default delegates to [`Self::get_core_tx_record`]. + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + Ok(self + .get_core_tx_record(wallet_id, txid)? + .map(|record| record.transaction)) + } + /// Enumerate the persisted Core transaction ids that belong to /// `wallet_id`, each tagged with whether the transaction spends an /// input this wallet funded. @@ -533,13 +555,18 @@ pub trait PlatformWalletPersistence: Send + Sync { /// [`WalletAccounting`] yet, or `None` once this wallet's history /// backfill has completed (see [`Self::store_history_accounting`]). /// - /// Read once per wallet at load. The manager computes the missing rows - /// with [`history_accounting`](crate::changeset::history_accounting), + /// Read at every load of the wallet until the backfill completes. The + /// manager computes the missing rows with + /// [`history_accounting`](crate::changeset::history_accounting), /// fetching the funding transactions it needs through - /// [`Self::get_core_tx_record`] itself, and hands them back through + /// [`Self::get_core_transaction`] itself, and hands them back through /// [`Self::store_history_accounting`]. Any subset is safe: a transaction /// left out only stays without a row. /// + /// A transaction without a row is "not yet accounted", never "accounted + /// as nothing": the host displays it without an amount or direction + /// until a backfill or a live update writes the row. + /// /// The default (`None`) suits backends that never stored rows without /// accounting, such as the SQLite store, whose own migration repairs /// history. @@ -551,14 +578,18 @@ pub trait PlatformWalletPersistence: Send + Sync { } /// Persist the backfilled rows (upsert keyed by `(txid, wallet_id)`) - /// and record that this wallet's history backfill is complete, as one - /// atomic write; `rows` may be empty. After it, - /// [`Self::load_core_history_for_accounting`] returns `None` for the - /// wallet. The default drops both. + /// and, when `complete`, record that this wallet's history backfill is + /// done, as one atomic write; `rows` may be empty. After a complete + /// write, [`Self::load_core_history_for_accounting`] returns `None` for + /// the wallet. An incomplete one (some transaction could not be + /// accounted yet) leaves the backfill open, so the next load reads the + /// transactions still without a row and tries again. The default drops + /// both. fn store_history_accounting( &self, _wallet_id: WalletId, _rows: &[WalletAccounting], + _complete: bool, ) -> Result<(), PersistenceError> { Ok(()) } diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 7f54b88215b..e1b013335ce 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -21,7 +21,9 @@ use std::collections::{BTreeMap, BTreeSet}; -use dashcore::{Address, OutPoint, ScriptBuf, Transaction, Txid}; +use dashcore::blockdata::script::Instruction; +use dashcore::hashes::Hash; +use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxIn, Txid}; use key_wallet::managed_account::managed_account_ref::ManagedAccountRef; use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::managed_account::transaction_record::{ @@ -144,15 +146,28 @@ pub fn funding_txids(wallet: &ManagedWalletInfo, unaccounted: &[Transaction]) -> /// - type, net, direction and fee follow [`record_accounting`]. /// /// A transaction that touches no owned script yields no row, like a -/// detail-less record. An owned input whose funding transaction the host no -/// longer stores, and that is no longer a UTXO, cannot be resolved and reads -/// as foreign. Any subset of a host's unaccounted transactions is therefore -/// safe to pass: omitting one only leaves it without a row. +/// detail-less record. +/// +/// An input whose prevout none of the above resolves is either someone +/// else's coin (every input of a genuine receipt) or the wallet's own coin +/// whose funding transaction the host could not supply. The two are told +/// apart by the input's redeem: when its scriptSig ends with a public key +/// whose P2PKH script the wallet owns, the wallet signed it. Such a +/// transaction gets no row and is listed in +/// [`HistoryAccounting::unresolved`] instead: reading the input as foreign +/// would turn a payment with change into an `Incoming` receipt of the +/// change. An input redeemed any other way (a script the wallet does not +/// spend from) reads as foreign. +/// +/// No row is never a wrong row: a transaction left without one is not yet +/// accounted, and the host shows it as such until a later backfill (or the +/// live projection) writes it. Any subset of a host's unaccounted +/// transactions is therefore safe to pass. pub fn history_accounting( wallet: &ManagedWalletInfo, unaccounted: &[Transaction], funding: &[Transaction], -) -> Vec { +) -> HistoryAccounting { let mut owned_scripts: BTreeSet = BTreeSet::new(); let mut prevouts: BTreeMap = BTreeMap::new(); for funds in wallet_funds(wallet) { @@ -176,13 +191,24 @@ pub fn history_accounting( } } - let mut rows = Vec::new(); + let mut accounted = HistoryAccounting::default(); for transaction in unaccounted { - let spent: Vec = transaction - .input - .iter() - .filter_map(|input| prevouts.get(&input.previous_output).copied()) - .collect(); + let mut spent: Vec = Vec::new(); + let mut owned_but_unresolved = false; + for input in &transaction.input { + match prevouts.get(&input.previous_output) { + Some(value) => spent.push(*value), + None if !input.previous_output.is_null() => { + owned_but_unresolved |= p2pkh_redeemed_script(input) + .is_some_and(|script| owned_scripts.contains(&script)); + } + None => {} + } + } + if owned_but_unresolved { + accounted.unresolved.push(transaction.txid()); + continue; + } let owned: Vec<(usize, u64)> = transaction .output .iter() @@ -210,14 +236,44 @@ pub fn history_accounting( &owned, &unspendable, ); - rows.push(WalletAccounting { + accounted.rows.push(WalletAccounting { txid: transaction.txid(), net_amount: saturating_net(accounting.net), direction: accounting.direction, fee: accounting.fee, }); } - rows + accounted +} + +/// What [`history_accounting`] made of a host's unaccounted transactions. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct HistoryAccounting { + /// One row per transaction it could account for. + pub rows: Vec, + /// Transactions left without a row because an input spends a coin the + /// wallet signed for but could not resolve. A later backfill retries + /// them. + pub unresolved: Vec, +} + +impl HistoryAccounting { + /// Whether every transaction that touches the wallet got its row, so the + /// backfill may be recorded as complete. + pub fn is_complete(&self) -> bool { + self.unresolved.is_empty() + } +} + +/// The P2PKH script `input` redeems, when its scriptSig ends with a public +/// key push (` `, how a P2PKH output is spent). `None` for any +/// other redeem. +fn p2pkh_redeemed_script(input: &TxIn) -> Option { + let Instruction::PushBytes(pubkey) = input.script_sig.instructions().last()?.ok()? else { + return None; + }; + let pubkey = pubkey.as_bytes(); + matches!(pubkey.len(), 33 | 65).then(|| ScriptBuf::new_p2pkh(&PubkeyHash::hash(pubkey))) } /// The wallet's funds accounts, without contacts' watch-only chains. @@ -353,6 +409,7 @@ mod tests { use dashcore::transaction::special_transaction::TransactionPayload; use dashcore::{Network, TxOut}; use key_wallet::account::{Account, AccountType, StandardAccountType}; + use key_wallet::managed_account::address_pool::PublicKeyType; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::Wallet; @@ -408,7 +465,7 @@ mod tests { ) -> Vec { let (info, live, funding, spend) = live(spend).await; assert_eq!(live.len(), 2, "the live projection accounts for both"); - let backfilled = history_accounting(&info, &[funding, spend], &[]); + let backfilled = history_accounting(&info, &[funding, spend], &[]).rows; assert_eq!(backfilled, live); backfilled } @@ -500,7 +557,7 @@ mod tests { async fn should_resolve_inputs_from_funding_transactions_and_answer_only_unaccounted() { let (info, live, funding, spend) = live(payment_with_change).await; - let backfilled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]); + let backfilled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]).rows; assert_eq!(backfilled, vec![live[1]]); } @@ -517,7 +574,7 @@ mod tests { vec![pay(FUNDING - FEE, &foreign_address(9))], ); - let rows = history_accounting(&info, std::slice::from_ref(&spend), &[]); + let rows = history_accounting(&info, std::slice::from_ref(&spend), &[]).rows; assert_eq!( rows, @@ -563,7 +620,10 @@ mod tests { let (info, _, _, _) = live(payment_with_change).await; let unrelated = spend_of(&[outside(3)], vec![pay(5_000, &foreign_address(4))]); - assert!(history_accounting(&info, &[unrelated], &[]).is_empty()); + assert_eq!( + history_accounting(&info, &[unrelated], &[]), + HistoryAccounting::default() + ); } /// An output on a contact's watch-only chain is the contact's money, @@ -601,13 +661,110 @@ mod tests { vec![pay(FUNDING - FEE, &contact_address)], ); - let rows = history_accounting(&info, std::slice::from_ref(&to_contact), &[funding]); + let rows = history_accounting(&info, std::slice::from_ref(&to_contact), &[funding]).rows; assert_eq!(rows.len(), 1); assert_eq!(rows[0].net_amount, -(FUNDING as i64)); assert_eq!(rows[0].direction, TransactionDirection::Outgoing); } + /// `tx` with input `index` redeemed the P2PKH way, ` `. + fn signed_by(mut tx: Transaction, index: usize, pubkey: &[u8]) -> Transaction { + let signature = [0x30; 71]; + tx.input[index].script_sig = dashcore::script::Builder::new() + .push_slice(<&dashcore::script::PushBytes>::try_from(&signature[..]).unwrap()) + .push_slice(<&dashcore::script::PushBytes>::try_from(pubkey).unwrap()) + .into_script(); + tx + } + + /// The compressed public key behind one of the wallet's own addresses. + fn owned_pubkey(info: &ManagedWalletInfo, address: &Address) -> Vec { + let account = &info.accounts.standard_bip44_accounts[&0]; + match ManagedAccountRef::Funds(account) + .get_address_info(address) + .and_then(|info| info.public_key) + { + Some(PublicKeyType::ECDSA(bytes)) => bytes, + other => panic!("the wallet derived {address} from an ECDSA key, got {other:?}"), + } + } + + /// A spend signed by the wallet's own key whose coin the host no longer + /// holds (funding transaction gone, no longer a UTXO) cannot be accounted: + /// reading the input as foreign would turn a payment with change into + /// an `Incoming` receipt of the change. It gets no row and is reported + /// unresolved, and accounts normally once the funding body is passed. + #[test] + fn should_withhold_the_row_of_a_spend_whose_owned_input_cannot_be_resolved() { + let (wallet, mut info, receive) = wallet_with_receive_address(); + let xpub = wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let change = info + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .next_change_address(Some(&xpub), true) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let spend = signed_by( + payment_with_change(OutPoint::new(funding.txid(), 0), &change), + 0, + &owned_pubkey(&info, &receive), + ); + + let missing = history_accounting(&info, std::slice::from_ref(&spend), &[]); + assert_eq!( + missing, + HistoryAccounting { + rows: Vec::new(), + unresolved: vec![spend.txid()], + } + ); + assert!(!missing.is_complete()); + + let filled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]); + assert_eq!( + filled.rows, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: CHANGE as i64 - FUNDING as i64, + direction: TransactionDirection::Outgoing, + fee: Some(FEE), + }] + ); + assert!(filled.is_complete()); + } + + /// A genuine receipt's inputs are someone else's and never resolve; that + /// must not hold back its row. + #[test] + fn should_still_account_a_receipt_whose_foreign_inputs_cannot_be_resolved() { + let (_, info, receive) = wallet_with_receive_address(); + let mut foreign_key = vec![0x02]; + foreign_key.extend([7u8; 32]); + let receipt = signed_by( + spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]), + 0, + &foreign_key, + ); + + let accounted = history_accounting(&info, std::slice::from_ref(&receipt), &[]); + + assert_eq!( + accounted, + HistoryAccounting { + rows: vec![WalletAccounting { + txid: receipt.txid(), + net_amount: FUNDING as i64, + direction: TransactionDirection::Incoming, + fee: None, + }], + unresolved: Vec::new(), + } + ); + } + /// A record rebuilt at load from bytes alone (an asset lock's funding /// transaction) is re-emitted on chain-lock promotion with a placeholder /// net of 0: it must reach the host as "no accounting", not as 0. diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 96f6d277cdf..2a07ec6f12f 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -128,6 +128,10 @@ impl PlatformWalletManager

{ let mut inserted_in_wallets: Vec<(WalletId, Arc)> = Vec::new(); let mut load_error: Option = None; + // Wallets this load found already registered and skipped. Their + // accounting backfill still runs below: one that did not complete at + // an earlier load is retried by this one. + let mut already_registered: Vec = Vec::new(); // Each wallet's start state, and whether it was re-read under the // durable-cursor lock that `held` still holds (see the publication @@ -387,6 +391,7 @@ impl PlatformWalletManager

{ .get_wallet(&wallet_id) .is_some() { + already_registered.push(wallet_id); continue 'load; } if !reread && durable_cursors.contains_key(&wallet_id) { @@ -660,9 +665,16 @@ impl PlatformWalletManager

{ } // Past the rollback point. Accounting is display-only, so a failure - // here is logged and never fails the load. - for (wallet_id, _) in &inserted_in_wallets { - self.backfill_history_accounting(*wallet_id).await; + // here is logged and never fails the load. Every wallet this load saw + // is offered the backfill, so an unfinished one is retried at the + // next load; a finished one costs the host one marker read. + let seen: std::collections::BTreeSet = inserted_in_wallets + .iter() + .map(|(wallet_id, _)| *wallet_id) + .chain(already_registered) + .collect(); + for wallet_id in seen { + self.backfill_history_accounting(wallet_id).await; } // Past the rollback point: every registration here is one this load @@ -749,7 +761,9 @@ impl PlatformWalletManager

{ /// persister holds without one (see /// [`PlatformWalletPersistence::load_core_history_for_accounting`]). /// Nothing is written unless every step succeeds, so a failed backfill - /// simply runs again at the next load. + /// simply runs again at the next load. A transaction whose wallet-signed + /// input cannot be resolved yet gets no row and keeps the backfill open + /// (no completion marker), so the next load retries it. async fn backfill_history_accounting(&self, wallet_id: WalletId) { let persister = Arc::clone(&self.persister); let unaccounted = @@ -775,12 +789,14 @@ impl PlatformWalletManager

{ funding_txids(&info.core_wallet, &unaccounted) }; // Fetched without the manager lock held: each lookup calls the host. + // A stored funding transaction the host cannot hand over fails the + // round: read as missing, a wallet input could pass for a foreign one. let persister = Arc::clone(&self.persister); let funding = match run_blocking_load(move || { let mut funding = Vec::new(); for txid in &needed { - if let Some(record) = persister.get_core_tx_record(wallet_id, txid)? { - funding.push(record.transaction); + if let Some(transaction) = persister.get_core_transaction(wallet_id, txid)? { + funding.push(transaction); } } Ok(funding) @@ -797,17 +813,29 @@ impl PlatformWalletManager

{ return; } }; - let rows = { + let accounted = { let wm = self.wallet_manager.read().await; let Some(info) = wm.get_wallet_info(&wallet_id) else { return; }; history_accounting(&info.core_wallet, &unaccounted, &funding) }; + let complete = accounted.is_complete(); + if !complete { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + unresolved = accounted.unresolved.len(), + "load: some stored transactions spend wallet coins whose funding \ + transaction is unavailable; leaving them unaccounted for the next load" + ); + } + let rows = accounted.rows; let persister = Arc::clone(&self.persister); let count = rows.len(); - if let Err(e) = - run_blocking_load(move || persister.store_history_accounting(wallet_id, &rows)).await + if let Err(e) = run_blocking_load(move || { + persister.store_history_accounting(wallet_id, &rows, complete) + }) + .await { tracing::warn!( wallet_id = %hex::encode(wallet_id), @@ -1405,15 +1433,20 @@ mod idempotent_load_tests { /// Hands back the stored transactions lacking accounting (`None` once /// backfilled), serves funding transactions by txid, and records what - /// the manager persists. + /// the manager persists. Like a host, a stored row takes its + /// transaction out of the unaccounted set and a complete backfill sets + /// the marker. struct HistoryPersister { wallet: Wallet, managed: ManagedWalletInfo, - unaccounted: Option>, - stored_transactions: BTreeMap, + unaccounted: Mutex>>, + stored_transactions: Mutex>, failing_lookup: bool, + /// Every stored transaction is found but cannot be handed over: the + /// lenient record read reports a miss, the strict body read fails. + unusable_funding: bool, lookups: Mutex>, - stored: Mutex)>>, + stored: Mutex, bool)>>, } impl PlatformWalletPersistence for HistoryPersister { @@ -1442,40 +1475,72 @@ mod idempotent_load_tests { _wallet_id: WalletId, txid: &Txid, ) -> Result, PersistenceError> { - self.lookups.lock().unwrap().push(*txid); if self.failing_lookup { return Err(PersistenceError::backend("store unreadable")); } - Ok(self.stored_transactions.get(txid).map(|tx| { - TransactionRecord::new( - tx.clone(), - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - TransactionContext::Mempool, - TransactionType::Standard, - TransactionDirection::Incoming, - Vec::new(), - Vec::new(), - 0, - ) - })) + if self.unusable_funding { + return Ok(None); + } + Ok(self + .stored_transactions + .lock() + .unwrap() + .get(txid) + .map(|tx| { + TransactionRecord::new( + tx.clone(), + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + TransactionContext::Mempool, + TransactionType::Standard, + TransactionDirection::Incoming, + Vec::new(), + Vec::new(), + 0, + ) + })) + } + + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + self.lookups.lock().unwrap().push(*txid); + if self.unusable_funding && self.stored_transactions.lock().unwrap().contains_key(txid) + { + return Err(PersistenceError::backend("stored but unreadable")); + } + Ok(self + .get_core_tx_record(wallet_id, txid)? + .map(|record| record.transaction)) } fn load_core_history_for_accounting( &self, _wallet_id: WalletId, ) -> Result>, PersistenceError> { - Ok(self.unaccounted.clone()) + Ok(self.unaccounted.lock().unwrap().clone()) } fn store_history_accounting( &self, wallet_id: WalletId, rows: &[WalletAccounting], + complete: bool, ) -> Result<(), PersistenceError> { - self.stored.lock().unwrap().push((wallet_id, rows.to_vec())); + self.stored + .lock() + .unwrap() + .push((wallet_id, rows.to_vec(), complete)); + let mut unaccounted = self.unaccounted.lock().unwrap(); + if complete { + *unaccounted = None; + } else if let Some(unaccounted) = unaccounted.as_mut() { + unaccounted.retain(|tx| rows.iter().all(|row| row.txid != tx.txid())); + } Ok(()) } } @@ -1511,9 +1576,12 @@ mod idempotent_load_tests { HistoryPersister { wallet, managed, - unaccounted, - stored_transactions: stored.iter().map(|tx| (tx.txid(), (*tx).clone())).collect(), + unaccounted: Mutex::new(unaccounted), + stored_transactions: Mutex::new( + stored.iter().map(|tx| (tx.txid(), (*tx).clone())).collect(), + ), failing_lookup: false, + unusable_funding: false, lookups: Mutex::new(Vec::new()), stored: Mutex::new(Vec::new()), } @@ -1559,7 +1627,8 @@ mod idempotent_load_tests { direction: TransactionDirection::Outgoing, fee: Some(26_000), }, - ] + ], + true )] ); assert!( @@ -1585,6 +1654,7 @@ mod idempotent_load_tests { assert_eq!(*persister.lookups.lock().unwrap(), vec![funding.txid()]); let stored = persister.stored.lock().unwrap().clone(); assert_eq!(stored.len(), 1); + assert!(stored[0].2, "every transaction was accounted"); assert_eq!( stored[0].1, vec![WalletAccounting { @@ -1610,6 +1680,110 @@ mod idempotent_load_tests { assert!(persister.stored.lock().unwrap().is_empty()); } + /// A funding transaction the host stores but cannot hand over (an + /// InstantSend-only row the lenient record read drops, a failing read) + /// fails the round: nothing is written, not even the rows that did + /// resolve, so the next load runs the whole backfill again. + #[tokio::test] + async fn load_writes_no_backfill_when_a_stored_funding_transaction_is_unusable() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let mut persister = history_persister( + wallet, + without_utxos(managed), + Some(vec![spend]), + &[&funding], + ); + persister.unusable_funding = true; + let persister = backfill(persister).await; + + assert_eq!(*persister.lookups.lock().unwrap(), vec![funding.txid()]); + assert!(persister.stored.lock().unwrap().is_empty()); + } + + /// `spend` with its input redeemed by the key behind `funding`'s output + /// 0, the wallet's own receive address: what the wallet itself signed. + fn signed_by_the_wallet( + managed: &ManagedWalletInfo, + funding: &Transaction, + mut spend: Transaction, + ) -> Transaction { + use key_wallet::managed_account::address_pool::PublicKeyType; + use key_wallet::managed_account::ManagedAccountRef; + + let address = + dashcore::Address::from_script(&funding.output[0].script_pubkey, managed.network) + .expect("the funding pays a P2PKH address"); + let Some(PublicKeyType::ECDSA(pubkey)) = + ManagedAccountRef::Funds(&managed.accounts.standard_bip44_accounts[&0]) + .get_address_info(&address) + .and_then(|info| info.public_key) + else { + panic!("the wallet derived the funded address from an ECDSA key"); + }; + spend.input[0].script_sig = dashcore::script::Builder::new() + .push_slice(<&dashcore::script::PushBytes>::try_from(&[0x30; 71][..]).unwrap()) + .push_slice(<&dashcore::script::PushBytes>::try_from(&pubkey[..]).unwrap()) + .into_script(); + spend + } + + /// A spend the wallet signed whose funding transaction the host does not + /// have yet is left without a row and keeps the backfill open: no + /// completion marker. A later load of the same, already registered, + /// wallet retries it and writes the row once the funding transaction is + /// there, and only then completes. A load after that reads nothing. + #[tokio::test] + async fn load_leaves_an_unresolvable_wallet_spend_open_then_fills_it_at_a_later_load() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let managed = without_utxos(managed); + let spend = signed_by_the_wallet(&managed, &funding, spend); + let persister = Arc::new(history_persister( + wallet, + managed, + Some(vec![spend.clone()]), + &[], + )); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let event_handler: Arc = Arc::new(NoopTestEventHandler); + let manager = PlatformWalletManager::new(sdk, Arc::clone(&persister), event_handler); + + manager.load_from_persistor().await.expect("first load"); + assert_eq!( + *persister.stored.lock().unwrap(), + vec![(wallet_id, Vec::new(), false)], + "skipped: no row, and the backfill stays open" + ); + + persister + .stored_transactions + .lock() + .unwrap() + .insert(funding.txid(), funding.clone()); + manager.load_from_persistor().await.expect("second load"); + assert_eq!( + persister.stored.lock().unwrap()[1..], + [( + wallet_id, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), + }], + true + )], + "filled by the retry on the already registered wallet" + ); + + manager.load_from_persistor().await.expect("third load"); + assert_eq!( + persister.stored.lock().unwrap().len(), + 2, + "a completed backfill is not run again" + ); + } + /// Nothing unaccounted still completes the backfill, so the host stops /// being asked; an already backfilled wallet is left alone. #[tokio::test] @@ -1623,7 +1797,10 @@ mod idempotent_load_tests { &[], )) .await; - assert_eq!(*empty.stored.lock().unwrap(), vec![(wallet_id, Vec::new())]); + assert_eq!( + *empty.stored.lock().unwrap(), + vec![(wallet_id, Vec::new(), true)] + ); let finished = backfill(history_persister(wallet, managed, None, &[])).await; assert!(finished.stored.lock().unwrap().is_empty()); From 4b0879a66e47eaf9d71686f410050d5411e9f457 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:36:22 +0000 Subject: [PATCH 14/21] fix(platform-wallet-ffi): read accounting funding bodies strictly and refuse an unbracketed backfill - `get_core_transaction` shares one raw read with `get_core_tx_record` but keeps every hit, InstantSend included, and reports a non-zero return, a hit without bytes or an undecodable body as an error, so the backfill fails its round instead of reading a wallet input as foreign. `get_core_tx_record` keeps its miss policy for the asset-lock and DashPay callers. - A history answer with a null array and a non-zero count is an error (the loan is still freed), not an empty history that completes the backfill. - The backfill needs the changeset begin/end pair like the other atomic capabilities: without it the host is not read and a store writes nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- .../rs-platform-wallet-ffi/src/persistence.rs | 529 +++++++++++++----- 1 file changed, 381 insertions(+), 148 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 0309bf18b7a..0828d7eef04 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -340,9 +340,10 @@ pub type PersistTransactionAccountingFn = unsafe extern "C" fn( /// stored for this wallet. Otherwise returns the wallet's stored /// transactions that have no accounting row for it; any subset is safe, /// since Rust fetches the funding transactions it needs itself -/// (`on_get_core_tx_record_fn`) and a transaction left out only stays -/// without a row. The host keeps the array valid until -/// [`FreeCoreHistoryForAccountingFn`]. +/// (`on_get_core_tx_record_fn`, where a hit in any context is used and a +/// non-zero return fails the backfill) and a transaction left out only +/// stays without a row. A null array with a non-zero count is an error. The +/// host keeps the array valid until [`FreeCoreHistoryForAccountingFn`]. pub type LoadCoreHistoryForAccountingFn = unsafe extern "C" fn( context: *mut c_void, wallet_id: *const u8, @@ -565,7 +566,8 @@ pub struct PersistenceCallbacksExtension { ), >, /// Backfill completion marker (see [`PersistCoreHistoryAccountedFn`]). - /// The backfill runs only when all four accounting slots are present. + /// The backfill runs only when all four accounting slots and the + /// changeset begin/end pair are present. pub on_persist_core_history_accounted_fn: Option i32>, } @@ -1845,6 +1847,120 @@ impl FFIPersister { } } +/// What `on_get_core_tx_record_fn` answered for one txid. +enum CoreTxRead { + /// The callback is unset, or the host does not store the txid. + Missing, + /// The callback returned this non-zero code. + Failed(i32), + /// The host reported a hit but handed over no decodable body. + Unreadable(&'static str), + /// A hit: the host's raw context fields and the decoded body. + Found { + context_kind: u8, + block_height: u32, + block_hash: [u8; 32], + block_timestamp: u32, + transaction: dashcore::Transaction, + }, +} + +impl FFIPersister { + /// Call `on_get_core_tx_record_fn` for `txid` and decode its answer, + /// handing the tx-bytes buffer back to the host on every path. Each + /// caller applies its own policy to the outcome. + fn read_core_tx(&self, wallet_id: WalletId, txid: &dashcore::Txid) -> CoreTxRead { + use dashcore::consensus::Decodable; + use dashcore::hashes::Hash; + + let Some(get_cb) = self.callbacks.on_get_core_tx_record_fn else { + return CoreTxRead::Missing; + }; + + let txid_bytes: [u8; 32] = *txid.as_byte_array(); + let mut context_kind: u8 = 0; + let mut block_height: u32 = 0; + let mut block_hash: [u8; 32] = [0u8; 32]; + let mut block_timestamp: u32 = 0; + let mut tx_bytes_ptr: *const u8 = std::ptr::null(); + let mut tx_bytes_len: usize = 0; + let mut found: bool = false; + + // SAFETY: All output pointers reference Rust-owned stack + // locals that outlive the callback invocation. `wallet_id` + // and `txid` are fixed-size byte arrays. + let rc = unsafe { + get_cb( + self.callbacks.context, + wallet_id.as_ptr(), + txid_bytes.as_ptr(), + &mut context_kind, + &mut block_height, + block_hash.as_mut_ptr(), + &mut block_timestamp, + &mut tx_bytes_ptr, + &mut tx_bytes_len, + &mut found, + ) + }; + + // RAII guard so the tx-bytes free callback fires on every + // exit path past this point, handing the buffer back to the host. + struct TxBytesGuard { + ptr: *const u8, + len: usize, + free_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + tx_bytes: *const u8, + tx_bytes_len: usize, + ), + >, + ctx: *mut c_void, + } + impl Drop for TxBytesGuard { + fn drop(&mut self) { + if let (Some(free), false) = (self.free_fn, self.ptr.is_null()) { + // SAFETY: ptr+len match the values the lookup + // callback wrote; the host owns the allocation + // until this free fires. + unsafe { free(self.ctx, self.ptr, self.len) }; + } + } + } + let _bytes_guard = TxBytesGuard { + ptr: tx_bytes_ptr, + len: tx_bytes_len, + free_fn: self.callbacks.on_get_core_tx_record_free_fn, + ctx: self.callbacks.context, + }; + + if rc != 0 { + return CoreTxRead::Failed(rc); + } + if !found { + return CoreTxRead::Missing; + } + if tx_bytes_ptr.is_null() || tx_bytes_len == 0 { + return CoreTxRead::Unreadable("handed over no transaction bytes"); + } + // SAFETY: the host guarantees `tx_bytes_ptr` points to + // `tx_bytes_len` valid bytes until the free callback — + // `_bytes_guard` keeps that window open until this returns. + let tx_slice = unsafe { slice::from_raw_parts(tx_bytes_ptr, tx_bytes_len) }; + match dashcore::Transaction::consensus_decode(&mut &tx_slice[..]) { + Ok(transaction) => CoreTxRead::Found { + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + }, + Err(_) => CoreTxRead::Unreadable("its transaction bytes do not decode"), + } + } +} + impl PlatformWalletPersistence for FFIPersister { /// A host attests `ATOMIC_CHANGESETS` for a round it commits or rolls back /// whole; nothing of a rolled-back round survives on this side either, so @@ -1934,13 +2050,15 @@ impl PlatformWalletPersistence for FFIPersister { &self, wallet_id: WalletId, ) -> Result>, PersistenceError> { - // A backfill that could not be stored or completed is not worth - // reading. - let (Some(load), Some(free), Some(_), Some(_)) = ( + // A backfill that could not be stored or completed, or not as one + // round, is not worth reading. + let (Some(load), Some(free), Some(_), Some(_), Some(_), Some(_)) = ( self.load_core_history_for_accounting_callback, self.load_core_history_for_accounting_free_callback, self.persist_transaction_accounting_callback, self.persist_core_history_accounted_callback, + self.callbacks.on_changeset_begin_fn, + self.callbacks.on_changeset_end_fn, ) else { return Ok(None); }; @@ -1962,6 +2080,13 @@ impl PlatformWalletPersistence for FFIPersister { format!("on_load_core_history_for_accounting_fn returned error code {rc}"), )); } + if rows_ptr.is_null() && count > 0 { + unsafe { free(self.callbacks.context, rows_ptr, count) }; + return Err(PersistenceError::backend(format!( + "on_load_core_history_for_accounting_fn reported {count} transactions \ + but handed over no array" + ))); + } let mut out = Vec::with_capacity(count); if !rows_ptr.is_null() && count > 0 { let rows = unsafe { slice::from_raw_parts(rows_ptr, count) }; @@ -1996,20 +2121,29 @@ impl PlatformWalletPersistence for FFIPersister { ) else { return Ok(()); }; + // Rows and marker must commit as one unit; without the round pair + // nothing would make them, so nothing is written. + let (Some(begin), Some(end)) = ( + self.callbacks.on_changeset_begin_fn, + self.callbacks.on_changeset_end_fn, + ) else { + return Err(PersistenceError::backend( + "the accounting backfill needs the changeset begin/end pair to commit \ + atomically; nothing was written", + )); + }; let mut round = self.round_lock.lock(); round.begin_round()?; - if let Some(cb) = self.callbacks.on_changeset_begin_fn { - let result = unsafe { cb(self.callbacks.context, wallet_id.as_ptr()) }; - if result != 0 { - let _ = round.end_round(); - return Err(persist_callback_error( - result, - format!( - "changeset-begin callback returned error code {result}; \ - accounting backfill aborted before any write" - ), - )); - } + let result = unsafe { begin(self.callbacks.context, wallet_id.as_ptr()) }; + if result != 0 { + let _ = round.end_round(); + return Err(persist_callback_error( + result, + format!( + "changeset-begin callback returned error code {result}; \ + accounting backfill aborted before any write" + ), + )); } let mut outcome = RoundOutcome::default(); if !rows.is_empty() { @@ -2033,20 +2167,18 @@ impl PlatformWalletPersistence for FFIPersister { outcome.record(result); } } - if let Some(cb) = self.callbacks.on_changeset_end_fn { - let result = unsafe { - cb( - self.callbacks.context, - wallet_id.as_ptr(), - outcome.is_success(), - ) - }; - if result != 0 { - if outcome.is_success() { - outcome.record(result); - } else { - outcome.record_fatal(); - } + let result = unsafe { + end( + self.callbacks.context, + wallet_id.as_ptr(), + outcome.is_success(), + ) + }; + if result != 0 { + if outcome.is_success() { + outcome.record(result); + } else { + outcome.record_fatal(); } } round.end_round()?; @@ -2096,6 +2228,13 @@ impl PlatformWalletPersistence for FFIPersister { format!("on_load_tracked_masternodes_fn returned error code {rc}"), )); } + if rows_ptr.is_null() && count > 0 { + unsafe { free(self.callbacks.context, rows_ptr, count) }; + return Err(PersistenceError::backend(format!( + "on_load_core_history_for_accounting_fn reported {count} transactions \ + but handed over no array" + ))); + } let mut out = Vec::with_capacity(count); if !rows_ptr.is_null() && count > 0 { let rows = unsafe { slice::from_raw_parts(rows_ptr, count) }; @@ -4270,7 +4409,6 @@ impl PlatformWalletPersistence for FFIPersister { Option, PersistenceError, > { - use dashcore::consensus::Decodable; use dashcore::hashes::Hash; use key_wallet::account::{AccountType, StandardAccountType}; use key_wallet::managed_account::transaction_record::{ @@ -4278,91 +4416,49 @@ impl PlatformWalletPersistence for FFIPersister { }; use key_wallet::transaction_checking::{BlockInfo, TransactionContext, TransactionType}; - let Some(get_cb) = self.callbacks.on_get_core_tx_record_fn else { - return Ok(None); - }; - - let txid_bytes: [u8; 32] = *txid.as_byte_array(); - let mut context_kind: u8 = 0; - let mut block_height: u32 = 0; - let mut block_hash: [u8; 32] = [0u8; 32]; - let mut block_timestamp: u32 = 0; - let mut tx_bytes_ptr: *const u8 = std::ptr::null(); - let mut tx_bytes_len: usize = 0; - let mut found: bool = false; - - // SAFETY: All output pointers reference Rust-owned stack - // locals that outlive the callback invocation. `wallet_id` - // and `txid` are fixed-size byte arrays. - let rc = unsafe { - get_cb( - self.callbacks.context, - wallet_id.as_ptr(), - txid_bytes.as_ptr(), - &mut context_kind, - &mut block_height, - block_hash.as_mut_ptr(), - &mut block_timestamp, - &mut tx_bytes_ptr, - &mut tx_bytes_len, - &mut found, - ) - }; - - // RAII guard so the tx-bytes free callback fires on every - // exit path past this point — early returns for unknown - // context kinds, decode failures, and the IS-skip case all - // correctly hand the buffer back to Swift. - struct TxBytesGuard<'a> { - ptr: *const u8, - len: usize, - free_fn: Option< - unsafe extern "C" fn( - context: *mut c_void, - tx_bytes: *const u8, - tx_bytes_len: usize, - ), - >, - ctx: *mut c_void, - _marker: std::marker::PhantomData<&'a ()>, - } - impl<'a> Drop for TxBytesGuard<'a> { - fn drop(&mut self) { - if let (Some(free), false) = (self.free_fn, self.ptr.is_null()) { - // SAFETY: ptr+len match the values the lookup - // callback wrote; Swift owns the allocation - // until this free fires. - unsafe { free(self.ctx, self.ptr, self.len) }; + let (context_kind, block_height, block_hash, block_timestamp, transaction) = + match self.read_core_tx(wallet_id, txid) { + CoreTxRead::Missing => return Ok(None), + CoreTxRead::Failed(rc) => { + // TODO(tx-record-read-sentinels): `on_get_core_tx_record_fn` + // collapses every non-zero return, sentinels included, into a + // miss, so the transient-vs-permanent read distinction is + // unreachable from FFI hosts. Deferred deliberately: converting it + // to `persist_callback_error` and letting + // `get_core_tx_record_or_transient_miss` do the collapsing is a + // behaviour change for every host on the current contract. + // `get_core_transaction` already reports it. + tracing::debug!( + txid = %txid, + rc, + "on_get_core_tx_record_fn returned a non-zero result; \ + treating as miss" + ); + return Ok(None); } - } - } - let _bytes_guard = TxBytesGuard { - ptr: tx_bytes_ptr, - len: tx_bytes_len, - free_fn: self.callbacks.on_get_core_tx_record_free_fn, - ctx: self.callbacks.context, - _marker: std::marker::PhantomData, - }; - - if rc != 0 { - // TODO(tx-record-read-sentinels): `on_get_core_tx_record_fn` - // collapses every non-zero return, sentinels included, into a - // miss, so the transient-vs-permanent read distinction is - // unreachable from FFI hosts. Deferred deliberately: converting it - // to `persist_callback_error` and letting - // `get_core_tx_record_or_transient_miss` do the collapsing is a - // behaviour change for every host on the current contract. - tracing::debug!( - txid = %txid, - rc, - "on_get_core_tx_record_fn returned a non-zero result; \ - treating as miss" - ); - return Ok(None); - } - if !found { - return Ok(None); - } + CoreTxRead::Unreadable(reason) => { + tracing::debug!( + txid = %txid, + reason, + "on_get_core_tx_record_fn reported a hit it could not hand \ + over; treating as miss" + ); + return Ok(None); + } + CoreTxRead::Found { + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + } => ( + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + ), + }; let context = match context_kind { 0 => TransactionContext::Mempool, @@ -4398,34 +4494,6 @@ impl PlatformWalletPersistence for FFIPersister { } }; - if tx_bytes_ptr.is_null() || tx_bytes_len == 0 { - tracing::debug!( - txid = %txid, - "on_get_core_tx_record_fn reported a hit but no tx \ - bytes; treating as miss" - ); - return Ok(None); - } - // SAFETY: Swift guarantees `tx_bytes_ptr` points to - // `tx_bytes_len` valid bytes for the duration of the - // callback window — `_bytes_guard` keeps that window open - // until this function returns. - let tx_slice = unsafe { slice::from_raw_parts(tx_bytes_ptr, tx_bytes_len) }; - let transaction = match dashcore::blockdata::transaction::Transaction::consensus_decode( - &mut &tx_slice[..], - ) { - Ok(tx) => tx, - Err(err) => { - tracing::debug!( - txid = %txid, - error = %err, - "on_get_core_tx_record_fn returned undecodable \ - tx bytes; treating as miss" - ); - return Ok(None); - } - }; - Ok(Some(TransactionRecord { transaction, txid: *txid, @@ -4444,6 +4512,30 @@ impl PlatformWalletPersistence for FFIPersister { })) } + /// The body behind `on_get_core_tx_record_fn`, read strictly for the + /// accounting backfill: a hit in any context (InstantSend included) is + /// returned, and a host failure or a hit with no decodable body is an + /// error, so the backfill fails its round instead of reading a wallet + /// input as foreign. [`Self::get_core_tx_record`]'s miss policy is + /// unchanged. + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &dashcore::Txid, + ) -> Result, PersistenceError> { + match self.read_core_tx(wallet_id, txid) { + CoreTxRead::Missing => Ok(None), + CoreTxRead::Failed(rc) => Err(persist_callback_error( + rc, + format!("on_get_core_tx_record_fn returned error code {rc} for {txid}"), + )), + CoreTxRead::Unreadable(reason) => Err(PersistenceError::backend(format!( + "on_get_core_tx_record_fn reported {txid} but {reason}" + ))), + CoreTxRead::Found { transaction, .. } => Ok(Some(transaction)), + } + } + fn list_wallet_core_txids( &self, wallet_id: WalletId, @@ -10176,6 +10268,9 @@ mod tests { struct Host { backfilled: bool, rows: Vec, + /// Reported instead of `rows.len()`, with a null array when + /// `rows` is empty. + claimed_count: Option, fail_rows: bool, loads: std::sync::Mutex, frees: std::sync::Mutex, @@ -10195,8 +10290,12 @@ mod tests { *host.loads.lock().unwrap() += 1; *out_backfilled = host.backfilled; if !host.backfilled { - *out_rows = host.rows.as_ptr(); - *out_count = host.rows.len(); + *out_rows = if host.rows.is_empty() { + std::ptr::null() + } else { + host.rows.as_ptr() + }; + *out_count = host.claimed_count.unwrap_or(host.rows.len()); } 0 } @@ -10255,6 +10354,7 @@ mod tests { tx_bytes_len: body.len(), }) .collect(), + claimed_count: None, fail_rows, loads: std::sync::Mutex::new(0), frees: std::sync::Mutex::new(0), @@ -10375,6 +10475,139 @@ mod tests { None ); assert_eq!(*host.loads.lock().unwrap(), 0); + + // A count with no array is a broken answer, not an empty history: + // reading it as empty would let the round record completion. + let mut host = make_host(false, false); + host.rows.clear(); + host.claimed_count = Some(3); + assert!(persister_for(&host, extensions) + .load_core_history_for_accounting([1; 32]) + .is_err()); + assert_eq!(*host.frees.lock().unwrap(), 1, "the loan is still returned"); + + // Without the begin/end pair the rows and the marker could not commit + // as one unit: the host is never read, and a store writes nothing. + let host = make_host(false, false); + let unbracketed = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_changeset_begin_fn: Some(begin), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + extensions, + ); + assert_eq!( + unbracketed + .load_core_history_for_accounting([1; 32]) + .expect("a host without the round pair is not read"), + None + ); + assert_eq!(*host.loads.lock().unwrap(), 0); + assert!(unbracketed + .store_history_accounting([1; 32], &[row], true) + .is_err()); + assert!(host.events.lock().unwrap().is_empty()); + } + + /// The accounting backfill's funding read uses a stored body in any + /// context, InstantSend included, and reports a host failure or an + /// unusable body as an error rather than a miss. `get_core_tx_record` + /// keeps its policy: all three read as a miss there. + #[test] + fn should_read_a_funding_body_strictly_for_the_accounting_backfill() { + use platform_wallet::test_support::core_fixtures::{ + foreign_address, outside, pay, spend_of, + }; + + struct Host { + rc: i32, + found: bool, + context_kind: u8, + body: Vec, + frees: std::sync::Mutex, + } + unsafe extern "C" fn get( + ctx: *mut c_void, + _wallet_id: *const u8, + _txid: *const u8, + out_context_kind: *mut u8, + _out_block_height: *mut u32, + _out_block_hash: *mut u8, + _out_block_timestamp: *mut u32, + out_tx_bytes: *mut *const u8, + out_tx_bytes_len: *mut usize, + out_found: *mut bool, + ) -> i32 { + let host = &*(ctx as *const Host); + *out_context_kind = host.context_kind; + *out_found = host.found; + if !host.body.is_empty() { + *out_tx_bytes = host.body.as_ptr(); + *out_tx_bytes_len = host.body.len(); + } + host.rc + } + unsafe extern "C" fn free(ctx: *mut c_void, _bytes: *const u8, _len: usize) { + *(&*(ctx as *const Host)).frees.lock().unwrap() += 1; + } + + let funding = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let body = dashcore::consensus::encode::serialize(&funding); + let read = |rc: i32, found: bool, context_kind: u8, body: Vec| { + let host = Host { + rc, + found, + context_kind, + body, + frees: std::sync::Mutex::new(0), + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_get_core_tx_record_fn: Some(get), + on_get_core_tx_record_free_fn: Some(free), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks::default(), + ); + let strict = persister.get_core_transaction([1; 32], &funding.txid()); + let record = persister + .get_core_tx_record([1; 32], &funding.txid()) + .expect("the record read keeps reading failures as a miss") + .map(|record| record.transaction); + let frees = *host.frees.lock().unwrap(); + drop(persister); + (strict, record, frees) + }; + + // An InstantSend hit: the body is real, only the IS lock is missing. + let (strict, record, frees) = read(0, true, 1, body.clone()); + assert_eq!(strict.expect("an IS hit is a hit"), Some(funding.clone())); + assert_eq!(record, None, "the proof-facing read still skips IS"); + assert_eq!(frees, 2, "each read hands the buffer back"); + + let (strict, record, _) = read(0, true, TX_CONTEXT_RAW_IN_BLOCK as u8, body.clone()); + assert_eq!(strict.unwrap(), Some(funding.clone())); + assert_eq!(record, Some(funding.clone())); + + let (strict, record, _) = read(0, false, 0, Vec::new()); + assert_eq!(strict.unwrap(), None, "not stored is the only miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(7, false, 0, Vec::new()); + assert!(strict.is_err(), "a failing host is not a miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(0, true, 0, vec![0xFF]); + assert!(strict.is_err(), "an undecodable stored body is not a miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(0, true, 0, Vec::new()); + assert!(strict.is_err(), "a hit without a body is not a miss"); + assert_eq!(record, None); } #[test] From 1144d8bb7ad794d804165d6ccd71564f9edde655 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:37:07 +0000 Subject: [PATCH 15/21] fix(swift-sdk): fail the accounting round on a transaction-row read error `upsertTransactionAccounting` looked its transaction up with the non-throwing `fetchTransactionRow`, so a failed read looked like a missing row: the row was skipped and the backfill marker still committed. It now uses the throwing lookup and `persistTransactionAccounting` returns false on a throw, failing the round; a genuine miss is still skipped. Also documents that a missing accounting row (nil netAmount, direction and fee) means "not yet accounted, will be filled", never zero. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- .../Models/PersistentTransaction.swift | 4 ++- .../PersistentTransactionAccounting.swift | 12 ++++++--- .../PlatformWalletPersistenceHandler.swift | 26 ++++++++++++++----- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index d097d974b34..d2a88879e0e 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -242,7 +242,9 @@ public final class PersistentTransaction { } /// Core value movement for one wallet; `nil` when Rust has emitted no - /// accounting for this wallet and transaction. + /// accounting for this wallet and transaction yet. `nil` means "not yet + /// resolved, will be filled", not zero (see + /// `PersistentTransactionAccounting`). public func netAmount(for walletId: Data) -> Int64? { accounting(for: walletId)?.netAmount } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift index e2da437cbef..28df6c50ddd 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift @@ -6,10 +6,14 @@ import SwiftData /// `on_persist_transaction_accounting_fn`). /// /// A transaction shared by several local wallets has one row per wallet, -/// so each wallet shows its own side of it. A wallet with no row has no -/// accounting from Rust yet (a payload-only provider transaction, or a -/// stored row the load-time backfill has not reached). Pure storage: -/// nothing on the Swift side derives, corrects or vetoes these values. +/// so each wallet shows its own side of it. A missing row means "not yet +/// accounted, will be filled", never "accounted as zero": a payload-only +/// provider transaction, a stored row the load-time backfill has not +/// reached, or one whose wallet-signed input Rust cannot resolve yet (its +/// funding transaction is unavailable). Rust writes the row once it can, +/// at a later backfill or a live update, and never writes a guessed one. +/// Pure storage: nothing on the Swift side derives, corrects or vetoes +/// these values. @Model public final class PersistentTransactionAccounting { #Unique([\.walletId, \.txid]) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 404897a3f50..0f00c016433 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -9075,7 +9075,9 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// `(walletId, txid)`. Always fired inside a round (a sync round, or the /// load-time backfill's own), so they stage with it and commit at /// `endChangeset`. Returns `false` to fail the round, like the other - /// per-kind callbacks. + /// per-kind callbacks, including when a transaction row cannot be read: + /// read as a miss, the backfill would record completion over a row it + /// never wrote. func persistTransactionAccounting( walletId: Data, rows: UnsafePointer?, @@ -9088,8 +9090,19 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { case .found: break } guard count > 0, let rows else { return true } - for i in 0.. Date: Thu, 29 Oct 2026 09:45:04 +0000 Subject: [PATCH 16/21] docs(platform-wallet): state the P2PKH premise of the owned-input check Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- .../src/changeset/wallet_accounting.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index e1b013335ce..33310c5df2c 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -156,13 +156,14 @@ pub fn funding_txids(wallet: &ManagedWalletInfo, unaccounted: &[Transaction]) -> /// transaction gets no row and is listed in /// [`HistoryAccounting::unresolved`] instead: reading the input as foreign /// would turn a payment with change into an `Incoming` receipt of the -/// change. An input redeemed any other way (a script the wallet does not -/// spend from) reads as foreign. +/// change. An input redeemed any other way reads as foreign; the wallet's +/// funds accounts hold only P2PKH scripts, so its own coins are always +/// redeemed with a public key. /// -/// No row is never a wrong row: a transaction left without one is not yet -/// accounted, and the host shows it as such until a later backfill (or the -/// live projection) writes it. Any subset of a host's unaccounted -/// transactions is therefore safe to pass. +/// A transaction left without a row is not yet accounted, and the host +/// shows it as such until a later backfill (or the live projection) writes +/// it. Any subset of a host's unaccounted transactions is therefore safe to +/// pass. pub fn history_accounting( wallet: &ManagedWalletInfo, unaccounted: &[Transaction], From 3a111c14d0389a0123ddf0590ec426f2ff9005ae Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:46:01 +0000 Subject: [PATCH 17/21] docs(swift-sdk): describe the open backfill in the V5 schema notes Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- packages/swift-sdk/SCHEMA_RELEASES.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index 62882b7cf65..03231e279c2 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -33,9 +33,12 @@ of each wallet Rust reads back the wallet's transactions that lack a row (`on_load_core_history_for_accounting_fn`), fetches any funding transaction it needs through `on_get_core_tx_record_fn`, and writes the rows (`on_persist_transaction_accounting_fn`) and the marker -(`on_persist_core_history_accounted_fn`) in one round. Later loads see the -marker and skip the read. Deleting a wallet deletes its rows; deleting a -transaction cascades to them. +(`on_persist_core_history_accounted_fn`) in one round. A transaction whose +wallet-signed input Rust cannot resolve yet gets no row, and the round then +writes no marker, so the next load reads the transactions still without a +row again. A missing row means "not yet accounted", never zero. Once the +marker is set, later loads skip the read. Deleting a wallet deletes its rows; +deleting a transaction cascades to them. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, From f3238493a44a4c65353f394bbac66acbe073d47c Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:48:52 +0000 Subject: [PATCH 18/21] fix(swift-sdk): report a failed tx-record fetch to Rust instead of a miss `getCoreTxRecordCallback` returned 0 with `found = false` when the SwiftData fetch threw, so Rust's strict accounting read could not tell a read error from a transaction the store does not hold. `coreTxRecord` now throws on a fetch error and the callback logs it and returns 1. Rust's `get_core_tx_record` still reads a non-zero return as a miss, so the asset-lock and DashPay callers are unchanged; the accounting backfill now fails its round (no marker). A genuine miss, a row with no transaction bytes and an in-block row without a block hash stay misses (rc 0): those are stored data states, not read failures. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WunYYjpfLux1ByK7fGF7SG --- .../PlatformWalletPersistenceHandler.swift | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 0f00c016433..31c2b1963e4 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -9327,7 +9327,9 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// hash that would round-trip back to Rust as a real block id), /// or when the row has no `transactionData` (the FFI write path /// always populates it, so a missing one signals a corrupt row - /// the Rust side can't decode anyway). + /// the Rust side can't decode anyway). Throws when the fetch + /// itself fails: that is not a miss, and Rust's accounting + /// backfill must not read it as one. /// /// The wallet-id is currently unused (`txid` is globally /// unique), but is accepted to match the Rust trait signature @@ -9335,13 +9337,13 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { func coreTxRecord( walletId: Data, txid: Data - ) -> (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? { + ) throws -> (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? { _ = walletId - return onQueue { + return try onQueue { let descriptor = FetchDescriptor( predicate: #Predicate { $0.txid == txid } ) - guard let row = try? backgroundContext.fetch(descriptor).first else { + guard let row = try backgroundContext.fetch(descriptor).first else { return nil } // The Rust side decodes `transactionData` into a @@ -11420,9 +11422,11 @@ private func loadShieldedViewingKeysFreeCallback( /// the three block fields when context is 2 or 3, plus the tx /// bytes pointer + length) on a hit; returns `0`. /// - Sets `*outFound = false` on a miss; returns `0`. -/// - Returns `0` even on Swift-side errors (treated as miss); the -/// Rust side's `record_or_persister` helper logs and falls -/// through to the caller's existing not-found / poll path. +/// - Returns `1` when the SwiftData fetch fails. Rust's +/// `get_core_tx_record` still reads that as a miss (the asset-lock +/// and DashPay callers retry); the accounting backfill's strict +/// read fails its round instead of reading a wallet input as +/// foreign. private func getCoreTxRecordCallback( context: UnsafeMutableRawPointer?, walletIdPtr: UnsafePointer?, @@ -11451,7 +11455,17 @@ private func getCoreTxRecordCallback( let walletId = Data(bytes: walletIdPtr, count: 32) let txid = Data(bytes: txidPtr, count: 32) - guard let row = handler.coreTxRecord(walletId: walletId, txid: txid) else { + let found: (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? + do { + found = try handler.coreTxRecord(walletId: walletId, txid: txid) + } catch { + SDKLogger.event( + "persistence_core_tx_record_read_failed", category: .persistence, severity: .error, + fields: ["wallet_reference": .reference(walletId)], error: error + ) + return 1 + } + guard let row = found else { // Miss — outFound already set to false above. return 0 } From 71012537301a23eb187cd93e9a533f91399611ca Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:38:01 +0000 Subject: [PATCH 19/21] fix(swift-sdk): expose accounting formatter and correct migration assertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow SwiftExampleApp to call the shared formatter across the module boundary. Assert that migrated V4 rows have no wallet-scoped amount until accounting rows are written, retaining the explicit missing-data contract. Validated 32 scoped Rust/FFI tests and three-crate Clippy. SwiftData and SwiftExampleApp execution require a macOS runner unavailable on this host. 🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent Co-Authored-By: Claude GPT-6 --- .../Persistence/Models/PersistentTransaction.swift | 2 +- .../SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index d2a88879e0e..fde28b02c95 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -288,7 +288,7 @@ public final class PersistentTransaction { public static let noAccounting = "—" /// Signed DASH text for a duff amount; `magnitude` cannot trap on `Int64.min`. - static func format(duffs: Int64) -> String { + public static func format(duffs: Int64) -> String { String(format: "%@%.8f DASH", duffs >= 0 ? "+" : "-", Double(duffs.magnitude) / 100_000_000) } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift index ae7e3e0713f..aa42eab571b 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift @@ -110,6 +110,7 @@ final class DashReleasedSchemaTests: XCTestCase { /// without one and keep their stored amount, and a row written after the /// migration survives a reopen and cascades with its transaction. @MainActor + // TODO(macOS-validation): Run the V4-to-V5 SwiftData migration test on macOS. func testShouldMigrateV4ToPerWalletAccountingRows() throws { let fixture = try XCTUnwrap(DashReleasedSchemaRegistry.fixtures.first { $0.version.versionIdentifier == Schema.Version(3, 0, 0) @@ -133,7 +134,7 @@ final class DashReleasedSchemaTests: XCTestCase { let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) .first { $0.txid == txid }) XCTAssertTrue(row.walletAccounting.isEmpty, "V4 rows carry no per-wallet accounting yet") - XCTAssertEqual(row.netAmount(for: walletId), row.netAmount) + XCTAssertNil(row.netAmount(for: walletId), "Global V4 accounting is not wallet-scoped") let accounting = PersistentTransactionAccounting( walletId: walletId, txid: txid, netAmount: -1_234, direction: CoreDirectionCode.outgoing, fee: 34, transaction: nil From 28a665097c6d74d0d2cdc76fce88d9756114b280 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:16:47 +0000 Subject: [PATCH 20/21] fix(platform-wallet): follow the rust-dashcore hash API in transaction accounting rust-dashcore c9aeac0 (inherited from v5.1-dev) drops the `Hash` trait constructor on hash newtypes: build the P2PKH pubkey hash via `PubkeyHash::from_raw_hash(hash160::Hash::hash(..))` and remove the `dashcore::hashes::Hash` imports that are now unused. Co-Authored-By: Claude Opus 5.5 --- packages/rs-platform-wallet-ffi/src/persistence.rs | 2 -- .../rs-platform-wallet/src/changeset/wallet_accounting.rs | 5 +++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 5fc22f71d5c..f406884378a 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -1875,7 +1875,6 @@ impl FFIPersister { /// caller applies its own policy to the outcome. fn read_core_tx(&self, wallet_id: WalletId, txid: &dashcore::Txid) -> CoreTxRead { use dashcore::consensus::Decodable; - use dashcore::hashes::Hash; let Some(get_cb) = self.callbacks.on_get_core_tx_record_fn else { return CoreTxRead::Missing; @@ -10135,7 +10134,6 @@ mod tests { /// slot still succeeds. #[test] fn store_delivers_transaction_accounting_after_the_changeset_skipping_detailless_records() { - use dashcore::hashes::Hash as _; use key_wallet::account::{AccountType, StandardAccountType}; use key_wallet::managed_account::transaction_record::{ OutputDetail, OutputRole, TransactionDirection, TransactionRecord, diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 9a8b660c02f..3299385ddf5 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -22,7 +22,7 @@ use std::collections::{BTreeMap, BTreeSet}; use dashcore::blockdata::script::Instruction; -use dashcore::hashes::Hash; +use dashcore::hashes::hash160; use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxIn, Txid}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_ref::ManagedAccountRef; @@ -299,7 +299,8 @@ fn p2pkh_redeemed_script(input: &TxIn) -> Option { return None; }; let pubkey = pubkey.as_bytes(); - matches!(pubkey.len(), 33 | 65).then(|| ScriptBuf::new_p2pkh(&PubkeyHash::hash(pubkey))) + matches!(pubkey.len(), 33 | 65) + .then(|| ScriptBuf::new_p2pkh(&PubkeyHash::from_raw_hash(hash160::Hash::hash(pubkey)))) } /// The wallet's funds accounts, without contacts' watch-only chains. From d8bbf50d32601e124e9796d17a486c5841c7933d Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 6 Nov 2026 16:37:15 +0000 Subject: [PATCH 21/21] test(swift-sdk): assert migrations land on the current schema version The base froze App Store schema 4.0.0 and this branch adds DashSchemaV5, so the hard-coded 4.0.0 in testPublishedV3MigratesOperationIdToNil went stale. Compare with the last schema of DashMigrationPlan instead. Co-Authored-By: OpenAI Codex Co-Authored-By: Claude Opus 5.5 --- .../SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift index f1dec9f0c87..33552994ed9 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift @@ -86,7 +86,8 @@ final class AssetLockOperationPersistenceTests: XCTestCase { XCTAssertEqual(rows.count, 1) XCTAssertNil(rows.first?.operationId) XCTAssertEqual(rows.first?.amountDuffs, 1000) - XCTAssertEqual(DashModelContainer.schema.version, Schema.Version(4, 0, 0)) + let currentSchema = try XCTUnwrap(DashMigrationPlan.schemas.last) + XCTAssertEqual(DashModelContainer.schema.version, currentSchema.versionIdentifier) } func testMalformedOperationIdFailsLoadInsteadOfBecomingAbsent() throws {