Skip to content

Commit c80ea92

Browse files
make a more exaustive test
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 3e41b90 commit c80ea92

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

lightning/src/ln/functional_test_utils.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,28 @@ macro_rules! get_route_and_payment_hash {
11901190
}}
11911191
}
11921192

1193+
#[cfg(test)]
1194+
#[macro_export]
1195+
macro_rules! fail_to_get_route {
1196+
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
1197+
let payment_params = $crate::routing::router::PaymentParameters::from_node_id($recv_node.node.get_our_node_id())
1198+
.with_features($crate::ln::features::InvoiceFeatures::known());
1199+
$crate::fail_to_get_route!($send_node, $recv_node, payment_params, $recv_value, TEST_FINAL_CLTV)
1200+
}};
1201+
($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr, $cltv: expr) => {{
1202+
use $crate::chain::keysinterface::KeysInterface;
1203+
let (_, _, _) = $crate::get_payment_preimage_hash!($recv_node, Some($recv_value));
1204+
let scorer = $crate::util::test_utils::TestScorer::with_penalty(0);
1205+
let keys_manager = $crate::util::test_utils::TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
1206+
let random_seed_bytes = keys_manager.get_secure_random_bytes();
1207+
$crate::routing::router::get_route(
1208+
&$send_node.node.get_our_node_id(), &$payment_params, &$send_node.network_graph.read_only(),
1209+
Some(&$send_node.node.list_usable_channels().iter().collect::<Vec<_>>()),
1210+
$recv_value, $cltv, $send_node.logger, &scorer, &random_seed_bytes
1211+
)
1212+
}}
1213+
}
1214+
11931215
#[macro_export]
11941216
/// Clears (and ignores) a PendingHTLCsForwardable event
11951217
macro_rules! expect_pending_htlcs_forwardable_ignore {
@@ -1203,6 +1225,24 @@ macro_rules! expect_pending_htlcs_forwardable_ignore {
12031225
}}
12041226
}
12051227

1228+
#[macro_export]
1229+
macro_rules! expect_warning_due_wrong_channel_reestablish {
1230+
($node: expr) => {
1231+
for msg in $node.node.get_and_clear_pending_msg_events() {
1232+
if let MessageSendEvent::HandleError { ref action, .. } = msg {
1233+
match action {
1234+
&ErrorAction::SendWarningMessage { ref msg, .. } => {
1235+
assert!(msg.data.contains(&"Peer attempted to reestablish channel with a very old local commitment transaction".to_owned()));
1236+
},
1237+
_ => panic!("Unexpected event!"),
1238+
}
1239+
} else {
1240+
panic!("Unexpected event")
1241+
}
1242+
}
1243+
}
1244+
}
1245+
12061246
#[macro_export]
12071247
/// Handles a PendingHTLCsForwardable event
12081248
macro_rules! expect_pending_htlcs_forwardable {

lightning/src/ln/functional_tests.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -7282,8 +7282,20 @@ fn test_user_configurable_csv_delay() {
72827282
} else { assert!(false); }
72837283
}
72847284

7285-
#[test]
7286-
fn test_data_loss_protect() {
7285+
/// describe the test case as a enum, istead as boolean in this case!
7286+
/// with the enum there is the possibility to pass more data and
7287+
/// check more corner case.
7288+
#[derive(Debug)]
7289+
enum DataLossProtectTestCase {
7290+
/// The node that send the warning message, will try to
7291+
/// use the channel, but it can't, because after the
7292+
/// warning message we don't change the channel state.
7293+
UseChannel,
7294+
/// Try to reconnect to the node that have send the warning message
7295+
TryToReconnect,
7296+
}
7297+
7298+
fn do_test_data_loss_protect(case: DataLossProtectTestCase) {
72877299
// We want to be sure that :
72887300
// * we don't broadcast our Local Commitment Tx in case of fallen behind
72897301
// (but this is not quite true - we broadcast during Drop because chanmon is out of sync with chanmgr)
@@ -7382,17 +7394,27 @@ fn test_data_loss_protect() {
73827394
// Check we close channel detecting A is fallen-behind
73837395
// Check if we sent the warning message when we detecting that A is fallen-behind,
73847396
// and we give the possibility to A to be able to recover from error.
7385-
for msg in nodes[1].node.get_and_clear_pending_msg_events() {
7386-
if let MessageSendEvent::HandleError { ref action, .. } = msg {
7387-
match action {
7388-
&ErrorAction::SendWarningMessage { ref msg, .. } => {
7389-
assert!(msg.data.contains(&"Peer attempted to reestablish channel with a very old local commitment transaction".to_owned()));
7390-
},
7391-
_ => panic!("Unexpected event!"),
7392-
}
7393-
} else {
7394-
panic!("Unexpected event")
7395-
}
7397+
expect_warning_due_wrong_channel_reestablish!(nodes[1]);
7398+
7399+
// after the waring message sent by B, we should not able to
7400+
// use the channel, or reconnect with success to the channel.
7401+
match case {
7402+
DataLossProtectTestCase::UseChannel => {
7403+
// try to make a payment, but this should not be allowed
7404+
// from A -> B
7405+
let result = fail_to_get_route!(nodes[0], nodes[1], 1000000);
7406+
match result {
7407+
Ok(_) => panic!("Unexpected event"),
7408+
Err(msgs::LightningError{ ref err, ..}) =>
7409+
assert_eq!(err, "Cannot route when there are no outbound routes away from us"),
7410+
};
7411+
},
7412+
DataLossProtectTestCase::TryToReconnect => {
7413+
// FIXME: We sent the warning message to give the possibility to restore
7414+
// from the error, so we should be able to allow the reconnection here!
7415+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None });
7416+
expect_warning_due_wrong_channel_reestablish!(nodes[1]);
7417+
},
73967418
}
73977419

73987420
// Check A is able to claim to_remote output
@@ -7406,6 +7428,12 @@ fn test_data_loss_protect() {
74067428
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: exp_err.to_string() });
74077429
}
74087430

7431+
#[test]
7432+
fn test_data_loss_protect() {
7433+
do_test_data_loss_protect(DataLossProtectTestCase::UseChannel);
7434+
do_test_data_loss_protect(DataLossProtectTestCase::TryToReconnect);
7435+
}
7436+
74097437
#[test]
74107438
fn test_check_htlc_underpaying() {
74117439
// Send payment through A -> B but A is maliciously

0 commit comments

Comments
 (0)