]> git.bitcoin.ninja Git - rust-lightning/blob - lightning-persister/src/utils.rs
Consider anchor outputs value on channel open
[rust-lightning] / lightning-persister / src / utils.rs
1 use lightning::util::persist::{KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN};
2 use lightning::util::string::PrintableString;
3
4
5 pub(crate) fn is_valid_kvstore_str(key: &str) -> bool {
6         key.len() <= KVSTORE_NAMESPACE_KEY_MAX_LEN && key.chars().all(|c| KVSTORE_NAMESPACE_KEY_ALPHABET.contains(c))
7 }
8
9 pub(crate) fn check_namespace_key_validity(
10         primary_namespace: &str, secondary_namespace: &str, key: Option<&str>, operation: &str)
11 -> Result<(), std::io::Error> {
12         if let Some(key) = key {
13                 if key.is_empty() {
14                         debug_assert!(false, "Failed to {} {}/{}/{}: key may not be empty.", operation,
15                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
16                         let msg = format!("Failed to {} {}/{}/{}: key may not be empty.", operation,
17                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
18                         return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
19                 }
20
21                 if primary_namespace.is_empty() && !secondary_namespace.is_empty() {
22                         debug_assert!(false,
23                                 "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
24                                 operation,
25                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
26                         let msg = format!(
27                                 "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", operation,
28                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
29                         return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
30                 }
31
32                 if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) || !is_valid_kvstore_str(key) {
33                         debug_assert!(false, "Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.",
34                                 operation,
35                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
36                         let msg = format!("Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.",
37                                 operation,
38                                 PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key));
39                         return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
40                 }
41         } else {
42                 if primary_namespace.is_empty() && !secondary_namespace.is_empty() {
43                         debug_assert!(false,
44                                 "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
45                                 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
46                         let msg = format!(
47                                 "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.",
48                                 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
49                         return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
50                 }
51                 if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) {
52                         debug_assert!(false, "Failed to {} {}/{}: primary namespace and secondary namespace must be valid.",
53                                 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
54                         let msg = format!("Failed to {} {}/{}: primary namespace and secondary namespace must be valid.",
55                                 operation, PrintableString(primary_namespace), PrintableString(secondary_namespace));
56                         return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
57                 }
58         }
59
60         Ok(())
61 }