Skip to content

Commit af6bd7f

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent 9d65dc9 commit af6bd7f

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/event.rs

+38-3
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,
@@ -24,7 +25,7 @@ use crate::io::{
2425
EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE,
2526
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE,
2627
};
27-
use crate::logger::{log_debug, log_error, log_info, Logger};
28+
use crate::logger::{log_debug, log_error, log_info, FilesystemLogger, Logger};
2829

2930
use lightning::events::bump_transaction::BumpTransactionEvent;
3031
use lightning::events::{ClosureReason, PaymentPurpose, ReplayEvent};
@@ -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

+1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ impl Node {
535535
Arc::clone(&self.connection_manager),
536536
Arc::clone(&self.output_sweeper),
537537
Arc::clone(&self.network_graph),
538+
self.liquidity_source.clone(),
538539
Arc::clone(&self.payment_store),
539540
Arc::clone(&self.peer_store),
540541
Arc::clone(&self.runtime),

src/liquidity.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ use crate::logger::{log_debug, log_error, log_info, FilesystemLogger, 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, InvoiceBuilder, RoutingFees};
2023
use lightning_liquidity::events::Event;
@@ -891,6 +894,54 @@ where
891894
Error::InvoiceCreationFailed
892895
})
893896
}
897+
898+
pub(crate) fn handle_channel_ready(
899+
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
900+
) {
901+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
902+
if let Err(e) = lsps2_service_handler.channel_ready(
903+
user_channel_id,
904+
channel_id,
905+
counterparty_node_id,
906+
) {
907+
log_error!(self.logger, "Errored processing ChannelReady event: {:?}", e);
908+
}
909+
}
910+
}
911+
912+
pub(crate) fn handle_htlc_intercepted(
913+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
914+
payment_hash: PaymentHash,
915+
) {
916+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
917+
if let Err(e) = lsps2_service_handler.htlc_intercepted(
918+
intercept_scid,
919+
intercept_id,
920+
expected_outbound_amount_msat,
921+
payment_hash,
922+
) {
923+
log_error!(self.logger, "Failed to handle HTLCIntercepted event: {:?}", e);
924+
}
925+
}
926+
}
927+
928+
pub(crate) fn handle_htlc_handling_failed(&self, failed_next_destination: HTLCDestination) {
929+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
930+
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failed_next_destination) {
931+
log_error!(self.logger, "Errored processing HTLCHandlingFailed event: {:?}", e);
932+
}
933+
}
934+
}
935+
936+
pub(crate) fn handle_payment_forwarded(&self, next_channel_id: Option<ChannelId>) {
937+
if let Some(next_channel_id) = next_channel_id {
938+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
939+
if let Err(e) = lsps2_service_handler.payment_forwarded(next_channel_id) {
940+
log_error!(self.logger, "Failed to handle PaymentForwarded: {:?}", e);
941+
}
942+
}
943+
}
944+
}
894945
}
895946

896947
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)