@@ -11,7 +11,6 @@ use crate::nodemanager::ChannelClosure;
1111use crate :: storage:: { MutinyStorage , VersionedValue } ;
1212use crate :: utils;
1313use crate :: utils:: { sleep, spawn} ;
14- use crate :: vss:: VssKeyValueItem ;
1514use crate :: { chain:: MutinyChain , scorer:: HubPreferentialScorer } ;
1615use anyhow:: anyhow;
1716use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
@@ -93,6 +92,15 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
9392 format ! ( "{}_{}" , key, self . node_id)
9493 }
9594
95+ pub ( crate ) fn get_monitor_key ( & self , funding_txo : & OutPoint ) -> String {
96+ let key = format ! (
97+ "{MONITORS_PREFIX_KEY}{}_{}" ,
98+ funding_txo. txid. to_hex( ) ,
99+ funding_txo. index
100+ ) ;
101+ self . get_key ( & key)
102+ }
103+
96104 fn init_persist_monitor < W : Writeable > (
97105 & self ,
98106 key : String ,
@@ -105,47 +113,30 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
105113 let logger = self . logger . clone ( ) ;
106114 let object = object. encode ( ) ;
107115
108- // currently we only retry storage to VSS because we don't have a way to detect
109- // for local storage if a higher version has been persisted. Without handling this
110- // we could end up with a previous state being persisted over a newer one.
111- // VSS does not have this problem because it verifies the version before storing
112- // and will not overwrite a newer version, so it is safe to retry.
113116 spawn ( async move {
114- let mut is_retry = false ;
115117 // Sleep before persisting to give chance for the manager to be persisted
116118 sleep ( 50 ) . await ;
117- loop {
118- match persist_monitor ( & storage, & key, & object, Some ( version) , is_retry, & logger)
119- . await
120- {
121- Ok ( ( ) ) => {
122- log_debug ! ( logger, "Persisted channel monitor: {update_id:?}" ) ;
123-
124- // unwrap is safe, we set it up immediately
125- let chain_monitor = chain_monitor. lock ( ) . await ;
126- let chain_monitor = chain_monitor. as_ref ( ) . unwrap ( ) ;
127-
128- // these errors are not fatal, so we don't return them just log
129- if let Err ( e) = chain_monitor. channel_monitor_updated (
130- update_id. funding_txo ,
131- update_id. monitor_update_id ,
132- ) {
133- log_error ! (
134- logger,
135- "Error notifying chain monitor of channel monitor update: {e:?}"
136- ) ;
137- } else {
138- break ; // successful storage, no more attempts
139- }
140- }
141- Err ( e) => {
142- log_error ! ( logger, "Error persisting channel monitor: {e}" ) ;
119+ match persist_monitor ( & storage, & key, & object, Some ( version) , & logger) . await {
120+ Ok ( ( ) ) => {
121+ log_debug ! ( logger, "Persisted channel monitor: {update_id:?}" ) ;
122+
123+ // unwrap is safe, we set it up immediately
124+ let chain_monitor = chain_monitor. lock ( ) . await ;
125+ let chain_monitor = chain_monitor. as_ref ( ) . unwrap ( ) ;
126+
127+ // these errors are not fatal, so we don't return them just log
128+ if let Err ( e) = chain_monitor
129+ . channel_monitor_updated ( update_id. funding_txo , update_id. monitor_update_id )
130+ {
131+ log_error ! (
132+ logger,
133+ "Error notifying chain monitor of channel monitor update: {e:?}"
134+ ) ;
143135 }
144136 }
145-
146- // if we get here, we failed to persist, so we retry
147- is_retry = true ;
148- sleep ( 1_000 ) . await ;
137+ Err ( e) => {
138+ log_error ! ( logger, "Error persisting channel monitor: {e}" ) ;
139+ }
149140 }
150141 } ) ;
151142
@@ -675,15 +666,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, S: MutinyStorage> Persist<Chann
675666 monitor : & ChannelMonitor < ChannelSigner > ,
676667 monitor_update_id : MonitorUpdateId ,
677668 ) -> ChannelMonitorUpdateStatus {
678- let key = format ! (
679- "{MONITORS_PREFIX_KEY}{}_{}" ,
680- funding_txo. txid. to_hex( ) ,
681- funding_txo. index
682- ) ;
683- let key = self . get_key ( & key) ;
669+ let key = self . get_monitor_key ( & funding_txo) ;
684670
685671 let update_id = monitor. get_latest_update_id ( ) ;
686- debug_assert ! ( update_id == utils:: get_monitor_version( monitor. encode( ) ) ) ;
672+ debug_assert ! ( update_id == utils:: get_monitor_version( & monitor. encode( ) ) ) ;
687673
688674 // safely convert u64 to u32
689675 let version = if update_id >= u32:: MAX as u64 {
@@ -707,14 +693,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, S: MutinyStorage> Persist<Chann
707693 monitor : & ChannelMonitor < ChannelSigner > ,
708694 monitor_update_id : MonitorUpdateId ,
709695 ) -> ChannelMonitorUpdateStatus {
710- let key = format ! (
711- "{MONITORS_PREFIX_KEY}{}_{}" ,
712- funding_txo. txid. to_hex( ) ,
713- funding_txo. index
714- ) ;
715- let key = self . get_key ( & key) ;
696+ let key = self . get_monitor_key ( & funding_txo) ;
716697 let update_id = monitor. get_latest_update_id ( ) ;
717- debug_assert ! ( update_id == utils:: get_monitor_version( monitor. encode( ) ) ) ;
698+ debug_assert ! ( update_id == utils:: get_monitor_version( & monitor. encode( ) ) ) ;
718699
719700 // safely convert u64 to u32
720701 let version = if update_id >= u32:: MAX as u64 {
@@ -738,33 +719,14 @@ pub struct MonitorUpdateIdentifier {
738719 pub monitor_update_id : MonitorUpdateId ,
739720}
740721
741- async fn persist_monitor (
722+ pub ( crate ) async fn persist_monitor (
742723 storage : & impl MutinyStorage ,
743724 key : & str ,
744725 object : & Vec < u8 > ,
745726 version : Option < u32 > ,
746- vss_only : bool ,
747727 logger : & MutinyLogger ,
748728) -> Result < ( ) , lightning:: io:: Error > {
749- let res = if vss_only {
750- // if we are only storing to VSS, we don't need to store to local storage
751- // just need to call put_objects on VSS
752- if let ( Some ( vss) , Some ( version) ) = ( storage. vss_client ( ) , version) {
753- let value =
754- serde_json:: to_value ( object) . map_err ( |_| lightning:: io:: ErrorKind :: Other ) ?;
755- let item = VssKeyValueItem {
756- key : key. to_string ( ) ,
757- value,
758- version,
759- } ;
760-
761- vss. put_objects ( vec ! [ item] ) . await
762- } else {
763- Ok ( ( ) )
764- }
765- } else {
766- storage. set_data_async ( key, object, version) . await
767- } ;
729+ let res = storage. set_data_async ( key, object, version) . await ;
768730
769731 res. map_err ( |e| {
770732 match e {
0 commit comments