Skip to content

Commit dbc8b4f

Browse files
fixup: create new 'time' gated constructor that uses DefaultTimeProvider.
also refactor handle_set_webhook to not lookup client_webhooks multiple times
1 parent cad9ec6 commit dbc8b4f

File tree

1 file changed

+66
-39
lines changed

1 file changed

+66
-39
lines changed

lightning-liquidity/src/lsps5/service.rs

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::lsps5::msgs::{
1717
SetWebhookRequest, SetWebhookResponse, WebhookNotification, WebhookNotificationMethod,
1818
};
1919
use crate::message_queue::MessageQueue;
20+
use crate::prelude::hash_map::Entry;
2021
use crate::prelude::*;
2122
use crate::sync::{Arc, Mutex};
2223

@@ -140,8 +141,8 @@ where
140141
CM::Target: AChannelManager,
141142
TP::Target: TimeProvider,
142143
{
143-
/// Constructs a `LSPS5ServiceHandler`.
144-
pub(crate) fn new(
144+
/// Constructs a `LSPS5ServiceHandler` using the given time provider.
145+
pub(crate) fn new_with_time_provider(
145146
event_queue: Arc<EventQueue>, pending_messages: Arc<MessageQueue>, channel_manager: CM,
146147
config: LSPS5ServiceConfig, time_provider: TP,
147148
) -> Self {
@@ -184,43 +185,49 @@ where
184185
let now =
185186
LSPSDateTime::new_from_duration_since_epoch(self.time_provider.duration_since_epoch());
186187

187-
let no_change = client_webhooks
188-
.get(&params.app_name)
189-
.map_or(false, |webhook| webhook.url == params.webhook);
190-
191-
if !client_webhooks.contains_key(&params.app_name)
192-
&& client_webhooks.len() >= self.config.max_webhooks_per_client as usize
193-
{
194-
let error = LSPS5ProtocolError::TooManyWebhooks;
195-
let msg = LSPS5Message::Response(
196-
request_id,
197-
LSPS5Response::SetWebhookError(error.clone().into()),
198-
)
199-
.into();
200-
self.pending_messages.enqueue(&counterparty_node_id, msg);
201-
return Err(LightningError {
202-
err: error.message().into(),
203-
action: ErrorAction::IgnoreAndLog(Level::Info),
204-
});
188+
let num_webhooks = client_webhooks.len();
189+
let mut no_change = false;
190+
match client_webhooks.entry(params.app_name.clone()) {
191+
Entry::Occupied(mut entry) => {
192+
no_change = entry.get().url == params.webhook;
193+
let (last_used, last_notification_sent) = if no_change {
194+
(entry.get().last_used.clone(), entry.get().last_notification_sent.clone())
195+
} else {
196+
(now, new_hash_map())
197+
};
198+
entry.insert(StoredWebhook {
199+
_app_name: params.app_name.clone(),
200+
url: params.webhook.clone(),
201+
_counterparty_node_id: counterparty_node_id,
202+
last_used,
203+
last_notification_sent,
204+
});
205+
},
206+
Entry::Vacant(entry) => {
207+
if num_webhooks >= self.config.max_webhooks_per_client as usize {
208+
let error = LSPS5ProtocolError::TooManyWebhooks;
209+
let msg = LSPS5Message::Response(
210+
request_id,
211+
LSPS5Response::SetWebhookError(error.clone().into()),
212+
)
213+
.into();
214+
self.pending_messages.enqueue(&counterparty_node_id, msg);
215+
return Err(LightningError {
216+
err: error.message().into(),
217+
action: ErrorAction::IgnoreAndLog(Level::Info),
218+
});
219+
}
220+
221+
entry.insert(StoredWebhook {
222+
_app_name: params.app_name.clone(),
223+
url: params.webhook.clone(),
224+
_counterparty_node_id: counterparty_node_id,
225+
last_used: now,
226+
last_notification_sent: new_hash_map(),
227+
});
228+
},
205229
}
206230

207-
let (last_used, last_notification_sent) = if no_change {
208-
let existing_webhook = client_webhooks.get(&params.app_name).unwrap();
209-
(existing_webhook.last_used.clone(), existing_webhook.last_notification_sent.clone())
210-
} else {
211-
(now, new_hash_map())
212-
};
213-
214-
let stored_webhook = StoredWebhook {
215-
_app_name: params.app_name.clone(),
216-
url: params.webhook.clone(),
217-
_counterparty_node_id: counterparty_node_id,
218-
last_used,
219-
last_notification_sent,
220-
};
221-
222-
client_webhooks.insert(params.app_name.clone(), stored_webhook);
223-
224231
if !no_change {
225232
self.send_webhook_registered_notification(
226233
counterparty_node_id,
@@ -465,6 +472,27 @@ where
465472
}
466473
}
467474

475+
#[cfg(feature = "time")]
476+
impl<CM: Deref> LSPS5ServiceHandler<CM, Arc<DefaultTimeProvider>>
477+
where
478+
CM::Target: AChannelManager,
479+
{
480+
/// Constructs a `LSPS5ServiceHandler` using [`DefaultTimeProvider`].
481+
#[allow(dead_code)]
482+
pub(crate) fn new(
483+
event_queue: Arc<EventQueue>, pending_messages: Arc<MessageQueue>, channel_manager: CM,
484+
config: LSPS5ServiceConfig,
485+
) -> Self {
486+
Self::new_with_time_provider(
487+
event_queue,
488+
pending_messages,
489+
channel_manager,
490+
config,
491+
Arc::new(DefaultTimeProvider),
492+
)
493+
}
494+
}
495+
468496
impl<CM: Deref, TP: Deref> LSPSProtocolMessageHandler for LSPS5ServiceHandler<CM, TP>
469497
where
470498
CM::Target: AChannelManager,
@@ -497,8 +525,7 @@ where
497525
"Service handler received LSPS5 response message. This should never happen."
498526
);
499527
let err = format!(
500-
"Service handler received LSPS5 response message from node {:?}.
501-
This should never happen.",
528+
"Service handler received LSPS5 response message from node {:?}. This should never happen.",
502529
counterparty_node_id
503530
);
504531
Err(LightningError { err, action: ErrorAction::IgnoreAndLog(Level::Info) })

0 commit comments

Comments
 (0)