Skip to content

Commit

Permalink
add back useful typing in error variants, rustfmt new code
Browse files Browse the repository at this point in the history
  • Loading branch information
domZippilli committed Aug 21, 2023
1 parent 3f052f9 commit a7962e3
Showing 1 changed file with 104 additions and 82 deletions.
186 changes: 104 additions & 82 deletions lightning/src/util/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,43 +198,54 @@ where
}
Ok(res)
}

enum KVStoreUpdatingPersisterError {
/// The monitor name was improperly formatted.
BadMonitorName {reason: String, context: String},
/// The monitor could not be decoded.
MonitorDecodeFailed {reason: String, context: String},
/// The update could not be decoded.
UpdateDecodeFailed {reason: String, context: String},
/// Storage could not be read.
StorageReadFailed {reason: String, context: String},
/// An update could not be applied to a monitor.
UpdateFailed {reason: String, context: String},
/// The monitor name was improperly formatted.
BadMonitorName { reason: String, context: String },
/// The monitor could not be decoded.
MonitorDecodeFailed {
reason: DecodeError,
context: String,
},
/// The update could not be decoded.
UpdateDecodeFailed {
reason: DecodeError,
context: String,
},
/// Storage could not be read.
StorageReadFailed { reason: io::Error, context: String },
/// An update could not be applied to a monitor.
UpdateFailed { reason: String, context: String },
}

impl From<KVStoreUpdatingPersisterError> for io::Error {
fn from(value: KVStoreUpdatingPersisterError) -> Self {
match value {
KVStoreUpdatingPersisterError::BadMonitorName{reason, context} => io::Error::new(
io::ErrorKind::InvalidInput,
format!("{}, context: {}'", reason, context),
),
KVStoreUpdatingPersisterError::MonitorDecodeFailed{reason, context} => io::Error::new(
io::ErrorKind::InvalidData,
format!("{}, context: {}'", reason, context),
),
KVStoreUpdatingPersisterError::UpdateDecodeFailed{reason, context} => io::Error::new(
io::ErrorKind::InvalidData,
format!("{}, context: {}'", reason, context),
),
KVStoreUpdatingPersisterError::StorageReadFailed{reason, context} => {
io::Error::new(io::ErrorKind::Other, format!("{}, context: {}'", reason, context))
}
KVStoreUpdatingPersisterError::UpdateFailed{reason, context} => {
io::Error::new(io::ErrorKind::InvalidData, format!("{}, context: {}'", reason, context))
}
}
}
fn from(value: KVStoreUpdatingPersisterError) -> Self {
match value {
KVStoreUpdatingPersisterError::BadMonitorName { reason, context } => io::Error::new(
io::ErrorKind::InvalidInput,
format!("{}, context: {}'", reason, context),
),
KVStoreUpdatingPersisterError::MonitorDecodeFailed { reason, context } => {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{}, context: {}'", reason, context),
)
}
KVStoreUpdatingPersisterError::UpdateDecodeFailed { reason, context } => {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{}, context: {}'", reason, context),
)
}
KVStoreUpdatingPersisterError::StorageReadFailed { reason, context } => io::Error::new(
io::ErrorKind::Other,
format!("{}, context: {}'", reason, context),
),
KVStoreUpdatingPersisterError::UpdateFailed { reason, context } => io::Error::new(
io::ErrorKind::InvalidData,
format!("{}, context: {}'", reason, context),
),
}
}
}

/// A struct representing a name for a monitor.
Expand All @@ -253,26 +264,27 @@ impl TryFrom<MonitorName> for OutPoint {

fn try_from(value: MonitorName) -> Result<Self, io::Error> {
let mut parts = value.0.splitn(2, '_');
let txid_hex = parts.next().ok_or_else(|| {
KVStoreUpdatingPersisterError::BadMonitorName{
reason: "no txid found, maybe there is no underscore".to_string(),
context: value.0.clone(),
}
})?;
let index = parts.next().ok_or_else(|| {
KVStoreUpdatingPersisterError::BadMonitorName{
let txid_hex =
parts
.next()
.ok_or_else(|| KVStoreUpdatingPersisterError::BadMonitorName {
reason: "no txid found, maybe there is no underscore".to_string(),
context: value.0.clone(),
})?;
let index = parts
.next()
.ok_or_else(|| KVStoreUpdatingPersisterError::BadMonitorName {
reason: "no index value found after underscore".to_string(),
context: value.0.clone(),
}
})?;
let index = index.parse().map_err(|e| {
KVStoreUpdatingPersisterError::BadMonitorName {
reason: format!("bad index value, caused by {e}"),
context: value.0.clone(),
}
})?;
})?;
let index = index
.parse()
.map_err(|e| KVStoreUpdatingPersisterError::BadMonitorName {
reason: format!("bad index value, caused by {e}"),
context: value.0.clone(),
})?;
let txid = Txid::from_hex(txid_hex).map_err(|e| {
KVStoreUpdatingPersisterError::BadMonitorName{
KVStoreUpdatingPersisterError::BadMonitorName {
reason: format!("bad txid, caused by: {e}"),
context: value.0.clone(),
}
Expand Down Expand Up @@ -353,11 +365,9 @@ where
{
monitor
.update_monitor(&update, broadcaster, fee_estimator.clone(), &self.logger)
.map_err(|_| {
KVStoreUpdatingPersisterError::UpdateFailed{
reason: "update_monitor returned Err(())".to_string(),
context: format!("monitor: {:?}", monitor_name),
}
.map_err(|_| KVStoreUpdatingPersisterError::UpdateFailed {
reason: "update_monitor returned Err(())".to_string(),
context: format!("monitor: {:?}", monitor_name),
})?;
}
}
Expand Down Expand Up @@ -414,22 +424,29 @@ where
&mut self
.kv
.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, &key)
.map_err(|e| KVStoreUpdatingPersisterError::StorageReadFailed{ reason: e.to_string(), context: key.clone()})?,
.map_err(|e| KVStoreUpdatingPersisterError::StorageReadFailed {
reason: e,
context: key.clone(),
})?,
(&*entropy_source, &*signer_provider),
) {
Ok((blockhash, channel_monitor)) => {
if channel_monitor.get_funding_txo().0.txid != outpoint.txid
|| channel_monitor.get_funding_txo().0.index != outpoint.index
{
return Err(KVStoreUpdatingPersisterError::MonitorDecodeFailed{
reason: DecodeError::InvalidValue.to_string(),
return Err(KVStoreUpdatingPersisterError::MonitorDecodeFailed {
reason: DecodeError::InvalidValue,
context: key,
}
.into());
}
Ok((blockhash, channel_monitor))
}
Err(e) => Err(KVStoreUpdatingPersisterError::MonitorDecodeFailed{ reason: e.to_string(), context: key}.into()),
Err(e) => Err(KVStoreUpdatingPersisterError::MonitorDecodeFailed {
reason: e,
context: key,
}
.into()),
}
}

Expand All @@ -441,13 +458,18 @@ where
) -> io::Result<ChannelMonitorUpdate> {
let ns = self.monitor_update_namespace(monitor_name);
let key = update_name.storage_key();
Ok(ChannelMonitorUpdate::read(
&mut self
.kv
.read(&ns, &key)
.map_err(|e| KVStoreUpdatingPersisterError::StorageReadFailed{ reason: e.to_string(), context: key.clone()})?,
Ok(
ChannelMonitorUpdate::read(&mut self.kv.read(&ns, &key).map_err(|e| {
KVStoreUpdatingPersisterError::StorageReadFailed {
reason: e,
context: key.clone(),
}
})?)
.map_err(|e| KVStoreUpdatingPersisterError::UpdateDecodeFailed {
reason: e,
context: key,
})?,
)
.map_err(|e| KVStoreUpdatingPersisterError::UpdateDecodeFailed{reason: e.to_string(), context: key})?)
}

/// Delete updates with an update_id lower than the given channel monitor.
Expand Down Expand Up @@ -681,12 +703,12 @@ mod tests {
assert!(persister.kv.remove("namespace", "key",).is_err());
}

fn is_sorted<T>(data: &[T]) -> bool
where
T: Ord,
{
data.windows(2).all(|w| w[0] <= w[1])
}
fn is_sorted<T>(data: &[T]) -> bool
where
T: Ord,
{
data.windows(2).all(|w| w[0] <= w[1])
}

// =================================
// TESTS
Expand Down Expand Up @@ -790,20 +812,20 @@ mod tests {
index: 1,
};
let monitor_name = MonitorName::try_from(outpoint).unwrap();
for i in 2..=1000 {
persister
.kv
.write(
&persister.monitor_update_namespace(&monitor_name),
&UpdateName::from(i).storage_key(),
&[0],
)
.unwrap();
}
for i in 2..=1000 {
persister
.kv
.write(
&persister.monitor_update_namespace(&monitor_name),
&UpdateName::from(i).storage_key(),
&[0],
)
.unwrap();
}
// Check that we get the right number of updates, in order
let listed_update_names = persister.list_update_names(&monitor_name).unwrap();
assert_eq!(listed_update_names.len(), 999);
assert!(is_sorted(&listed_update_names));
assert!(is_sorted(&listed_update_names));
}

#[test]
Expand Down

0 comments on commit a7962e3

Please sign in to comment.