Skip to content

Commit

Permalink
Fix local tx cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Feb 26, 2025
1 parent 69e17c6 commit 48ac36f
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ops::Not as _;
use std::time::Instant;
use std::{fs, path::PathBuf, str::FromStr, sync::Arc, time::Duration};
Expand Down Expand Up @@ -2717,11 +2717,14 @@ impl LiquidSdk {
/// it inserts or updates a corresponding entry in our Payments table.
async fn sync_payments_with_chain_data(&self, partial_sync: bool) -> Result<()> {
let mut recoverable_swaps = self.get_monitored_swaps_list(partial_sync).await?;
let mut tx_map = self
let mut wallet_tx_map = self
.recoverer
.recover_from_onchain(&mut recoverable_swaps)
.await?;

let all_wallet_tx_ids: HashSet<String> =
wallet_tx_map.keys().map(|txid| txid.to_string()).collect();

for swap in recoverable_swaps {
let swap_id = &swap.id();

Expand All @@ -2735,7 +2738,7 @@ impl LiquidSdk {
.collect::<Vec<&String>>()
{
if let Some(tx) =
tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
wallet_tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
{
self.persister
.insert_or_update_payment_with_wallet_tx(&tx)?;
Expand All @@ -2753,7 +2756,7 @@ impl LiquidSdk {
.collect::<Vec<&String>>()
{
if let Some(tx) =
tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
wallet_tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
{
self.persister
.insert_or_update_payment_with_wallet_tx(&tx)?;
Expand All @@ -2776,7 +2779,7 @@ impl LiquidSdk {
.collect::<Vec<&String>>()
{
if let Some(tx) =
tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
wallet_tx_map.remove(&lwk_wollet::elements::Txid::from_str(tx_id)?)
{
self.persister
.insert_or_update_payment_with_wallet_tx(&tx)?;
Expand All @@ -2789,19 +2792,20 @@ impl LiquidSdk {
};
}

let non_swap_wallet_tx_map = wallet_tx_map;

let payments = self
.persister
.get_payments_by_tx_id(&ListPaymentsRequest::default())?;

// We query only these that may need update, should be a fast query.
let unconfirmed_payment_txs_data = self.persister.list_unconfirmed_payment_txs_data()?;
let mut unconfirmed_txs_by_id: HashMap<String, PaymentTxData> =
unconfirmed_payment_txs_data
.into_iter()
.map(|tx| (tx.tx_id.clone(), tx))
.collect::<HashMap<String, PaymentTxData>>();
let unconfirmed_txs_by_id: HashMap<String, PaymentTxData> = unconfirmed_payment_txs_data
.into_iter()
.map(|tx| (tx.tx_id.clone(), tx))
.collect::<HashMap<String, PaymentTxData>>();

for tx in tx_map.values() {
for tx in non_swap_wallet_tx_map.values() {
let tx_id = tx.txid.to_string();
let maybe_payment = payments.get(&tx_id);
let mut updated = false;
Expand Down Expand Up @@ -2829,21 +2833,30 @@ impl LiquidSdk {
// An unconfirmed tx that was not found in the payments table
self.persister.insert_or_update_payment_with_wallet_tx(tx)?;
}
unconfirmed_txs_by_id.remove(&tx_id);
}

for unknown_unconfirmed_tx_data in unconfirmed_txs_by_id.values() {
if unknown_unconfirmed_tx_data.timestamp.is_some_and(|t| {
let unknown_unconfirmed_txs: Vec<_> = unconfirmed_txs_by_id
.iter()
.filter(|(txid, _)| !all_wallet_tx_ids.contains(*txid))
.map(|(_, tx)| tx)
.collect();

for unknown_unconfirmed_tx in unknown_unconfirmed_txs {
if unknown_unconfirmed_tx.timestamp.is_some_and(|t| {
(utils::now().saturating_sub(t)) > NETWORK_PROPAGATION_GRACE_PERIOD.as_secs() as u32
}) {
self.persister
.delete_payment_tx_data(&unknown_unconfirmed_tx_data.tx_id)?;
.delete_payment_tx_data(&unknown_unconfirmed_tx.tx_id)?;
info!(
"Found an unknown unconfirmed tx and deleted it. Txid: {}",
unknown_unconfirmed_tx_data.tx_id
unknown_unconfirmed_tx.tx_id
);
} else {
debug!("Found an unknown unconfirmed tx. Keeping it to allow propagation through the network. Txid: {}", unknown_unconfirmed_tx_data.tx_id)
debug!(
"Found an unknown unconfirmed tx that was inserted at {:?}. \
Keeping it to allow propagation through the network. Txid: {}",
unknown_unconfirmed_tx.timestamp, unknown_unconfirmed_tx.tx_id
)
}
}

Expand Down

0 comments on commit 48ac36f

Please sign in to comment.