Skip to content

Commit c5e771b

Browse files
committed
f Account for renamed namespaces
1 parent 8bc74cf commit c5e771b

File tree

10 files changed

+190
-159
lines changed

10 files changed

+190
-159
lines changed

src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use lightning::sign::EntropySource;
2929
use lightning::util::config::UserConfig;
3030
use lightning::util::persist::{
3131
read_channel_monitors, KVStore, CHANNEL_MANAGER_PERSISTENCE_KEY,
32-
CHANNEL_MANAGER_PERSISTENCE_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE,
32+
CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
3333
};
3434
use lightning::util::ser::ReadableArgs;
3535

@@ -555,8 +555,8 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
555555
}
556556
let channel_manager = {
557557
if let Ok(res) = kv_store.read(
558-
CHANNEL_MANAGER_PERSISTENCE_NAMESPACE,
559-
CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE,
558+
CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE,
559+
CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
560560
CHANNEL_MANAGER_PERSISTENCE_KEY,
561561
) {
562562
let mut reader = Cursor::new(res);

src/event.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::payment_store::{
88
};
99

1010
use crate::io::{
11-
EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_NAMESPACE,
12-
EVENT_QUEUE_PERSISTENCE_SUB_NAMESPACE,
11+
EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE,
12+
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE,
1313
};
1414
use crate::logger::{log_debug, log_error, log_info, Logger};
1515

@@ -176,17 +176,17 @@ where
176176
let data = EventQueueSerWrapper(locked_queue).encode();
177177
self.kv_store
178178
.write(
179-
EVENT_QUEUE_PERSISTENCE_NAMESPACE,
180-
EVENT_QUEUE_PERSISTENCE_SUB_NAMESPACE,
179+
EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE,
180+
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE,
181181
EVENT_QUEUE_PERSISTENCE_KEY,
182182
&data,
183183
)
184184
.map_err(|e| {
185185
log_error!(
186186
self.logger,
187187
"Write for key {}/{}/{} failed due to: {}",
188-
EVENT_QUEUE_PERSISTENCE_NAMESPACE,
189-
EVENT_QUEUE_PERSISTENCE_SUB_NAMESPACE,
188+
EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE,
189+
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE,
190190
EVENT_QUEUE_PERSISTENCE_KEY,
191191
e
192192
);
@@ -825,8 +825,8 @@ mod tests {
825825
// Check we can read back what we persisted.
826826
let persisted_bytes = store
827827
.read(
828-
EVENT_QUEUE_PERSISTENCE_NAMESPACE,
829-
EVENT_QUEUE_PERSISTENCE_SUB_NAMESPACE,
828+
EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE,
829+
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE,
830830
EVENT_QUEUE_PERSISTENCE_KEY,
831831
)
832832
.unwrap();

src/io/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ pub(crate) mod test_utils;
66
pub(crate) mod utils;
77

88
/// The event queue will be persisted under this key.
9-
pub(crate) const EVENT_QUEUE_PERSISTENCE_NAMESPACE: &str = "";
10-
pub(crate) const EVENT_QUEUE_PERSISTENCE_SUB_NAMESPACE: &str = "";
9+
pub(crate) const EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE: &str = "";
10+
pub(crate) const EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
1111
pub(crate) const EVENT_QUEUE_PERSISTENCE_KEY: &str = "events";
1212

1313
/// The peer information will be persisted under this key.
14-
pub(crate) const PEER_INFO_PERSISTENCE_NAMESPACE: &str = "";
15-
pub(crate) const PEER_INFO_PERSISTENCE_SUB_NAMESPACE: &str = "";
14+
pub(crate) const PEER_INFO_PERSISTENCE_PRIMARY_NAMESPACE: &str = "";
15+
pub(crate) const PEER_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
1616
pub(crate) const PEER_INFO_PERSISTENCE_KEY: &str = "peers";
1717

1818
/// The payment information will be persisted under this prefix.
19-
pub(crate) const PAYMENT_INFO_PERSISTENCE_NAMESPACE: &str = "payments";
20-
pub(crate) const PAYMENT_INFO_PERSISTENCE_SUB_NAMESPACE: &str = "";
19+
pub(crate) const PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE: &str = "payments";
20+
pub(crate) const PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
2121

2222
/// RapidGossipSync's `latest_sync_timestamp` will be persisted under this key.
23-
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_NAMESPACE: &str = "";
24-
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_SUB_NAMESPACE: &str = "";
23+
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_PRIMARY_NAMESPACE: &str = "";
24+
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_SECONDARY_NAMESPACE: &str = "";
2525
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_KEY: &str = "latest_rgs_sync_timestamp";
2626

2727
/// The last time we broadcast a node announcement will be persisted under this key.
28-
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_NAMESPACE: &str = "";
29-
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_SUB_NAMESPACE: &str = "";
28+
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_PRIMARY_NAMESPACE: &str = "";
29+
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_SECONDARY_NAMESPACE: &str = "";
3030
pub(crate) const LATEST_NODE_ANN_BCAST_TIMESTAMP_KEY: &str = "latest_node_ann_bcast_timestamp";

src/io/sqlite_store/migrations.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@ pub(super) fn migrate_schema(
99
if from_version == 1 && to_version == 2 {
1010
let sql = format!(
1111
"ALTER TABLE {}
12-
ADD sub_namespace TEXT DEFAULT \"\" NOT NULL;",
12+
RENAME COLUMN namespace TO primary_namespace;",
13+
kv_table_name
14+
);
15+
connection.execute(&sql, []).map_err(|e| {
16+
let msg = format!(
17+
"Failed to migrate table {} from user_version {} to {}: {}",
18+
kv_table_name, from_version, to_version, e
19+
);
20+
io::Error::new(io::ErrorKind::Other, msg)
21+
})?;
22+
23+
let sql = format!(
24+
"ALTER TABLE {}
25+
ADD secondary_namespace TEXT DEFAULT \"\" NOT NULL;",
1326
kv_table_name
1427
);
1528
connection.execute(&sql, []).map_err(|e| {

src/io/sqlite_store/mod.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ impl SqliteStore {
9999

100100
let sql = format!(
101101
"CREATE TABLE IF NOT EXISTS {} (
102-
namespace TEXT NOT NULL,
103-
sub_namespace TEXT DEFAULT \"\" NOT NULL,
102+
primary_namespace TEXT NOT NULL,
103+
secondary_namespace TEXT DEFAULT \"\" NOT NULL,
104104
key TEXT NOT NULL CHECK (key <> ''),
105-
value BLOB, PRIMARY KEY ( namespace, sub_namespace, key )
105+
value BLOB, PRIMARY KEY ( primary_namespace, secondary_namespace, key )
106106
);",
107107
kv_table_name
108108
);
@@ -123,12 +123,14 @@ impl SqliteStore {
123123
}
124124

125125
impl KVStore for SqliteStore {
126-
fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> std::io::Result<Vec<u8>> {
127-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "read")?;
126+
fn read(
127+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
128+
) -> std::io::Result<Vec<u8>> {
129+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?;
128130

129131
let locked_conn = self.connection.lock().unwrap();
130132
let sql =
131-
format!("SELECT value FROM {} WHERE namespace=:namespace AND sub_namespace=:sub_namespace AND key=:key;",
133+
format!("SELECT value FROM {} WHERE primary_namespace=:primary_namespace AND secondary_namespace=:secondary_namespace AND key=:key;",
132134
self.kv_table_name);
133135

134136
let mut stmt = locked_conn.prepare_cached(&sql).map_err(|e| {
@@ -139,8 +141,8 @@ impl KVStore for SqliteStore {
139141
let res = stmt
140142
.query_row(
141143
named_params! {
142-
":namespace": namespace,
143-
":sub_namespace": sub_namespace,
144+
":primary_namespace": primary_namespace,
145+
":secondary_namespace": secondary_namespace,
144146
":key": key,
145147
},
146148
|row| row.get(0),
@@ -149,17 +151,17 @@ impl KVStore for SqliteStore {
149151
rusqlite::Error::QueryReturnedNoRows => {
150152
let msg = format!(
151153
"Failed to read as key could not be found: {}/{}/{}",
152-
PrintableString(namespace),
153-
PrintableString(sub_namespace),
154+
PrintableString(primary_namespace),
155+
PrintableString(secondary_namespace),
154156
PrintableString(key)
155157
);
156158
std::io::Error::new(std::io::ErrorKind::NotFound, msg)
157159
}
158160
e => {
159161
let msg = format!(
160162
"Failed to read from key {}/{}/{}: {}",
161-
PrintableString(namespace),
162-
PrintableString(sub_namespace),
163+
PrintableString(primary_namespace),
164+
PrintableString(secondary_namespace),
163165
PrintableString(key),
164166
e
165167
);
@@ -170,14 +172,14 @@ impl KVStore for SqliteStore {
170172
}
171173

172174
fn write(
173-
&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8],
175+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8],
174176
) -> std::io::Result<()> {
175-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "write")?;
177+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?;
176178

177179
let locked_conn = self.connection.lock().unwrap();
178180

179181
let sql = format!(
180-
"INSERT OR REPLACE INTO {} (namespace, sub_namespace, key, value) VALUES (:namespace, :sub_namespace, :key, :value);",
182+
"INSERT OR REPLACE INTO {} (primary_namespace, secondary_namespace, key, value) VALUES (:primary_namespace, :secondary_namespace, :key, :value);",
181183
self.kv_table_name
182184
);
183185

@@ -187,17 +189,17 @@ impl KVStore for SqliteStore {
187189
})?;
188190

189191
stmt.execute(named_params! {
190-
":namespace": namespace,
191-
":sub_namespace": sub_namespace,
192+
":primary_namespace": primary_namespace,
193+
":secondary_namespace": secondary_namespace,
192194
":key": key,
193195
":value": buf,
194196
})
195197
.map(|_| ())
196198
.map_err(|e| {
197199
let msg = format!(
198200
"Failed to write to key {}/{}/{}: {}",
199-
PrintableString(namespace),
200-
PrintableString(sub_namespace),
201+
PrintableString(primary_namespace),
202+
PrintableString(secondary_namespace),
201203
PrintableString(key),
202204
e
203205
);
@@ -206,29 +208,29 @@ impl KVStore for SqliteStore {
206208
}
207209

208210
fn remove(
209-
&self, namespace: &str, sub_namespace: &str, key: &str, _lazy: bool,
211+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, _lazy: bool,
210212
) -> std::io::Result<()> {
211-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "remove")?;
213+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?;
212214

213215
let locked_conn = self.connection.lock().unwrap();
214216

215-
let sql = format!("DELETE FROM {} WHERE namespace=:namespace AND sub_namespace=:sub_namespace AND key=:key;", self.kv_table_name);
217+
let sql = format!("DELETE FROM {} WHERE primary_namespace=:primary_namespace AND secondary_namespace=:secondary_namespace AND key=:key;", self.kv_table_name);
216218

217219
let mut stmt = locked_conn.prepare_cached(&sql).map_err(|e| {
218220
let msg = format!("Failed to prepare statement: {}", e);
219221
std::io::Error::new(std::io::ErrorKind::Other, msg)
220222
})?;
221223

222224
stmt.execute(named_params! {
223-
":namespace": namespace,
224-
":sub_namespace": sub_namespace,
225+
":primary_namespace": primary_namespace,
226+
":secondary_namespace": secondary_namespace,
225227
":key": key,
226228
})
227229
.map_err(|e| {
228230
let msg = format!(
229231
"Failed to delete key {}/{}/{}: {}",
230-
PrintableString(namespace),
231-
PrintableString(sub_namespace),
232+
PrintableString(primary_namespace),
233+
PrintableString(secondary_namespace),
232234
PrintableString(key),
233235
e
234236
);
@@ -237,13 +239,15 @@ impl KVStore for SqliteStore {
237239
Ok(())
238240
}
239241

240-
fn list(&self, namespace: &str, sub_namespace: &str) -> std::io::Result<Vec<String>> {
241-
check_namespace_key_validity(namespace, sub_namespace, None, "list")?;
242+
fn list(
243+
&self, primary_namespace: &str, secondary_namespace: &str,
244+
) -> std::io::Result<Vec<String>> {
245+
check_namespace_key_validity(primary_namespace, secondary_namespace, None, "list")?;
242246

243247
let locked_conn = self.connection.lock().unwrap();
244248

245249
let sql = format!(
246-
"SELECT key FROM {} WHERE namespace=:namespace AND sub_namespace=:sub_namespace",
250+
"SELECT key FROM {} WHERE primary_namespace=:primary_namespace AND secondary_namespace=:secondary_namespace",
247251
self.kv_table_name
248252
);
249253
let mut stmt = locked_conn.prepare_cached(&sql).map_err(|e| {
@@ -256,8 +260,8 @@ impl KVStore for SqliteStore {
256260
let rows_iter = stmt
257261
.query_map(
258262
named_params! {
259-
":namespace": namespace,
260-
":sub_namespace": sub_namespace,
263+
":primary_namespace": primary_namespace,
264+
":secondary_namespace": secondary_namespace,
261265
},
262266
|row| row.get(0),
263267
)

src/io/test_utils.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,36 @@ use std::path::PathBuf;
1919
pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_store: &K) {
2020
let data = [42u8; 32];
2121

22-
let namespace = "testspace";
23-
let sub_namespace = "testsubspace";
22+
let primary_namespace = "testspace";
23+
let secondary_namespace = "testsubspace";
2424
let key = "testkey";
2525

2626
// Test the basic KVStore operations.
27-
kv_store.write(namespace, sub_namespace, key, &data).unwrap();
27+
kv_store.write(primary_namespace, secondary_namespace, key, &data).unwrap();
2828

29-
// Test empty namespace/sub_namespace is allowed, but not empty namespace and non-empty
30-
// sub-namespace, and not empty key.
29+
// Test empty primary/secondary namespaces are allowed, but not empty primary namespace and non-empty
30+
// secondary primary_namespace, and not empty key.
3131
kv_store.write("", "", key, &data).unwrap();
32-
let res = std::panic::catch_unwind(|| kv_store.write("", sub_namespace, key, &data));
32+
let res = std::panic::catch_unwind(|| kv_store.write("", secondary_namespace, key, &data));
3333
assert!(res.is_err());
34-
let res = std::panic::catch_unwind(|| kv_store.write(namespace, sub_namespace, "", &data));
34+
let res = std::panic::catch_unwind(|| {
35+
kv_store.write(primary_namespace, secondary_namespace, "", &data)
36+
});
3537
assert!(res.is_err());
3638

37-
let listed_keys = kv_store.list(namespace, sub_namespace).unwrap();
39+
let listed_keys = kv_store.list(primary_namespace, secondary_namespace).unwrap();
3840
assert_eq!(listed_keys.len(), 1);
3941
assert_eq!(listed_keys[0], key);
4042

41-
let read_data = kv_store.read(namespace, sub_namespace, key).unwrap();
43+
let read_data = kv_store.read(primary_namespace, secondary_namespace, key).unwrap();
4244
assert_eq!(data, &*read_data);
4345

44-
kv_store.remove(namespace, sub_namespace, key, false).unwrap();
46+
kv_store.remove(primary_namespace, secondary_namespace, key, false).unwrap();
4547

46-
let listed_keys = kv_store.list(namespace, sub_namespace).unwrap();
48+
let listed_keys = kv_store.list(primary_namespace, secondary_namespace).unwrap();
4749
assert_eq!(listed_keys.len(), 0);
4850

49-
// Ensure we have no issue operating with namespace/sub_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN
51+
// Ensure we have no issue operating with primary_namespace/secondary_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN
5052
let max_chars: String = std::iter::repeat('A').take(KVSTORE_NAMESPACE_KEY_MAX_LEN).collect();
5153
kv_store.write(&max_chars, &max_chars, &max_chars, &data).unwrap();
5254

0 commit comments

Comments
 (0)