Skip to content

Commit 5643d9c

Browse files
amackillopmartinsaposnic
authored andcommitted
wip
1 parent 282b632 commit 5643d9c

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
@@ -519,8 +519,12 @@ where
519519
}
520520
}
521521

522-
pub(crate) async fn handle_next_event(&self) {
523-
match self.liquidity_manager.next_event_async().await {
522+
/// Handles a single liquidity event. Must be called from a context that
523+
/// guarantees the future runs to completion (e.g. a `select!` handler block),
524+
/// since event processing includes `.await` points that are not
525+
/// cancellation-safe.
526+
pub(crate) async fn handle_event(&self, event: LiquidityEvent) {
527+
match event {
524528
LiquidityEvent::LSPS1Client(LSPS1ClientEvent::SupportedOptionsReady {
525529
request_id,
526530
counterparty_node_id,
@@ -881,14 +885,20 @@ where
881885
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
882886
let w = Arc::clone(&self.wallet);
883887
let spendable_amount_sats = tokio::task::spawn_blocking(move || {
884-
w.get_spendable_amount_sats(cur_anchor_reserve_sats)
888+
w.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps2_open_channel")
885889
})
886890
.await
887891
.unwrap_or_else(|e| {
888892
log_error!(self.logger, "Failed to get spendable amount: {}", e);
889893
Err(Error::WalletOperationFailed)
890894
})
891895
.unwrap_or(0);
896+
log_info!(
897+
self.logger,
898+
"LSPS2 OpenChannel wallet check returned spendable_amount_sats={} for peer {}",
899+
spendable_amount_sats,
900+
their_network_key,
901+
);
892902
let required_funds_sats = channel_amount_sats
893903
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
894904
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -1192,6 +1202,14 @@ where
11921202
amt_to_forward_msat,
11931203
channel_count
11941204
}) => {
1205+
log_info!(
1206+
self.logger,
1207+
"Handling LSPS4 OpenChannel for peer {} (amt_to_forward_msat={}, channel_count={}, thread_id={:?})",
1208+
their_network_key,
1209+
amt_to_forward_msat,
1210+
channel_count,
1211+
std::thread::current().id(),
1212+
);
11951213
if self.liquidity_manager.lsps4_service_handler().is_none() {
11961214
log_error!(self.logger, "Failed to handle LSPS4ServiceEvent as LSPS4 liquidity service was not configured.",);
11971215
return;
@@ -1228,6 +1246,11 @@ where
12281246
log_error!(self.logger, "Failed to handle LSPS4ServiceEvent as peer manager isn't available. This should never happen.",);
12291247
return;
12301248
};
1249+
log_info!(
1250+
self.logger,
1251+
"LSPS4 OpenChannel peer {} is connected; computing channel size",
1252+
their_network_key,
1253+
);
12311254

12321255
// Fail if we have insufficient onchain funds available.
12331256
let over_provisioning_msat = (amt_to_forward_msat
@@ -1248,18 +1271,63 @@ where
12481271
channel_amount_sats = channel_amount_sats.max(tier_value_sats);
12491272
}
12501273
}
1274+
log_info!(
1275+
self.logger,
1276+
"LSPS4 OpenChannel peer {} computed channel_amount_sats={} (amt_to_forward_msat={}, over_provisioning_ppm={}, min_channel_size_msat={}, tier_count={})",
1277+
their_network_key,
1278+
channel_amount_sats,
1279+
amt_to_forward_msat,
1280+
service_config.channel_over_provisioning_ppm,
1281+
service_config.min_channel_size_msat,
1282+
service_config.channel_size_tiers.len(),
1283+
);
12511284
let cur_anchor_reserve_sats =
12521285
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
12531286
let w = Arc::clone(&self.wallet);
1254-
let spendable_amount_sats = tokio::task::spawn_blocking(move || {
1255-
w.get_spendable_amount_sats(cur_anchor_reserve_sats)
1287+
let wallet_check_peer = their_network_key;
1288+
log_info!(
1289+
self.logger,
1290+
"LSPS4 OpenChannel spawning wallet check for peer {} (reserve_sats={}, thread_id={:?})",
1291+
wallet_check_peer,
1292+
cur_anchor_reserve_sats,
1293+
std::thread::current().id(),
1294+
);
1295+
let spendable_amount_result = tokio::task::spawn_blocking(move || {
1296+
let blocking_thread_id = format!("{:?}", std::thread::current().id());
1297+
let result = w.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps4_open_channel");
1298+
(blocking_thread_id, result)
12561299
})
1257-
.await
1258-
.unwrap_or_else(|e| {
1300+
.await;
1301+
log_info!(
1302+
self.logger,
1303+
"LSPS4 OpenChannel wallet check join completed for peer {} (join_ok={}, thread_id={:?})",
1304+
their_network_key,
1305+
spendable_amount_result.is_ok(),
1306+
std::thread::current().id(),
1307+
);
1308+
let (blocking_thread_id, spendable_amount_result) = spendable_amount_result
1309+
.unwrap_or_else(|e| {
1310+
log_error!(self.logger, "Failed to get spendable amount: {}", e);
1311+
(format!("{:?}", std::thread::current().id()), Err(Error::WalletOperationFailed))
1312+
});
1313+
log_info!(
1314+
self.logger,
1315+
"LSPS4 OpenChannel wallet check blocking task returned for peer {} (blocking_thread_id={}, result={:?})",
1316+
their_network_key,
1317+
blocking_thread_id,
1318+
spendable_amount_result,
1319+
);
1320+
let spendable_amount_sats = spendable_amount_result.unwrap_or_else(|e| {
12591321
log_error!(self.logger, "Failed to get spendable amount: {}", e);
1260-
Err(Error::WalletOperationFailed)
1322+
0
12611323
})
1262-
.unwrap_or(0);
1324+
;
1325+
log_info!(
1326+
self.logger,
1327+
"LSPS4 OpenChannel wallet check returned spendable_amount_sats={} for peer {}",
1328+
spendable_amount_sats,
1329+
their_network_key,
1330+
);
12631331
let required_funds_sats = channel_amount_sats
12641332
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
12651333
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -1270,16 +1338,32 @@ where
12701338
0
12711339
}
12721340
});
1341+
log_info!(
1342+
self.logger,
1343+
"LSPS4 OpenChannel peer {} computed required_funds_sats={} (channel_amount_sats={}, anchor_reserve_applied={})",
1344+
their_network_key,
1345+
required_funds_sats,
1346+
channel_amount_sats,
1347+
required_funds_sats.saturating_sub(channel_amount_sats),
1348+
);
12731349
if spendable_amount_sats < required_funds_sats {
12741350
log_error!(self.logger,
1275-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1276-
spendable_amount_sats, channel_amount_sats
1351+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats, Peer: {}",
1352+
spendable_amount_sats, required_funds_sats, their_network_key
12771353
);
12781354
// TODO: We just silently fail here. Eventually we will need to remember
12791355
// the pending requests and regularly retry opening the channel until we
12801356
// succeed.
12811357
return;
12821358
}
1359+
log_info!(
1360+
self.logger,
1361+
"LSPS4 OpenChannel peer {} passed funding checks (channel_amount_sats={}, required_funds_sats={}, spendable_amount_sats={})",
1362+
their_network_key,
1363+
channel_amount_sats,
1364+
required_funds_sats,
1365+
spendable_amount_sats,
1366+
);
12831367

12841368
let mut config = self.channel_manager.get_current_config().clone();
12851369

@@ -1303,6 +1387,13 @@ where
13031387

13041388
// TODO: does LSPS4 service need to track this? seems like no?
13051389
let user_channel_id = 0;
1390+
log_info!(
1391+
self.logger,
1392+
"Calling create_channel for LSPS4 peer {} (channel_amount_sats={}, user_channel_id={})",
1393+
their_network_key,
1394+
channel_amount_sats,
1395+
user_channel_id,
1396+
);
13061397

13071398
match self.channel_manager.create_channel(
13081399
their_network_key,
@@ -1312,7 +1403,14 @@ where
13121403
None,
13131404
Some(config),
13141405
) {
1315-
Ok(_) => {},
1406+
Ok(_) => {
1407+
log_info!(
1408+
self.logger,
1409+
"LSPS4 create_channel accepted for peer {} (channel_amount_sats={})",
1410+
their_network_key,
1411+
channel_amount_sats,
1412+
);
1413+
},
13161414
Err(e) => {
13171415
// TODO: We just silently fail here. Eventually we will need to remember
13181416
// the pending requests and regularly retry opening the channel until we
@@ -2082,6 +2180,14 @@ where
20822180
&self, their_network_key: PublicKey, amt_to_forward_msat: u64,
20832181
channel_count: usize,
20842182
) -> Result<(), Error> {
2183+
log_info!(
2184+
self.logger,
2185+
"Entering open_channel_for_lsps4 for peer {} (amt_to_forward_msat={}, channel_count={}, thread_id={:?})",
2186+
their_network_key,
2187+
amt_to_forward_msat,
2188+
channel_count,
2189+
std::thread::current().id(),
2190+
);
20852191
let service_config = if let Some(service_config) =
20862192
self.lsps4_service.as_ref().map(|s| s.service_config.clone())
20872193
{
@@ -2127,10 +2233,32 @@ where
21272233
channel_amount_sats = channel_amount_sats.max(tier_value_sats);
21282234
}
21292235
}
2236+
log_info!(
2237+
self.logger,
2238+
"open_channel_for_lsps4 peer {} computed channel_amount_sats={} (amt_to_forward_msat={}, over_provisioning_ppm={}, min_channel_size_msat={}, tier_count={})",
2239+
their_network_key,
2240+
channel_amount_sats,
2241+
amt_to_forward_msat,
2242+
service_config.channel_over_provisioning_ppm,
2243+
service_config.min_channel_size_msat,
2244+
service_config.channel_size_tiers.len(),
2245+
);
21302246
let cur_anchor_reserve_sats =
21312247
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
2248+
log_info!(
2249+
self.logger,
2250+
"open_channel_for_lsps4 peer {} fetching spendable_amount_sats with reserve_sats={}",
2251+
their_network_key,
2252+
cur_anchor_reserve_sats,
2253+
);
21322254
let spendable_amount_sats =
2133-
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
2255+
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats, "lsps4_fallback").unwrap_or(0);
2256+
log_info!(
2257+
self.logger,
2258+
"open_channel_for_lsps4 peer {} wallet returned spendable_amount_sats={}",
2259+
their_network_key,
2260+
spendable_amount_sats,
2261+
);
21342262
let required_funds_sats = channel_amount_sats
21352263
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
21362264
if init_features.requires_anchors_zero_fee_htlc_tx()
@@ -2141,13 +2269,29 @@ where
21412269
0
21422270
}
21432271
});
2272+
log_info!(
2273+
self.logger,
2274+
"open_channel_for_lsps4 peer {} computed required_funds_sats={} (channel_amount_sats={}, anchor_reserve_applied={})",
2275+
their_network_key,
2276+
required_funds_sats,
2277+
channel_amount_sats,
2278+
required_funds_sats.saturating_sub(channel_amount_sats),
2279+
);
21442280
if spendable_amount_sats < required_funds_sats {
21452281
log_error!(self.logger,
2146-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
2147-
spendable_amount_sats, channel_amount_sats
2282+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats, Peer: {}",
2283+
spendable_amount_sats, required_funds_sats, their_network_key
21482284
);
21492285
return Err(Error::InsufficientFunds);
21502286
}
2287+
log_info!(
2288+
self.logger,
2289+
"open_channel_for_lsps4 peer {} passed funding checks (channel_amount_sats={}, required_funds_sats={}, spendable_amount_sats={})",
2290+
their_network_key,
2291+
channel_amount_sats,
2292+
required_funds_sats,
2293+
spendable_amount_sats,
2294+
);
21512295

21522296
let mut config = self.channel_manager.get_current_config().clone();
21532297
debug_assert_eq!(
@@ -2163,6 +2307,13 @@ where
21632307
config.channel_handshake_config.their_channel_reserve_proportional_millionths = 0;
21642308

21652309
let user_channel_id = 0;
2310+
log_info!(
2311+
self.logger,
2312+
"open_channel_for_lsps4 calling create_channel for peer {} (channel_amount_sats={}, user_channel_id={})",
2313+
their_network_key,
2314+
channel_amount_sats,
2315+
user_channel_id,
2316+
);
21662317

21672318
self.channel_manager
21682319
.create_channel(
@@ -2173,7 +2324,14 @@ where
21732324
None,
21742325
Some(config),
21752326
)
2176-
.map(|_| ())
2327+
.map(|_| {
2328+
log_info!(
2329+
self.logger,
2330+
"open_channel_for_lsps4 create_channel accepted for peer {} (channel_amount_sats={})",
2331+
their_network_key,
2332+
channel_amount_sats,
2333+
);
2334+
})
21772335
.map_err(|e| {
21782336
log_error!(
21792337
self.logger,

0 commit comments

Comments
 (0)