Skip to content

Commit 15d7bc6

Browse files
SWvheerdenclaude
andcommitted
refactor: move payref archival into set_as_unmined for atomicity
Move the PayRef history archival logic from process_reorg into CompletedTransactionSql::set_as_unmined so that archive + delete happens atomically on the same connection. This covers all callers: both process_reorg (reorg path) and transaction_validation_protocol (normal validation path). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent da04720 commit 15d7bc6

File tree

1 file changed

+8
-10
lines changed
  • base_layer/wallet/src/transaction_service/storage

1 file changed

+8
-10
lines changed

base_layer/wallet/src/transaction_service/storage/sqlite_db.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,16 +1486,7 @@ impl TransactionBackend for TransactionServiceSqliteDatabase {
14861486
.filter(completed_transactions::mined_height.ge(reorg_height as i64))
14871487
.load::<i64>(&mut conn)?;
14881488
for tx_id in &tx_ids {
1489-
// Archive existing payrefs to history before unmining (for reorg tracking).
1490-
// This is done here rather than in set_as_unmined because set_as_unmined is also
1491-
// called during normal validation (not just reorgs), and the extra DB operations
1492-
// would cause lock contention under high load.
1493-
let tx_id_typed = TxId::from(*tx_id as u64);
1494-
let existing_payrefs = PayrefSql::find_all_by_tx_id(tx_id_typed, &mut conn)?;
1495-
for pr in &existing_payrefs {
1496-
PayrefHistorySql::archive_from_payref(pr, &mut conn)?;
1497-
}
1498-
CompletedTransactionSql::set_as_unmined(tx_id_typed, &mut conn)?;
1489+
CompletedTransactionSql::set_as_unmined((*tx_id as u64).into(), &mut conn)?;
14991490
}
15001491
trace!(
15011492
target: LOG_TARGET,
@@ -2477,6 +2468,13 @@ impl CompletedTransactionSql {
24772468
.execute(conn)
24782469
.num_rows_affected_or_not_found(1)?;
24792470

2471+
// Archive existing payrefs to history before deleting, so that
2472+
// PayRefs from before a reorg can still be looked up.
2473+
let existing_payrefs = PayrefSql::find_all_by_tx_id(tx_id, conn)?;
2474+
for pr in &existing_payrefs {
2475+
PayrefHistorySql::archive_from_payref(pr, conn)?;
2476+
}
2477+
24802478
let sent = match existing_tx.sent_output_hashes.as_ref() {
24812479
Some(bytes) => bytes_to_fixedhash_vec(bytes),
24822480
_ => vec![],

0 commit comments

Comments
 (0)