@@ -31,6 +31,7 @@ use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
3131use crate :: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
3232use crate :: chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , Balance , MonitorEvent , TransactionOutputs , WithChannelMonitor , LATENCY_GRACE_PERIOD_BLOCKS } ;
3333use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
34+ use crate :: ln:: ChannelId ;
3435use crate :: sign:: ecdsa:: WriteableEcdsaChannelSigner ;
3536use crate :: events;
3637use crate :: events:: { Event , EventHandler } ;
@@ -158,7 +159,7 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
158159 ///
159160 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
160161 /// [`Writeable::write`]: crate::util::ser::Writeable::write
161- fn persist_new_channel ( & self , channel_id : OutPoint , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
162+ fn persist_new_channel ( & self , channel_funding_outpoint : OutPoint , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
162163
163164 /// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
164165 /// update.
@@ -193,7 +194,7 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
193194 /// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
194195 ///
195196 /// [`Writeable::write`]: crate::util::ser::Writeable::write
196- fn update_persisted_channel ( & self , channel_id : OutPoint , update : Option < & ChannelMonitorUpdate > , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
197+ fn update_persisted_channel ( & self , channel_funding_outpoint : OutPoint , update : Option < & ChannelMonitorUpdate > , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
197198}
198199
199200struct MonitorHolder < ChannelSigner : WriteableEcdsaChannelSigner > {
@@ -287,7 +288,7 @@ pub struct ChainMonitor<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T:
287288 persister : P ,
288289 /// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
289290 /// from the user and not from a [`ChannelMonitor`].
290- pending_monitor_events : Mutex < Vec < ( OutPoint , Vec < MonitorEvent > , Option < PublicKey > ) > > ,
291+ pending_monitor_events : Mutex < Vec < ( OutPoint , ChannelId , Vec < MonitorEvent > , Option < PublicKey > ) > > ,
291292 /// The best block height seen, used as a proxy for the passage of time.
292293 highest_chain_height : AtomicUsize ,
293294
@@ -471,12 +472,15 @@ where C::Target: chain::Filter,
471472 }
472473 }
473474
474- /// Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
475+ /// Lists the funding outpoint and channel ID of each [`ChannelMonitor`] being monitored.
475476 ///
476477 /// Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
477478 /// monitoring for on-chain state resolutions.
478- pub fn list_monitors ( & self ) -> Vec < OutPoint > {
479- self . monitors . read ( ) . unwrap ( ) . keys ( ) . map ( |outpoint| * outpoint) . collect ( )
479+ pub fn list_monitors ( & self ) -> Vec < ( OutPoint , ChannelId ) > {
480+ self . monitors . read ( ) . unwrap ( ) . iter ( ) . map ( |( outpoint, monitor_holder) | {
481+ let channel_id = monitor_holder. monitor . channel_id ( ) ;
482+ ( * outpoint, channel_id)
483+ } ) . collect ( )
480484 }
481485
482486 #[ cfg( not( c_bindings) ) ]
@@ -542,8 +546,9 @@ where C::Target: chain::Filter,
542546 // Completed event.
543547 return Ok ( ( ) ) ;
544548 }
545- self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, vec ! [ MonitorEvent :: Completed {
546- funding_txo,
549+ let channel_id = monitor_data. monitor . channel_id ( ) ;
550+ self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, channel_id, vec ! [ MonitorEvent :: Completed {
551+ funding_txo, channel_id,
547552 monitor_update_id: monitor_data. monitor. get_latest_update_id( ) ,
548553 } ] , monitor_data. monitor . get_counterparty_node_id ( ) ) ) ;
549554 } ,
@@ -565,9 +570,14 @@ where C::Target: chain::Filter,
565570 #[ cfg( any( test, fuzzing) ) ]
566571 pub fn force_channel_monitor_updated ( & self , funding_txo : OutPoint , monitor_update_id : u64 ) {
567572 let monitors = self . monitors . read ( ) . unwrap ( ) ;
568- let counterparty_node_id = monitors. get ( & funding_txo) . and_then ( |m| m. monitor . get_counterparty_node_id ( ) ) ;
569- self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, vec ! [ MonitorEvent :: Completed {
573+ let ( counterparty_node_id, channel_id) = if let Some ( m) = monitors. get ( & funding_txo) {
574+ ( m. monitor . get_counterparty_node_id ( ) , m. monitor . channel_id ( ) )
575+ } else {
576+ ( None , ChannelId :: v1_from_funding_outpoint ( funding_txo) )
577+ } ;
578+ self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, channel_id, vec ! [ MonitorEvent :: Completed {
570579 funding_txo,
580+ channel_id,
571581 monitor_update_id,
572582 } ] , counterparty_node_id) ) ;
573583 self . event_notifier . notify ( ) ;
@@ -753,11 +763,14 @@ where C::Target: chain::Filter,
753763 }
754764
755765 fn update_channel ( & self , funding_txo : OutPoint , update : & ChannelMonitorUpdate ) -> ChannelMonitorUpdateStatus {
766+ // `ChannelMonitorUpdate`'s `channel_id` is `None` prior to 0.0.121 and all channels in those
767+ // versions are V1-established. For 0.0.121+ the `channel_id` fields is always `Some`.
768+ let channel_id = update. channel_id . unwrap_or ( ChannelId :: v1_from_funding_outpoint ( funding_txo) ) ;
756769 // Update the monitor that watches the channel referred to by the given outpoint.
757770 let monitors = self . monitors . read ( ) . unwrap ( ) ;
758771 match monitors. get ( & funding_txo) {
759772 None => {
760- let logger = WithContext :: from ( & self . logger , update. counterparty_node_id , Some ( funding_txo . to_channel_id ( ) ) ) ;
773+ let logger = WithContext :: from ( & self . logger , update. counterparty_node_id , Some ( channel_id ) ) ;
761774 log_error ! ( logger, "Failed to update channel monitor: no such monitor registered" ) ;
762775
763776 // We should never ever trigger this from within ChannelManager. Technically a
@@ -815,7 +828,7 @@ where C::Target: chain::Filter,
815828 }
816829 }
817830
818- fn release_pending_monitor_events ( & self ) -> Vec < ( OutPoint , Vec < MonitorEvent > , Option < PublicKey > ) > {
831+ fn release_pending_monitor_events ( & self ) -> Vec < ( OutPoint , ChannelId , Vec < MonitorEvent > , Option < PublicKey > ) > {
819832 let mut pending_monitor_events = self . pending_monitor_events . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
820833 for monitor_state in self . monitors . read ( ) . unwrap ( ) . values ( ) {
821834 let logger = WithChannelMonitor :: from ( & self . logger , & monitor_state. monitor ) ;
@@ -829,8 +842,9 @@ where C::Target: chain::Filter,
829842 let monitor_events = monitor_state. monitor . get_and_clear_pending_monitor_events ( ) ;
830843 if monitor_events. len ( ) > 0 {
831844 let monitor_outpoint = monitor_state. monitor . get_funding_txo ( ) . 0 ;
845+ let monitor_channel_id = monitor_state. monitor . channel_id ( ) ;
832846 let counterparty_node_id = monitor_state. monitor . get_counterparty_node_id ( ) ;
833- pending_monitor_events. push ( ( monitor_outpoint, monitor_events, counterparty_node_id) ) ;
847+ pending_monitor_events. push ( ( monitor_outpoint, monitor_channel_id , monitor_events, counterparty_node_id) ) ;
834848 }
835849 }
836850 }
0 commit comments