Skip to content

fix(swaps): remove swap records in wipe_all_databases (#197) - #199

Open
Tyagiquamar wants to merge 54 commits into
synonymdev:masterfrom
Tyagiquamar:fix/197-wipe-swap-records
Open

Tyagiquamar wants to merge 54 commits into
synonymdev:masterfrom
Tyagiquamar:fix/197-wipe-swap-records

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Fixes #197. Follow-up to #182 (Pubky Swaps routes).

Ensure that wipe_all_databases() removes pubky_swaps records from disk alongside existing activity and Blocktank database wiping, so that swap records and preimages do not survive a wallet reset.

Root Cause & Problem

pubky_swaps writes each swap's persistent record to core/pubky_swaps/*.json. These records contain the swap contract, destination address, amounts, and before broadcast, the saved claim whose witness carries the preimage.

Previously, wipe_all_databases() only wiped the activity_db and blocktank_db instances. During an in-app wallet wipe, LDK storage was wiped by the app reset and SQLite databases were wiped by wipe_all_databases(), but the swap records in core/pubky_swaps/ were left untouched on disk. Consequently, swap history and saved preimages survived a wallet reset.

Technical Solution

  1. In src/modules/pubky_swaps/store.rs, implement pub(crate) fn wipe_all() -> Result<(), PubkySwapError>:
    • Acquires the store file lock (LOCK).
    • Reads entries in the configured pubky_swaps directory (dir()).
    • Removes all record files and subdirectories so the directory is completely emptied while keeping the base path valid for subsequent writes.
  2. In src/modules/pubky_swaps/mod.rs, export wipe_all from store.
  3. In src/lib.rs, call modules::pubky_swaps::wipe_all() within wipe_all_databases(). If wiping fails, it returns a descriptive DbError::InitializationError.
  4. Unit tests:
    • Added test_store_wipe_all in src/modules/pubky_swaps/store.rs to verify that wipe_all() clears all persisted swap records and that new records can be created after wiping.
    • Added test_wipe_all_databases_removes_swap_records in src/lib.rs to verify end-to-end that calling wipe_all_databases() removes swap records from disk.

Test Plan

  • Verified cargo fmt --check passes with zero formatting issues.
  • Added unit test test_store_wipe_all in src/modules/pubky_swaps/store.rs.
  • Added unit test test_wipe_all_databases_removes_swap_records in src/lib.rs.

iroh reads network interfaces through Network.framework, so apps that link BitkitCore without it fail with undefined nw_* symbols.
# Conflicts:
#	bindings/ios/bitkitcore.swift
# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	Package.swift
#	bindings/android/gradle.properties
# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	Package.swift
#	bindings/android/gradle.properties
ovitrif and others added 23 commits November 6, 2026 21:36
…bky-swaps-route

# Conflicts:
#	bindings/ios/bitkitcore.swift
#	src/modules/paykit_route/router.rs
#	src/modules/paykit_route/types.rs
…bky-swaps-route

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	Package.swift
#	bindings/android/gradle.properties
A listed claim no longer counts as published, a claim is told from a refund by the preimage in its witness, and a restored swap needs a Lightning payment the wallet sent for its hash that covers its lockup. Restore also skips finished swaps and other networks, refuses a second record for a hash, reads only a missing record as absent, keeps partial lists, asks swap-list-v1 providers at once, and reports known and incomplete ids.
…pubky-swaps-route

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	Package.swift
#	bindings/android/gradle.properties
Quotes and contracts now come over paykit-cap on direct/1 and are checked by swap-common's CapClient: terms and commit hashes, signatures, the invoice blob and both parties. A reply must be signed by the provider's key or the capability signing key its published records delegate to (Capabilities §5); unsigned replies and providers without paykit-cap-v1 are refused. swap-common moves to f45e204, and restore uses its min_reverse_lockup_sat.
An unsent saved claim waits for the lockup's confirmations again after a reorganization, and a claim of a lockup the provider replaced is built again for the new outpoint.
# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	Package.swift
#	bindings/android/gradle.properties
Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com>

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 finding (1 P1). Adds pubky_swaps::store::wipe_all() and calls it from wipe_all_databases() so swap records and their saved preimages do not survive a wallet reset. Reviewed 3016158, full tier, against base feat/pubky-swaps-route (4f3e215).

What I checked, and 3 candidates I ruled out

Build: not run here. The head is on a fork, so I did not run it locally. This is a static review plus the PR's own CI.
Read in full: src/modules/pubky_swaps/store.rs, the pubky_swaps/mod.rs exports, wipe_all_databases and init_db in src/lib.rs
CI: paykit-route-tests fails compiling the lib test target with E0603: enum SwapDirection is private at store.rs:157 (finding below). The bindgen and usdt/wake jobs were still pending when I read it.

Ruled out

  • Wipe racing a concurrent save/advance_swap: a reset is not run while swaps advance, and save takes the same LOCK. The trigger is not reachable in practice.
  • Partial wipe on error: if the swap wipe fails after activity and blocktank were wiped, the result is a partial wipe. That is the same shape as the existing two steps, which already fail one after the other, so it is not new here.
  • Wipe before init_db: dir() returns the "not initialized" error, and wipe_all_databases already fails earlier on the same condition.

Merge confidence: 1/5, CI fails on the change itself: the lib test target does not compile.

Comment thread src/modules/pubky_swaps/store.rs Outdated
#[cfg(test)]
mod tests {
use super::*;
use swap_common::messages::{SwapDirection, SwapScript};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Import SwapDirection from the crate root, it is private in messages

swap_common::messages only uses SwapDirection privately (messages.rs:16, use crate::swap::{NetworkSpec, SwapDirection, SwapState};). Because of that, this import fails with E0603 and the whole bitkitcore lib test target stops compiling. That breaks every cargo test --lib run, including paykit-route-tests on this PR, not only the new tests. The sibling modules import it from the root (client.rs:28, cap.rs:169).

Suggested change
use swap_common::messages::{SwapDirection, SwapScript};
use swap_common::messages::SwapScript;
use swap_common::SwapDirection;

Verified by: the paykit-route-tests job on this head, error[E0603]: enum SwapDirection is private --> src/modules/pubky_swaps/store.rs:157:33.

@ovitrif

ovitrif commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Retargeted to master: #182, which this PR was based on, merges now, and merging it deletes feat/pubky-swaps-route, which would close this PR unmerged.

@ovitrif
ovitrif changed the base branch from feat/pubky-swaps-route to master November 10, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: wipe swap records with the wallet

3 participants