@@ -198,43 +198,54 @@ where
198198 }
199199 Ok ( res)
200200}
201-
202201enum KVStoreUpdatingPersisterError {
203- /// The monitor name was improperly formatted.
204- BadMonitorName { reason : String , context : String } ,
205- /// The monitor could not be decoded.
206- MonitorDecodeFailed { reason : String , context : String } ,
207- /// The update could not be decoded.
208- UpdateDecodeFailed { reason : String , context : String } ,
209- /// Storage could not be read.
210- StorageReadFailed { reason : String , context : String } ,
211- /// An update could not be applied to a monitor.
212- UpdateFailed { reason : String , context : String } ,
202+ /// The monitor name was improperly formatted.
203+ BadMonitorName { reason : String , context : String } ,
204+ /// The monitor could not be decoded.
205+ MonitorDecodeFailed {
206+ reason : DecodeError ,
207+ context : String ,
208+ } ,
209+ /// The update could not be decoded.
210+ UpdateDecodeFailed {
211+ reason : DecodeError ,
212+ context : String ,
213+ } ,
214+ /// Storage could not be read.
215+ StorageReadFailed { reason : io:: Error , context : String } ,
216+ /// An update could not be applied to a monitor.
217+ UpdateFailed { reason : String , context : String } ,
213218}
214219
215220impl From < KVStoreUpdatingPersisterError > for io:: Error {
216- fn from ( value : KVStoreUpdatingPersisterError ) -> Self {
217- match value {
218- KVStoreUpdatingPersisterError :: BadMonitorName { reason, context} => io:: Error :: new (
219- io:: ErrorKind :: InvalidInput ,
220- format ! ( "{}, context: {}'" , reason, context) ,
221- ) ,
222- KVStoreUpdatingPersisterError :: MonitorDecodeFailed { reason, context} => io:: Error :: new (
223- io:: ErrorKind :: InvalidData ,
224- format ! ( "{}, context: {}'" , reason, context) ,
225- ) ,
226- KVStoreUpdatingPersisterError :: UpdateDecodeFailed { reason, context} => io:: Error :: new (
227- io:: ErrorKind :: InvalidData ,
228- format ! ( "{}, context: {}'" , reason, context) ,
229- ) ,
230- KVStoreUpdatingPersisterError :: StorageReadFailed { reason, context} => {
231- io:: Error :: new ( io:: ErrorKind :: Other , format ! ( "{}, context: {}'" , reason, context) )
232- }
233- KVStoreUpdatingPersisterError :: UpdateFailed { reason, context} => {
234- io:: Error :: new ( io:: ErrorKind :: InvalidData , format ! ( "{}, context: {}'" , reason, context) )
235- }
236- }
237- }
221+ fn from ( value : KVStoreUpdatingPersisterError ) -> Self {
222+ match value {
223+ KVStoreUpdatingPersisterError :: BadMonitorName { reason, context } => io:: Error :: new (
224+ io:: ErrorKind :: InvalidInput ,
225+ format ! ( "{}, context: {}'" , reason, context) ,
226+ ) ,
227+ KVStoreUpdatingPersisterError :: MonitorDecodeFailed { reason, context } => {
228+ io:: Error :: new (
229+ io:: ErrorKind :: InvalidData ,
230+ format ! ( "{}, context: {}'" , reason, context) ,
231+ )
232+ }
233+ KVStoreUpdatingPersisterError :: UpdateDecodeFailed { reason, context } => {
234+ io:: Error :: new (
235+ io:: ErrorKind :: InvalidData ,
236+ format ! ( "{}, context: {}'" , reason, context) ,
237+ )
238+ }
239+ KVStoreUpdatingPersisterError :: StorageReadFailed { reason, context } => io:: Error :: new (
240+ io:: ErrorKind :: Other ,
241+ format ! ( "{}, context: {}'" , reason, context) ,
242+ ) ,
243+ KVStoreUpdatingPersisterError :: UpdateFailed { reason, context } => io:: Error :: new (
244+ io:: ErrorKind :: InvalidData ,
245+ format ! ( "{}, context: {}'" , reason, context) ,
246+ ) ,
247+ }
248+ }
238249}
239250
240251/// A struct representing a name for a monitor.
@@ -253,26 +264,27 @@ impl TryFrom<MonitorName> for OutPoint {
253264
254265 fn try_from ( value : MonitorName ) -> Result < Self , io:: Error > {
255266 let mut parts = value. 0 . splitn ( 2 , '_' ) ;
256- let txid_hex = parts. next ( ) . ok_or_else ( || {
257- KVStoreUpdatingPersisterError :: BadMonitorName {
258- reason : "no txid found, maybe there is no underscore" . to_string ( ) ,
259- context : value. 0 . clone ( ) ,
260- }
261- } ) ?;
262- let index = parts. next ( ) . ok_or_else ( || {
263- KVStoreUpdatingPersisterError :: BadMonitorName {
267+ let txid_hex =
268+ parts
269+ . next ( )
270+ . ok_or_else ( || KVStoreUpdatingPersisterError :: BadMonitorName {
271+ reason : "no txid found, maybe there is no underscore" . to_string ( ) ,
272+ context : value. 0 . clone ( ) ,
273+ } ) ?;
274+ let index = parts
275+ . next ( )
276+ . ok_or_else ( || KVStoreUpdatingPersisterError :: BadMonitorName {
264277 reason : "no index value found after underscore" . to_string ( ) ,
265278 context : value. 0 . clone ( ) ,
266- }
267- } ) ?;
268- let index = index. parse ( ) . map_err ( |e| {
269- KVStoreUpdatingPersisterError :: BadMonitorName {
270- reason : format ! ( "bad index value, caused by {e}" ) ,
271- context : value. 0 . clone ( ) ,
272- }
273- } ) ?;
279+ } ) ?;
280+ let index = index
281+ . parse ( )
282+ . map_err ( |e| KVStoreUpdatingPersisterError :: BadMonitorName {
283+ reason : format ! ( "bad index value, caused by {e}" ) ,
284+ context : value. 0 . clone ( ) ,
285+ } ) ?;
274286 let txid = Txid :: from_hex ( txid_hex) . map_err ( |e| {
275- KVStoreUpdatingPersisterError :: BadMonitorName {
287+ KVStoreUpdatingPersisterError :: BadMonitorName {
276288 reason : format ! ( "bad txid, caused by: {e}" ) ,
277289 context : value. 0 . clone ( ) ,
278290 }
@@ -353,11 +365,9 @@ where
353365 {
354366 monitor
355367 . update_monitor ( & update, broadcaster, fee_estimator. clone ( ) , & self . logger )
356- . map_err ( |_| {
357- KVStoreUpdatingPersisterError :: UpdateFailed {
358- reason : "update_monitor returned Err(())" . to_string ( ) ,
359- context : format ! ( "monitor: {:?}" , monitor_name) ,
360- }
368+ . map_err ( |_| KVStoreUpdatingPersisterError :: UpdateFailed {
369+ reason : "update_monitor returned Err(())" . to_string ( ) ,
370+ context : format ! ( "monitor: {:?}" , monitor_name) ,
361371 } ) ?;
362372 }
363373 }
@@ -414,22 +424,29 @@ where
414424 & mut self
415425 . kv
416426 . read ( CHANNEL_MONITOR_PERSISTENCE_NAMESPACE , & key)
417- . map_err ( |e| KVStoreUpdatingPersisterError :: StorageReadFailed { reason : e. to_string ( ) , context : key. clone ( ) } ) ?,
427+ . map_err ( |e| KVStoreUpdatingPersisterError :: StorageReadFailed {
428+ reason : e,
429+ context : key. clone ( ) ,
430+ } ) ?,
418431 ( & * entropy_source, & * signer_provider) ,
419432 ) {
420433 Ok ( ( blockhash, channel_monitor) ) => {
421434 if channel_monitor. get_funding_txo ( ) . 0 . txid != outpoint. txid
422435 || channel_monitor. get_funding_txo ( ) . 0 . index != outpoint. index
423436 {
424- return Err ( KVStoreUpdatingPersisterError :: MonitorDecodeFailed {
425- reason : DecodeError :: InvalidValue . to_string ( ) ,
437+ return Err ( KVStoreUpdatingPersisterError :: MonitorDecodeFailed {
438+ reason : DecodeError :: InvalidValue ,
426439 context : key,
427440 }
428441 . into ( ) ) ;
429442 }
430443 Ok ( ( blockhash, channel_monitor) )
431444 }
432- Err ( e) => Err ( KVStoreUpdatingPersisterError :: MonitorDecodeFailed { reason : e. to_string ( ) , context : key} . into ( ) ) ,
445+ Err ( e) => Err ( KVStoreUpdatingPersisterError :: MonitorDecodeFailed {
446+ reason : e,
447+ context : key,
448+ }
449+ . into ( ) ) ,
433450 }
434451 }
435452
@@ -441,13 +458,18 @@ where
441458 ) -> io:: Result < ChannelMonitorUpdate > {
442459 let ns = self . monitor_update_namespace ( monitor_name) ;
443460 let key = update_name. storage_key ( ) ;
444- Ok ( ChannelMonitorUpdate :: read (
445- & mut self
446- . kv
447- . read ( & ns, & key)
448- . map_err ( |e| KVStoreUpdatingPersisterError :: StorageReadFailed { reason : e. to_string ( ) , context : key. clone ( ) } ) ?,
461+ Ok (
462+ ChannelMonitorUpdate :: read ( & mut self . kv . read ( & ns, & key) . map_err ( |e| {
463+ KVStoreUpdatingPersisterError :: StorageReadFailed {
464+ reason : e,
465+ context : key. clone ( ) ,
466+ }
467+ } ) ?)
468+ . map_err ( |e| KVStoreUpdatingPersisterError :: UpdateDecodeFailed {
469+ reason : e,
470+ context : key,
471+ } ) ?,
449472 )
450- . map_err ( |e| KVStoreUpdatingPersisterError :: UpdateDecodeFailed { reason : e. to_string ( ) , context : key} ) ?)
451473 }
452474
453475 /// Delete updates with an update_id lower than the given channel monitor.
@@ -681,12 +703,12 @@ mod tests {
681703 assert ! ( persister. kv. remove( "namespace" , "key" , ) . is_err( ) ) ;
682704 }
683705
684- fn is_sorted < T > ( data : & [ T ] ) -> bool
685- where
686- T : Ord ,
687- {
688- data. windows ( 2 ) . all ( |w| w[ 0 ] <= w[ 1 ] )
689- }
706+ fn is_sorted < T > ( data : & [ T ] ) -> bool
707+ where
708+ T : Ord ,
709+ {
710+ data. windows ( 2 ) . all ( |w| w[ 0 ] <= w[ 1 ] )
711+ }
690712
691713 // =================================
692714 // TESTS
@@ -790,20 +812,20 @@ mod tests {
790812 index : 1 ,
791813 } ;
792814 let monitor_name = MonitorName :: try_from ( outpoint) . unwrap ( ) ;
793- for i in 2 ..=1000 {
794- persister
795- . kv
796- . write (
797- & persister. monitor_update_namespace ( & monitor_name) ,
798- & UpdateName :: from ( i) . storage_key ( ) ,
799- & [ 0 ] ,
800- )
801- . unwrap ( ) ;
802- }
815+ for i in 2 ..=1000 {
816+ persister
817+ . kv
818+ . write (
819+ & persister. monitor_update_namespace ( & monitor_name) ,
820+ & UpdateName :: from ( i) . storage_key ( ) ,
821+ & [ 0 ] ,
822+ )
823+ . unwrap ( ) ;
824+ }
803825 // Check that we get the right number of updates, in order
804826 let listed_update_names = persister. list_update_names ( & monitor_name) . unwrap ( ) ;
805827 assert_eq ! ( listed_update_names. len( ) , 999 ) ;
806- assert ! ( is_sorted( & listed_update_names) ) ;
828+ assert ! ( is_sorted( & listed_update_names) ) ;
807829 }
808830
809831 #[ test]
0 commit comments