X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Ffunctional_test_utils.rs;h=8f9c206f108c66c6c23bb0fc006ecc13caa20532;hb=55da9c434eeb6f108463e1c8d78914c759f43999;hp=207c0692031a7c89b9f2f6c75da9fc71997abcc3;hpb=bf9144039cccb30d269c899f69de6a0c366f041f;p=rust-lightning diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 207c0692..8f9c206f 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -15,7 +15,7 @@ use crate::sign::EntropySource; use crate::chain::channelmonitor::ChannelMonitor; use crate::chain::transaction::OutPoint; use crate::events::{ClaimedHTLC, ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason}; -use crate::events::bump_transaction::{BumpTransactionEventHandler, Wallet, WalletSource}; +use crate::events::bump_transaction::{BumpTransactionEvent, BumpTransactionEventHandler, Wallet, WalletSource}; use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::channelmanager::{AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA}; use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate}; @@ -665,6 +665,12 @@ pub fn get_err_msg(node: &Node, recipient: &PublicKey) -> msgs::ErrorMessage { assert_eq!(node_id, recipient); (*msg).clone() }, + MessageSendEvent::HandleError { + action: msgs::ErrorAction::DisconnectPeer { ref msg }, ref node_id + } => { + assert_eq!(node_id, recipient); + msg.as_ref().unwrap().clone() + }, _ => panic!("Unexpected event"), } } @@ -1446,10 +1452,15 @@ pub fn check_closed_broadcast(node: &Node, num_channels: usize, with_error_msg: assert_eq!(msg.contents.flags & 2, 2); None }, - MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => { + MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage { msg }, node_id: _ } => { assert!(with_error_msg); // TODO: Check node_id - Some(msg.clone()) + Some(msg) + }, + MessageSendEvent::HandleError { action: msgs::ErrorAction::DisconnectPeer { msg }, node_id: _ } => { + assert!(with_error_msg); + // TODO: Check node_id + Some(msg.unwrap()) }, _ => panic!("Unexpected event"), } @@ -1467,27 +1478,61 @@ macro_rules! check_closed_broadcast { } } +#[derive(Default)] +pub struct ExpectedCloseEvent { + pub channel_capacity_sats: Option, + pub channel_id: Option, + pub counterparty_node_id: Option, + pub discard_funding: bool, + pub reason: Option, +} + +/// Check that multiple channel closing events have been issued. +pub fn check_closed_events(node: &Node, expected_close_events: &[ExpectedCloseEvent]) { + let closed_events_count = expected_close_events.len(); + let discard_events_count = expected_close_events.iter().filter(|e| e.discard_funding).count(); + let events = node.node.get_and_clear_pending_events(); + assert_eq!(events.len(), closed_events_count + discard_events_count, "{:?}", events); + for expected_event in expected_close_events { + assert!(events.iter().any(|e| matches!( + e, + Event::ChannelClosed { + channel_id, + reason, + counterparty_node_id, + channel_capacity_sats, + .. + } if ( + expected_event.channel_id.map(|expected| *channel_id == expected).unwrap_or(true) && + expected_event.reason.as_ref().map(|expected| reason == expected).unwrap_or(true) && + expected_event.counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) && + expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true) + ) + ))); + } + assert_eq!(events.iter().filter(|e| matches!( + e, + Event::DiscardFunding { .. }, + )).count(), discard_events_count); +} + /// Check that a channel's closing channel events has been issued pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: ClosureReason, is_check_discard_funding: bool, expected_counterparty_node_ids: &[PublicKey], expected_channel_capacity: u64) { - let events = node.node.get_and_clear_pending_events(); - assert_eq!(events.len(), events_count, "{:?}", events); - let mut issues_discard_funding = false; - for event in events { - match event { - Event::ChannelClosed { ref reason, counterparty_node_id, - channel_capacity_sats, .. } => { - assert_eq!(*reason, expected_reason); - assert!(expected_counterparty_node_ids.iter().any(|id| id == &counterparty_node_id.unwrap())); - assert_eq!(channel_capacity_sats.unwrap(), expected_channel_capacity); - }, - Event::DiscardFunding { .. } => { - issues_discard_funding = true; - } - _ => panic!("Unexpected event"), - } - } - assert_eq!(is_check_discard_funding, issues_discard_funding); + let expected_events_count = if is_check_discard_funding { + 2 * expected_counterparty_node_ids.len() + } else { + expected_counterparty_node_ids.len() + }; + assert_eq!(events_count, expected_events_count); + let expected_close_events = expected_counterparty_node_ids.iter().map(|node_id| ExpectedCloseEvent { + channel_capacity_sats: Some(expected_channel_capacity), + channel_id: None, + counterparty_node_id: Some(*node_id), + discard_funding: is_check_discard_funding, + reason: Some(expected_reason.clone()), + }).collect::>(); + check_closed_events(node, expected_close_events.as_slice()); } /// Check that a channel's closing channel events has been issued @@ -1504,6 +1549,21 @@ macro_rules! check_closed_event { } } +pub fn handle_bump_htlc_event(node: &Node, count: usize) { + let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events(); + assert_eq!(events.len(), count); + for event in events { + match event { + Event::BumpTransaction(bump_event) => { + if let BumpTransactionEvent::HTLCResolution { .. } = &bump_event {} + else { panic!(); } + node.bump_tx_handler.handle_event(&bump_event); + }, + _ => panic!(), + } + } +} + pub fn close_channel<'a, 'b, 'c>(outbound_node: &Node<'a, 'b, 'c>, inbound_node: &Node<'a, 'b, 'c>, channel_id: &ChannelId, funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) { let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) }; let (node_b, broadcaster_b, struct_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) } else { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) }; @@ -1878,7 +1938,7 @@ macro_rules! get_route { macro_rules! get_route_and_payment_hash { ($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(), TEST_FINAL_CLTV) - .with_bolt11_features($recv_node.node.invoice_features()).unwrap(); + .with_bolt11_features($recv_node.node.bolt11_invoice_features()).unwrap(); $crate::get_route_and_payment_hash!($send_node, $recv_node, payment_params, $recv_value) }}; ($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr) => {{ @@ -2491,7 +2551,7 @@ pub const TEST_FINAL_CLTV: u32 = 70; pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret, PaymentId) { let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV) - .with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap(); + .with_bolt11_features(expected_route.last().unwrap().node.bolt11_invoice_features()).unwrap(); let route_params = RouteParameters::from_payment_params_and_value(payment_params, recv_value); let route = get_route(origin_node, &route_params).unwrap(); assert_eq!(route.paths.len(), 1); @@ -2506,7 +2566,7 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) { let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV) - .with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap(); + .with_bolt11_features(expected_route.last().unwrap().node.bolt11_invoice_features()).unwrap(); let route_params = RouteParameters::from_payment_params_and_value(payment_params, recv_value); let network_graph = origin_node.network_graph.read_only(); let scorer = test_utils::TestScorer::new(); @@ -2780,7 +2840,8 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec(nodes: &Vec { + assert_eq!(node_id, nodes[b].node.get_our_node_id()); + assert_eq!(msg.as_ref().unwrap().data, expected_error); + if needs_err_handle { + nodes[b].node.handle_error(&nodes[a].node.get_our_node_id(), msg.as_ref().unwrap()); + } + }, _ => panic!("Unexpected event"), } @@ -2922,6 +2990,10 @@ pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec { + assert_eq!(node_id, nodes[a].node.get_our_node_id()); + assert_eq!(msg.as_ref().unwrap().data, expected_error); + }, _ => panic!("Unexpected event"), } }