@@ -111,6 +111,9 @@ pub struct ChannelMonitorUpdate {
111111 /// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
112112 /// always `Some` otherwise.
113113 pub channel_id : Option < ChannelId > ,
114+
115+ /// The encoded channel data associated with this ChannelMonitor, if any.
116+ pub encoded_channel : Option < Vec < u8 > > ,
114117}
115118
116119impl ChannelMonitorUpdate {
@@ -156,6 +159,13 @@ impl Writeable for ChannelMonitorUpdate {
156159 for update_step in self . updates . iter ( ) {
157160 update_step. write ( w) ?;
158161 }
162+ #[ cfg( feature = "safe_channels" ) ]
163+ write_tlv_fields ! ( w, {
164+ // 1 was previously used to store `counterparty_node_id`
165+ ( 3 , self . channel_id, option) ,
166+ ( 5 , self . encoded_channel, option)
167+ } ) ;
168+ #[ cfg( not( feature = "safe_channels" ) ) ]
159169 write_tlv_fields ! ( w, {
160170 // 1 was previously used to store `counterparty_node_id`
161171 ( 3 , self . channel_id, option) ,
@@ -176,11 +186,13 @@ impl Readable for ChannelMonitorUpdate {
176186 }
177187 }
178188 let mut channel_id = None ;
189+ let mut encoded_channel = None ;
179190 read_tlv_fields ! ( r, {
180191 // 1 was previously used to store `counterparty_node_id`
181192 ( 3 , channel_id, option) ,
193+ ( 5 , encoded_channel, option)
182194 } ) ;
183- Ok ( Self { update_id, updates, channel_id } )
195+ Ok ( Self { update_id, updates, channel_id, encoded_channel } )
184196 }
185197}
186198
@@ -1402,6 +1414,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
14021414 /// make deciding whether to do so simple, here we track whether this monitor was last written
14031415 /// prior to 0.1.
14041416 written_by_0_1_or_later : bool ,
1417+
1418+ encoded_channel : Option < Vec < u8 > > ,
14051419}
14061420
14071421// Returns a `&FundingScope` for the one we are currently observing/handling commitment transactions
@@ -1733,6 +1747,32 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
17331747 _ => channel_monitor. pending_monitor_events . clone ( ) ,
17341748 } ;
17351749
1750+ #[ cfg( feature = "safe_channels" ) ]
1751+ write_tlv_fields ! ( writer, {
1752+ ( 1 , channel_monitor. funding_spend_confirmed, option) ,
1753+ ( 3 , channel_monitor. htlcs_resolved_on_chain, required_vec) ,
1754+ ( 5 , pending_monitor_events, required_vec) ,
1755+ ( 7 , channel_monitor. funding_spend_seen, required) ,
1756+ ( 9 , channel_monitor. counterparty_node_id, required) ,
1757+ ( 11 , channel_monitor. confirmed_commitment_tx_counterparty_output, option) ,
1758+ ( 13 , channel_monitor. spendable_txids_confirmed, required_vec) ,
1759+ ( 15 , channel_monitor. counterparty_fulfilled_htlcs, required) ,
1760+ ( 17 , channel_monitor. initial_counterparty_commitment_info, option) ,
1761+ ( 19 , channel_monitor. channel_id, required) ,
1762+ ( 21 , channel_monitor. balances_empty_height, option) ,
1763+ ( 23 , channel_monitor. holder_pays_commitment_tx_fee, option) ,
1764+ ( 25 , channel_monitor. payment_preimages, required) ,
1765+ ( 27 , channel_monitor. first_negotiated_funding_txo, required) ,
1766+ ( 29 , channel_monitor. initial_counterparty_commitment_tx, option) ,
1767+ ( 31 , channel_monitor. funding. channel_parameters, required) ,
1768+ ( 32 , channel_monitor. pending_funding, optional_vec) ,
1769+ ( 33 , channel_monitor. htlcs_resolved_to_user, required) ,
1770+ ( 34 , channel_monitor. alternative_funding_confirmed, option) ,
1771+ ( 35 , channel_monitor. is_manual_broadcast, required) ,
1772+ ( 37 , channel_monitor. funding_seen_onchain, required) ,
1773+ ( 39 , channel_monitor. encoded_channel, option) ,
1774+ } ) ;
1775+ #[ cfg( not( feature = "safe_channels" ) ) ]
17361776 write_tlv_fields ! ( writer, {
17371777 ( 1 , channel_monitor. funding_spend_confirmed, option) ,
17381778 ( 3 , channel_monitor. htlcs_resolved_on_chain, required_vec) ,
@@ -1994,6 +2034,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19942034 alternative_funding_confirmed : None ,
19952035
19962036 written_by_0_1_or_later : true ,
2037+ encoded_channel : None ,
19972038 } )
19982039 }
19992040
@@ -2114,6 +2155,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
21142155 inner. update_monitor ( updates, broadcaster, fee_estimator, & logger)
21152156 }
21162157
2158+ /// Gets the encoded channel data, if any, associated with this ChannelMonitor.
2159+ pub fn get_encoded_channel ( & self ) -> Option < Vec < u8 > > {
2160+ self . inner . lock ( ) . unwrap ( ) . encoded_channel . clone ( )
2161+ }
2162+
2163+ /// Updates the encoded channel data associated with this ChannelMonitor.
2164+ pub fn update_encoded_channel ( & self , encoded : Vec < u8 > ) {
2165+ self . inner . lock ( ) . unwrap ( ) . encoded_channel = Some ( encoded) ;
2166+ }
2167+
21172168 /// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
21182169 /// ChannelMonitor.
21192170 ///
@@ -4405,9 +4456,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
44054456 }
44064457 }
44074458
4408- if ret. is_ok ( ) && self . no_further_updates_allowed ( ) && is_pre_close_update {
4409- log_error ! ( logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent" ) ;
4410- Err ( ( ) )
4459+ if ret. is_ok ( ) {
4460+ if self . no_further_updates_allowed ( ) && is_pre_close_update {
4461+ log_error ! ( logger, "Refusing Channel Monitor Update as counterparty attempted to update commitment after funding was spent" ) ;
4462+ Err ( ( ) )
4463+ } else {
4464+ // Assume that if the updates contains no encoded channel, that the channel remained unchanged. We
4465+ // therefore do not update the monitor.
4466+ if let Some ( encoded_channel) = updates. encoded_channel . as_ref ( ) {
4467+ self . encoded_channel = Some ( encoded_channel. clone ( ) ) ;
4468+ }
4469+ Ok ( ( ) )
4470+ }
44114471 } else { ret }
44124472 }
44134473
@@ -6645,6 +6705,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66456705 let mut alternative_funding_confirmed = None ;
66466706 let mut is_manual_broadcast = RequiredWrapper ( None ) ;
66476707 let mut funding_seen_onchain = RequiredWrapper ( None ) ;
6708+ let mut encoded_channel = None ;
66486709 read_tlv_fields ! ( reader, {
66496710 ( 1 , funding_spend_confirmed, option) ,
66506711 ( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -6667,6 +6728,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66676728 ( 34 , alternative_funding_confirmed, option) ,
66686729 ( 35 , is_manual_broadcast, ( default_value, false ) ) ,
66696730 ( 37 , funding_seen_onchain, ( default_value, true ) ) ,
6731+ ( 39 , encoded_channel, option) ,
66706732 } ) ;
66716733 // Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
66726734 // we can use it to determine if this monitor was last written by LDK 0.1 or later.
@@ -6844,6 +6906,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
68446906 alternative_funding_confirmed,
68456907
68466908 written_by_0_1_or_later,
6909+ encoded_channel,
68476910 } ) ;
68486911
68496912 if counterparty_node_id. is_none ( ) {
0 commit comments