Skip to content

Commit 9ad2733

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent 45c0b77 commit 9ad2733

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

src/event.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{
1414

1515
use crate::connection::ConnectionManager;
1616
use crate::fee_estimator::ConfirmationTarget;
17+
use crate::liquidity::LiquiditySource;
18+
use crate::logger::Logger;
1719

1820
use crate::payment::store::{
1921
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
@@ -445,6 +447,7 @@ where
445447
connection_manager: Arc<ConnectionManager<L>>,
446448
output_sweeper: Arc<Sweeper>,
447449
network_graph: Arc<Graph>,
450+
liquidity_source: Option<Arc<LiquiditySource<Arc<Logger>>>>,
448451
payment_store: Arc<PaymentStore<L>>,
449452
peer_store: Arc<PeerStore<L>>,
450453
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
@@ -461,6 +464,7 @@ where
461464
bump_tx_event_handler: Arc<BumpTransactionEventHandler>,
462465
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
463466
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
467+
liquidity_source: Option<Arc<LiquiditySource<Arc<Logger>>>>,
464468
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
465469
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
466470
) -> Self {
@@ -472,6 +476,7 @@ where
472476
connection_manager,
473477
output_sweeper,
474478
network_graph,
479+
liquidity_source,
475480
payment_store,
476481
peer_store,
477482
logger,
@@ -1009,7 +1014,11 @@ where
10091014
LdkEvent::PaymentPathFailed { .. } => {},
10101015
LdkEvent::ProbeSuccessful { .. } => {},
10111016
LdkEvent::ProbeFailed { .. } => {},
1012-
LdkEvent::HTLCHandlingFailed { .. } => {},
1017+
LdkEvent::HTLCHandlingFailed { failed_next_destination, .. } => {
1018+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1019+
liquidity_source.handle_htlc_handling_failed(failed_next_destination);
1020+
}
1021+
},
10131022
LdkEvent::PendingHTLCsForwardable { time_forwardable } => {
10141023
let forwarding_channel_manager = self.channel_manager.clone();
10151024
let min = time_forwardable.as_millis() as u64;
@@ -1230,6 +1239,10 @@ where
12301239
fee_earned,
12311240
);
12321241
}
1242+
1243+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1244+
liquidity_source.handle_payment_forwarded(next_channel_id);
1245+
}
12331246
},
12341247
LdkEvent::ChannelPending {
12351248
channel_id,
@@ -1303,6 +1316,14 @@ where
13031316
counterparty_node_id,
13041317
);
13051318

1319+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1320+
liquidity_source.handle_channel_ready(
1321+
user_channel_id,
1322+
&channel_id,
1323+
&counterparty_node_id,
1324+
);
1325+
}
1326+
13061327
let event = Event::ChannelReady {
13071328
channel_id,
13081329
user_channel_id: UserChannelId(user_channel_id),
@@ -1341,7 +1362,22 @@ where
13411362
};
13421363
},
13431364
LdkEvent::DiscardFunding { .. } => {},
1344-
LdkEvent::HTLCIntercepted { .. } => {},
1365+
LdkEvent::HTLCIntercepted {
1366+
requested_next_hop_scid,
1367+
intercept_id,
1368+
expected_outbound_amount_msat,
1369+
payment_hash,
1370+
..
1371+
} => {
1372+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1373+
liquidity_source.handle_htlc_intercepted(
1374+
requested_next_hop_scid,
1375+
intercept_id,
1376+
expected_outbound_amount_msat,
1377+
payment_hash,
1378+
);
1379+
}
1380+
},
13451381
LdkEvent::InvoiceReceived { .. } => {
13461382
debug_assert!(false, "We currently don't handle BOLT12 invoices manually, so this event should never be emitted.");
13471383
},

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ impl Node {
533533
Arc::clone(&self.connection_manager),
534534
Arc::clone(&self.output_sweeper),
535535
Arc::clone(&self.network_graph),
536+
self.liquidity_source.clone(),
536537
Arc::clone(&self.payment_store),
537538
Arc::clone(&self.peer_store),
538539
Arc::clone(&self.runtime),

src/liquidity.rs

+55-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ 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;
1820
use lightning::routing::router::{RouteHint, RouteHintHop};
21+
1922
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, InvoiceBuilder, RoutingFees};
23+
2024
use lightning_liquidity::events::Event;
2125
use lightning_liquidity::lsps0::ser::RequestId;
2226
use lightning_liquidity::lsps1::client::LSPS1ClientConfig;
@@ -29,6 +33,8 @@ use lightning_liquidity::lsps2::service::LSPS2ServiceConfig;
2933
use lightning_liquidity::lsps2::utils::compute_opening_fee;
3034
use lightning_liquidity::{LiquidityClientConfig, LiquidityServiceConfig};
3135

36+
use lightning_types::payment::PaymentHash;
37+
3238
use bitcoin::hashes::{sha256, Hash};
3339
use bitcoin::secp256k1::{PublicKey, Secp256k1};
3440

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

898952
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)