Skip to content

Commit 280947c

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 7bc4100 commit 280947c

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
@@ -734,9 +734,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
734734
Ok(funding_address)
735735
}
736736

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

742747
/// Send an on-chain payment to the given address.
@@ -766,16 +771,6 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
766771
self.wallet.send_to_address(address, None)
767772
}
768773

769-
/// Retrieve the currently spendable on-chain balance in satoshis.
770-
pub fn spendable_onchain_balance_sats(&self) -> Result<u64, Error> {
771-
Ok(self.wallet.get_balance().map(|bal| bal.get_spendable())?)
772-
}
773-
774-
/// Retrieve the current total on-chain balance in satoshis.
775-
pub fn total_onchain_balance_sats(&self) -> Result<u64, Error> {
776-
Ok(self.wallet.get_balance().map(|bal| bal.get_total())?)
777-
}
778-
779774
/// Retrieve a list of known channels.
780775
pub fn list_channels(&self) -> Vec<ChannelDetails> {
781776
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);
@@ -115,12 +115,11 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
115115
node_b.sync_wallets().unwrap();
116116

117117
let onchain_fee_buffer_sat = 1500;
118-
let node_a_balance = node_a.onchain_balance().unwrap();
119118
let node_a_upper_bound_sat = premine_amount_sat - funding_amount_sat;
120119
let node_a_lower_bound_sat = premine_amount_sat - funding_amount_sat - onchain_fee_buffer_sat;
121-
assert!(node_a_balance.get_spendable() < node_a_upper_bound_sat);
122-
assert!(node_a_balance.get_spendable() > node_a_lower_bound_sat);
123-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
120+
assert!(node_a.spendable_onchain_balance_sats().unwrap() < node_a_upper_bound_sat);
121+
assert!(node_a.spendable_onchain_balance_sats().unwrap() > node_a_lower_bound_sat);
122+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
124123

125124
expect_event!(node_a, ChannelReady);
126125

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

261260
// Check we handled all events
262261
assert_eq!(node_a.next_event(), None);
@@ -298,8 +297,8 @@ fn channel_open_fails_when_funds_insufficient() {
298297
);
299298
node_a.sync_wallets().unwrap();
300299
node_b.sync_wallets().unwrap();
301-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
302-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), premine_amount_sat);
300+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
301+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), premine_amount_sat);
303302

304303
println!("\nA -- connect_open_channel -> B");
305304
assert_eq!(
@@ -340,13 +339,13 @@ fn start_stop_reinit() {
340339
let expected_amount = Amount::from_sat(100000);
341340

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

345344
node.start().unwrap();
346345
assert_eq!(node.start(), Err(Error::AlreadyRunning));
347346

348347
node.sync_wallets().unwrap();
349-
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
348+
assert_eq!(node.spendable_onchain_balance_sats().unwrap(), expected_amount.to_sat());
350349

351350
let log_file_symlink = format!("{}/logs/ldk_node_latest.log", config.storage_dir_path);
352351
assert!(std::path::Path::new(&log_file_symlink).is_symlink());
@@ -369,13 +368,13 @@ fn start_stop_reinit() {
369368
reinitialized_node.start().unwrap();
370369

371370
assert_eq!(
372-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
371+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
373372
expected_amount.to_sat()
374373
);
375374

376375
reinitialized_node.sync_wallets().unwrap();
377376
assert_eq!(
378-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
377+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
379378
expected_amount.to_sat()
380379
);
381380

@@ -396,13 +395,13 @@ fn start_stop_reinit_fs_store() {
396395
let expected_amount = Amount::from_sat(100000);
397396

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

401400
node.start().unwrap();
402401
assert_eq!(node.start(), Err(Error::AlreadyRunning));
403402

404403
node.sync_wallets().unwrap();
405-
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
404+
assert_eq!(node.spendable_onchain_balance_sats().unwrap(), expected_amount.to_sat());
406405

407406
node.stop().unwrap();
408407
assert_eq!(node.stop(), Err(Error::NotRunning));
@@ -422,13 +421,13 @@ fn start_stop_reinit_fs_store() {
422421
reinitialized_node.start().unwrap();
423422

424423
assert_eq!(
425-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
424+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
426425
expected_amount.to_sat()
427426
);
428427

429428
reinitialized_node.sync_wallets().unwrap();
430429
assert_eq!(
431-
reinitialized_node.onchain_balance().unwrap().get_spendable(),
430+
reinitialized_node.spendable_onchain_balance_sats().unwrap(),
432431
expected_amount.to_sat()
433432
);
434433

@@ -463,7 +462,7 @@ fn onchain_spend_receive() {
463462

464463
node_a.sync_wallets().unwrap();
465464
node_b.sync_wallets().unwrap();
466-
assert_eq!(node_b.onchain_balance().unwrap().get_spendable(), 100000);
465+
assert_eq!(node_b.spendable_onchain_balance_sats().unwrap(), 100000);
467466

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

@@ -474,9 +473,9 @@ fn onchain_spend_receive() {
474473
node_a.sync_wallets().unwrap();
475474
node_b.sync_wallets().unwrap();
476475

477-
assert_eq!(node_a.onchain_balance().unwrap().get_spendable(), 1000);
478-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 98000);
479-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
476+
assert_eq!(node_a.spendable_onchain_balance_sats().unwrap(), 1000);
477+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 98000);
478+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
480479

481480
let addr_b = node_b.new_funding_address().unwrap();
482481
let txid = node_a.send_all_to_onchain_address(&addr_b).unwrap();
@@ -486,9 +485,9 @@ fn onchain_spend_receive() {
486485
node_a.sync_wallets().unwrap();
487486
node_b.sync_wallets().unwrap();
488487

489-
assert_eq!(node_a.onchain_balance().unwrap().get_total(), 0);
490-
assert!(node_b.onchain_balance().unwrap().get_spendable() > 99000);
491-
assert!(node_b.onchain_balance().unwrap().get_spendable() < 100000);
488+
assert_eq!(node_a.total_onchain_balance_sats().unwrap(), 0);
489+
assert!(node_b.spendable_onchain_balance_sats().unwrap() > 99000);
490+
assert!(node_b.spendable_onchain_balance_sats().unwrap() < 100000);
492491
}
493492

494493
#[test]

0 commit comments

Comments
 (0)