From 5a5554249ee10c2a2b4e49a026bcd0ae015098a2 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 25 Apr 2022 21:44:28 +0200 Subject: [PATCH] make a more exaustive test Signed-off-by: Vincenzo Palazzo --- lightning/src/ln/functional_test_utils.rs | 40 +++++++++++++++++++ lightning/src/ln/functional_tests.rs | 47 ++++++++++++++++------- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 9bbd6c0e92a..1329edf2d0a 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1190,6 +1190,28 @@ macro_rules! get_route_and_payment_hash { }} } +#[cfg(test)] +#[macro_export] +macro_rules! fail_to_get_route { + ($send_node: expr, $recv_node: expr, $recv_value: expr) => {{ + let payment_params = $crate::routing::router::PaymentParameters::from_node_id($recv_node.node.get_our_node_id()) + .with_features($crate::ln::features::InvoiceFeatures::known()); + $crate::fail_to_get_route!($send_node, $recv_node, payment_params, $recv_value, TEST_FINAL_CLTV) + }}; + ($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr, $cltv: expr) => {{ + use $crate::chain::keysinterface::KeysInterface; + let (_, _, _) = $crate::get_payment_preimage_hash!($recv_node, Some($recv_value)); + let scorer = $crate::util::test_utils::TestScorer::with_penalty(0); + let keys_manager = $crate::util::test_utils::TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet); + let random_seed_bytes = keys_manager.get_secure_random_bytes(); + $crate::routing::router::get_route( + &$send_node.node.get_our_node_id(), &$payment_params, &$send_node.network_graph.read_only(), + Some(&$send_node.node.list_usable_channels().iter().collect::>()), + $recv_value, $cltv, $send_node.logger, &scorer, &random_seed_bytes + ) + }} +} + #[macro_export] /// Clears (and ignores) a PendingHTLCsForwardable event macro_rules! expect_pending_htlcs_forwardable_ignore { @@ -1203,6 +1225,24 @@ macro_rules! expect_pending_htlcs_forwardable_ignore { }} } +#[macro_export] +macro_rules! expect_warning_due_wrong_channel_reestablish { + ($node: expr) => { + for msg in $node.node.get_and_clear_pending_msg_events() { + if let MessageSendEvent::HandleError { ref action, .. } = msg { + match action { + &ErrorAction::SendWarningMessage { ref msg, .. } => { + assert!(msg.data.contains(&"Peer attempted to reestablish channel with a very old local commitment transaction".to_owned())); + }, + _ => panic!("Unexpected event!"), + } + } else { + panic!("Unexpected event") + } + } + } +} + #[macro_export] /// Handles a PendingHTLCsForwardable event macro_rules! expect_pending_htlcs_forwardable { diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 35d3139ec92..ce6f1b04671 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -7282,8 +7282,20 @@ fn test_user_configurable_csv_delay() { } else { assert!(false); } } -#[test] -fn test_data_loss_protect() { +/// describe the test case as a enum, istead as boolean in this case! +/// with the enum there is the possibility to pass more data and +/// check more corner case. +#[derive(Debug)] +enum DataLossProtectTestCase { + /// The node that send the warning message, will try to + /// use the channel, but it can't, because after the + /// warning message we don't change the channel state. + UseChannel, + /// Try to reconnect to the node that have send the warning message + TryToReconnect, +} + +fn do_test_data_loss_protect(case: DataLossProtectTestCase) { // We want to be sure that : // * we don't broadcast our Local Commitment Tx in case of fallen behind // (but this is not quite true - we broadcast during Drop because chanmon is out of sync with chanmgr) @@ -7382,17 +7394,20 @@ fn test_data_loss_protect() { // Check we close channel detecting A is fallen-behind // Check if we sent the warning message when we detecting that A is fallen-behind, // and we give the possibility to A to be able to recover from error. - for msg in nodes[1].node.get_and_clear_pending_msg_events() { - if let MessageSendEvent::HandleError { ref action, .. } = msg { - match action { - &ErrorAction::SendWarningMessage { ref msg, .. } => { - assert!(msg.data.contains(&"Peer attempted to reestablish channel with a very old local commitment transaction".to_owned())); - }, - _ => panic!("Unexpected event!"), - } - } else { - panic!("Unexpected event") - } + expect_warning_due_wrong_channel_reestablish!(nodes[1]); + + // after the waring message sent by B, we should not able to + // use the channel, or reconnect with success to the channel. + match case { + DataLossProtectTestCase::UseChannel => { + assert!(nodes[0].node.list_usable_channels().is_empty()); + }, + DataLossProtectTestCase::TryToReconnect => { + // FIXME: We sent the warning message to give the possibility to restore + // from the error, so we should be able to allow the reconnection here! + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); + expect_warning_due_wrong_channel_reestablish!(nodes[1]); + }, } // Check A is able to claim to_remote output @@ -7406,6 +7421,12 @@ fn test_data_loss_protect() { check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: exp_err.to_string() }); } +#[test] +fn test_data_loss_protect() { + do_test_data_loss_protect(DataLossProtectTestCase::UseChannel); + do_test_data_loss_protect(DataLossProtectTestCase::TryToReconnect); +} + #[test] fn test_check_htlc_underpaying() { // Send payment through A -> B but A is maliciously