From: Matt Corallo Date: Fri, 25 Mar 2022 23:16:32 +0000 (+0000) Subject: Correct balance printing in cli X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=719ac734966f6e65f883059f40772101d5a255df;p=ldk-sample Correct balance printing in cli The `outbound_capacity_msat + unspendable_punishment_reserve` calculation is fine, as long as the channel's current balance is above the reserve value. If it is not, we'll spuriously report a balance higher than we actually have. This corrects that be havior by using the new(er) `balance_msat` field instead. --- diff --git a/src/cli.rs b/src/cli.rs index c7dffd2..88f8542 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -309,7 +309,7 @@ pub(crate) async fn poll_for_user_input( println!("SUCCESS: connected to peer {}", pubkey); } } - "listchannels" => list_channels(channel_manager.clone()), + "listchannels" => list_channels(&channel_manager), "listpayments" => { list_payments(inbound_payments.clone(), outbound_payments.clone()) } @@ -343,7 +343,7 @@ pub(crate) async fn poll_for_user_input( channel_id.copy_from_slice(&channel_id_vec.unwrap()); force_close_channel(channel_id, channel_manager.clone()); } - "nodeinfo" => node_info(channel_manager.clone(), peer_manager.clone()), + "nodeinfo" => node_info(&channel_manager, &peer_manager), "listpeers" => list_peers(peer_manager.clone()), "signmessage" => { const MSG_STARTPOS: usize = "signmessage".len() + 1; @@ -379,16 +379,13 @@ fn help() { println!("signmessage "); } -fn node_info(channel_manager: Arc, peer_manager: Arc) { +fn node_info(channel_manager: &Arc, peer_manager: &Arc) { println!("\t{{"); println!("\t\t node_pubkey: {}", channel_manager.get_our_node_id()); let chans = channel_manager.list_channels(); println!("\t\t num_channels: {}", chans.len()); println!("\t\t num_usable_channels: {}", chans.iter().filter(|c| c.is_usable).count()); - let local_balance_msat = chans - .iter() - .map(|c| c.unspendable_punishment_reserve.unwrap_or(0) * 1000 + c.outbound_capacity_msat) - .sum::(); + let local_balance_msat = chans.iter().map(|c| c.balance_msat).sum::(); println!("\t\t local_balance_msat: {}", local_balance_msat); println!("\t\t num_peers: {}", peer_manager.get_peer_node_ids().len()); println!("\t}},"); @@ -402,7 +399,7 @@ fn list_peers(peer_manager: Arc) { println!("\t}},"); } -fn list_channels(channel_manager: Arc) { +fn list_channels(channel_manager: &Arc) { print!("["); for chan_info in channel_manager.list_channels() { println!(""); @@ -420,11 +417,7 @@ fn list_channels(channel_manager: Arc) { } println!("\t\tis_confirmed_onchain: {},", chan_info.is_funding_locked); println!("\t\tchannel_value_satoshis: {},", chan_info.channel_value_satoshis); - println!( - "\t\tlocal_balance_msat: {},", - chan_info.outbound_capacity_msat - + chan_info.unspendable_punishment_reserve.unwrap_or(0) * 1000 - ); + println!("\t\tlocal_balance_msat: {},", chan_info.balance_msat); if chan_info.is_usable { println!("\t\tavailable_balance_for_send_msat: {},", chan_info.outbound_capacity_msat); println!("\t\tavailable_balance_for_recv_msat: {},", chan_info.inbound_capacity_msat);