Skip to content

Commit

Permalink
make a more exaustive test
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 22, 2022
1 parent 3e41b90 commit c80ea92
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
40 changes: 40 additions & 0 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()),
$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 {
Expand All @@ -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 {
Expand Down
54 changes: 41 additions & 13 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -7382,17 +7394,27 @@ 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 => {
// try to make a payment, but this should not be allowed
// from A -> B
let result = fail_to_get_route!(nodes[0], nodes[1], 1000000);
match result {
Ok(_) => panic!("Unexpected event"),
Err(msgs::LightningError{ ref err, ..}) =>
assert_eq!(err, "Cannot route when there are no outbound routes away from us"),
};
},
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
Expand All @@ -7406,6 +7428,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
Expand Down

0 comments on commit c80ea92

Please sign in to comment.