Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override pending payments. #678

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl BreezServices {
// update both closed channels and lightning transaction payments
let mut payments = closed_channel_payments;
payments.extend(new_data.payments.clone());
self.persister.insert_or_update_payments(&payments)?;
self.persister.insert_or_update_payments(&payments, true)?;

let duration = start.elapsed();
info!("Sync duration: {:?}", duration);
Expand Down Expand Up @@ -955,30 +955,33 @@ impl BreezServices {
invoice: &LNInvoice,
amount_msat: u64,
) -> Result<(), SendPaymentError> {
self.persister.insert_or_update_payments(&[Payment {
id: invoice.payment_hash.clone(),
payment_type: PaymentType::Sent,
payment_time: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64,
amount_msat,
fee_msat: 0,
status: PaymentStatus::Pending,
description: invoice.description.clone(),
details: PaymentDetails::Ln {
data: LnPaymentDetails {
payment_hash: invoice.payment_hash.clone(),
label: String::new(),
destination_pubkey: invoice.payee_pubkey.clone(),
payment_preimage: String::new(),
keysend: false,
bolt11: invoice.bolt11.clone(),
lnurl_success_action: None,
ln_address: None,
lnurl_metadata: None,
lnurl_withdraw_endpoint: None,
swap_info: None,
self.persister.insert_or_update_payments(
&[Payment {
id: invoice.payment_hash.clone(),
payment_type: PaymentType::Sent,
payment_time: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64,
amount_msat,
fee_msat: 0,
status: PaymentStatus::Pending,
description: invoice.description.clone(),
details: PaymentDetails::Ln {
data: LnPaymentDetails {
payment_hash: invoice.payment_hash.clone(),
label: String::new(),
destination_pubkey: invoice.payee_pubkey.clone(),
payment_preimage: String::new(),
keysend: false,
bolt11: invoice.bolt11.clone(),
lnurl_success_action: None,
ln_address: None,
lnurl_metadata: None,
lnurl_withdraw_endpoint: None,
swap_info: None,
},
},
},
}])?;
}],
false,
)?;

if invoice.amount_msat.is_none() {
self.persister.insert_payment_external_info(
Expand Down Expand Up @@ -1227,7 +1230,7 @@ impl BreezServices {
if payment.is_some() {
let res = cloned
.persister
.insert_or_update_payments(&vec![payment.clone().unwrap()]);
.insert_or_update_payments(&vec![payment.clone().unwrap()], false);
debug!("paid invoice was added to payments list {:?}", res);
}
_ = cloned.on_event(BreezEvent::InvoicePaid {
Expand Down Expand Up @@ -2341,7 +2344,7 @@ pub(crate) mod tests {
let test_config = create_test_config();
let persister = Arc::new(create_test_persister(test_config.clone()));
persister.init()?;
persister.insert_or_update_payments(&dummy_transactions)?;
persister.insert_or_update_payments(&dummy_transactions, false)?;
persister.insert_payment_external_info(
payment_hash_with_lnurl_success_action,
Some(&sa),
Expand Down Expand Up @@ -2544,7 +2547,7 @@ pub(crate) mod tests {
let test_config = create_test_config();
let persister = Arc::new(create_test_persister(test_config.clone()));
persister.init()?;
persister.insert_or_update_payments(&known_payments)?;
persister.insert_or_update_payments(&known_payments, false)?;
persister.set_lsp_id(MockBreezServer {}.lsp_id())?;

let mut builder = BreezServicesBuilder::new(test_config.clone());
Expand Down
18 changes: 12 additions & 6 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ impl SqliteStorage {
/// Note that, if a payment has details of type [LnPaymentDetails] which contain a [SuccessActionProcessed],
/// then the [LnPaymentDetails] will NOT be persisted. In that case, the [SuccessActionProcessed]
/// can be inserted separately via [SqliteStorage::insert_payment_external_info].
pub fn insert_or_update_payments(&self, transactions: &[Payment]) -> PersistResult<()> {
let deleted = self.delete_pending_lightning_payments()?;
debug!("Deleted {deleted} pending payments");
pub fn insert_or_update_payments(
&self,
transactions: &[Payment],
delete_pending: bool,
) -> PersistResult<()> {
if delete_pending {
let deleted = self.delete_pending_lightning_payments()?;
debug!("Deleted {deleted} pending payments");
}

let con = self.get_connection()?;
let mut prep_statement = con.prepare(
Expand Down Expand Up @@ -526,8 +532,8 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
}];
let storage = SqliteStorage::new(test_utils::create_test_sql_dir());
storage.init()?;
storage.insert_or_update_payments(&txs)?;
storage.insert_or_update_payments(&failed_txs)?;
storage.insert_or_update_payments(&txs, false)?;
storage.insert_or_update_payments(&failed_txs, false)?;
storage.insert_payment_external_info(
payment_hash_with_lnurl_success_action,
Some(&sa),
Expand Down Expand Up @@ -587,7 +593,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
let max_ts = storage.last_payment_timestamp()?;
assert_eq!(max_ts, 2000);

storage.insert_or_update_payments(&txs)?;
storage.insert_or_update_payments(&txs, false)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest::default())?;
assert_eq!(retrieve_txs.len(), 3);
assert_eq!(retrieve_txs, txs);
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-core/src/swap_in/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ mod tests {
},
},
};
persister.insert_or_update_payments(&vec![payment.clone()])?;
persister.insert_or_update_payments(&vec![payment.clone()], false)?;

// We test the case that a confirmed transaction was detected on chain that
// sent funds to this address.
Expand Down Expand Up @@ -904,7 +904,7 @@ mod tests {
// paid_amount of the swap.
let mut payment = payment.clone();
payment.amount_msat = 2000;
persister.insert_or_update_payments(&vec![payment])?;
persister.insert_or_update_payments(&vec![payment], false)?;
swapper
.on_event(BreezEvent::InvoicePaid {
details: crate::InvoicePaidDetails {
Expand Down