Skip to content

Commit

Permalink
Use Balances to calculate balances instead of ChannelDetails
Browse files Browse the repository at this point in the history
LDK has deprecated the `ChannelDetails` balance retrieval because
its generally not what users want. Instead, we use the
`ChainMonitor` `Balance` list.
  • Loading branch information
TheBlueMatt committed Oct 14, 2024
1 parent 9e6de1b commit c6bf6ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::disk::{self, INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME};
use crate::hex_utils;
use crate::{
ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, NetworkGraph,
OutboundPaymentInfoStorage, PaymentInfo, PeerManager,
ChainMonitor, ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount,
NetworkGraph, OutboundPaymentInfoStorage, PaymentInfo, PeerManager,
};
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
use bitcoin::network::Network;
use bitcoin::secp256k1::PublicKey;
use lightning::chain::channelmonitor::Balance;
use lightning::ln::bolt11_payment::payment_parameters_from_invoice;
use lightning::ln::bolt11_payment::payment_parameters_from_zero_amount_invoice;
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry};
Expand Down Expand Up @@ -46,8 +47,8 @@ pub(crate) struct LdkUserInfo {

pub(crate) fn poll_for_user_input(
peer_manager: Arc<PeerManager>, channel_manager: Arc<ChannelManager>,
keys_manager: Arc<KeysManager>, network_graph: Arc<NetworkGraph>,
inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
chain_monitor: Arc<ChainMonitor>, keys_manager: Arc<KeysManager>,
network_graph: Arc<NetworkGraph>, inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
outbound_payments: Arc<Mutex<OutboundPaymentInfoStorage>>, ldk_data_dir: String,
network: Network, logger: Arc<disk::FilesystemLogger>, fs_store: Arc<FilesystemStore>,
) {
Expand Down Expand Up @@ -461,7 +462,7 @@ pub(crate) fn poll_for_user_input(

force_close_channel(channel_id, peer_pubkey, channel_manager.clone());
},
"nodeinfo" => node_info(&channel_manager, &peer_manager),
"nodeinfo" => node_info(&channel_manager, &chain_monitor, &peer_manager),
"listpeers" => list_peers(peer_manager.clone()),
"signmessage" => {
const MSG_STARTPOS: usize = "signmessage".len() + 1;
Expand Down Expand Up @@ -515,14 +516,26 @@ fn help() {
println!(" nodeinfo");
}

fn node_info(channel_manager: &Arc<ChannelManager>, peer_manager: &Arc<PeerManager>) {
fn node_info(
channel_manager: &Arc<ChannelManager>, chain_monitor: &Arc<ChainMonitor>,
peer_manager: &Arc<PeerManager>,
) {
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.balance_msat).sum::<u64>();
println!("\t\t local_balance_msat: {}", local_balance_msat);
let balances = chain_monitor.get_claimable_balances(&[]);
let local_balance_sat = balances.iter().map(|b| b.claimable_amount_satoshis()).sum::<u64>();
println!("\t\t local_balance_sat: {}", local_balance_sat);
let close_fees_map = |b| match b {
&Balance::ClaimableOnChannelClose { transaction_fee_satoshis, .. } => {
transaction_fee_satoshis
},
_ => 0,
};
let close_fees_sats = balances.iter().map(close_fees_map).sum::<u64>();
println!("\t\t eventual_close_fees_sat: {}", close_fees_sats);
println!("\t\t num_peers: {}", peer_manager.list_peers().len());
println!("\t}},");
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,15 @@ async fn start_ldk() {

// Start the CLI.
let cli_channel_manager = Arc::clone(&channel_manager);
let cli_chain_monitor = Arc::clone(&chain_monitor);
let cli_persister = Arc::clone(&persister);
let cli_logger = Arc::clone(&logger);
let cli_peer_manager = Arc::clone(&peer_manager);
let cli_poll = tokio::task::spawn_blocking(move || {
cli::poll_for_user_input(
cli_peer_manager,
cli_channel_manager,
cli_chain_monitor,
keys_manager,
network_graph,
inbound_payments,
Expand Down

0 comments on commit c6bf6ab

Please sign in to comment.