Skip to content

Commit 27cba43

Browse files
committed
Rename namespace validity constants to be more concise
The constants in `lightning::util::persist` are sufficiently long that its often difficult eyeball their correctness which nearly led to several bugs when adding async support. Here we take the first step towards condensing them by making them somewhat more concise, dropping words which are obvious from context.
1 parent 0abc47b commit 27cba43

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

lightning-persister/src/test_utils.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use lightning::ln::functional_test_utils::{
55
};
66
use lightning::util::persist::{
77
migrate_kv_store_data, read_channel_monitors, KVStoreSync, MigratableKVStore,
8-
KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
8+
NAMESPACE_ALPHABET, NAMESPACE_MAX_LEN,
99
};
1010
use lightning::util::test_utils;
1111
use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event};
@@ -46,8 +46,8 @@ pub(crate) fn do_read_write_remove_list_persist<K: KVStoreSync + RefUnwindSafe>(
4646
assert_eq!(listed_keys.len(), 0);
4747

4848
// Ensure we have no issue operating with primary_namespace/secondary_namespace/key being
49-
// KVSTORE_NAMESPACE_KEY_MAX_LEN
50-
let max_chars = "A".repeat(KVSTORE_NAMESPACE_KEY_MAX_LEN);
49+
// NAMESPACE_MAX_LEN
50+
let max_chars = "A".repeat(NAMESPACE_MAX_LEN);
5151
kv_store.write(&max_chars, &max_chars, &max_chars, data.clone()).unwrap();
5252

5353
let listed_keys = kv_store.list(&max_chars, &max_chars).unwrap();
@@ -76,17 +76,16 @@ pub(crate) fn do_test_data_migration<S: MigratableKVStore, T: MigratableKVStore>
7676
let primary_namespace = if i == 0 {
7777
String::new()
7878
} else {
79-
format!("testspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(i).unwrap())
79+
format!("testspace{}", NAMESPACE_ALPHABET.chars().nth(i).unwrap())
8080
};
8181
for j in 0..num_secondary_namespaces {
8282
let secondary_namespace = if i == 0 || j == 0 {
8383
String::new()
8484
} else {
85-
format!("testsubspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(j).unwrap())
85+
format!("testsubspace{}", NAMESPACE_ALPHABET.chars().nth(j).unwrap())
8686
};
8787
for k in 0..num_keys {
88-
let key =
89-
format!("testkey{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k).unwrap());
88+
let key = format!("testkey{}", NAMESPACE_ALPHABET.chars().nth(k).unwrap());
9089
source_store
9190
.write(&primary_namespace, &secondary_namespace, &key, dummy_data.clone())
9291
.unwrap();

lightning-persister/src/utils.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use lightning::types::string::PrintableString;
2-
use lightning::util::persist::{KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN};
2+
use lightning::util::persist::{NAMESPACE_ALPHABET, NAMESPACE_MAX_LEN};
33

44
pub(crate) fn is_valid_kvstore_str(key: &str) -> bool {
5-
key.len() <= KVSTORE_NAMESPACE_KEY_MAX_LEN
6-
&& key.chars().all(|c| KVSTORE_NAMESPACE_KEY_ALPHABET.contains(c))
5+
key.len() <= NAMESPACE_MAX_LEN && key.chars().all(|c| NAMESPACE_ALPHABET.contains(c))
76
}
87

98
pub(crate) fn check_namespace_key_validity(

lightning/src/util/persist.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ use crate::util::native_async::FutureSpawner;
4040
use crate::util::ser::{Readable, ReadableArgs, Writeable};
4141

4242
/// The alphabet of characters allowed for namespaces and keys.
43-
pub const KVSTORE_NAMESPACE_KEY_ALPHABET: &str =
43+
pub const NAMESPACE_ALPHABET: &str =
4444
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
4545

4646
/// The maximum number of characters namespaces and keys may have.
47-
pub const KVSTORE_NAMESPACE_KEY_MAX_LEN: usize = 120;
47+
pub const NAMESPACE_MAX_LEN: usize = 120;
4848

4949
/// The primary namespace under which the [`ChannelManager`] will be persisted.
5050
///
@@ -198,15 +198,14 @@ where
198198
/// ways, as long as per-namespace key uniqueness is asserted.
199199
///
200200
/// Keys and namespaces are required to be valid ASCII strings in the range of
201-
/// [`KVSTORE_NAMESPACE_KEY_ALPHABET`] and no longer than [`KVSTORE_NAMESPACE_KEY_MAX_LEN`]. Empty
202-
/// primary namespaces and secondary namespaces (`""`) are assumed to be a valid, however, if
203-
/// `primary_namespace` is empty, `secondary_namespace` is required to be empty, too. This means
204-
/// that concerns should always be separated by primary namespace first, before secondary
205-
/// namespaces are used. While the number of primary namespaces will be relatively small and is
206-
/// determined at compile time, there may be many secondary namespaces per primary namespace. Note
207-
/// that per-namespace uniqueness needs to also hold for keys *and* namespaces in any given
208-
/// namespace, i.e., conflicts between keys and equally named
209-
/// primary namespaces/secondary namespaces must be avoided.
201+
/// [`NAMESPACE_ALPHABET`] and no longer than [`NAMESPACE_MAX_LEN`]. Empty primary namespaces and
202+
/// secondary namespaces (`""`) are assumed to be a valid, however, if `primary_namespace` is
203+
/// empty, `secondary_namespace` is required to be empty, too. This means that concerns should
204+
/// always be separated by primary namespace first, before secondary namespaces are used. While the
205+
/// number of primary namespaces will be relatively small and is determined at compile time, there
206+
/// may be many secondary namespaces per primary namespace. Note that per-namespace uniqueness
207+
/// needs to also hold for keys *and* namespaces in any given namespace, i.e., conflicts between
208+
/// keys and equally named primary namespaces/secondary namespaces must be avoided.
210209
///
211210
/// **Note:** Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister`
212211
/// interface can use a concatenation of `[{primary_namespace}/[{secondary_namespace}/]]{key}` to

0 commit comments

Comments
 (0)