Skip to content

Commit 124af8b

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent a8a1172 commit 124af8b

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/event.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414

1515
use crate::connection::ConnectionManager;
1616
use crate::fee_estimator::ConfirmationTarget;
17+
use crate::liquidity::LiquiditySource;
1718

1819
use crate::payment::store::{
1920
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
@@ -445,6 +446,7 @@ where
445446
connection_manager: Arc<ConnectionManager<L>>,
446447
output_sweeper: Arc<Sweeper>,
447448
network_graph: Arc<Graph>,
449+
liquidity_source: Option<Arc<LiquiditySource<Arc<FilesystemLogger>>>>,
448450
payment_store: Arc<PaymentStore<L>>,
449451
peer_store: Arc<PeerStore<L>>,
450452
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
@@ -461,6 +463,7 @@ where
461463
bump_tx_event_handler: Arc<BumpTransactionEventHandler>,
462464
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
463465
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
466+
liquidity_source: Option<Arc<LiquiditySource<Arc<FilesystemLogger>>>>,
464467
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
465468
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
466469
) -> Self {
@@ -472,6 +475,7 @@ where
472475
connection_manager,
473476
output_sweeper,
474477
network_graph,
478+
liquidity_source,
475479
payment_store,
476480
peer_store,
477481
logger,
@@ -1009,7 +1013,11 @@ where
10091013
LdkEvent::PaymentPathFailed { .. } => {},
10101014
LdkEvent::ProbeSuccessful { .. } => {},
10111015
LdkEvent::ProbeFailed { .. } => {},
1012-
LdkEvent::HTLCHandlingFailed { .. } => {},
1016+
LdkEvent::HTLCHandlingFailed { failed_next_destination, .. } => {
1017+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1018+
liquidity_source.handle_htlc_handling_failed(failed_next_destination);
1019+
}
1020+
},
10131021
LdkEvent::PendingHTLCsForwardable { time_forwardable } => {
10141022
let forwarding_channel_manager = self.channel_manager.clone();
10151023
let min = time_forwardable.as_millis() as u64;
@@ -1230,6 +1238,10 @@ where
12301238
fee_earned,
12311239
);
12321240
}
1241+
1242+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1243+
liquidity_source.handle_payment_forwarded(next_channel_id);
1244+
}
12331245
},
12341246
LdkEvent::ChannelPending {
12351247
channel_id,
@@ -1303,6 +1315,14 @@ where
13031315
counterparty_node_id,
13041316
);
13051317

1318+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1319+
liquidity_source.handle_channel_ready(
1320+
user_channel_id,
1321+
&channel_id,
1322+
&counterparty_node_id,
1323+
);
1324+
}
1325+
13061326
let event = Event::ChannelReady {
13071327
channel_id,
13081328
user_channel_id: UserChannelId(user_channel_id),
@@ -1341,7 +1361,22 @@ where
13411361
};
13421362
},
13431363
LdkEvent::DiscardFunding { .. } => {},
1344-
LdkEvent::HTLCIntercepted { .. } => {},
1364+
LdkEvent::HTLCIntercepted {
1365+
requested_next_hop_scid,
1366+
intercept_id,
1367+
expected_outbound_amount_msat,
1368+
payment_hash,
1369+
..
1370+
} => {
1371+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1372+
liquidity_source.handle_htlc_intercepted(
1373+
requested_next_hop_scid,
1374+
intercept_id,
1375+
expected_outbound_amount_msat,
1376+
payment_hash,
1377+
);
1378+
}
1379+
},
13451380
LdkEvent::InvoiceReceived { .. } => {
13461381
debug_assert!(false, "We currently don't handle BOLT12 invoices manually, so this event should never be emitted.");
13471382
},

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ impl Node {
534534
Arc::clone(&self.connection_manager),
535535
Arc::clone(&self.output_sweeper),
536536
Arc::clone(&self.network_graph),
537+
self.liquidity_source.clone(),
537538
Arc::clone(&self.payment_store),
538539
Arc::clone(&self.peer_store),
539540
Arc::clone(&self.runtime),

src/liquidity.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ use crate::logger::{log_debug, log_error, log_info, LdkLogger, Logger};
1313
use crate::types::{ChannelManager, KeysManager, LiquidityManager, PeerManager, Wallet};
1414
use crate::{Config, Error};
1515

16-
use lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA;
16+
use lightning::events::HTLCDestination;
17+
use lightning::ln::channelmanager::{InterceptId, MIN_FINAL_CLTV_EXPIRY_DELTA};
1718
use lightning::ln::msgs::SocketAddress;
19+
use lightning::ln::types::ChannelId;
20+
use lightning::ln::PaymentHash;
1821
use lightning::routing::router::{RouteHint, RouteHintHop};
1922
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, InvoiceBuilder, RoutingFees};
2023
use lightning_liquidity::events::Event;
@@ -889,6 +892,54 @@ where
889892
Error::InvoiceCreationFailed
890893
})
891894
}
895+
896+
pub(crate) fn handle_channel_ready(
897+
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
898+
) {
899+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
900+
if let Err(e) = lsps2_service_handler.channel_ready(
901+
user_channel_id,
902+
channel_id,
903+
counterparty_node_id,
904+
) {
905+
log_error!(self.logger, "Errored processing ChannelReady event: {:?}", e);
906+
}
907+
}
908+
}
909+
910+
pub(crate) fn handle_htlc_intercepted(
911+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
912+
payment_hash: PaymentHash,
913+
) {
914+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
915+
if let Err(e) = lsps2_service_handler.htlc_intercepted(
916+
intercept_scid,
917+
intercept_id,
918+
expected_outbound_amount_msat,
919+
payment_hash,
920+
) {
921+
log_error!(self.logger, "Failed to handle HTLCIntercepted event: {:?}", e);
922+
}
923+
}
924+
}
925+
926+
pub(crate) fn handle_htlc_handling_failed(&self, failed_next_destination: HTLCDestination) {
927+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
928+
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failed_next_destination) {
929+
log_error!(self.logger, "Errored processing HTLCHandlingFailed event: {:?}", e);
930+
}
931+
}
932+
}
933+
934+
pub(crate) fn handle_payment_forwarded(&self, next_channel_id: Option<ChannelId>) {
935+
if let Some(next_channel_id) = next_channel_id {
936+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
937+
if let Err(e) = lsps2_service_handler.payment_forwarded(next_channel_id) {
938+
log_error!(self.logger, "Failed to handle PaymentForwarded: {:?}", e);
939+
}
940+
}
941+
}
942+
}
892943
}
893944

894945
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)