Skip to content

Commit c45c3a3

Browse files
committed
Drop Node::onchain_balance from public API
We already have `Node::spendable_onchain_balance` and `Node::total_onchain_balance` exposed, so the general `onchain_balance` is kind of redudant. As it is also not exposed (and not easily exposable) in bindings, we can make the bindings and Rust APIs more homogeneous by dropping the call. Plus, we probably should avoid exposing any BDK types in the public API until BDK 1.0 landed and we made the switch, as many breaking changes will be coming with it.
1 parent adc409b commit c45c3a3

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
736736
Ok(funding_address)
737737
}
738738

739-
/// Retrieve the current on-chain balance.
740-
pub fn onchain_balance(&self) -> Result<bdk::Balance, Error> {
741-
self.wallet.get_balance()
739+
/// Retrieve the currently spendable on-chain balance in satoshis.
740+
pub fn spendable_onchain_balance_sats(&self) -> Result<u64, Error> {
741+
Ok(self.wallet.get_balance().map(|bal| bal.get_spendable())?)
742+
}
743+
744+
/// Retrieve the current total on-chain balance in satoshis.
745+
pub fn total_onchain_balance_sats(&self) -> Result<u64, Error> {
746+
Ok(self.wallet.get_balance().map(|bal| bal.get_total())?)
742747
}
743748

744749
/// Send an on-chain payment to the given address.
@@ -768,16 +773,6 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
768773
self.wallet.send_to_address(address, None)
769774
}
770775

771-
/// Retrieve the currently spendable on-chain balance in satoshis.
772-
pub fn spendable_onchain_balance_sats(&self) -> Result<u64, Error> {
773-
Ok(self.wallet.get_balance().map(|bal| bal.get_spendable())?)
774-
}
775-
776-
/// Retrieve the current total on-chain balance in satoshis.
777-
pub fn total_onchain_balance_sats(&self) -> Result<u64, Error> {
778-
Ok(self.wallet.get_balance().map(|bal| bal.get_total())?)
779-
}
780-
781776
/// Retrieve a list of known channels.
782777
pub fn list_channels(&self) -> Vec<ChannelDetails> {
783778
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()

src/test/functional_tests.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
6969
);
7070
node_a.sync_wallets().unwrap();
7171
node_b.sync_wallets().unwrap();
72-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
73-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
72+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
73+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
7474

7575
// Check we haven't got any events yet
7676
assert_eq!(node_a.next_event(), None);
@@ -116,12 +116,11 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
116116
node_b.sync_wallets().unwrap();
117117

118118
let onchain_fee_buffer_sat = 1500;
119-
let node_a_balance = node_a.onchain_balance().unwrap();
120119
let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
121120
let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
122-
assert!(node_a_balance.get_spendable() < node_a_upper_bound_sat);
123-
assert!(node_a_balance.get_spendable() > node_a_lower_bound_sat);
124-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
121+
assert!(node_a.spendable_onchain_balance_sats().unwrap() < node_a_upper_bound_sat);
122+
assert!(node_a.spendable_onchain_balance_sats().unwrap() > node_a_lower_bound_sat);
123+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
125124

126125
expect_event!(node_a, ChannelReady);
127126

@@ -254,10 +253,10 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
254253
let node_a_upper_bound_sat =
255254
(premine_amount_sat - funding_amount_sat) + (funding_amount_sat - sum_of_all_payments_sat);
256255
let node_a_lower_bound_sat = node_a_upper_bound_sat - onchain_fee_buffer_sat;
257-
assert!(node_a.onchain_balance().unwrap().get_spendable() > node_a_lower_bound_sat);
258-
assert!(node_a.onchain_balance().unwrap().get_spendable() < node_a_upper_bound_sat);
256+
assert!(node_a.spendable_onchain_balance_sats().unwrap() > node_a_lower_bound_sat);
257+
assert!(node_a.spendable_onchain_balance_sats().unwrap() < node_a_upper_bound_sat);
259258
let expected_final_amount_node_b_sat = premine_amount_sat + sum_of_all_payments_sat;
260-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), expected_final_amount_node_b_sat);
259+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), expected_final_amount_node_b_sat);
261260

262261
// Check we handled all events
263262
assert_eq!(node_a.next_event(), None);
@@ -299,8 +298,8 @@ fn channel_open_fails_when_funds_insufficient() {
299298
);
300299
node_a.sync_wallets().unwrap();
301300
node_b.sync_wallets().unwrap();
302-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
303-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
301+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
302+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
304303

305304
println!("\nA -- connect_open_channel -> B");
306305
assert_eq!(
@@ -342,13 +341,13 @@ fn start_stop_reinit() {
342341
let expected_amount = Amount::from_sat(100000);
343342

344343
premine_and_distribute_funds(&bitcoind, &electrsd, vec![funding_address], expected_amount);
345-
assert_eq!(node.onchain_balance().unwrap().get_total(), 0);
344+
assert_eq!(node.total_onchain_balance_sats().unwrap(), 0);
346345

347346
node.start().unwrap();
348347
assert_eq!(node.start(), Err(Error::AlreadyRunning));
349348

350349
node.sync_wallets().unwrap();
351-
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
350+
assert_eq!(node.spendable_onchain_balance_sats().unwrap(), expected_amount.to_sat());
352351

353352
let log_file_symlink = format!("{}/logs/ldk_node_latest.log", config.storage_dir_path);
354353
assert!(std::path::Path::new(&log_file_symlink).is_symlink());
@@ -371,13 +370,13 @@ fn start_stop_reinit() {
371370
reinitialized_node.start().unwrap();
372371

373372
assert_eq!(
374-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
373+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
375374
expected_amount.to_sat()
376375
);
377376

378377
reinitialized_node.sync_wallets().unwrap();
379378
assert_eq!(
380-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
379+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
381380
expected_amount.to_sat()
382381
);
383382

@@ -398,13 +397,13 @@ fn start_stop_reinit_fs_store() {
398397
let expected_amount = Amount::from_sat(100000);
399398

400399
premine_and_distribute_funds(&bitcoind, &electrsd, vec![funding_address], expected_amount);
401-
assert_eq!(node.onchain_balance().unwrap().get_total(), 0);
400+
assert_eq!(node.total_onchain_balance_sats().unwrap(), 0);
402401

403402
node.start().unwrap();
404403
assert_eq!(node.start(), Err(Error::AlreadyRunning));
405404

406405
node.sync_wallets().unwrap();
407-
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
406+
assert_eq!(node.spendable_onchain_balance_sats().unwrap(), expected_amount.to_sat());
408407

409408
node.stop().unwrap();
410409
assert_eq!(node.stop(), Err(Error::NotRunning));
@@ -424,13 +423,13 @@ fn start_stop_reinit_fs_store() {
424423
reinitialized_node.start().unwrap();
425424

426425
assert_eq!(
427-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
426+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
428427
expected_amount.to_sat()
429428
);
430429

431430
reinitialized_node.sync_wallets().unwrap();
432431
assert_eq!(
433-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
432+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
434433
expected_amount.to_sat()
435434
);
436435

@@ -465,7 +464,7 @@ fn onchain_spend_receive() {
465464

466465
node_a.sync_wallets().unwrap();
467466
node_b.sync_wallets().unwrap();
468-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), 100000);
467+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), 100000);
469468

470469
assert_eq!(Err(Error::InsufficientFunds), node_a.send_to_onchain_address(&addr_b, 1000));
471470

@@ -476,9 +475,9 @@ fn onchain_spend_receive() {
476475
node_a.sync_wallets().unwrap();
477476
node_b.sync_wallets().unwrap();
478477

479-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), 1000);
480-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 98000);
481-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
478+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), 1000);
479+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 98000);
480+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
482481

483482
let addr_b = node_b.new_funding_address().unwrap();
484483
let txid = node_a.send_all_to_onchain_address(&addr_b).unwrap();
@@ -488,9 +487,9 @@ fn onchain_spend_receive() {
488487
node_a.sync_wallets().unwrap();
489488
node_b.sync_wallets().unwrap();
490489

491-
assert_eq!(node_a.onchain_balance().unwrap().get_total(), 0);
492-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 99000);
493-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
490+
assert_eq!(node_a.total_onchain_balance_sats().unwrap(), 0);
491+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 99000);
492+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
494493
}
495494

496495
#[test]

0 commit comments

Comments
 (0)