@@ -261,9 +261,9 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
261261 // just shut down the node since we're not retrying persistence!
262262
263263 fn persist_new_channel (
264- & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ,
264+ & self , monitor : & ChannelMonitor < ChannelSigner > ,
265265 ) -> chain:: ChannelMonitorUpdateStatus {
266- let monitor_name = MonitorName :: from ( funding_txo ) ;
266+ let monitor_name = monitor . persistence_key ( ) ;
267267 match self . write (
268268 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
269269 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -276,10 +276,9 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
276276 }
277277
278278 fn update_persisted_channel (
279- & self , funding_txo : OutPoint , _update : Option < & ChannelMonitorUpdate > ,
280- monitor : & ChannelMonitor < ChannelSigner > ,
279+ & self , _update : Option < & ChannelMonitorUpdate > , monitor : & ChannelMonitor < ChannelSigner > ,
281280 ) -> chain:: ChannelMonitorUpdateStatus {
282- let monitor_name = MonitorName :: from ( funding_txo ) ;
281+ let monitor_name = monitor . persistence_key ( ) ;
283282 match self . write (
284283 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
285284 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -291,8 +290,8 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
291290 }
292291 }
293292
294- fn archive_persisted_channel ( & self , funding_txo : OutPoint ) {
295- let monitor_name = MonitorName :: from ( funding_txo ) ;
293+ fn archive_persisted_channel ( & self , monitor : & ChannelMonitor < ChannelSigner > ) {
294+ let monitor_name = monitor . persistence_key ( ) ;
296295 let monitor = match self . read (
297296 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
298297 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -334,21 +333,6 @@ where
334333 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
335334 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
336335 ) ? {
337- if stored_key. len ( ) < 66 {
338- return Err ( io:: Error :: new (
339- io:: ErrorKind :: InvalidData ,
340- "Stored key has invalid length" ,
341- ) ) ;
342- }
343-
344- let txid = Txid :: from_str ( stored_key. split_at ( 64 ) . 0 ) . map_err ( |_| {
345- io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid tx ID in stored key" )
346- } ) ?;
347-
348- let index: u16 = stored_key. split_at ( 65 ) . 1 . parse ( ) . map_err ( |_| {
349- io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid tx index in stored key" )
350- } ) ?;
351-
352336 match <( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) >:: read (
353337 & mut io:: Cursor :: new ( kv_store. read (
354338 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
@@ -358,14 +342,14 @@ where
358342 ( & * entropy_source, & * signer_provider) ,
359343 ) {
360344 Ok ( ( block_hash, channel_monitor) ) => {
361- if channel_monitor. get_funding_txo ( ) . 0 . txid != txid
362- || channel_monitor. get_funding_txo ( ) . 0 . index != index
363- {
345+ let monitor_name = MonitorName :: new ( stored_key) ?;
346+ if channel_monitor. persistence_key ( ) . as_str ( ) != monitor_name. as_str ( ) {
364347 return Err ( io:: Error :: new (
365348 io:: ErrorKind :: InvalidData ,
366349 "ChannelMonitor was stored under the wrong key" ,
367350 ) ) ;
368351 }
352+
369353 res. push ( ( block_hash, channel_monitor) ) ;
370354 } ,
371355 Err ( _) => {
@@ -406,12 +390,13 @@ where
406390/// - [`Persist::update_persisted_channel`], which persists only a [`ChannelMonitorUpdate`]
407391///
408392/// Whole [`ChannelMonitor`]s are stored in the [`CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE`],
409- /// using the familiar encoding of an [`OutPoint`] (for example, `[SOME-64-CHAR-HEX-STRING]_1`).
393+ /// using the familiar encoding of an [`OutPoint`] (e.g., `[SOME-64-CHAR-HEX-STRING]_1`) for v1
394+ /// channels or a [`ChannelId`] (e.g., `[SOME-64-CHAR-HEX-STRING]`) for v2 channels.
410395///
411396/// Each [`ChannelMonitorUpdate`] is stored in a dynamic secondary namespace, as follows:
412397///
413398/// - primary namespace: [`CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE`]
414- /// - secondary namespace: [the monitor's encoded outpoint name]
399+ /// - secondary namespace: [the monitor's encoded outpoint or channel id name]
415400///
416401/// Under that secondary namespace, each update is stored with a number string, like `21`, which
417402/// represents its `update_id` value.
@@ -550,15 +535,17 @@ where
550535 /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
551536 /// documentation for [`MonitorUpdatingPersister`].
552537 ///
553- /// For `monitor_key`, channel storage keys be the channel's transaction ID and index, or
554- /// [`OutPoint`], with an underscore `_` between them . For example, given:
538+ /// For `monitor_key`, channel storage keys can be the channel's funding [`OutPoint`], with an
539+ /// underscore `_` between txid and index for v1 channels . For example, given:
555540 ///
556541 /// - Transaction ID: `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef`
557542 /// - Index: `1`
558543 ///
559544 /// The correct `monitor_key` would be:
560545 /// `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1`
561546 ///
547+ /// For v2 channels, the hex-encoded [`ChannelId`] is used directly for `monitor_key` instead.
548+ ///
562549 /// Loading a large number of monitors will be faster if done in parallel. You can use this
563550 /// function to accomplish this. Take care to limit the number of parallel readers.
564551 pub fn read_channel_monitor_with_updates (
@@ -604,7 +591,6 @@ where
604591 & self , monitor_name : & MonitorName ,
605592 ) -> Result < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) , io:: Error >
606593 {
607- let outpoint: OutPoint = monitor_name. try_into ( ) ?;
608594 let mut monitor_cursor = io:: Cursor :: new ( self . kv_store . read (
609595 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
610596 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -619,9 +605,7 @@ where
619605 ( & * self . entropy_source , & * self . signer_provider ) ,
620606 ) {
621607 Ok ( ( blockhash, channel_monitor) ) => {
622- if channel_monitor. get_funding_txo ( ) . 0 . txid != outpoint. txid
623- || channel_monitor. get_funding_txo ( ) . 0 . index != outpoint. index
624- {
608+ if channel_monitor. persistence_key ( ) . as_str ( ) != monitor_name. as_str ( ) {
625609 log_error ! (
626610 self . logger,
627611 "ChannelMonitor {} was stored under the wrong key!" ,
@@ -724,10 +708,10 @@ where
724708 /// Persists a new channel. This means writing the entire monitor to the
725709 /// parametrized [`KVStore`].
726710 fn persist_new_channel (
727- & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ,
711+ & self , monitor : & ChannelMonitor < ChannelSigner > ,
728712 ) -> chain:: ChannelMonitorUpdateStatus {
729713 // Determine the proper key for this monitor
730- let monitor_name = MonitorName :: from ( funding_txo ) ;
714+ let monitor_name = monitor . persistence_key ( ) ;
731715 // Serialize and write the new monitor
732716 let mut monitor_bytes = Vec :: with_capacity (
733717 MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL . len ( ) + monitor. serialized_length ( ) ,
@@ -765,15 +749,14 @@ where
765749 /// `update` is `None`.
766750 /// - The update is at [`u64::MAX`], indicating an update generated by pre-0.1 LDK.
767751 fn update_persisted_channel (
768- & self , funding_txo : OutPoint , update : Option < & ChannelMonitorUpdate > ,
769- monitor : & ChannelMonitor < ChannelSigner > ,
752+ & self , update : Option < & ChannelMonitorUpdate > , monitor : & ChannelMonitor < ChannelSigner > ,
770753 ) -> chain:: ChannelMonitorUpdateStatus {
771754 const LEGACY_CLOSED_CHANNEL_UPDATE_ID : u64 = u64:: MAX ;
772755 if let Some ( update) = update {
756+ let monitor_name = monitor. persistence_key ( ) ;
773757 let persist_update = update. update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
774758 && update. update_id % self . maximum_pending_updates != 0 ;
775759 if persist_update {
776- let monitor_name = MonitorName :: from ( funding_txo) ;
777760 let update_name = UpdateName :: from ( update. update_id ) ;
778761 match self . kv_store . write (
779762 CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE ,
@@ -795,7 +778,6 @@ where
795778 } ,
796779 }
797780 } else {
798- let monitor_name = MonitorName :: from ( funding_txo) ;
799781 // In case of channel-close monitor update, we need to read old monitor before persisting
800782 // the new one in order to determine the cleanup range.
801783 let maybe_old_monitor = match monitor. get_latest_update_id ( ) {
@@ -804,7 +786,7 @@ where
804786 } ;
805787
806788 // We could write this update, but it meets criteria of our design that calls for a full monitor write.
807- let monitor_update_status = self . persist_new_channel ( funding_txo , monitor) ;
789+ let monitor_update_status = self . persist_new_channel ( monitor) ;
808790
809791 if let chain:: ChannelMonitorUpdateStatus :: Completed = monitor_update_status {
810792 let channel_closed_legacy =
@@ -835,12 +817,12 @@ where
835817 }
836818 } else {
837819 // There is no update given, so we must persist a new monitor.
838- self . persist_new_channel ( funding_txo , monitor)
820+ self . persist_new_channel ( monitor)
839821 }
840822 }
841823
842- fn archive_persisted_channel ( & self , funding_txo : OutPoint ) {
843- let monitor_name = MonitorName :: from ( funding_txo ) ;
824+ fn archive_persisted_channel ( & self , monitor : & ChannelMonitor < ChannelSigner > ) {
825+ let monitor_name = monitor . persistence_key ( ) ;
844826 let monitor_key = monitor_name. as_str ( ) . to_string ( ) ;
845827 let monitor = match self . read_channel_monitor_with_updates ( monitor_key) {
846828 Ok ( ( _block_hash, monitor) ) => monitor,
@@ -901,14 +883,15 @@ where
901883/// in functions that store or retrieve [`ChannelMonitor`] snapshots.
902884/// It provides a consistent way to generate a unique key for channel
903885/// monitors based on the channel's funding [`OutPoint`] for v1 channels or
904- /// [`ChannelId`] for v2 channels.
886+ /// [`ChannelId`] for v2 channels. Use [`ChannelMonitor::persistence_key`] to
887+ /// obtain the correct `MonitorName`.
905888///
906889/// While users of the Lightning Dev Kit library generally won't need
907890/// to interact with [`MonitorName`] directly, it can be useful for:
908891/// - Custom persistence implementations
909892/// - Debugging or logging channel monitor operations
910893/// - Extending the functionality of the `MonitorUpdatingPersister`
911- //
894+ ///
912895/// # Examples
913896///
914897/// ```
@@ -1402,10 +1385,6 @@ mod tests {
14021385 let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
14031386 let cmu_map = nodes[ 1 ] . chain_monitor . monitor_updates . lock ( ) . unwrap ( ) ;
14041387 let cmu = & cmu_map. get ( & added_monitors[ 0 ] . 1 . channel_id ( ) ) . unwrap ( ) [ 0 ] ;
1405- let txid =
1406- Txid :: from_str ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" )
1407- . unwrap ( ) ;
1408- let test_txo = OutPoint { txid, index : 0 } ;
14091388
14101389 let ro_persister = MonitorUpdatingPersister {
14111390 kv_store : & TestStore :: new ( true ) ,
@@ -1416,7 +1395,7 @@ mod tests {
14161395 broadcaster : node_cfgs[ 0 ] . tx_broadcaster ,
14171396 fee_estimator : node_cfgs[ 0 ] . fee_estimator ,
14181397 } ;
1419- match ro_persister. persist_new_channel ( test_txo , & added_monitors[ 0 ] . 1 ) {
1398+ match ro_persister. persist_new_channel ( & added_monitors[ 0 ] . 1 ) {
14201399 ChannelMonitorUpdateStatus :: UnrecoverableError => {
14211400 // correct result
14221401 } ,
@@ -1427,7 +1406,7 @@ mod tests {
14271406 panic ! ( "Returned InProgress when shouldn't have" )
14281407 } ,
14291408 }
1430- match ro_persister. update_persisted_channel ( test_txo , Some ( cmu) , & added_monitors[ 0 ] . 1 ) {
1409+ match ro_persister. update_persisted_channel ( Some ( cmu) , & added_monitors[ 0 ] . 1 ) {
14311410 ChannelMonitorUpdateStatus :: UnrecoverableError => {
14321411 // correct result
14331412 } ,
0 commit comments