Skip to content

Commit 76a4206

Browse files
committed
Add LDK event handling
.. and forward it to our `LiquditySource`.
1 parent 3e0f6ee commit 76a4206

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

src/builder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,12 @@ fn build_with_store_internal(
10891089
100;
10901090
}
10911091

1092+
if liquidity_source_config.and_then(|lsc| lsc.lsps2_service.as_ref()).is_some() {
1093+
// If we act as an LSPS2 service, we need to to be able to intercept HTLCs and forward the
1094+
// information to the service handler.
1095+
user_config.accept_intercept_htlcs = true;
1096+
}
1097+
10921098
let message_router =
10931099
Arc::new(MessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager)));
10941100

src/event.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::{
1515
use crate::config::{may_announce_channel, Config};
1616
use crate::connection::ConnectionManager;
1717
use crate::fee_estimator::ConfirmationTarget;
18+
use crate::liquidity::LiquiditySource;
19+
use crate::logger::Logger;
1820

1921
use crate::payment::store::{
2022
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
@@ -446,6 +448,7 @@ where
446448
connection_manager: Arc<ConnectionManager<L>>,
447449
output_sweeper: Arc<Sweeper>,
448450
network_graph: Arc<Graph>,
451+
liquidity_source: Option<Arc<LiquiditySource<Arc<Logger>>>>,
449452
payment_store: Arc<PaymentStore<L>>,
450453
peer_store: Arc<PeerStore<L>>,
451454
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
@@ -462,6 +465,7 @@ where
462465
bump_tx_event_handler: Arc<BumpTransactionEventHandler>,
463466
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
464467
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
468+
liquidity_source: Option<Arc<LiquiditySource<Arc<Logger>>>>,
465469
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
466470
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
467471
) -> Self {
@@ -473,6 +477,7 @@ where
473477
connection_manager,
474478
output_sweeper,
475479
network_graph,
480+
liquidity_source,
476481
payment_store,
477482
peer_store,
478483
logger,
@@ -1013,7 +1018,11 @@ where
10131018
LdkEvent::PaymentPathFailed { .. } => {},
10141019
LdkEvent::ProbeSuccessful { .. } => {},
10151020
LdkEvent::ProbeFailed { .. } => {},
1016-
LdkEvent::HTLCHandlingFailed { .. } => {},
1021+
LdkEvent::HTLCHandlingFailed { failed_next_destination, .. } => {
1022+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1023+
liquidity_source.handle_htlc_handling_failed(failed_next_destination);
1024+
}
1025+
},
10171026
LdkEvent::PendingHTLCsForwardable { time_forwardable } => {
10181027
let forwarding_channel_manager = self.channel_manager.clone();
10191028
let min = time_forwardable.as_millis() as u64;
@@ -1248,6 +1257,10 @@ where
12481257
fee_earned,
12491258
);
12501259
}
1260+
1261+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1262+
liquidity_source.handle_payment_forwarded(next_channel_id);
1263+
}
12511264
},
12521265
LdkEvent::ChannelPending {
12531266
channel_id,
@@ -1321,6 +1334,14 @@ where
13211334
counterparty_node_id,
13221335
);
13231336

1337+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1338+
liquidity_source.handle_channel_ready(
1339+
user_channel_id,
1340+
&channel_id,
1341+
&counterparty_node_id,
1342+
);
1343+
}
1344+
13241345
let event = Event::ChannelReady {
13251346
channel_id,
13261347
user_channel_id: UserChannelId(user_channel_id),
@@ -1359,7 +1380,22 @@ where
13591380
};
13601381
},
13611382
LdkEvent::DiscardFunding { .. } => {},
1362-
LdkEvent::HTLCIntercepted { .. } => {},
1383+
LdkEvent::HTLCIntercepted {
1384+
requested_next_hop_scid,
1385+
intercept_id,
1386+
expected_outbound_amount_msat,
1387+
payment_hash,
1388+
..
1389+
} => {
1390+
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
1391+
liquidity_source.handle_htlc_intercepted(
1392+
requested_next_hop_scid,
1393+
intercept_id,
1394+
expected_outbound_amount_msat,
1395+
payment_hash,
1396+
);
1397+
}
1398+
},
13631399
LdkEvent::InvoiceReceived { .. } => {
13641400
debug_assert!(false, "We currently don't handle BOLT12 invoices manually, so this event should never be emitted.");
13651401
},

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ impl Node {
525525
Arc::clone(&self.connection_manager),
526526
Arc::clone(&self.output_sweeper),
527527
Arc::clone(&self.network_graph),
528+
self.liquidity_source.clone(),
528529
Arc::clone(&self.payment_store),
529530
Arc::clone(&self.peer_store),
530531
Arc::clone(&self.runtime),

src/liquidity.rs

+71-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 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,70 @@ 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!(
964+
self.logger,
965+
"LSPS2 service failed to handle ChannelReady event: {:?}",
966+
e
967+
);
968+
}
969+
}
970+
}
971+
972+
pub(crate) fn handle_htlc_intercepted(
973+
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
974+
payment_hash: PaymentHash,
975+
) {
976+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
977+
if let Err(e) = lsps2_service_handler.htlc_intercepted(
978+
intercept_scid,
979+
intercept_id,
980+
expected_outbound_amount_msat,
981+
payment_hash,
982+
) {
983+
log_error!(
984+
self.logger,
985+
"LSPS2 service failed to handle HTLCIntercepted event: {:?}",
986+
e
987+
);
988+
}
989+
}
990+
}
991+
992+
pub(crate) fn handle_htlc_handling_failed(&self, failed_next_destination: HTLCDestination) {
993+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
994+
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failed_next_destination) {
995+
log_error!(
996+
self.logger,
997+
"LSPS2 service failed to handle HTLCHandlingFailed event: {:?}",
998+
e
999+
);
1000+
}
1001+
}
1002+
}
1003+
1004+
pub(crate) fn handle_payment_forwarded(&self, next_channel_id: Option<ChannelId>) {
1005+
if let Some(next_channel_id) = next_channel_id {
1006+
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
1007+
if let Err(e) = lsps2_service_handler.payment_forwarded(next_channel_id) {
1008+
log_error!(
1009+
self.logger,
1010+
"LSPS2 service failed to handle PaymentForwarded: {:?}",
1011+
e
1012+
);
1013+
}
1014+
}
1015+
}
1016+
}
9471017
}
9481018

9491019
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)