Skip to content

Commit cd4714f

Browse files
committed
Make monitor update name public
These structs are meant for MonitoringUpdatingPersister implementation, but some external implementations may still reuse them, so going to make them public.
1 parent 81923c7 commit cd4714f

File tree

1 file changed

+111
-6
lines changed

1 file changed

+111
-6
lines changed

lightning/src/util/persist.rs

+111-6
Original file line numberDiff line numberDiff line change
@@ -859,21 +859,59 @@ where
859859
}
860860
}
861861

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+
/// ```
863895
#[derive(Debug)]
864-
struct MonitorName(String);
896+
pub struct MonitorName(String);
865897

866898
impl MonitorName {
867899
/// Constructs a [`MonitorName`], after verifying that an [`OutPoint`] can
868900
/// 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.
869903
pub fn new(name: String) -> Result<Self, io::Error> {
870904
MonitorName::do_try_into_outpoint(&name)?;
871905
Ok(Self(name))
872906
}
907+
873908
/// 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.
874911
pub fn as_str(&self) -> &str {
875912
&self.0
876913
}
914+
877915
/// Attempt to form a valid [`OutPoint`] from a given name string.
878916
fn do_try_into_outpoint(name: &str) -> Result<OutPoint, io::Error> {
879917
let mut parts = name.splitn(2, '_');
@@ -904,20 +942,60 @@ impl MonitorName {
904942
impl TryFrom<&MonitorName> for OutPoint {
905943
type Error = io::Error;
906944

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.
907949
fn try_from(value: &MonitorName) -> Result<Self, io::Error> {
908950
MonitorName::do_try_into_outpoint(&value.0)
909951
}
910952
}
911953

912954
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.
913959
fn from(value: OutPoint) -> Self {
914960
MonitorName(format!("{}_{}", value.txid.to_string(), value.index))
915961
}
916962
}
917963

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+
/// ```
919997
#[derive(Debug)]
920-
struct UpdateName(u64, String);
998+
pub struct UpdateName(pub u64, String);
921999

9221000
impl UpdateName {
9231001
/// Constructs an [`UpdateName`], after verifying that an update sequence ID
@@ -930,14 +1008,41 @@ impl UpdateName {
9301008
},
9311009
}
9321010
}
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+
/// ```
9351025
pub fn as_str(&self) -> &str {
9361026
&self.1
9371027
}
9381028
}
9391029

9401030
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+
/// ```
9411046
fn from(value: u64) -> Self {
9421047
Self(value, value.to_string())
9431048
}

0 commit comments

Comments
 (0)