@@ -859,21 +859,59 @@ where
859
859
}
860
860
}
861
861
862
- /// A struct representing a name for a monitor.
862
+ /// A struct representing a name for a channel monitor.
863
+ ///
864
+ /// `MonitorName` is primarily used within the [`MonitorUpdatingPersister`]
865
+ /// in functions that store or retrieve channel monitor snapshots.
866
+ /// It provides a consistent way to generate a unique key for channel
867
+ /// monitors based on their funding outpoints.
868
+ ///
869
+ /// While users of the Lightning Dev Kit library generally won't need
870
+ /// to interact with [`MonitorName`] directly, it can be useful for:
871
+ /// - Custom persistence implementations
872
+ /// - Debugging or logging channel monitor operations
873
+ /// - Extending the functionality of the `MonitorUpdatingPersister`
874
+ //
875
+ /// # Examples
876
+ ///
877
+ /// ```
878
+ /// use std::str::FromStr;
879
+ ///
880
+ /// use bitcoin::Txid;
881
+ ///
882
+ /// use lightning::util::persist::MonitorName;
883
+ /// use lightning::chain::transaction::OutPoint;
884
+ ///
885
+ /// let outpoint = OutPoint {
886
+ /// txid: Txid::from_str("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef").unwrap(),
887
+ /// index: 1,
888
+ /// };
889
+ /// let monitor_name = MonitorName::from(outpoint);
890
+ /// assert_eq!(monitor_name.as_str(), "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1");
891
+ ///
892
+ /// // Using MonitorName to generate a storage key
893
+ /// let storage_key = format!("channel_monitors/{}", monitor_name.as_str());
894
+ /// ```
863
895
#[ derive( Debug ) ]
864
- struct MonitorName ( String ) ;
896
+ pub struct MonitorName ( String ) ;
865
897
866
898
impl MonitorName {
867
899
/// Constructs a [`MonitorName`], after verifying that an [`OutPoint`] can
868
900
/// be formed from the given `name`.
901
+ /// This method is useful if you have a String and you want to verify that
902
+ /// it's a valid storage key for a channel monitor.
869
903
pub fn new ( name : String ) -> Result < Self , io:: Error > {
870
904
MonitorName :: do_try_into_outpoint ( & name) ?;
871
905
Ok ( Self ( name) )
872
906
}
907
+
873
908
/// Convert this monitor name to a str.
909
+ /// This method is particularly useful when you need to use the monitor name
910
+ /// as a key in a key-value store or when logging.
874
911
pub fn as_str ( & self ) -> & str {
875
912
& self . 0
876
913
}
914
+
877
915
/// Attempt to form a valid [`OutPoint`] from a given name string.
878
916
fn do_try_into_outpoint ( name : & str ) -> Result < OutPoint , io:: Error > {
879
917
let mut parts = name. splitn ( 2 , '_' ) ;
@@ -904,20 +942,60 @@ impl MonitorName {
904
942
impl TryFrom < & MonitorName > for OutPoint {
905
943
type Error = io:: Error ;
906
944
945
+ /// Attempts to convert a `MonitorName` back into an `OutPoint`.
946
+ ///
947
+ /// This is useful when you have a `MonitorName` (perhaps retrieved from storage)
948
+ /// and need to reconstruct the original `OutPoint` it represents.
907
949
fn try_from ( value : & MonitorName ) -> Result < Self , io:: Error > {
908
950
MonitorName :: do_try_into_outpoint ( & value. 0 )
909
951
}
910
952
}
911
953
912
954
impl From < OutPoint > for MonitorName {
955
+ /// Creates a `MonitorName` from an `OutPoint`.
956
+ ///
957
+ /// This is typically used when you need to generate a storage key or identifier
958
+ /// for a new or existing channel monitor.
913
959
fn from ( value : OutPoint ) -> Self {
914
960
MonitorName ( format ! ( "{}_{}" , value. txid. to_string( ) , value. index) )
915
961
}
916
962
}
917
963
918
- /// A struct representing a name for an update.
964
+ /// A struct representing a name for a channel monitor update.
965
+ ///
966
+ /// `UpdateName` is primarily used within the `MonitorUpdatingPersister` in
967
+ /// functions that store or retrieve partial updates to channel monitors. It
968
+ /// provides a consistent way to generate and parse unique identifiers for
969
+ /// monitor updates based on their sequence number.
970
+ ///
971
+ /// The name is derived from the update's sequence ID, which is a monotonically
972
+ /// increasing u64 value. This format allows for easy ordering of updates and
973
+ /// efficient storage and retrieval in key-value stores.
974
+ ///
975
+ /// # Usage
976
+ ///
977
+ /// While users of the Lightning Dev Kit library generally won't need to
978
+ /// interact with `UpdateName` directly, it still can be useful for custom
979
+ /// persistence implementations. The u64 value is the update_id that can be
980
+ /// compared with [ChannelMonitor::get_latest_update_id] to check if this update
981
+ /// has been applied to the channel monitor or not, which is useful for pruning
982
+ /// stale channel monitor updates off persistence.
983
+ ///
984
+ /// # Examples
985
+ ///
986
+ /// ```
987
+ /// use lightning::util::persist::UpdateName;
988
+ ///
989
+ /// let update_id: u64 = 42;
990
+ /// let update_name = UpdateName::from(update_id);
991
+ /// assert_eq!(update_name.as_str(), "42");
992
+ ///
993
+ /// // Using UpdateName to generate a storage key
994
+ /// let monitor_name = "some_monitor_name";
995
+ /// let storage_key = format!("channel_monitor_updates/{}/{}", monitor_name, update_name.as_str());
996
+ /// ```
919
997
#[ derive( Debug ) ]
920
- struct UpdateName ( u64 , String ) ;
998
+ pub struct UpdateName ( pub u64 , String ) ;
921
999
922
1000
impl UpdateName {
923
1001
/// Constructs an [`UpdateName`], after verifying that an update sequence ID
@@ -930,14 +1008,41 @@ impl UpdateName {
930
1008
} ,
931
1009
}
932
1010
}
933
-
934
- /// Convert this monitor update name to a &str
1011
+
1012
+ /// Convert this update name to a string slice.
1013
+ ///
1014
+ /// This method is particularly useful when you need to use the update name
1015
+ /// as part of a key in a key-value store or when logging.
1016
+ ///
1017
+ /// # Examples
1018
+ ///
1019
+ /// ```
1020
+ /// use lightning::util::persist::UpdateName;
1021
+ ///
1022
+ /// let update_name = UpdateName::from(42);
1023
+ /// assert_eq!(update_name.as_str(), "42");
1024
+ /// ```
935
1025
pub fn as_str ( & self ) -> & str {
936
1026
& self . 1
937
1027
}
938
1028
}
939
1029
940
1030
impl From < u64 > for UpdateName {
1031
+ /// Creates an `UpdateName` from a `u64`.
1032
+ ///
1033
+ /// This is typically used when you need to generate a storage key or
1034
+ /// identifier
1035
+ /// for a new channel monitor update.
1036
+ ///
1037
+ /// # Examples
1038
+ ///
1039
+ /// ```
1040
+ /// use lightning::util::persist::UpdateName;
1041
+ ///
1042
+ /// let update_id: u64 = 42;
1043
+ /// let update_name = UpdateName::from(update_id);
1044
+ /// assert_eq!(update_name.as_str(), "42");
1045
+ /// ```
941
1046
fn from ( value : u64 ) -> Self {
942
1047
Self ( value, value. to_string ( ) )
943
1048
}
0 commit comments