Skip to content

Commit cf8c802

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent 07f3833 commit cf8c802

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

src/event.rs

Lines changed: 38 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 55 additions & 1 deletion
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 as LdkLSPS1ClientConfig;
@@ -29,6 +33,8 @@ use lightning_liquidity::lsps2::service::LSPS2ServiceConfig as LdkLSPS2ServiceCo
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

@@ -944,6 +950,54 @@ where
944950
Error::InvoiceCreationFailed
945951
})
946952
}
953+
954+
pub(crate) fn handle_channel_ready(
955+
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
956+
) {
957+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
958+
if let Err(e) = lsps2_service_handler.channel_ready(
959+
user_channel_id,
960+
channel_id,
961+
counterparty_node_id,
962+
) {
963+
log_error!(self.logger, "Errored processing ChannelReady event: {:?}", e);
964+
}
965+
}
966+
}
967+
968+
pub(crate) fn handle_htlc_intercepted(
969+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
970+
payment_hash: PaymentHash,
971+
) {
972+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
973+
if let Err(e) = lsps2_service_handler.htlc_intercepted(
974+
intercept_scid,
975+
intercept_id,
976+
expected_outbound_amount_msat,
977+
payment_hash,
978+
) {
979+
log_error!(self.logger, "Failed to handle HTLCIntercepted event: {:?}", e);
980+
}
981+
}
982+
}
983+
984+
pub(crate) fn handle_htlc_handling_failed(&self, failed_next_destination: HTLCDestination) {
985+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
986+
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failed_next_destination) {
987+
log_error!(self.logger, "Errored processing HTLCHandlingFailed event: {:?}", e);
988+
}
989+
}
990+
}
991+
992+
pub(crate) fn handle_payment_forwarded(&self, next_channel_id: Option<ChannelId>) {
993+
if let Some(next_channel_id) = next_channel_id {
994+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
995+
if let Err(e) = lsps2_service_handler.payment_forwarded(next_channel_id) {
996+
log_error!(self.logger, "Failed to handle PaymentForwarded: {:?}", e);
997+
}
998+
}
999+
}
1000+
}
9471001
}
9481002

9491003
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)