Skip to content

Commit a1af347

Browse files
committed
f Extend checks for trusted_peers_no_reserve
1 parent e72d4a3 commit a1af347

File tree

2 files changed

+88
-50
lines changed

2 files changed

+88
-50
lines changed

tests/common/mod.rs

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ macro_rules! setup_builder {
207207
pub(crate) use setup_builder;
208208

209209
pub(crate) fn setup_two_nodes(
210-
electrsd: &ElectrsD, allow_0conf: bool, anchor_channels: bool,
210+
electrsd: &ElectrsD, allow_0conf: bool, anchor_channels: bool, anchors_trusted_no_reserve: bool,
211211
) -> (TestNode, TestNode) {
212212
println!("== Node A ==");
213213
let config_a = random_config(anchor_channels);
@@ -218,6 +218,14 @@ pub(crate) fn setup_two_nodes(
218218
if allow_0conf {
219219
config_b.trusted_peers_0conf.push(node_a.node_id());
220220
}
221+
if anchor_channels && anchors_trusted_no_reserve {
222+
config_b
223+
.anchor_channels_config
224+
.as_mut()
225+
.unwrap()
226+
.trusted_peers_no_reserve
227+
.push(node_a.node_id());
228+
}
221229
let node_b = setup_node(electrsd, config_b);
222230
(node_a, node_b)
223231
}
@@ -417,20 +425,40 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
417425
node_b.sync_wallets().unwrap();
418426

419427
let onchain_fee_buffer_sat = 1500;
420-
let anchor_reserve_sat = if expect_anchor_channel { 25_000 } else { 0 };
421-
let node_a_upper_bound_sat = premine_amount_sat - anchor_reserve_sat - funding_amount_sat;
422-
let node_a_lower_bound_sat =
423-
premine_amount_sat - anchor_reserve_sat - funding_amount_sat - onchain_fee_buffer_sat;
428+
let node_a_anchor_reserve_sat = if expect_anchor_channel { 25_000 } else { 0 };
429+
let node_a_upper_bound_sat =
430+
premine_amount_sat - node_a_anchor_reserve_sat - funding_amount_sat;
431+
let node_a_lower_bound_sat = premine_amount_sat
432+
- node_a_anchor_reserve_sat
433+
- funding_amount_sat
434+
- onchain_fee_buffer_sat;
424435
assert!(node_a.list_balances().spendable_onchain_balance_sats < node_a_upper_bound_sat);
425436
assert!(node_a.list_balances().spendable_onchain_balance_sats > node_a_lower_bound_sat);
426437
assert_eq!(
427-
node_b.list_balances().spendable_onchain_balance_sats,
428-
premine_amount_sat - anchor_reserve_sat
438+
node_a.list_balances().total_anchor_channels_reserve_sats,
439+
node_a_anchor_reserve_sat
429440
);
430441

431-
expect_channel_ready_event!(node_a, node_b.node_id());
442+
let node_b_anchor_reserve_sat = if node_b
443+
.config()
444+
.anchor_channels_config
445+
.map_or(true, |acc| acc.trusted_peers_no_reserve.contains(&node_a.node_id()))
446+
{
447+
0
448+
} else {
449+
25_000
450+
};
451+
assert_eq!(
452+
node_b.list_balances().spendable_onchain_balance_sats,
453+
premine_amount_sat - node_b_anchor_reserve_sat
454+
);
455+
assert_eq!(
456+
node_b.list_balances().total_anchor_channels_reserve_sats,
457+
node_b_anchor_reserve_sat
458+
);
432459

433-
let user_channel_id = expect_channel_ready_event!(node_b, node_a.node_id());
460+
let user_channel_id = expect_channel_ready_event!(node_a, node_b.node_id());
461+
expect_channel_ready_event!(node_b, node_a.node_id());
434462

435463
println!("\nB receive");
436464
let invoice_amount_1_msat = 2500_000;
@@ -589,9 +617,9 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
589617
println!("\nB close_channel (force: {})", force_close);
590618
if force_close {
591619
std::thread::sleep(Duration::from_secs(1));
592-
node_b.close_channel(&user_channel_id, node_a.node_id(), true).unwrap();
620+
node_a.close_channel(&user_channel_id, node_b.node_id(), true).unwrap();
593621
} else {
594-
node_b.close_channel(&user_channel_id, node_a.node_id(), false).unwrap();
622+
node_a.close_channel(&user_channel_id, node_b.node_id(), false).unwrap();
595623
}
596624

597625
expect_event!(node_a, ChannelClosed);
@@ -604,84 +632,84 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
604632
node_b.sync_wallets().unwrap();
605633

606634
if force_close {
607-
// Check node_a properly sees all balances and sweeps them.
608-
assert_eq!(node_a.list_balances().lightning_balances.len(), 1);
609-
match node_a.list_balances().lightning_balances[0] {
635+
// Check node_b properly sees all balances and sweeps them.
636+
assert_eq!(node_b.list_balances().lightning_balances.len(), 1);
637+
match node_b.list_balances().lightning_balances[0] {
610638
LightningBalance::ClaimableAwaitingConfirmations {
611639
counterparty_node_id,
612640
confirmation_height,
613641
..
614642
} => {
615-
assert_eq!(counterparty_node_id, node_b.node_id());
616-
let cur_height = node_a.status().current_best_block.height;
643+
assert_eq!(counterparty_node_id, node_a.node_id());
644+
let cur_height = node_b.status().current_best_block.height;
617645
let blocks_to_go = confirmation_height - cur_height;
618646
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
619-
node_a.sync_wallets().unwrap();
620647
node_b.sync_wallets().unwrap();
648+
node_a.sync_wallets().unwrap();
621649
},
622650
_ => panic!("Unexpected balance state!"),
623651
}
624652

625-
assert!(node_a.list_balances().lightning_balances.is_empty());
626-
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
627-
match node_a.list_balances().pending_balances_from_channel_closures[0] {
653+
assert!(node_b.list_balances().lightning_balances.is_empty());
654+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
655+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
628656
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
629657
_ => panic!("Unexpected balance state!"),
630658
}
631659
generate_blocks_and_wait(&bitcoind, electrsd, 1);
632-
node_a.sync_wallets().unwrap();
633660
node_b.sync_wallets().unwrap();
661+
node_a.sync_wallets().unwrap();
634662

635-
assert!(node_a.list_balances().lightning_balances.is_empty());
636-
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
637-
match node_a.list_balances().pending_balances_from_channel_closures[0] {
663+
assert!(node_b.list_balances().lightning_balances.is_empty());
664+
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
665+
match node_b.list_balances().pending_balances_from_channel_closures[0] {
638666
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
639667
_ => panic!("Unexpected balance state!"),
640668
}
641669
generate_blocks_and_wait(&bitcoind, electrsd, 5);
642-
node_a.sync_wallets().unwrap();
643670
node_b.sync_wallets().unwrap();
671+
node_a.sync_wallets().unwrap();
644672

645-
assert!(node_a.list_balances().lightning_balances.is_empty());
646-
assert!(node_a.list_balances().pending_balances_from_channel_closures.is_empty());
673+
assert!(node_b.list_balances().lightning_balances.is_empty());
674+
assert!(node_b.list_balances().pending_balances_from_channel_closures.is_empty());
647675

648-
// Check node_b properly sees all balances and sweeps them.
649-
assert_eq!(node_b.list_balances().lightning_balances.len(), 1);
650-
match node_b.list_balances().lightning_balances[0] {
676+
// Check node_a properly sees all balances and sweeps them.
677+
assert_eq!(node_a.list_balances().lightning_balances.len(), 1);
678+
match node_a.list_balances().lightning_balances[0] {
651679
LightningBalance::ClaimableAwaitingConfirmations {
652680
counterparty_node_id,
653681
confirmation_height,
654682
..
655683
} => {
656-
assert_eq!(counterparty_node_id, node_a.node_id());
657-
let cur_height = node_b.status().current_best_block.height;
684+
assert_eq!(counterparty_node_id, node_b.node_id());
685+
let cur_height = node_a.status().current_best_block.height;
658686
let blocks_to_go = confirmation_height - cur_height;
659687
generate_blocks_and_wait(&bitcoind, electrsd, blocks_to_go as usize);
660-
node_b.sync_wallets().unwrap();
661688
node_a.sync_wallets().unwrap();
689+
node_b.sync_wallets().unwrap();
662690
},
663691
_ => panic!("Unexpected balance state!"),
664692
}
665693

666-
assert!(node_b.list_balances().lightning_balances.is_empty());
667-
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
668-
match node_b.list_balances().pending_balances_from_channel_closures[0] {
694+
assert!(node_a.list_balances().lightning_balances.is_empty());
695+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
696+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
669697
PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {},
670698
_ => panic!("Unexpected balance state!"),
671699
}
672700
generate_blocks_and_wait(&bitcoind, electrsd, 1);
673-
node_b.sync_wallets().unwrap();
674701
node_a.sync_wallets().unwrap();
702+
node_b.sync_wallets().unwrap();
675703

676-
assert!(node_b.list_balances().lightning_balances.is_empty());
677-
assert_eq!(node_b.list_balances().pending_balances_from_channel_closures.len(), 1);
678-
match node_b.list_balances().pending_balances_from_channel_closures[0] {
704+
assert!(node_a.list_balances().lightning_balances.is_empty());
705+
assert_eq!(node_a.list_balances().pending_balances_from_channel_closures.len(), 1);
706+
match node_a.list_balances().pending_balances_from_channel_closures[0] {
679707
PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {},
680708
_ => panic!("Unexpected balance state!"),
681709
}
682710
generate_blocks_and_wait(&bitcoind, electrsd, 5);
683-
node_b.sync_wallets().unwrap();
684711
node_a.sync_wallets().unwrap();
712+
node_b.sync_wallets().unwrap();
685713
}
686714

687715
let sum_of_all_payments_sat = (push_msat
@@ -701,6 +729,9 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
701729
assert!(node_b.list_balances().spendable_onchain_balance_sats > node_b_lower_bound_sat);
702730
assert!(node_b.list_balances().spendable_onchain_balance_sats <= node_b_upper_bound_sat);
703731

732+
assert_eq!(node_a.list_balances().total_anchor_channels_reserve_sats, 0);
733+
assert_eq!(node_b.list_balances().total_anchor_channels_reserve_sats, 0);
734+
704735
// Check we handled all events
705736
assert_eq!(node_a.next_event(), None);
706737
assert_eq!(node_b.next_event(), None);

tests/integration_tests_rust.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,42 @@ use crate::common::expect_channel_ready_event;
2222
#[test]
2323
fn channel_full_cycle() {
2424
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
25-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
25+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
2626
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
2727
}
2828

2929
#[test]
3030
fn channel_full_cycle_force_close() {
3131
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
32-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
32+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
33+
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
34+
}
35+
36+
#[test]
37+
fn channel_full_cycle_force_close_trusted_no_reserve() {
38+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
39+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, true);
3340
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
3441
}
3542

3643
#[test]
3744
fn channel_full_cycle_0conf() {
3845
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
39-
let (node_a, node_b) = setup_two_nodes(&electrsd, true, true);
46+
let (node_a, node_b) = setup_two_nodes(&electrsd, true, true, false);
4047
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
4148
}
4249

4350
#[test]
4451
fn channel_full_cycle_legacy_staticremotekey() {
4552
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
46-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false);
53+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false, false);
4754
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false);
4855
}
4956

5057
#[test]
5158
fn channel_open_fails_when_funds_insufficient() {
5259
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
53-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
60+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
5461

5562
let addr_a = node_a.onchain_payment().new_address().unwrap();
5663
let addr_b = node_b.onchain_payment().new_address().unwrap();
@@ -241,7 +248,7 @@ fn start_stop_reinit() {
241248
#[test]
242249
fn onchain_spend_receive() {
243250
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
244-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
251+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
245252

246253
let addr_a = node_a.onchain_payment().new_address().unwrap();
247254
let addr_b = node_b.onchain_payment().new_address().unwrap();
@@ -307,7 +314,7 @@ fn connection_restart_behavior() {
307314

308315
fn do_connection_restart_behavior(persist: bool) {
309316
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
310-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false);
317+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, false, false);
311318

312319
let node_id_a = node_a.node_id();
313320
let node_id_b = node_b.node_id();
@@ -358,7 +365,7 @@ fn do_connection_restart_behavior(persist: bool) {
358365
#[test]
359366
fn concurrent_connections_succeed() {
360367
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
361-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
368+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
362369

363370
let node_a = Arc::new(node_a);
364371
let node_b = Arc::new(node_b);
@@ -388,7 +395,7 @@ fn concurrent_connections_succeed() {
388395
#[test]
389396
fn simple_bolt12_send_receive() {
390397
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
391-
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true);
398+
let (node_a, node_b) = setup_two_nodes(&electrsd, false, true, false);
392399

393400
let address_a = node_a.onchain_payment().new_address().unwrap();
394401
let premine_amount_sat = 5_000_000;

0 commit comments

Comments
 (0)