Skip to content

Commit

Permalink
Merge branch 'main' into mock-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed May 28, 2024
2 parents 2b440a6 + f389380 commit 7df5485
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/notification_handler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustyline::history::DefaultHistory;
use rustyline::{CompletionType, Editor};
use std::collections::HashSet;
use std::env;
use std::time::Duration;
use uniffi_lipalightninglib::{
handle_notification, mnemonic_to_secret, Config, EnvironmentCode, NotificationToggles, TzConfig,
};
Expand Down Expand Up @@ -168,6 +169,7 @@ fn start_payment_received(words: &mut dyn Iterator<Item = &str>) -> Result<()> {
address_txs_confirmed_is_enabled: true,
lnurl_pay_request_is_enabled: true,
},
Duration::from_secs(60),
)
.unwrap();

Expand Down Expand Up @@ -203,6 +205,7 @@ fn start_address_txs_confirmed(words: &mut dyn Iterator<Item = &str>) -> Result<
address_txs_confirmed_is_enabled: true,
lnurl_pay_request_is_enabled: true,
},
Duration::from_secs(60),
)
.unwrap();

Expand Down Expand Up @@ -250,6 +253,7 @@ fn start_lnurl_pay_request(words: &mut dyn Iterator<Item = &str>) -> Result<()>
address_txs_confirmed_is_enabled: true,
lnurl_pay_request_is_enabled: true,
},
Duration::from_secs(60),
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ namespace lipalightninglib {
void parse_lightning_address([ByRef] string address);

[Throws=NotificationHandlingError]
Notification handle_notification(Config config, string notification_payload, NotificationToggles notification_toggles);
Notification handle_notification(Config config, string notification_payload, NotificationToggles notification_toggles, duration timeout);
};

dictionary Secret {
Expand Down
24 changes: 16 additions & 8 deletions src/notification_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::sync::{mpsc, Arc, Mutex};
use std::time::{Duration, Instant};

const TIMEOUT: Duration = Duration::from_secs(60);

/// A notification to be displayed to the user.
#[derive(Debug)]
pub enum Notification {
Expand Down Expand Up @@ -74,12 +72,14 @@ pub struct NotificationToggles {
/// requests are:
/// * Receive a payment from a previously issued bolt11 invoice.
/// * Receive a payment from a confirmed swap.
/// * Issue an invoice in order to receive an LNURL payment.
///
/// Requires network: **yes**
pub fn handle_notification(
config: Config,
notification_payload: String,
notification_toggles: NotificationToggles,
timeout: Duration,
) -> NotificationHandlingResult<Notification> {
enable_backtrace();
if let Some(level) = config.file_logging_level {
Expand All @@ -91,6 +91,8 @@ pub fn handle_notification(
}
debug!("Started handling a notification.");

let timeout_instant = Instant::now() + timeout;

let payload = match serde_json::from_str::<Payload>(&notification_payload) {
Ok(p) => p,
Err(e) => {
Expand Down Expand Up @@ -140,10 +142,10 @@ pub fn handle_notification(

match payload {
Payload::PaymentReceived { payment_hash } => {
handle_payment_received_notification(rt, sdk, rx, payment_hash)
handle_payment_received_notification(rt, sdk, rx, payment_hash, timeout_instant)
}
Payload::AddressTxsConfirmed { address } => {
handle_address_txs_confirmed_notification(rt, sdk, rx, address)
handle_address_txs_confirmed_notification(rt, sdk, rx, address, timeout_instant)
}
Payload::LnurlPayRequest { data } => {
handle_lnurl_pay_request_notification(rt, sdk, config, data)
Expand Down Expand Up @@ -193,6 +195,7 @@ fn handle_payment_received_notification(
sdk: Arc<BreezServices>,
event_receiver: Receiver<BreezEvent>,
payment_hash: String,
timeout_instant: Instant,
) -> NotificationHandlingResult<Notification> {
// Check if the payment was already received
if let Some(payment) = get_confirmed_payment(&rt, &sdk, &payment_hash)? {
Expand All @@ -203,7 +206,9 @@ fn handle_payment_received_notification(
}

// Wait for payment to be received
if let Some(details) = wait_for_payment_with_timeout(&event_receiver, &payment_hash)? {
if let Some(details) =
wait_for_payment_with_timeout(&event_receiver, &payment_hash, timeout_instant)?
{
// We want to wait as long as possible to decrease the likelihood of the signer being shut down
// while HTLCs are still in-flight.
wait_for_synced_event(&event_receiver)?;
Expand All @@ -225,6 +230,7 @@ fn handle_address_txs_confirmed_notification(
sdk: Arc<BreezServices>,
event_receiver: Receiver<BreezEvent>,
address: String,
timeout_instant: Instant,
) -> NotificationHandlingResult<Notification> {
let in_progress_swap = rt
.handle()
Expand Down Expand Up @@ -265,7 +271,9 @@ fn handle_address_txs_confirmed_notification(
}

// Wait for payment to arrive
if let Some(details) = wait_for_payment_with_timeout(&event_receiver, &payment_hash)? {
if let Some(details) =
wait_for_payment_with_timeout(&event_receiver, &payment_hash, timeout_instant)?
{
// We want to wait as long as possible to decrease the likelihood of the signer being shut down
// while HTLCs are still in-flight.
wait_for_synced_event(&event_receiver)?;
Expand Down Expand Up @@ -419,9 +427,9 @@ fn get_confirmed_payment(
fn wait_for_payment_with_timeout(
event_receiver: &Receiver<BreezEvent>,
payment_hash: &str,
timeout_instant: Instant,
) -> NotificationHandlingResult<Option<InvoicePaidDetails>> {
let start = Instant::now();
while Instant::now().duration_since(start) < TIMEOUT {
while Instant::now() < timeout_instant {
match event_receiver.recv_timeout(Duration::from_secs(1)) {
Ok(BreezEvent::InvoicePaid { details }) if details.payment_hash == payment_hash => {
return Ok(Some(details))
Expand Down

0 comments on commit 7df5485

Please sign in to comment.