@@ -58,10 +58,10 @@ pub const CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
58
58
/// The primary namespace under which [`ChannelMonitorUpdate`]s will be persisted.
59
59
pub const CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE : & str = "monitor_updates" ;
60
60
61
- /// The primary namespace under which [`ChannelMonitor`]s will be persisted.
62
- pub const PRUNED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE : & str = "pruned_monitors " ;
63
- /// The secondary namespace under which [`ChannelMonitor`]s will be persisted.
64
- pub const PRUNED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE : & str = "" ;
61
+ /// The primary namespace under which archived [`ChannelMonitor`]s will be persisted.
62
+ pub const ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE : & str = "archived_monitors " ;
63
+ /// The secondary namespace under which archived [`ChannelMonitor`]s will be persisted.
64
+ pub const ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE : & str = "" ;
65
65
66
66
/// The primary namespace under which the [`NetworkGraph`] will be persisted.
67
67
pub const NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE : & str = "" ;
@@ -247,15 +247,38 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
247
247
}
248
248
}
249
249
250
- fn prune_persisted_channel ( & self , funding_txo : OutPoint ) -> bool {
250
+ fn archive_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
251
+ let monitor_name = MonitorName :: from ( funding_txo) ;
252
+ let monitor = match self . read (
253
+ CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
254
+ CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
255
+ monitor_name. as_str ( ) ,
256
+ ) {
257
+ Ok ( monitor) => monitor,
258
+ Err ( _) => return chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
259
+
260
+ } ;
261
+ match self . write (
262
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
263
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
264
+ monitor_name. as_str ( ) ,
265
+ & monitor
266
+ ) {
267
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
268
+ Err ( _e) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError // TODO: Should we return UnrecoverableError here?
269
+ }
270
+ }
271
+
272
+ fn remove_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
251
273
let key = format ! ( "{}_{}" , funding_txo. txid. to_string( ) , funding_txo. index) ;
252
274
match self . remove (
253
275
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
254
276
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
255
- & key, false )
256
- {
257
- Ok ( ( ) ) => true ,
258
- Err ( _) => false
277
+ & key,
278
+ false ,
279
+ ) {
280
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
281
+ Err ( _) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
259
282
}
260
283
}
261
284
}
@@ -290,15 +313,38 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> Persist<ChannelSigner> for dyn
290
313
}
291
314
}
292
315
293
- fn prune_persisted_channel ( & self , funding_txo : OutPoint ) -> bool {
316
+ fn archive_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
317
+ let monitor_name = MonitorName :: from ( funding_txo) ;
318
+ let monitor = match self . read (
319
+ CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
320
+ CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
321
+ monitor_name. as_str ( ) ,
322
+ ) {
323
+ Ok ( monitor) => monitor,
324
+ Err ( _) => return chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
325
+
326
+ } ;
327
+ match self . write (
328
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
329
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
330
+ monitor_name. as_str ( ) ,
331
+ & monitor
332
+ ) {
333
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
334
+ Err ( _e) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError // TODO: Should we return UnrecoverableError here?
335
+ }
336
+ }
337
+
338
+ fn remove_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
294
339
let key = format ! ( "{}_{}" , funding_txo. txid. to_string( ) , funding_txo. index) ;
295
340
match self . remove (
296
341
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
297
342
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
298
- & key, false )
299
- {
300
- Ok ( ( ) ) => true ,
301
- Err ( _) => false
343
+ & key,
344
+ false ,
345
+ ) {
346
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
347
+ Err ( _) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
302
348
}
303
349
}
304
350
}
@@ -623,6 +669,46 @@ where
623
669
}
624
670
}
625
671
}
672
+ /// Read an archived channel monitor.
673
+ fn read_archived_channel_monitor ( & self , archived_monitor_name : & MonitorName ) -> Result < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) , io:: Error > {
674
+ let outpoint: OutPoint = archived_monitor_name. try_into ( ) ?;
675
+ let mut monitor_cursor = io:: Cursor :: new ( self . kv_store . read (
676
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
677
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
678
+ archived_monitor_name. as_str ( ) ,
679
+ ) ?) ;
680
+ match <( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) >:: read (
681
+ & mut monitor_cursor,
682
+ ( & * self . entropy_source , & * self . signer_provider ) ,
683
+ ) {
684
+ Ok ( ( blockhash, channel_monitor) ) => {
685
+ if channel_monitor. get_funding_txo ( ) . 0 . txid != outpoint. txid
686
+ || channel_monitor. get_funding_txo ( ) . 0 . index != outpoint. index
687
+ {
688
+ log_error ! (
689
+ self . logger,
690
+ "Archived ChannelMonitor {} was stored under the wrong key!" ,
691
+ archived_monitor_name. as_str( )
692
+ ) ;
693
+ Err ( io:: Error :: new (
694
+ io:: ErrorKind :: InvalidData ,
695
+ "Archived ChannelMonitor was stored under the wrong key" ,
696
+ ) )
697
+ } else {
698
+ Ok ( ( blockhash, channel_monitor) )
699
+ }
700
+ }
701
+ Err ( e) => {
702
+ log_error ! (
703
+ self . logger,
704
+ "Failed to read archived ChannelMonitor {}, reason: {}" ,
705
+ archived_monitor_name. as_str( ) ,
706
+ e,
707
+ ) ;
708
+ Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "Failed to read archived ChannelMonitor" ) )
709
+ }
710
+ }
711
+ }
626
712
627
713
/// Read a channel monitor update.
628
714
fn read_monitor_update (
@@ -808,26 +894,33 @@ where
808
894
}
809
895
}
810
896
811
- fn prune_persisted_channel ( & self , funding_txo : OutPoint ) -> bool {
897
+ fn archive_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
898
+ let monitor_name = MonitorName :: from ( funding_txo) ;
899
+ let monitor = match self . read_monitor ( & monitor_name) {
900
+ Ok ( ( _block_hash, monitor) ) => monitor,
901
+ Err ( _) => return chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
902
+ } ;
903
+ match self . kv_store . write (
904
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
905
+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
906
+ monitor_name. as_str ( ) ,
907
+ & monitor. encode ( )
908
+ ) {
909
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
910
+ Err ( _e) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError // TODO: Should we return UnrecoverableError here?
911
+ }
912
+ }
913
+
914
+ fn remove_persisted_channel ( & self , funding_txo : OutPoint ) -> chain:: ChannelMonitorUpdateStatus {
812
915
let monitor_name = MonitorName :: from ( funding_txo) ;
813
916
match self . kv_store . remove (
814
917
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
815
918
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
816
919
monitor_name. as_str ( ) ,
817
920
false ,
818
921
) {
819
- Ok ( ( ) ) => true ,
820
- Err ( e) => {
821
- log_error ! (
822
- self . logger,
823
- "Failed to remove ChannelMonitor {}/{}/{} reason: {}" ,
824
- CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
825
- CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
826
- monitor_name. as_str( ) ,
827
- e
828
- ) ;
829
- false
830
- }
922
+ Ok ( ( ) ) => chain:: ChannelMonitorUpdateStatus :: Completed ,
923
+ Err ( _) => chain:: ChannelMonitorUpdateStatus :: UnrecoverableError
831
924
}
832
925
}
833
926
}
0 commit comments