X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fshutdown_tests.rs;h=12421ce9c3f6323e31747173d4064572f5ae1198;hb=bfff6fda6e2e6183285be59a999d5b74feb08779;hp=61d2233ef48ba18dba6a15e95d405bde2cb47e49;hpb=e594021052e251659e33c0f5e82c7ec2b9e99c18;p=rust-lightning diff --git a/lightning/src/ln/shutdown_tests.rs b/lightning/src/ln/shutdown_tests.rs index 61d2233e..12421ce9 100644 --- a/lightning/src/ln/shutdown_tests.rs +++ b/lightning/src/ln/shutdown_tests.rs @@ -16,7 +16,8 @@ use crate::chain::transaction::OutPoint; use crate::events::{Event, MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason}; use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, Retry, ChannelShutdownState, ChannelDetails}; use crate::routing::router::{PaymentParameters, get_route, RouteParameters}; -use crate::ln::{ChannelId, msgs}; +use crate::ln::msgs; +use crate::ln::types::ChannelId; use crate::ln::msgs::{ChannelMessageHandler, ErrorAction}; use crate::ln::onion_utils::INVALID_ONION_BLINDING; use crate::ln::script::ShutdownScript; @@ -25,6 +26,7 @@ use crate::util::test_utils::OnGetShutdownScriptpubkey; use crate::util::errors::APIError; use crate::util::config::UserConfig; use crate::util::string::UntrustedString; +use crate::prelude::*; use bitcoin::{Transaction, TxOut}; use bitcoin::blockdata::locktime::absolute::LockTime; @@ -33,11 +35,6 @@ use bitcoin::blockdata::opcodes; use bitcoin::network::constants::Network; use bitcoin::address::{WitnessProgram, WitnessVersion}; -use regex; - -use core::default::Default; -use std::convert::TryFrom; - use crate::ln::functional_test_utils::*; #[test] @@ -275,7 +272,7 @@ fn shutdown_on_unfunded_channel() { .into_script(); nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &msgs::Shutdown { - channel_id: open_chan.temporary_channel_id, scriptpubkey: script, + channel_id: open_chan.common_fields.temporary_channel_id, scriptpubkey: script, }); check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyCoopClosedUnfundedChannel, [nodes[1].node.get_our_node_id()], 1_000_000); } @@ -437,7 +434,7 @@ fn do_htlc_fail_async_shutdown(blinded_recipient: bool) { let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None); let route_params = if blinded_recipient { crate::ln::blinded_payment_tests::get_blinded_route_parameters( - amt_msat, our_payment_secret, + amt_msat, our_payment_secret, 1, 100000000, nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_2.0.contents], &chanmon_cfgs[2].keys_manager) } else { @@ -831,7 +828,7 @@ fn test_unsupported_anysegwit_upfront_shutdown_script() { // Check script when handling an open_channel message nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap(); let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); - open_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone()); + open_channel.common_fields.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone()); nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); let events = nodes[1].node.get_and_clear_pending_msg_events(); @@ -856,7 +853,7 @@ fn test_unsupported_anysegwit_upfront_shutdown_script() { let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()); - accept_channel.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone()); + accept_channel.common_fields.shutdown_scriptpubkey = Some(anysegwit_shutdown_script.clone()); nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel); let events = nodes[0].node.get_and_clear_pending_msg_events(); @@ -883,7 +880,7 @@ fn test_invalid_upfront_shutdown_script() { // Use a segwit v0 script with an unsupported witness program let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); - open_channel.shutdown_scriptpubkey = Some(Builder::new().push_int(0) + open_channel.common_fields.shutdown_scriptpubkey = Some(Builder::new().push_int(0) .push_slice(&[0, 0]) .into_script()); nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); @@ -1405,8 +1402,8 @@ fn batch_funding_failure() { let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]); let nodes = create_network(4, &node_cfgs, &node_chanmgrs); - exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0); - exchange_open_accept_chan(&nodes[0], &nodes[2], 1_000_000, 0); + let temp_chan_id_a = exchange_open_accept_chan(&nodes[0], &nodes[1], 1_000_000, 0); + let temp_chan_id_b = exchange_open_accept_chan(&nodes[0], &nodes[2], 1_000_000, 0); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 2); @@ -1423,14 +1420,44 @@ fn batch_funding_failure() { } else { panic!(); } } - // We should probably end up with an error for both channels, but currently we don't generate - // an error for the failing channel itself. let err = "Error in transaction funding: Misuse error: No output matched the script_pubkey and value in the FundingGenerationReady event".to_string(); - let close = [ExpectedCloseEvent::from_id_reason(ChannelId::v1_from_funding_txid(tx.txid().as_ref(), 0), true, ClosureReason::ProcessingError { err })]; + let temp_err = "No output matched the script_pubkey and value in the FundingGenerationReady event".to_string(); + let post_funding_chan_id_a = ChannelId::v1_from_funding_txid(tx.txid().as_ref(), 0); + let close = [ + ExpectedCloseEvent::from_id_reason(post_funding_chan_id_a, true, ClosureReason::ProcessingError { err: err.clone() }), + ExpectedCloseEvent::from_id_reason(temp_chan_id_b, false, ClosureReason::ProcessingError { err: temp_err }), + ]; nodes[0].node.batch_funding_transaction_generated(&chans, tx).unwrap_err(); - get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()); + let msgs = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(msgs.len(), 3); + // We currently spuriously send `FundingCreated` for the first channel and then immediately + // fail both channels, which isn't ideal but should be fine. + assert!(msgs.iter().any(|msg| { + if let MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage { + msg: msgs::ErrorMessage { channel_id, .. }, .. + }, .. } = msg { + *channel_id == temp_chan_id_b + } else { false } + })); + let funding_created_pos = msgs.iter().position(|msg| { + if let MessageSendEvent::SendFundingCreated { msg: msgs::FundingCreated { temporary_channel_id, .. }, .. } = msg { + assert_eq!(*temporary_channel_id, temp_chan_id_a); + true + } else { false } + }).unwrap(); + let funded_channel_close_pos = msgs.iter().position(|msg| { + if let MessageSendEvent::HandleError { action: msgs::ErrorAction::SendErrorMessage { + msg: msgs::ErrorMessage { channel_id, .. }, .. + }, .. } = msg { + *channel_id == post_funding_chan_id_a + } else { false } + }).unwrap(); + + // The error message uses the funded channel_id so must come after the funding_created + assert!(funded_channel_close_pos > funding_created_pos); + check_closed_events(&nodes[0], &close); assert_eq!(nodes[0].node.list_channels().len(), 0); }