Skip to content

Commit 4cd3755

Browse files
authored
Merge pull request #2290 from SatoshiPortal/swap-review
chore: clean up swaps
2 parents 926f4df + bf8ea01 commit 4cd3755

48 files changed

Lines changed: 12948 additions & 1468 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ CLAUDE.md
116116

117117
# Generated integration-test aggregator (see tool/gen_all_test.dart)
118118
integration_test/all_test.dart
119+
120+
# Generated swap debug log export (written during test runs)
121+
bull_logs.tsv

lib/core/background_tasks/handler.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:bb_mobile/core/background_tasks/tasks.dart';
22
import 'package:bb_mobile/core/storage/sqlite_database.dart';
3-
import 'package:bb_mobile/core/swaps/domain/usecases/restart_swap_watcher_usecase.dart';
3+
import 'package:bb_mobile/core/swaps/domain/usecases/process_ongoing_swaps_usecase.dart';
44
import 'package:bb_mobile/core/utils/logger.dart' show log;
55
import 'package:bb_mobile/core/wallet/domain/usecases/get_wallets_usecase.dart';
66
import 'package:bb_mobile/core/wallet/domain/usecases/sync_wallet_usecase.dart';
@@ -42,7 +42,7 @@ Future<bool> tasksHandler(String task) async {
4242

4343
final syncWalletUsecase = locator<SyncWalletUsecase>();
4444
final getWalletsUsecase = locator<GetWalletsUsecase>();
45-
final restartSwapWatcherUsecase = locator<RestartSwapWatcherUsecase>();
45+
final processOngoingSwapsUsecase = locator<ProcessOngoingSwapsUsecase>();
4646

4747
final backgroundTask = BackgroundTask.fromName(task);
4848

@@ -64,7 +64,14 @@ Future<bool> tasksHandler(String task) async {
6464
if (wallets.isEmpty) {
6565
log.warning('No wallets to sync');
6666
} else {
67-
await restartSwapWatcherUsecase.execute();
67+
// Poll + act to completion: the BG isolate dies right after this
68+
// returns, so a websocket-based restart would never see an event.
69+
// Bounded to respect the iOS background budget.
70+
await processOngoingSwapsUsecase.execute().timeout(
71+
const Duration(seconds: 25),
72+
onTimeout: () =>
73+
log.warning('Swaps background processing hit time budget'),
74+
);
6875
}
6976
case BackgroundTask.logsPrune:
7077
await log.prune();

lib/core/storage/migrations/migrations.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export 'package:bb_mobile/core/storage/migrations/schema_0_to_1.dart';
22
export 'package:bb_mobile/core/storage/migrations/schema_10_to_11.dart';
33
export 'package:bb_mobile/core/storage/migrations/schema_11_to_12.dart';
4+
export 'package:bb_mobile/core/storage/migrations/schema_12_to_13.dart';
45
export 'package:bb_mobile/core/storage/migrations/schema_1_to_2.dart';
56
export 'package:bb_mobile/core/storage/migrations/schema_2_to_3.dart';
67
export 'package:bb_mobile/core/storage/migrations/schema_3_to_4.dart';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:bb_mobile/core/storage/sqlite_database.steps.dart';
2+
import 'package:drift/drift.dart';
3+
4+
/// Migration from version 12 to 13
5+
///
6+
/// Changes to swaps table:
7+
/// - Adds 'refund_fees' column: network fees actually paid by a refund
8+
/// transaction (previously misrecorded into 'claim_fees')
9+
/// - Adds 'was_direct_payment' column: marks reverse swaps settled by a
10+
/// Magic Routing Hint direct payment (no lockup existed, nothing to claim)
11+
/// - Backfills status 'refunded' for swaps stored as 'completed' that have a
12+
/// refund txid: a refunded swap is a failed payment, not a successful one
13+
class Schema12To13 {
14+
static Future<void> migrate(Migrator m, Schema13 schema13) async {
15+
try {
16+
await m.addColumn(schema13.swaps, schema13.swaps.refundFees);
17+
} catch (e) {
18+
if (!e.toString().contains('duplicate column')) rethrow;
19+
}
20+
21+
try {
22+
await m.addColumn(schema13.swaps, schema13.swaps.wasDirectPayment);
23+
} catch (e) {
24+
if (!e.toString().contains('duplicate column')) rethrow;
25+
}
26+
27+
await m.database.customStatement(
28+
"UPDATE swaps SET status = 'refunded' "
29+
"WHERE status = 'completed' AND refund_txid IS NOT NULL",
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)