Skip to content

Commit 22ca418

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent c9b8b89 commit 22ca418

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};
@@ -432,6 +433,7 @@ where
432433
connection_manager: Arc<ConnectionManager<L>>,
433434
output_sweeper: Arc<Sweeper>,
434435
network_graph: Arc<Graph>,
436+
liquidity_source: Option<Arc<LiquiditySource<Arc<FilesystemLogger>>>>,
435437
payment_store: Arc<PaymentStore<L>>,
436438
peer_store: Arc<PeerStore<L>>,
437439
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
@@ -448,6 +450,7 @@ where
448450
bump_tx_event_handler: Arc<BumpTransactionEventHandler>,
449451
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
450452
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
453+
liquidity_source: Option<Arc<LiquiditySource<Arc<FilesystemLogger>>>>,
451454
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
452455
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
453456
) -> Self {
@@ -459,6 +462,7 @@ where
459462
connection_manager,
460463
output_sweeper,
461464
network_graph,
465+
liquidity_source,
462466
payment_store,
463467
peer_store,
464468
logger,
@@ -994,7 +998,11 @@ where
994998
LdkEvent::PaymentPathFailed { .. } => {},
995999
LdkEvent::ProbeSuccessful { .. } => {},
9961000
LdkEvent::ProbeFailed { .. } => {},
997-
LdkEvent::HTLCHandlingFailed { .. } => {},
1001+
LdkEvent::HTLCHandlingFailed { failed_next_destination, .. } => {
1002+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1003+
liquidity_source.handle_htlc_handling_failed(failed_next_destination);
1004+
}
1005+
},
9981006
LdkEvent::PendingHTLCsForwardable { time_forwardable } => {
9991007
let forwarding_channel_manager = self.channel_manager.clone();
10001008
let min = time_forwardable.as_millis() as u64;
@@ -1211,6 +1219,10 @@ where
12111219
fee_earned,
12121220
);
12131221
}
1222+
1223+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1224+
liquidity_source.handle_payment_forwarded(next_channel_id);
1225+
}
12141226
},
12151227
LdkEvent::ChannelPending {
12161228
channel_id,
@@ -1284,6 +1296,14 @@ where
12841296
counterparty_node_id,
12851297
);
12861298

1299+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1300+
liquidity_source.handle_channel_ready(
1301+
user_channel_id,
1302+
&channel_id,
1303+
&counterparty_node_id,
1304+
);
1305+
}
1306+
12871307
let event = Event::ChannelReady {
12881308
channel_id,
12891309
user_channel_id: UserChannelId(user_channel_id),
@@ -1322,7 +1342,22 @@ where
13221342
};
13231343
},
13241344
LdkEvent::DiscardFunding { .. } => {},
1325-
LdkEvent::HTLCIntercepted { .. } => {},
1345+
LdkEvent::HTLCIntercepted {
1346+
requested_next_hop_scid,
1347+
intercept_id,
1348+
expected_outbound_amount_msat,
1349+
payment_hash,
1350+
..
1351+
} => {
1352+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1353+
liquidity_source.handle_htlc_intercepted(
1354+
requested_next_hop_scid,
1355+
intercept_id,
1356+
expected_outbound_amount_msat,
1357+
payment_hash,
1358+
);
1359+
}
1360+
},
13261361
LdkEvent::InvoiceReceived { .. } => {
13271362
debug_assert!(false, "We currently don't handle BOLT12 invoices manually, so this event should never be emitted.");
13281363
},

src/lib.rs

+1
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

+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;
@@ -893,6 +896,54 @@ where
893896
Error::InvoiceCreationFailed
894897
})
895898
}
899+
900+
pub(crate) fn handle_channel_ready(
901+
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
902+
) {
903+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
904+
if let Err(e) = lsps2_service_handler.channel_ready(
905+
user_channel_id,
906+
channel_id,
907+
counterparty_node_id,
908+
) {
909+
log_error!(self.logger, "Errored processing ChannelReady event: {:?}", e);
910+
}
911+
}
912+
}
913+
914+
pub(crate) fn handle_htlc_intercepted(
915+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
916+
payment_hash: PaymentHash,
917+
) {
918+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
919+
if let Err(e) = lsps2_service_handler.htlc_intercepted(
920+
intercept_scid,
921+
intercept_id,
922+
expected_outbound_amount_msat,
923+
payment_hash,
924+
) {
925+
log_error!(self.logger, "Failed to handle HTLCIntercepted event: {:?}", e);
926+
}
927+
}
928+
}
929+
930+
pub(crate) fn handle_htlc_handling_failed(&self, failed_next_destination: HTLCDestination) {
931+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
932+
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failed_next_destination) {
933+
log_error!(self.logger, "Errored processing HTLCHandlingFailed event: {:?}", e);
934+
}
935+
}
936+
}
937+
938+
pub(crate) fn handle_payment_forwarded(&self, next_channel_id: Option<ChannelId>) {
939+
if let Some(next_channel_id) = next_channel_id {
940+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
941+
if let Err(e) = lsps2_service_handler.payment_forwarded(next_channel_id) {
942+
log_error!(self.logger, "Failed to handle PaymentForwarded: {:?}", e);
943+
}
944+
}
945+
}
946+
}
896947
}
897948

898949
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)