Skip to content

Commit e4fe159

Browse files
committed
NEW: Add test for timing out HTLCs which are in the holding cell
1 parent 75e41b3 commit e4fe159

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,77 @@ fn test_htlc_timeout() {
36783678
do_test_htlc_timeout(false);
36793679
}
36803680

3681+
fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
3682+
// Tests that HTLCs in the holding cell are timed out after the requisite number of blocks.
3683+
let chanmon_cfgs = create_chanmon_cfgs(3);
3684+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3685+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3686+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3687+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::supported(), InitFeatures::supported());
3688+
create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::supported(), InitFeatures::supported());
3689+
3690+
// Route a first payment to get the 1 -> 2 channel in awaiting_raa...
3691+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
3692+
let (_, first_payment_hash) = get_payment_preimage_hash!(nodes[0]);
3693+
nodes[1].node.send_payment(&route, first_payment_hash, &None).unwrap();
3694+
assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
3695+
check_added_monitors!(nodes[1], 1);
3696+
3697+
// Now attempt to route a second payment, which should be placed in the holding cell
3698+
let (_, second_payment_hash) = get_payment_preimage_hash!(nodes[0]);
3699+
if forwarded_htlc {
3700+
let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
3701+
nodes[0].node.send_payment(&route, second_payment_hash, &None).unwrap();
3702+
check_added_monitors!(nodes[0], 1);
3703+
let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
3704+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3705+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3706+
expect_pending_htlcs_forwardable!(nodes[1]);
3707+
check_added_monitors!(nodes[1], 0);
3708+
} else {
3709+
nodes[1].node.send_payment(&route, second_payment_hash, &None).unwrap();
3710+
check_added_monitors!(nodes[1], 0);
3711+
}
3712+
3713+
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3714+
nodes[1].block_notifier.block_connected_checked(&header, 101, &[], &[]);
3715+
for i in 102..TEST_FINAL_CLTV + 100 + 1 - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS {
3716+
header.prev_blockhash = header.bitcoin_hash();
3717+
nodes[1].block_notifier.block_connected_checked(&header, i, &[], &[]);
3718+
}
3719+
3720+
if forwarded_htlc {
3721+
expect_pending_htlcs_forwardable!(nodes[1]);
3722+
check_added_monitors!(nodes[1], 1);
3723+
let fail_commit = nodes[1].node.get_and_clear_pending_msg_events();
3724+
assert_eq!(fail_commit.len(), 1);
3725+
match fail_commit[0] {
3726+
MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, ref commitment_signed, .. }, .. } => {
3727+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3728+
commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, true, true);
3729+
},
3730+
_ => unreachable!(),
3731+
}
3732+
expect_payment_failed!(nodes[0], second_payment_hash, false);
3733+
if let &MessageSendEvent::PaymentFailureNetworkUpdate { ref update } = &nodes[0].node.get_and_clear_pending_msg_events()[0] {
3734+
match update {
3735+
&HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {},
3736+
_ => panic!("Unexpected event"),
3737+
}
3738+
} else {
3739+
panic!("Unexpected event");
3740+
}
3741+
} else {
3742+
expect_payment_failed!(nodes[1], second_payment_hash, true);
3743+
}
3744+
}
3745+
3746+
#[test]
3747+
fn test_holding_cell_htlc_add_timeouts() {
3748+
do_test_holding_cell_htlc_add_timeouts(false);
3749+
do_test_holding_cell_htlc_add_timeouts(true);
3750+
}
3751+
36813752
#[test]
36823753
fn test_invalid_channel_announcement() {
36833754
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs

0 commit comments

Comments
 (0)