X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmanager.rs;h=298f88ce0cc018162cf5f2a3201512641b1cd8fd;hb=2144166bc64b6350d22b5bbf4e25477a666ad402;hp=ad8603312fc714243a4129acf381d5b4a7332224;hpb=928bfb12d2d0d02ae0b5a68a81a6b4a651561c69;p=rust-lightning diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index ad860331..298f88ce 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -1384,6 +1384,18 @@ impl ChannelMana self.list_channels_with_filter(|&(_, ref channel)| channel.is_live()) } + /// Helper function that issues the channel close events + fn issue_channel_close_events(&self, channel: &Channel, closure_reason: ClosureReason) { + let mut pending_events_lock = self.pending_events.lock().unwrap(); + match channel.unbroadcasted_funding() { + Some(transaction) => { + pending_events_lock.push(events::Event::DiscardFunding { channel_id: channel.channel_id(), transaction }) + }, + None => {}, + } + pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason }); + } + fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option) -> Result<(), APIError> { let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier); @@ -1430,12 +1442,7 @@ impl ChannelMana msg: channel_update }); } - if let Ok(mut pending_events_lock) = self.pending_events.lock() { - pending_events_lock.push(events::Event::ChannelClosed { - channel_id: *channel_id, - reason: ClosureReason::HolderForceClosed - }); - } + self.issue_channel_close_events(&channel, ClosureReason::HolderForceClosed); } break Ok(()); }, @@ -1526,13 +1533,12 @@ impl ChannelMana if let Some(short_id) = chan.get().get_short_channel_id() { channel_state.short_to_id.remove(&short_id); } - let mut pending_events_lock = self.pending_events.lock().unwrap(); if peer_node_id.is_some() { if let Some(peer_msg) = peer_msg { - pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() } }); + self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() }); } } else { - pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::HolderForceClosed }); + self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed); } chan.remove_entry().1 } else { @@ -2178,7 +2184,7 @@ impl ChannelMana } } else { return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { - err: "Payment with ID {} not found".to_string() + err: format!("Payment with ID {} not found", log_bytes!(payment_id.0)), })) } }; @@ -3010,6 +3016,7 @@ impl ChannelMana network_update: None, all_paths_failed: payment.get().remaining_parts() == 0, path: path.clone(), + short_channel_id: None, #[cfg(test)] error_code: None, #[cfg(test)] @@ -3063,9 +3070,9 @@ impl ChannelMana match &onion_error { &HTLCFailReason::LightningError { ref err } => { #[cfg(test)] - let (network_update, payment_retryable, onion_error_code, onion_error_data) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone()); + let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone()); #[cfg(not(test))] - let (network_update, payment_retryable, _, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone()); + let (network_update, short_channel_id, payment_retryable, _, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone()); // TODO: If we decided to blame ourselves (or one of our channels) in // process_onion_failure we should close that channel as it implies our // next-hop is needlessly blaming us! @@ -3076,6 +3083,7 @@ impl ChannelMana network_update, all_paths_failed, path: path.clone(), + short_channel_id, #[cfg(test)] error_code: onion_error_code, #[cfg(test)] @@ -3103,6 +3111,7 @@ impl ChannelMana network_update: None, all_paths_failed, path: path.clone(), + short_channel_id: Some(path.first().unwrap().short_channel_id), #[cfg(test)] error_code: Some(*failure_code), #[cfg(test)] @@ -3308,8 +3317,12 @@ impl ChannelMana sessions.remove(&session_priv_bytes, path.last().unwrap().fee_msat) } else { false }; if found_payment { + let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()); self.pending_events.lock().unwrap().push( - events::Event::PaymentSent { payment_preimage } + events::Event::PaymentSent { + payment_preimage, + payment_hash: payment_hash + } ); } else { log_trace!(self.logger, "Received duplicative fulfill for HTLC with payment_preimage {}", log_bytes!(payment_preimage.0)); @@ -3701,7 +3714,7 @@ impl ChannelMana msg: update }); } - self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: msg.channel_id, reason: ClosureReason::CooperativeClosure }); + self.issue_channel_close_events(&chan, ClosureReason::CooperativeClosure); } Ok(()) } @@ -4113,7 +4126,7 @@ impl ChannelMana msg: update }); } - self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::CommitmentTxConfirmed }); + self.issue_channel_close_events(&chan, ClosureReason::CommitmentTxConfirmed); pending_msg_events.push(events::MessageSendEvent::HandleError { node_id: chan.get_counterparty_node_id(), action: msgs::ErrorAction::SendErrorMessage { @@ -4229,12 +4242,7 @@ impl ChannelMana }); } - if let Ok(mut pending_events_lock) = self.pending_events.lock() { - pending_events_lock.push(events::Event::ChannelClosed { - channel_id: *channel_id, - reason: ClosureReason::CooperativeClosure - }); - } + self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure); log_info!(self.logger, "Broadcasting {}", log_tx!(tx)); self.tx_broadcaster.broadcast_transaction(&tx); @@ -4671,7 +4679,7 @@ where msg: update }); } - self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: ClosureReason::CommitmentTxConfirmed }); + self.issue_channel_close_events(channel, ClosureReason::CommitmentTxConfirmed); pending_msg_events.push(events::MessageSendEvent::HandleError { node_id: channel.get_counterparty_node_id(), action: msgs::ErrorAction::SendErrorMessage { msg: e }, @@ -4862,7 +4870,7 @@ impl msg: update }); } - self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer }); + self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer); false } else { true @@ -4877,7 +4885,7 @@ impl if let Some(short_id) = chan.get_short_channel_id() { short_to_id.remove(&short_id); } - self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer }); + self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer); return false; } else { no_channels_remain = false; @@ -6017,8 +6025,9 @@ mod tests { // further events will be generated for subsequence path successes. let events = nodes[0].node.get_and_clear_pending_events(); match events[0] { - Event::PaymentSent { payment_preimage: ref preimage } => { + Event::PaymentSent { payment_preimage: ref preimage, payment_hash: ref hash } => { assert_eq!(payment_preimage, *preimage); + assert_eq!(our_payment_hash, *hash); }, _ => panic!("Unexpected event"), }