@@ -261,9 +261,9 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
261
261
// just shut down the node since we're not retrying persistence!
262
262
263
263
fn persist_new_channel (
264
- & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ,
264
+ & self , monitor : & ChannelMonitor < ChannelSigner > ,
265
265
) -> chain:: ChannelMonitorUpdateStatus {
266
- let monitor_name = MonitorName :: from ( funding_txo ) ;
266
+ let monitor_name = monitor . persistence_key ( ) ;
267
267
match self . write (
268
268
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
269
269
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -276,10 +276,9 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
276
276
}
277
277
278
278
fn update_persisted_channel (
279
- & self , funding_txo : OutPoint , _update : Option < & ChannelMonitorUpdate > ,
280
- monitor : & ChannelMonitor < ChannelSigner > ,
279
+ & self , _update : Option < & ChannelMonitorUpdate > , monitor : & ChannelMonitor < ChannelSigner > ,
281
280
) -> chain:: ChannelMonitorUpdateStatus {
282
- let monitor_name = MonitorName :: from ( funding_txo ) ;
281
+ let monitor_name = monitor . persistence_key ( ) ;
283
282
match self . write (
284
283
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
285
284
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -291,8 +290,8 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
291
290
}
292
291
}
293
292
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 ( ) ;
296
295
let monitor = match self . read (
297
296
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
298
297
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -334,21 +333,6 @@ where
334
333
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
335
334
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
336
335
) ? {
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
-
352
336
match <( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) >:: read (
353
337
& mut io:: Cursor :: new ( kv_store. read (
354
338
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
@@ -358,14 +342,14 @@ where
358
342
( & * entropy_source, & * signer_provider) ,
359
343
) {
360
344
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 ( ) {
364
347
return Err ( io:: Error :: new (
365
348
io:: ErrorKind :: InvalidData ,
366
349
"ChannelMonitor was stored under the wrong key" ,
367
350
) ) ;
368
351
}
352
+
369
353
res. push ( ( block_hash, channel_monitor) ) ;
370
354
} ,
371
355
Err ( _) => {
@@ -406,12 +390,13 @@ where
406
390
/// - [`Persist::update_persisted_channel`], which persists only a [`ChannelMonitorUpdate`]
407
391
///
408
392
/// 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.
410
395
///
411
396
/// Each [`ChannelMonitorUpdate`] is stored in a dynamic secondary namespace, as follows:
412
397
///
413
398
/// - 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]
415
400
///
416
401
/// Under that secondary namespace, each update is stored with a number string, like `21`, which
417
402
/// represents its `update_id` value.
@@ -550,15 +535,17 @@ where
550
535
/// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
551
536
/// documentation for [`MonitorUpdatingPersister`].
552
537
///
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:
555
540
///
556
541
/// - Transaction ID: `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef`
557
542
/// - Index: `1`
558
543
///
559
544
/// The correct `monitor_key` would be:
560
545
/// `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1`
561
546
///
547
+ /// For v2 channels, the hex-encoded [`ChannelId`] is used directly for `monitor_key` instead.
548
+ ///
562
549
/// Loading a large number of monitors will be faster if done in parallel. You can use this
563
550
/// function to accomplish this. Take care to limit the number of parallel readers.
564
551
pub fn read_channel_monitor_with_updates (
@@ -604,7 +591,6 @@ where
604
591
& self , monitor_name : & MonitorName ,
605
592
) -> Result < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) , io:: Error >
606
593
{
607
- let outpoint: OutPoint = monitor_name. try_into ( ) ?;
608
594
let mut monitor_cursor = io:: Cursor :: new ( self . kv_store . read (
609
595
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
610
596
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
@@ -619,9 +605,7 @@ where
619
605
( & * self . entropy_source , & * self . signer_provider ) ,
620
606
) {
621
607
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 ( ) {
625
609
log_error ! (
626
610
self . logger,
627
611
"ChannelMonitor {} was stored under the wrong key!" ,
@@ -724,10 +708,10 @@ where
724
708
/// Persists a new channel. This means writing the entire monitor to the
725
709
/// parametrized [`KVStore`].
726
710
fn persist_new_channel (
727
- & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ,
711
+ & self , monitor : & ChannelMonitor < ChannelSigner > ,
728
712
) -> chain:: ChannelMonitorUpdateStatus {
729
713
// Determine the proper key for this monitor
730
- let monitor_name = MonitorName :: from ( funding_txo ) ;
714
+ let monitor_name = monitor . persistence_key ( ) ;
731
715
// Serialize and write the new monitor
732
716
let mut monitor_bytes = Vec :: with_capacity (
733
717
MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL . len ( ) + monitor. serialized_length ( ) ,
@@ -765,15 +749,14 @@ where
765
749
/// `update` is `None`.
766
750
/// - The update is at [`u64::MAX`], indicating an update generated by pre-0.1 LDK.
767
751
fn update_persisted_channel (
768
- & self , funding_txo : OutPoint , update : Option < & ChannelMonitorUpdate > ,
769
- monitor : & ChannelMonitor < ChannelSigner > ,
752
+ & self , update : Option < & ChannelMonitorUpdate > , monitor : & ChannelMonitor < ChannelSigner > ,
770
753
) -> chain:: ChannelMonitorUpdateStatus {
771
754
const LEGACY_CLOSED_CHANNEL_UPDATE_ID : u64 = u64:: MAX ;
772
755
if let Some ( update) = update {
756
+ let monitor_name = monitor. persistence_key ( ) ;
773
757
let persist_update = update. update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
774
758
&& update. update_id % self . maximum_pending_updates != 0 ;
775
759
if persist_update {
776
- let monitor_name = MonitorName :: from ( funding_txo) ;
777
760
let update_name = UpdateName :: from ( update. update_id ) ;
778
761
match self . kv_store . write (
779
762
CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE ,
@@ -795,7 +778,6 @@ where
795
778
} ,
796
779
}
797
780
} else {
798
- let monitor_name = MonitorName :: from ( funding_txo) ;
799
781
// In case of channel-close monitor update, we need to read old monitor before persisting
800
782
// the new one in order to determine the cleanup range.
801
783
let maybe_old_monitor = match monitor. get_latest_update_id ( ) {
@@ -804,7 +786,7 @@ where
804
786
} ;
805
787
806
788
// 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) ;
808
790
809
791
if let chain:: ChannelMonitorUpdateStatus :: Completed = monitor_update_status {
810
792
let channel_closed_legacy =
@@ -835,12 +817,12 @@ where
835
817
}
836
818
} else {
837
819
// 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)
839
821
}
840
822
}
841
823
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 ( ) ;
844
826
let monitor_key = monitor_name. as_str ( ) . to_string ( ) ;
845
827
let monitor = match self . read_channel_monitor_with_updates ( monitor_key) {
846
828
Ok ( ( _block_hash, monitor) ) => monitor,
@@ -901,14 +883,15 @@ where
901
883
/// in functions that store or retrieve [`ChannelMonitor`] snapshots.
902
884
/// It provides a consistent way to generate a unique key for channel
903
885
/// 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`.
905
888
///
906
889
/// While users of the Lightning Dev Kit library generally won't need
907
890
/// to interact with [`MonitorName`] directly, it can be useful for:
908
891
/// - Custom persistence implementations
909
892
/// - Debugging or logging channel monitor operations
910
893
/// - Extending the functionality of the `MonitorUpdatingPersister`
911
- //
894
+ ///
912
895
/// # Examples
913
896
///
914
897
/// ```
@@ -1402,10 +1385,6 @@ mod tests {
1402
1385
let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
1403
1386
let cmu_map = nodes[ 1 ] . chain_monitor . monitor_updates . lock ( ) . unwrap ( ) ;
1404
1387
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 } ;
1409
1388
1410
1389
let ro_persister = MonitorUpdatingPersister {
1411
1390
kv_store : & TestStore :: new ( true ) ,
@@ -1416,7 +1395,7 @@ mod tests {
1416
1395
broadcaster : node_cfgs[ 0 ] . tx_broadcaster ,
1417
1396
fee_estimator : node_cfgs[ 0 ] . fee_estimator ,
1418
1397
} ;
1419
- match ro_persister. persist_new_channel ( test_txo , & added_monitors[ 0 ] . 1 ) {
1398
+ match ro_persister. persist_new_channel ( & added_monitors[ 0 ] . 1 ) {
1420
1399
ChannelMonitorUpdateStatus :: UnrecoverableError => {
1421
1400
// correct result
1422
1401
} ,
@@ -1427,7 +1406,7 @@ mod tests {
1427
1406
panic ! ( "Returned InProgress when shouldn't have" )
1428
1407
} ,
1429
1408
}
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 ) {
1431
1410
ChannelMonitorUpdateStatus :: UnrecoverableError => {
1432
1411
// correct result
1433
1412
} ,
0 commit comments