|
| 1 | +import XCTest |
| 2 | +import SwiftData |
| 3 | +@testable import SwiftDashSDK |
| 4 | + |
| 5 | +/// Regression guard for the "self-send flips to phantom-incoming after |
| 6 | +/// a mid-flight restart" bug. The mempool sighting taints |
| 7 | +/// `PersistentTxo.isSpent` for the input; the persister's load |
| 8 | +/// callback then filters that row out of the restored UTXO set, so |
| 9 | +/// the catch-up classifier on the next launch sees a missing input, |
| 10 | +/// emits `direction=Incoming`, and rewrites `netAmount` to the sum of |
| 11 | +/// the wallet's own outputs in the tx. |
| 12 | +final class PersisterRestartClassificationIntegrationTests: IntegrationTestCase { |
| 13 | + private let fundingDash: Double = 0.5 |
| 14 | + private var fundingDuffs: UInt64 { UInt64(fundingDash * 1e8) } |
| 15 | + private let sendAmount: UInt64 = 10_000 |
| 16 | + |
| 17 | + func testSelfSendClassificationSurvivesMidFlightRestart() async throws { |
| 18 | + try await env.walletManager.startSpv(config: env.spvConfig) |
| 19 | + let alice = try await env.makeTestWallet(name: "restart-class-alice") |
| 20 | + |
| 21 | + let aliceFundingAddr = try alice.getCoreWallet().nextReceiveAddress() |
| 22 | + _ = try await env.fund(address: aliceFundingAddr, dash: fundingDash) |
| 23 | + try await alice.waitForSpendable(exactly: fundingDuffs, timeout: 90) |
| 24 | + |
| 25 | + let beforeTxids = try await readTxids() |
| 26 | + |
| 27 | + let aliceSecondAddr = try alice.getCoreWallet().nextReceiveAddress() |
| 28 | + _ = try alice.getCoreWallet().sendToAddresses( |
| 29 | + recipients: [(address: aliceSecondAddr, amountDuffs: sendAmount)] |
| 30 | + ) |
| 31 | + |
| 32 | + guard let sendTxid = try await waitForNewTxid(notIn: beforeTxids) else { |
| 33 | + XCTFail("self-send PersistentTransaction row never appeared within 60s") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // Control: mempool sighting must already be Internal/-fee |
| 38 | + // (Rust still holds the input in memory at this point). |
| 39 | + try await assertSelfSendRow( |
| 40 | + txid: sendTxid, |
| 41 | + phase: "mempool sighting" |
| 42 | + ) |
| 43 | + |
| 44 | + try await env.restartWalletManager() |
| 45 | + _ = try await env.mine(1, including: sendTxid) |
| 46 | + try await env.walletManager.startSpv(config: env.spvConfig) |
| 47 | + try await env.walletManager.waitUntilUpToDate(height: try await env.coreRPC.getBlockCount()) |
| 48 | + |
| 49 | + try await assertTxIsMined( |
| 50 | + txid: sendTxid, |
| 51 | + phase: "post-restart catch-up" |
| 52 | + ) |
| 53 | + |
| 54 | + // Regression: classification must survive the restart. Pre-fix |
| 55 | + // the row flips to Incoming / +sum_of_outputs. |
| 56 | + try await assertSelfSendRow( |
| 57 | + txid: sendTxid, |
| 58 | + phase: "post-restart catch-up" |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + /// Same regression as the test above, but the self-send tx never |
| 63 | + /// gets a confirming block — it stays in the mempool across the |
| 64 | + /// restart. Exercises the catch-up classifier on the mempool-only |
| 65 | + /// path: after the SPV reconnects, the masternodes replay the |
| 66 | + /// wallet's own tx via INV/mempool and the classifier reprocesses |
| 67 | + /// it without any new block to anchor it. |
| 68 | + func testSelfSendClassificationSurvivesMempoolOnlyRestart() async throws { |
| 69 | + try await env.walletManager.startSpv(config: env.spvConfig) |
| 70 | + let alice = try await env.makeTestWallet(name: "restart-class-alice-no-mine") |
| 71 | + |
| 72 | + let aliceFundingAddr = try alice.getCoreWallet().nextReceiveAddress() |
| 73 | + _ = try await env.fund(address: aliceFundingAddr, dash: fundingDash) |
| 74 | + try await alice.waitForSpendable(exactly: fundingDuffs, timeout: 90) |
| 75 | + |
| 76 | + let beforeTxids = try await readTxids() |
| 77 | + |
| 78 | + let aliceSecondAddr = try alice.getCoreWallet().nextReceiveAddress() |
| 79 | + _ = try alice.getCoreWallet().sendToAddresses( |
| 80 | + recipients: [(address: aliceSecondAddr, amountDuffs: sendAmount)] |
| 81 | + ) |
| 82 | + |
| 83 | + guard let sendTxid = try await waitForNewTxid(notIn: beforeTxids) else { |
| 84 | + XCTFail("self-send PersistentTransaction row never appeared within 60s") |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + try await assertSelfSendRow( |
| 89 | + txid: sendTxid, |
| 90 | + phase: "mempool sighting" |
| 91 | + ) |
| 92 | + |
| 93 | + try await env.restartWalletManager() |
| 94 | + try await env.walletManager.startSpv(config: env.spvConfig) |
| 95 | + |
| 96 | + try await assertTxIsInMempool( |
| 97 | + txid: sendTxid, |
| 98 | + phase: "post-restart mempool-only catch-up" |
| 99 | + ) |
| 100 | + |
| 101 | + try await assertSelfSendRow( |
| 102 | + txid: sendTxid, |
| 103 | + phase: "post-restart mempool-only catch-up" |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + // MARK: - Helpers |
| 108 | + |
| 109 | + /// Sendable snapshot of the columns the test cares about. |
| 110 | + /// `PersistentTransaction` itself can't cross the `MainActor.run` |
| 111 | + /// boundary because `@Model` types are not Sendable. |
| 112 | + private struct TxSnapshot: Sendable { |
| 113 | + let direction: UInt32 |
| 114 | + let netAmount: Int64 |
| 115 | + let context: UInt32 |
| 116 | + } |
| 117 | + |
| 118 | + private func fetchTransaction(_ txid: Data) async throws -> TxSnapshot? { |
| 119 | + let container = env.modelContainer |
| 120 | + return try await MainActor.run { |
| 121 | + let ctx = ModelContext(container) |
| 122 | + guard let row = try ctx.fetch(FetchDescriptor<PersistentTransaction>( |
| 123 | + predicate: #Predicate { $0.txid == txid } |
| 124 | + )).first else { return nil } |
| 125 | + return TxSnapshot( |
| 126 | + direction: row.direction, |
| 127 | + netAmount: row.netAmount, |
| 128 | + context: row.context |
| 129 | + ) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Asserts the row is in a mined state: inBlock (2) |
| 134 | + private func assertTxIsMined(txid: Data, phase: String) async throws { |
| 135 | + guard let row = try await fetchTransaction(txid) else { |
| 136 | + XCTFail("\(phase): tx row missing for \(txid.toHexString()) (expected mined)") |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + let inBlock = TransactionContextType.inBlock.rawValue |
| 141 | + XCTAssertTrue( |
| 142 | + row.context == inBlock, |
| 143 | + "\(phase): context=\(row.context) — expected inBlock(\(inBlock))" |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + /// Asserts the row is in a mempool-equivalent state: mempool (0) |
| 148 | + /// or instantSend (1) — i.e., observed but not yet in a block. |
| 149 | + /// Used after the variant that restarts without mining. |
| 150 | + private func assertTxIsInMempool(txid: Data, phase: String) async throws { |
| 151 | + guard let row = try await fetchTransaction(txid) else { |
| 152 | + XCTFail("\(phase): tx row missing for \(txid.toHexString()) (expected mempool)") |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + let mempool = TransactionContextType.mempool.rawValue |
| 157 | + let instantSend = TransactionContextType.instantSend.rawValue |
| 158 | + |
| 159 | + XCTAssertTrue( |
| 160 | + row.context == mempool || row.context == instantSend, |
| 161 | + "\(phase): context=\(row.context) — expected mempool(\(mempool)) or instantSend(\(instantSend))" |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + private func assertSelfSendRow(txid: Data, phase: String) async throws { |
| 166 | + guard let row = try await fetchTransaction(txid) else { |
| 167 | + XCTFail("\(phase): row missing for txid \(txid.toHexString())") |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + XCTAssertEqual( |
| 172 | + row.direction, 2, |
| 173 | + "\(phase): direction=\(row.direction) (expected 2=Internal). " + |
| 174 | + "context=\(row.context), netAmount=\(row.netAmount). " + |
| 175 | + "Positive netAmount with direction=0 is the phantom-incoming signature." |
| 176 | + ) |
| 177 | + |
| 178 | + XCTAssertLessThan( |
| 179 | + row.netAmount, 0, |
| 180 | + "\(phase): netAmount=\(row.netAmount) (expected <0; self-send only leaves the fee)." |
| 181 | + ) |
| 182 | + |
| 183 | + XCTAssertLessThan( |
| 184 | + abs(row.netAmount), 100_000, |
| 185 | + "\(phase): fee too large: |netAmount|=\(abs(row.netAmount))" |
| 186 | + ) |
| 187 | + } |
| 188 | +} |
| 189 | + |
0 commit comments