Skip to content

Commit f6d8376

Browse files
committed
wip
1 parent e174f61 commit f6d8376

4 files changed

Lines changed: 219 additions & 25 deletions

File tree

src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ where
12201220
);
12211221
let spendable_amount_sats = self
12221222
.wallet
1223-
.get_spendable_amount_sats(cur_anchor_reserve_sats)
1223+
.get_spendable_amount_sats(cur_anchor_reserve_sats, "anchor_reserve_check")
12241224
.unwrap_or(0);
12251225

12261226
let required_amount_sats = if anchor_channels_config

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ impl Node {
651651
// First tick fires immediately; consume it so we don't run at t=0.
652652
pending_htlc_interval.tick().await;
653653
expiry_check_interval.tick().await;
654+
let lm = liquidity_handler.liquidity_manager();
654655
loop {
655656
tokio::select! {
656657
_ = stop_liquidity_handler.changed() => {
@@ -666,7 +667,9 @@ impl Node {
666667
_ = expiry_check_interval.tick() => {
667668
liquidity_handler.handle_expired_htlcs().await;
668669
}
669-
_ = liquidity_handler.handle_next_event() => {}
670+
event = lm.next_event_async() => {
671+
liquidity_handler.handle_event(event).await;
672+
}
670673
}
671674
}
672675
});
@@ -1173,7 +1176,7 @@ impl Node {
11731176
let cur_anchor_reserve_sats =
11741177
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
11751178
let spendable_amount_sats =
1176-
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
1179+
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats, "check_channel_funds").unwrap_or(0);
11771180

11781181
// Fail early if we have less than the channel value available.
11791182
if spendable_amount_sats < amount_sats {
@@ -1635,7 +1638,7 @@ impl Node {
16351638
let cur_anchor_reserve_sats =
16361639
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
16371640
let (total_onchain_balance_sats, spendable_onchain_balance_sats) =
1638-
self.wallet.get_balances(cur_anchor_reserve_sats).unwrap_or((0, 0));
1641+
self.wallet.get_balances(cur_anchor_reserve_sats, "list_balances").unwrap_or((0, 0));
16391642

16401643
let total_anchor_channels_reserve_sats =
16411644
std::cmp::min(cur_anchor_reserve_sats, total_onchain_balance_sats);

src/liquidity.rs

Lines changed: 174 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,12 @@ where
523523
}
524524
}
525525

526-
pub(crate) async fn handle_next_event(&self) {
527-
match self.liquidity_manager.next_event_async().await {
526+
/// Handles a single liquidity event. Must be called from a context that
527+
/// guarantees the future runs to completion (e.g. a `select!` handler block),
528+
/// since event processing includes `.await` points that are not
529+
/// cancellation-safe.
530+
pub(crate) async fn handle_event(&self, event: LiquidityEvent) {
531+
match event {
528532
LiquidityEvent::LSPS1Client(LSPS1ClientEvent::SupportedOptionsReady {
529533
request_id,
530534
counterparty_node_id,
@@ -885,14 +889,20 @@ where
885889
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
886890
let w = Arc::clone(&self.wallet);
887891
let spendable_amount_sats = tokio::task::spawn_blocking(move || {
888-
w.get_spendable_amount_sats(cur_anchor_reserve_sats)
892+
w.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps2_open_channel")
889893
})
890894
.await
891895
.unwrap_or_else(|e| {
892896
log_error!(self.logger, "Failed to get spendable amount: {}", e);
893897
Err(Error::WalletOperationFailed)
894898
})
895899
.unwrap_or(0);
900+
log_info!(
901+
self.logger,
902+
"LSPS2 OpenChannel wallet check returned spendable_amount_sats={} for peer {}",
903+
spendable_amount_sats,
904+
their_network_key,
905+
);
896906
let required_funds_sats = channel_amount_sats
897907
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
898908
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -1196,6 +1206,14 @@ where
11961206
amt_to_forward_msat,
11971207
channel_count
11981208
}) => {
1209+
log_info!(
1210+
self.logger,
1211+
"Handling LSPS4 OpenChannel for peer {} (amt_to_forward_msat={}, channel_count={}, thread_id={:?})",
1212+
their_network_key,
1213+
amt_to_forward_msat,
1214+
channel_count,
1215+
std::thread::current().id(),
1216+
);
11991217
if self.liquidity_manager.lsps4_service_handler().is_none() {
12001218
log_error!(self.logger, "Failed to handle LSPS4ServiceEvent as LSPS4 liquidity service was not configured.",);
12011219
return;
@@ -1232,6 +1250,11 @@ where
12321250
log_error!(self.logger, "Failed to handle LSPS4ServiceEvent as peer manager isn't available. This should never happen.",);
12331251
return;
12341252
};
1253+
log_info!(
1254+
self.logger,
1255+
"LSPS4 OpenChannel peer {} is connected; computing channel size",
1256+
their_network_key,
1257+
);
12351258

12361259
// Fail if we have insufficient onchain funds available.
12371260
let over_provisioning_msat = (amt_to_forward_msat
@@ -1252,18 +1275,63 @@ where
12521275
channel_amount_sats = channel_amount_sats.max(tier_value_sats);
12531276
}
12541277
}
1278+
log_info!(
1279+
self.logger,
1280+
"LSPS4 OpenChannel peer {} computed channel_amount_sats={} (amt_to_forward_msat={}, over_provisioning_ppm={}, min_channel_size_msat={}, tier_count={})",
1281+
their_network_key,
1282+
channel_amount_sats,
1283+
amt_to_forward_msat,
1284+
service_config.channel_over_provisioning_ppm,
1285+
service_config.min_channel_size_msat,
1286+
service_config.channel_size_tiers.len(),
1287+
);
12551288
let cur_anchor_reserve_sats =
12561289
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
12571290
let w = Arc::clone(&self.wallet);
1258-
let spendable_amount_sats = tokio::task::spawn_blocking(move || {
1259-
w.get_spendable_amount_sats(cur_anchor_reserve_sats)
1291+
let wallet_check_peer = their_network_key;
1292+
log_info!(
1293+
self.logger,
1294+
"LSPS4 OpenChannel spawning wallet check for peer {} (reserve_sats={}, thread_id={:?})",
1295+
wallet_check_peer,
1296+
cur_anchor_reserve_sats,
1297+
std::thread::current().id(),
1298+
);
1299+
let spendable_amount_result = tokio::task::spawn_blocking(move || {
1300+
let blocking_thread_id = format!("{:?}", std::thread::current().id());
1301+
let result = w.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps4_open_channel");
1302+
(blocking_thread_id, result)
12601303
})
1261-
.await
1262-
.unwrap_or_else(|e| {
1304+
.await;
1305+
log_info!(
1306+
self.logger,
1307+
"LSPS4 OpenChannel wallet check join completed for peer {} (join_ok={}, thread_id={:?})",
1308+
their_network_key,
1309+
spendable_amount_result.is_ok(),
1310+
std::thread::current().id(),
1311+
);
1312+
let (blocking_thread_id, spendable_amount_result) = spendable_amount_result
1313+
.unwrap_or_else(|e| {
1314+
log_error!(self.logger, "Failed to get spendable amount: {}", e);
1315+
(format!("{:?}", std::thread::current().id()), Err(Error::WalletOperationFailed))
1316+
});
1317+
log_info!(
1318+
self.logger,
1319+
"LSPS4 OpenChannel wallet check blocking task returned for peer {} (blocking_thread_id={}, result={:?})",
1320+
their_network_key,
1321+
blocking_thread_id,
1322+
spendable_amount_result,
1323+
);
1324+
let spendable_amount_sats = spendable_amount_result.unwrap_or_else(|e| {
12631325
log_error!(self.logger, "Failed to get spendable amount: {}", e);
1264-
Err(Error::WalletOperationFailed)
1326+
0
12651327
})
1266-
.unwrap_or(0);
1328+
;
1329+
log_info!(
1330+
self.logger,
1331+
"LSPS4 OpenChannel wallet check returned spendable_amount_sats={} for peer {}",
1332+
spendable_amount_sats,
1333+
their_network_key,
1334+
);
12671335
let required_funds_sats = channel_amount_sats
12681336
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
12691337
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -1274,16 +1342,32 @@ where
12741342
0
12751343
}
12761344
});
1345+
log_info!(
1346+
self.logger,
1347+
"LSPS4 OpenChannel peer {} computed required_funds_sats={} (channel_amount_sats={}, anchor_reserve_applied={})",
1348+
their_network_key,
1349+
required_funds_sats,
1350+
channel_amount_sats,
1351+
required_funds_sats.saturating_sub(channel_amount_sats),
1352+
);
12771353
if spendable_amount_sats < required_funds_sats {
12781354
log_error!(self.logger,
1279-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1280-
spendable_amount_sats, channel_amount_sats
1355+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats, Peer: {}",
1356+
spendable_amount_sats, required_funds_sats, their_network_key
12811357
);
12821358
// TODO: We just silently fail here. Eventually we will need to remember
12831359
// the pending requests and regularly retry opening the channel until we
12841360
// succeed.
12851361
return;
12861362
}
1363+
log_info!(
1364+
self.logger,
1365+
"LSPS4 OpenChannel peer {} passed funding checks (channel_amount_sats={}, required_funds_sats={}, spendable_amount_sats={})",
1366+
their_network_key,
1367+
channel_amount_sats,
1368+
required_funds_sats,
1369+
spendable_amount_sats,
1370+
);
12871371

12881372
let mut config = self.channel_manager.get_current_config().clone();
12891373

@@ -1307,6 +1391,13 @@ where
13071391

13081392
// TODO: does LSPS4 service need to track this? seems like no?
13091393
let user_channel_id = 0;
1394+
log_info!(
1395+
self.logger,
1396+
"Calling create_channel for LSPS4 peer {} (channel_amount_sats={}, user_channel_id={})",
1397+
their_network_key,
1398+
channel_amount_sats,
1399+
user_channel_id,
1400+
);
13101401

13111402
match self.channel_manager.create_channel(
13121403
their_network_key,
@@ -1316,7 +1407,14 @@ where
13161407
None,
13171408
Some(config),
13181409
) {
1319-
Ok(_) => {},
1410+
Ok(_) => {
1411+
log_info!(
1412+
self.logger,
1413+
"LSPS4 create_channel accepted for peer {} (channel_amount_sats={})",
1414+
their_network_key,
1415+
channel_amount_sats,
1416+
);
1417+
},
13201418
Err(e) => {
13211419
// TODO: We just silently fail here. Eventually we will need to remember
13221420
// the pending requests and regularly retry opening the channel until we
@@ -2107,6 +2205,14 @@ where
21072205
&self, their_network_key: PublicKey, amt_to_forward_msat: u64,
21082206
channel_count: usize,
21092207
) -> Result<(), Error> {
2208+
log_info!(
2209+
self.logger,
2210+
"Entering open_channel_for_lsps4 for peer {} (amt_to_forward_msat={}, channel_count={}, thread_id={:?})",
2211+
their_network_key,
2212+
amt_to_forward_msat,
2213+
channel_count,
2214+
std::thread::current().id(),
2215+
);
21102216
let service_config = if let Some(service_config) =
21112217
self.lsps4_service.as_ref().map(|s| s.service_config.clone())
21122218
{
@@ -2152,10 +2258,32 @@ where
21522258
channel_amount_sats = channel_amount_sats.max(tier_value_sats);
21532259
}
21542260
}
2261+
log_info!(
2262+
self.logger,
2263+
"open_channel_for_lsps4 peer {} computed channel_amount_sats={} (amt_to_forward_msat={}, over_provisioning_ppm={}, min_channel_size_msat={}, tier_count={})",
2264+
their_network_key,
2265+
channel_amount_sats,
2266+
amt_to_forward_msat,
2267+
service_config.channel_over_provisioning_ppm,
2268+
service_config.min_channel_size_msat,
2269+
service_config.channel_size_tiers.len(),
2270+
);
21552271
let cur_anchor_reserve_sats =
21562272
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
2273+
log_info!(
2274+
self.logger,
2275+
"open_channel_for_lsps4 peer {} fetching spendable_amount_sats with reserve_sats={}",
2276+
their_network_key,
2277+
cur_anchor_reserve_sats,
2278+
);
21572279
let spendable_amount_sats =
2158-
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
2280+
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps4_fallback").unwrap_or(0);
2281+
log_info!(
2282+
self.logger,
2283+
"open_channel_for_lsps4 peer {} wallet returned spendable_amount_sats={}",
2284+
their_network_key,
2285+
spendable_amount_sats,
2286+
);
21592287
let required_funds_sats = channel_amount_sats
21602288
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
21612289
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -2166,13 +2294,29 @@ where
21662294
0
21672295
}
21682296
});
2297+
log_info!(
2298+
self.logger,
2299+
"open_channel_for_lsps4 peer {} computed required_funds_sats={} (channel_amount_sats={}, anchor_reserve_applied={})",
2300+
their_network_key,
2301+
required_funds_sats,
2302+
channel_amount_sats,
2303+
required_funds_sats.saturating_sub(channel_amount_sats),
2304+
);
21692305
if spendable_amount_sats < required_funds_sats {
21702306
log_error!(self.logger,
2171-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
2172-
spendable_amount_sats, channel_amount_sats
2307+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats, Peer: {}",
2308+
spendable_amount_sats, required_funds_sats, their_network_key
21732309
);
21742310
return Err(Error::InsufficientFunds);
21752311
}
2312+
log_info!(
2313+
self.logger,
2314+
"open_channel_for_lsps4 peer {} passed funding checks (channel_amount_sats={}, required_funds_sats={}, spendable_amount_sats={})",
2315+
their_network_key,
2316+
channel_amount_sats,
2317+
required_funds_sats,
2318+
spendable_amount_sats,
2319+
);
21762320

21772321
let mut config = self.channel_manager.get_current_config().clone();
21782322
debug_assert_eq!(
@@ -2188,6 +2332,13 @@ where
21882332
config.channel_handshake_config.their_channel_reserve_proportional_millionths = 0;
21892333

21902334
let user_channel_id = 0;
2335+
log_info!(
2336+
self.logger,
2337+
"open_channel_for_lsps4 calling create_channel for peer {} (channel_amount_sats={}, user_channel_id={})",
2338+
their_network_key,
2339+
channel_amount_sats,
2340+
user_channel_id,
2341+
);
21912342

21922343
self.channel_manager
21932344
.create_channel(
@@ -2198,7 +2349,14 @@ where
21982349
None,
21992350
Some(config),
22002351
)
2201-
.map(|_| ())
2352+
.map(|_| {
2353+
log_info!(
2354+
self.logger,
2355+
"open_channel_for_lsps4 create_channel accepted for peer {} (channel_amount_sats={})",
2356+
their_network_key,
2357+
channel_amount_sats,
2358+
);
2359+
})
22022360
.map_err(|e| {
22032361
log_error!(
22042362
self.logger,

0 commit comments

Comments
 (0)