Skip to content

Commit c263bcb

Browse files
committed
Test new transactions_unconfirmed
1 parent 9be5b95 commit c263bcb

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn confirm_transaction_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &T
7777
}
7878

7979
/// An enum describing the possible ways we may notify a ChannelManager of a new block
80+
#[derive(Clone, Copy, PartialEq)]
8081
pub enum ConnectStyle {
8182
/// Calls update_best_block first, detecting transactions in the block only after receiving the
8283
/// header and height information.
@@ -1397,7 +1398,7 @@ pub fn check_preimage_claim<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, prev_txn: &Vec<
13971398
res
13981399
}
13991400

1400-
pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b, 'c>>, a: usize, b: usize) {
1401+
pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b, 'c>>, a: usize, b: usize, needs_err_handle: bool, expected_error: &str) {
14011402
let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
14021403
assert_eq!(events_1.len(), 2);
14031404
let as_update = match events_1[0] {
@@ -1409,25 +1410,30 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b,
14091410
match events_1[1] {
14101411
MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
14111412
assert_eq!(node_id, nodes[b].node.get_our_node_id());
1412-
assert_eq!(msg.data, "Commitment transaction was confirmed on chain.");
1413+
assert_eq!(msg.data, expected_error);
1414+
if needs_err_handle {
1415+
nodes[b].node.handle_error(&nodes[a].node.get_our_node_id(), msg);
1416+
}
14131417
},
14141418
_ => panic!("Unexpected event"),
14151419
}
14161420

14171421
let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
1418-
assert_eq!(events_2.len(), 2);
1422+
assert_eq!(events_2.len(), if needs_err_handle { 1 } else { 2 });
14191423
let bs_update = match events_2[0] {
14201424
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
14211425
msg.clone()
14221426
},
14231427
_ => panic!("Unexpected event"),
14241428
};
1425-
match events_2[1] {
1426-
MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
1427-
assert_eq!(node_id, nodes[a].node.get_our_node_id());
1428-
assert_eq!(msg.data, "Commitment transaction was confirmed on chain.");
1429-
},
1430-
_ => panic!("Unexpected event"),
1429+
if !needs_err_handle {
1430+
match events_2[1] {
1431+
MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
1432+
assert_eq!(node_id, nodes[a].node.get_our_node_id());
1433+
assert_eq!(msg.data, expected_error);
1434+
},
1435+
_ => panic!("Unexpected event"),
1436+
}
14311437
}
14321438

14331439
for node in nodes {
@@ -1436,6 +1442,10 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b,
14361442
}
14371443
}
14381444

1445+
pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b, 'c>>, a: usize, b: usize) {
1446+
handle_announce_close_broadcast_events(nodes, a, b, false, "Commitment transaction was confirmed on chain.");
1447+
}
1448+
14391449
#[cfg(test)]
14401450
macro_rules! get_channel_value_stat {
14411451
($node: expr, $channel_id: expr) => {{

lightning/src/ln/reorg_tests.rs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn test_onchain_htlc_timeout_delay_remote_commitment() {
183183
do_test_onchain_htlc_reorg(false, false);
184184
}
185185

186-
fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, connect_style: ConnectStyle) {
186+
fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_unconfirmed: bool, connect_style: ConnectStyle) {
187187
// After creating a chan between nodes, we disconnect all blocks previously seen to force a
188188
// channel close on nodes[0] side. We also use this to provide very basic testing of logic
189189
// around freeing background events which store monitor updates during block_[dis]connected.
@@ -196,16 +196,28 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, connect_styl
196196
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
197197
*nodes[0].connect_style.borrow_mut() = connect_style;
198198

199-
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
199+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
200200

201201
let channel_state = nodes[0].node.channel_state.lock().unwrap();
202202
assert_eq!(channel_state.by_id.len(), 1);
203203
assert_eq!(channel_state.short_to_id.len(), 1);
204204
mem::drop(channel_state);
205205

206206
if !reorg_after_reload {
207-
disconnect_all_blocks(&nodes[0]);
208-
check_closed_broadcast!(nodes[0], true);
207+
if use_funding_unconfirmed {
208+
let relevant_txids = nodes[0].node.get_relevant_txids();
209+
assert_eq!(relevant_txids.len(), 1);
210+
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
211+
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
212+
} else {
213+
disconnect_all_blocks(&nodes[0]);
214+
}
215+
if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed {
216+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs.");
217+
} else {
218+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
219+
}
220+
check_added_monitors!(nodes[1], 1);
209221
{
210222
let channel_state = nodes[0].node.channel_state.lock().unwrap();
211223
assert_eq!(channel_state.by_id.len(), 0);
@@ -255,8 +267,20 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, connect_styl
255267
}
256268

257269
if reorg_after_reload {
258-
disconnect_all_blocks(&nodes[0]);
259-
check_closed_broadcast!(nodes[0], true);
270+
if use_funding_unconfirmed {
271+
let relevant_txids = nodes[0].node.get_relevant_txids();
272+
assert_eq!(relevant_txids.len(), 1);
273+
assert_eq!(&relevant_txids[..], &[chan.3.txid()]);
274+
nodes[0].node.transaction_unconfirmed(&relevant_txids[0]);
275+
} else {
276+
disconnect_all_blocks(&nodes[0]);
277+
}
278+
if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed {
279+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs.");
280+
} else {
281+
handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs.");
282+
}
283+
check_added_monitors!(nodes[1], 1);
260284
{
261285
let channel_state = nodes[0].node.channel_state.lock().unwrap();
262286
assert_eq!(channel_state.by_id.len(), 0);
@@ -265,25 +289,42 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, connect_styl
265289
}
266290
// With expect_channel_force_closed set the TestChainMonitor will enforce that the next update
267291
// is a ChannelForcClosed on the right channel with should_broadcast set.
268-
*nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan_id, true));
292+
*nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan.2, true));
269293
nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update
270294
check_added_monitors!(nodes[0], 1);
295+
296+
// Now check that we can create a new channel
297+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
298+
send_payment(&nodes[0], &[&nodes[1]], 8000000, 8_000_000);
271299
}
272300

273301
#[test]
274302
fn test_unconf_chan() {
275-
do_test_unconf_chan(true, true, ConnectStyle::BlockSkippingBestBlockFirst);
276-
do_test_unconf_chan(false, true, ConnectStyle::BlockSkippingBestBlockFirst);
277-
do_test_unconf_chan(true, false, ConnectStyle::BlockSkippingBestBlockFirst);
278-
do_test_unconf_chan(false, false, ConnectStyle::BlockSkippingBestBlockFirst);
303+
do_test_unconf_chan(true, true, false, ConnectStyle::BlockSkippingBestBlockFirst);
304+
do_test_unconf_chan(false, true, false, ConnectStyle::BlockSkippingBestBlockFirst);
305+
do_test_unconf_chan(true, false, false, ConnectStyle::BlockSkippingBestBlockFirst);
306+
do_test_unconf_chan(false, false, false, ConnectStyle::BlockSkippingBestBlockFirst);
279307
}
280308

281309
#[test]
282310
fn test_unconf_chan_via_listen() {
283-
do_test_unconf_chan(true, true, ConnectStyle::FullBlockViaListen);
284-
do_test_unconf_chan(false, true, ConnectStyle::FullBlockViaListen);
285-
do_test_unconf_chan(true, false, ConnectStyle::FullBlockViaListen);
286-
do_test_unconf_chan(false, false, ConnectStyle::FullBlockViaListen);
311+
do_test_unconf_chan(true, true, false, ConnectStyle::FullBlockViaListen);
312+
do_test_unconf_chan(false, true, false, ConnectStyle::FullBlockViaListen);
313+
do_test_unconf_chan(true, false, false, ConnectStyle::FullBlockViaListen);
314+
do_test_unconf_chan(false, false, false, ConnectStyle::FullBlockViaListen);
315+
}
316+
317+
#[test]
318+
fn test_unconf_chan_via_funding_unconfirmed() {
319+
do_test_unconf_chan(true, true, true, ConnectStyle::BlockSkippingBestBlockFirst);
320+
do_test_unconf_chan(false, true, true, ConnectStyle::BlockSkippingBestBlockFirst);
321+
do_test_unconf_chan(true, false, true, ConnectStyle::BlockSkippingBestBlockFirst);
322+
do_test_unconf_chan(false, false, true, ConnectStyle::BlockSkippingBestBlockFirst);
323+
324+
do_test_unconf_chan(true, true, true, ConnectStyle::FullBlockViaListen);
325+
do_test_unconf_chan(false, true, true, ConnectStyle::FullBlockViaListen);
326+
do_test_unconf_chan(true, false, true, ConnectStyle::FullBlockViaListen);
327+
do_test_unconf_chan(false, false, true, ConnectStyle::FullBlockViaListen);
287328
}
288329

289330
#[test]

0 commit comments

Comments
 (0)