diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 76a7cfc9c4c..27363a118fa 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -1022,6 +1022,15 @@ mod tests { nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap(); let commitment_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap(); confirm_transaction_depth(&mut nodes[0], &commitment_tx, BREAKDOWN_TIMEOUT as u32); + + let event = receiver + .recv_timeout(Duration::from_secs(EVENT_DEADLINE)) + .expect("ChannelReady not handled within deadline"); + match event { + Event::ChannelReady{ .. } => {}, + _ => panic!("Unexpected event: {:?}", event), + } + let event = receiver .recv_timeout(Duration::from_secs(EVENT_DEADLINE)) .expect("SpendableOutputs not handled within deadline"); diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 8b45d5805c9..7689f44e92e 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -869,6 +869,9 @@ mod test { get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); + + expect_channel_ready_event!(nodes[0], nodes[2].node.get_our_node_id()); + expect_channel_ready_event!(nodes[2], nodes[0].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second // channel, the channel will never be assigned any `counterparty.forwarding_info`. @@ -1257,6 +1260,8 @@ mod test { get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id()); nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[3].node.get_our_node_id()); + expect_channel_ready_event!(nodes[3], nodes[1].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third // channel, the channel will never be assigned any `counterparty.forwarding_info`. diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index a78cf50acb9..ae094c50820 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -1915,6 +1915,8 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: node.gossip_sync.handle_channel_update(&as_update).unwrap(); node.gossip_sync.handle_channel_update(&bs_update).unwrap(); } + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); send_payment(&nodes[0], &[&nodes[1]], 8000000); close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true); diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 6b26143c920..14163e769c4 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -244,9 +244,9 @@ enum HTLCUpdateAwaitingACK { /// There are a few "states" and then a number of flags which can be applied: /// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent. /// TheirChannelReady and OurChannelReady then get set on FundingSent, and when both are set we -/// move on to ChannelFunded. -/// Note that PeerDisconnected can be set on both ChannelFunded and FundingSent. -/// ChannelFunded can then get all remaining flags set on it, until we finish shutdown, then we +/// move on to ChannelReady. +/// Note that PeerDisconnected can be set on both ChannelReady and FundingSent. +/// ChannelReady can then get all remaining flags set on it, until we finish shutdown, then we /// move on to ShutdownComplete, at which point most calls into this channel are disallowed. enum ChannelState { /// Implies we have (or are prepared to) send our open_channel/accept_channel message @@ -262,17 +262,17 @@ enum ChannelState { /// and our counterparty consider the funding transaction confirmed. FundingSent = 8, /// Flag which can be set on FundingSent to indicate they sent us a channel_ready message. - /// Once both TheirChannelReady and OurChannelReady are set, state moves on to ChannelFunded. + /// Once both TheirChannelReady and OurChannelReady are set, state moves on to ChannelReady. TheirChannelReady = 1 << 4, /// Flag which can be set on FundingSent to indicate we sent them a channel_ready message. - /// Once both TheirChannelReady and OurChannelReady are set, state moves on to ChannelFunded. + /// Once both TheirChannelReady and OurChannelReady are set, state moves on to ChannelReady. OurChannelReady = 1 << 5, - ChannelFunded = 64, - /// Flag which is set on ChannelFunded and FundingSent indicating remote side is considered + ChannelReady = 64, + /// Flag which is set on ChannelReady and FundingSent indicating remote side is considered /// "disconnected" and no updates are allowed until after we've done a channel_reestablish /// dance. PeerDisconnected = 1 << 7, - /// Flag which is set on ChannelFunded, FundingCreated, and FundingSent indicating the user has + /// Flag which is set on ChannelReady, FundingCreated, and FundingSent indicating the user has /// told us a ChannelMonitor update is pending async persistence somewhere and we should pause /// sending any outbound messages until they've managed to finish. MonitorUpdateInProgress = 1 << 8, @@ -281,13 +281,13 @@ enum ChannelState { /// messages as then we will be unable to determine which HTLCs they included in their /// revoke_and_ack implicit ACK, so instead we have to hold them away temporarily to be sent /// later. - /// Flag is set on ChannelFunded. + /// Flag is set on ChannelReady. AwaitingRemoteRevoke = 1 << 9, - /// Flag which is set on ChannelFunded or FundingSent after receiving a shutdown message from + /// Flag which is set on ChannelReady or FundingSent after receiving a shutdown message from /// the remote end. If set, they may not add any new HTLCs to the channel, and we are expected /// to respond with our own shutdown message when possible. RemoteShutdownSent = 1 << 10, - /// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this + /// Flag which is set on ChannelReady or FundingSent after sending a shutdown message. At this /// point, we may not add any new HTLCs to the channel. LocalShutdownSent = 1 << 11, /// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about @@ -1793,11 +1793,11 @@ impl Channel { } fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> UpdateFulfillFetch where L::Target: Logger { - // Either ChannelFunded got set (which means it won't be unset) or there is no way any + // Either ChannelReady got set (which means it won't be unset) or there is no way any // caller thought we could have something claimed (cause we wouldn't have accepted in an // incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us, // either. - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { panic!("Was asked to fulfill an HTLC when channel was not in an operational state"); } assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0); @@ -1940,7 +1940,7 @@ impl Channel { /// If we do fail twice, we debug_assert!(false) and return Ok(None). Thus, will always return /// Ok(_) if debug assertions are turned on or preconditions are met. pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L) -> Result, ChannelError> where L::Target: Logger { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { panic!("Was asked to fail an HTLC when channel was not in an operational state"); } assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0); @@ -2370,9 +2370,9 @@ impl Channel { if non_shutdown_state == ChannelState::FundingSent as u32 { self.channel_state |= ChannelState::TheirChannelReady as u32; } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) { - self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS); + self.channel_state = ChannelState::ChannelReady as u32 | (self.channel_state & MULTI_STATE_FLAGS); self.update_time_counter += 1; - } else if self.channel_state & (ChannelState::ChannelFunded as u32) != 0 || + } else if self.channel_state & (ChannelState::ChannelReady as u32) != 0 || // If we reconnected before sending our `channel_ready` they may still resend theirs: (self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) == (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32)) @@ -2733,12 +2733,12 @@ impl Channel { pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, mut pending_forward_status: PendingHTLCStatus, create_pending_htlc_status: F, logger: &L) -> Result<(), ChannelError> where F: for<'a> Fn(&'a Self, PendingHTLCStatus, u16) -> PendingHTLCStatus, L::Target: Logger { // We can't accept HTLCs sent after we've sent a shutdown. - let local_sent_shutdown = (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::LocalShutdownSent as u32)) != (ChannelState::ChannelFunded as u32); + let local_sent_shutdown = (self.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::LocalShutdownSent as u32)) != (ChannelState::ChannelReady as u32); if local_sent_shutdown { pending_forward_status = create_pending_htlc_status(self, pending_forward_status, 0x4000|8); } // If the remote has sent a shutdown prior to adding this HTLC, then they are in violation of the spec. - let remote_sent_shutdown = (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32); + let remote_sent_shutdown = (self.channel_state & (ChannelState::ChannelReady as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelReady as u32); if remote_sent_shutdown { return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state".to_owned())); } @@ -2915,7 +2915,7 @@ impl Channel { } pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64), ChannelError> { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state".to_owned())); } if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { @@ -2926,7 +2926,7 @@ impl Channel { } pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state".to_owned())); } if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { @@ -2938,7 +2938,7 @@ impl Channel { } pub fn update_fail_malformed_htlc(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state".to_owned())); } if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { @@ -2952,7 +2952,7 @@ impl Channel { pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<(msgs::RevokeAndACK, Option, ChannelMonitorUpdate), (Option, ChannelError)> where L::Target: Logger { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { return Err((None, ChannelError::Close("Got commitment signed message when channel was not in an operational state".to_owned()))); } if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { @@ -3145,7 +3145,7 @@ impl Channel { /// If we're not in a state where freeing the holding cell makes sense, this is a no-op and /// returns `(None, Vec::new())`. pub fn maybe_free_holding_cell_htlcs(&mut self, logger: &L) -> Result<(Option<(msgs::CommitmentUpdate, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>), ChannelError> where L::Target: Logger { - if self.channel_state >= ChannelState::ChannelFunded as u32 && + if self.channel_state >= ChannelState::ChannelReady as u32 && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 { self.free_holding_cell_htlcs(logger) } else { Ok((None, Vec::new())) } @@ -3273,7 +3273,7 @@ impl Channel { pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result where L::Target: Logger, { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state".to_owned())); } if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 { @@ -3717,7 +3717,7 @@ impl Channel { } else { None }; // That said, if the funding transaction is already confirmed (ie we're active with a // minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx. - if self.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelFunded as u32 && self.minimum_depth != Some(0) { + if self.channel_state & !MULTI_STATE_FLAGS >= ChannelState::ChannelReady as u32 && self.minimum_depth != Some(0) { funding_broadcastable = None; } @@ -4621,12 +4621,11 @@ impl Channel { // Checks whether we previously had emitted a `ChannelReady` event and sets the value if not. pub(crate) fn should_emit_channel_ready_event(&mut self) -> bool { - if self.channel_ready_event_emitted { - // Do nothing, already emitted. - false - } else { + if self.is_usable() && !self.channel_ready_event_emitted { self.channel_ready_event_emitted = true; true + } else { + false } } @@ -4798,8 +4797,8 @@ impl Channel { /// Returns true if this channel is fully established and not known to be closing. /// Allowed in any state (including after shutdown) pub fn is_usable(&self) -> bool { - let mask = ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK; - (self.channel_state & mask) == (ChannelState::ChannelFunded as u32) && !self.monitor_pending_channel_ready + let mask = ChannelState::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK; + (self.channel_state & mask) == (ChannelState::ChannelReady as u32) && !self.monitor_pending_channel_ready } /// Returns true if this channel is currently available for use. This is a superset of @@ -4858,7 +4857,7 @@ impl Channel { /// Returns true if our channel_ready has been sent pub fn is_our_channel_ready(&self) -> bool { - (self.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.channel_state >= ChannelState::ChannelFunded as u32 + (self.channel_state & ChannelState::OurChannelReady as u32) != 0 || self.channel_state >= ChannelState::ChannelReady as u32 } /// Returns true if our peer has either initiated or agreed to shut down the channel. @@ -4912,14 +4911,14 @@ impl Channel { self.channel_state |= ChannelState::OurChannelReady as u32; true } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirChannelReady as u32) { - self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS); + self.channel_state = ChannelState::ChannelReady as u32 | (self.channel_state & MULTI_STATE_FLAGS); self.update_time_counter += 1; true } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurChannelReady as u32) { // We got a reorg but not enough to trigger a force close, just ignore. false } else { - if self.funding_tx_confirmation_height != 0 && self.channel_state < ChannelState::ChannelFunded as u32 { + if self.funding_tx_confirmation_height != 0 && self.channel_state < ChannelState::ChannelReady as u32 { // We should never see a funding transaction on-chain until we've received // funding_signed (if we're an outbound channel), or seen funding_generated (if we're // an inbound channel - before that we have no known funding TXID). The fuzzer, @@ -5074,7 +5073,7 @@ impl Channel { } let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS); - if non_shutdown_state >= ChannelState::ChannelFunded as u32 || + if non_shutdown_state >= ChannelState::ChannelReady as u32 || (non_shutdown_state & ChannelState::OurChannelReady as u32) == ChannelState::OurChannelReady as u32 { let mut funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1; if self.funding_tx_confirmation_height == 0 { @@ -5102,7 +5101,7 @@ impl Channel { height >= self.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS { log_info!(logger, "Closing channel {} due to funding timeout", log_bytes!(self.channel_id)); // If funding_tx_confirmed_in is unset, the channel must not be active - assert!(non_shutdown_state <= ChannelState::ChannelFunded as u32); + assert!(non_shutdown_state <= ChannelState::ChannelReady as u32); assert_eq!(non_shutdown_state & ChannelState::OurChannelReady as u32, 0); return Err(ClosureReason::FundingTimedOut); } @@ -5528,7 +5527,7 @@ impl Channel { /// /// If an Err is returned, it's a ChannelError::Ignore! pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket, logger: &L) -> Result, ChannelError> where L::Target: Logger { - if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelReady as u32) { return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned())); } let channel_total_msat = self.channel_value_satoshis * 1000; @@ -5661,7 +5660,7 @@ impl Channel { /// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err. /// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc. pub fn send_commitment(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger { - if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) { + if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) { panic!("Cannot create commitment tx until channel is fully established"); } if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) { @@ -5950,7 +5949,7 @@ impl Channel { // funding transaction, don't return a funding txo (which prevents providing the // monitor update to the user, even if we return one). // See test_duplicate_chan_id and test_pre_lockin_no_chan_closed_update for more. - if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelFunded as u32 | ChannelState::ShutdownComplete as u32) != 0 { + if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::ChannelReady as u32 | ChannelState::ShutdownComplete as u32) != 0 { self.latest_monitor_update_id += 1; Some((funding_txo, ChannelMonitorUpdate { update_id: self.latest_monitor_update_id, diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index a7c40a59569..d7cd70e29b3 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -1520,7 +1520,7 @@ macro_rules! handle_chan_restoration_locked { }); } - if $channel_entry.get().is_usable() && $channel_entry.get_mut().should_emit_channel_ready_event() { + if $channel_entry.get_mut().should_emit_channel_ready_event() { let mut pending_events = $self.pending_events.lock().unwrap(); emit_channel_ready_event!(pending_events, $channel_entry.get()); } @@ -4546,7 +4546,7 @@ impl ChannelManager Result<(), MsgHandleErrInternal> { - let ((funding_msg, monitor, (mut channel_ready, emit_channel_ready_event)), mut chan) = { + let ((funding_msg, monitor, (mut channel_ready, _emit_channel_ready_event)), mut chan) = { let best_block = *self.best_block.read().unwrap(); let mut channel_lock = self.channel_state.lock().unwrap(); let channel_state = &mut *channel_lock; @@ -4610,10 +4610,6 @@ impl ChannelManager ChannelManager update, Err(e) => try_chan_entry!(self, Err(e), channel_state, chan), }; @@ -4653,10 +4649,6 @@ impl ChannelManager return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id)) @@ -4698,7 +4690,7 @@ impl ChannelManager(node_a: &'a Node<'b, 'c, 'd>, n create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags) } +#[macro_export] +#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))] +macro_rules! expect_channel_ready_event { + ($node: expr, $expected_counterparty_node_id: expr) => { + let events = $node.node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + $crate::util::events::Event::ChannelReady{ ref counterparty_node_id, .. } => { + assert_eq!($expected_counterparty_node_id, *counterparty_node_id); + }, + _ => panic!("Unexpected event"), + } + } +} + pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) { let (channel_ready, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags); let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &channel_ready); + expect_channel_ready_event!(node_a, node_b.node.get_our_node_id()); + expect_channel_ready_event!(node_b, node_a.node.get_our_node_id()); (announcement, as_update, bs_update, channel_id, tx) } @@ -741,6 +758,9 @@ pub fn open_zero_conf_channel<'a, 'b, 'c, 'd>(initiator: &'a Node<'b, 'c, 'd>, r assert_eq!(initiator.node.list_usable_channels().len(), initiator_channels + 1); assert_eq!(receiver.node.list_usable_channels().len(), receiver_channels + 1); + expect_channel_ready_event!(initiator, receiver.node.get_our_node_id()); + expect_channel_ready_event!(receiver, initiator.node.get_our_node_id()); + (tx, as_channel_ready.channel_id) } @@ -870,8 +890,10 @@ pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: & connect_blocks(&nodes[b], CHAN_CONFIRM_DEPTH - 1); let as_channel_ready = get_event_msg!(nodes[a], MessageSendEvent::SendChannelReady, nodes[b].node.get_our_node_id()); nodes[a].node.handle_channel_ready(&nodes[b].node.get_our_node_id(), &get_event_msg!(nodes[b], MessageSendEvent::SendChannelReady, nodes[a].node.get_our_node_id())); + expect_channel_ready_event!(nodes[a], nodes[b].node.get_our_node_id()); let as_update = get_event_msg!(nodes[a], MessageSendEvent::SendChannelUpdate, nodes[b].node.get_our_node_id()); nodes[b].node.handle_channel_ready(&nodes[a].node.get_our_node_id(), &as_channel_ready); + expect_channel_ready_event!(nodes[b], nodes[a].node.get_our_node_id()); let bs_update = get_event_msg!(nodes[b], MessageSendEvent::SendChannelUpdate, nodes[a].node.get_our_node_id()); nodes[a].node.handle_channel_update(&nodes[b].node.get_our_node_id(), &bs_update); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index eb6d7032fee..4bbc8bc0750 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -184,6 +184,8 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) { let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id); let funding_msgs = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &funding_tx); create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_msgs.0); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); // nodes[0] should now be able to send the full balance to nodes[1], violating nodes[1]'s // security model if it ever tries to send funds back to nodes[0] (but that's not our problem). @@ -558,6 +560,7 @@ fn do_test_sanity_on_in_flight_opens(steps: u8) { confirm_transaction_at(&nodes[0], &tx, 2); connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH); create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); } #[test] @@ -3711,11 +3714,24 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken } let events_1 = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events_1.len(), 1); - match events_1[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; + if messages_delivered == 0 { + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + assert_eq!(events_1.len(), 2); + match events_1[0] { + Event::ChannelReady { .. } => { }, + _ => panic!("Unexpected event"), + }; + match events_1[1] { + Event::PendingHTLCsForwardable { .. } => { }, + _ => panic!("Unexpected event"), + }; + } else { + assert_eq!(events_1.len(), 1); + match events_1[0] { + Event::PendingHTLCsForwardable { .. } => { }, + _ => panic!("Unexpected event"), + }; + } nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); @@ -3952,6 +3968,8 @@ fn test_funding_peer_disconnect() { }, _ => panic!("Unexpected event {:?}", events_6[0]), }; + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); // When we deliver nodes[1]'s announcement_signatures to nodes[0], nodes[0] should immediately // broadcast the channel announcement globally, as well as re-send its (now-public) @@ -4414,6 +4432,8 @@ fn test_no_txn_manager_serialize_deserialize() { node.gossip_sync.handle_channel_update(&as_update).unwrap(); node.gossip_sync.handle_channel_update(&bs_update).unwrap(); } + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); send_payment(&nodes[0], &[&nodes[1]], 1000000); } @@ -4535,6 +4555,8 @@ fn test_manager_serialize_deserialize_events() { node.gossip_sync.handle_channel_update(&as_update).unwrap(); node.gossip_sync.handle_channel_update(&bs_update).unwrap(); } + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); send_payment(&nodes[0], &[&nodes[1]], 1000000); } @@ -9429,6 +9451,9 @@ fn test_duplicate_chan_id() { let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx); let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready); update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); + send_payment(&nodes[0], &[&nodes[1]], 8000000); } @@ -10356,6 +10381,8 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx); let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready); update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); let dust_buffer_feerate = { let chan_lock = nodes[0].node.channel_state.lock().unwrap(); diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index cdc0b83894a..92db5a22254 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -483,6 +483,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) { nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]); check_added_monitors!(nodes[1], 1); commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false); + expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, false, false); if confirm_before_reload { let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone(); @@ -499,7 +500,6 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) { let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); assert_eq!(bs_htlc_claim_txn.len(), 1); check_spends!(bs_htlc_claim_txn[0], as_commitment_tx); - expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, false, false); if !confirm_before_reload { mine_transaction(&nodes[0], &as_commitment_tx); diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index 1f8ffbbfaec..6037fa66a78 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -202,6 +202,8 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) { } else { panic!("Unexpected event"); } nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready); + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events(); assert_eq!(bs_msg_events.len(), 1); if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] { @@ -407,8 +409,10 @@ fn test_inbound_scid_privacy() { connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1); let bs_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id()); nodes[1].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id())); + expect_channel_ready_event!(nodes[1], nodes[2].node.get_our_node_id()); let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready); + expect_channel_ready_event!(nodes[2], nodes[1].node.get_our_node_id()); let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update); @@ -672,6 +676,9 @@ fn test_0conf_channel_with_async_monitor() { } _ => panic!("Unexpected event"), } + expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id()); + expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id()); + let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); let as_channel_update = match &as_locked_update[1] { diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index 383e3581718..00df34999e2 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -15,6 +15,7 @@ //! few other things. use crate::chain::keysinterface::SpendableOutputDescriptor; +#[cfg(anchors)] use crate::ln::chan_utils::HTLCOutputInCommitment; use crate::ln::channelmanager::PaymentId; use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS; @@ -26,7 +27,9 @@ use crate::routing::gossip::NetworkUpdate; use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, VecReadWrapper, VecWriteWrapper}; use crate::routing::router::{RouteHop, RouteParameters}; -use bitcoin::{PackedLockTime, Transaction, OutPoint}; +use bitcoin::{PackedLockTime, Transaction}; +#[cfg(anchors)] +use bitcoin::OutPoint; use bitcoin::blockdata::script::Script; use bitcoin::hashes::Hash; use bitcoin::hashes::sha256::Hash as Sha256;