persistence: a client that starts while its owner creates the database is not an error

This commit is contained in:
DeFiDude committed 2026-11-15 11:48:09 -07:00
1 parent 9360fc8c4a
commit ac1da396b5
2 files changed
+77 -16

No files matched your search

+69 -16
View File
@@ -270,25 +270,33 @@ fn load_for_client(source: &StoreSource) -> Vec<KnownDestinationRow> {
match source {
StoreSource::Memory => Vec::new(),
#[cfg(feature = "sqlite-store")]
StoreSource::Sqlite { known, .. } => {
// Upstream logs an error for a missing file too; a client whose
// directory is not the owner's has none, and that is not a fault.
if !known.is_file() {
debug!("no known destinations database to load as a shared-instance client");
return Vec::new();
}
let read = SqliteKnownDestinations::open_ro(known)
.and_then(|mut reader| read_rows(&mut reader));
read.unwrap_or_else(|e| {
error!(
"could not load known destinations from the shared instance's database: {e}; continuing with an empty table"
);
Vec::new()
})
}
StoreSource::Sqlite { known, .. } => read_owner_rows(known).unwrap_or_else(|e| {
error!(
"could not load known destinations from the shared instance's database: {e}; continuing with an empty table"
);
Vec::new()
}),
}
}
/// The owner's rows. Upstream logs an error in two cases that are not faults:
/// a client whose directory is not the owner's has no file, and an owner binds
/// the shared instance before it creates the file and commits its schema
/// (Reticulum.py), so a client that starts in between finds no schema yet.
#[cfg(feature = "sqlite-store")]
fn read_owner_rows(known: &Path) -> Result<Vec<KnownDestinationRow>, StoreError> {
if !known.is_file() {
debug!("no known destinations database to load as a shared-instance client");
return Ok(Vec::new());
}
let mut reader = SqliteKnownDestinations::open_ro(known)?;
if reader.is_uninitialized()? {
debug!("the shared instance has not created its known destinations yet");
return Ok(Vec::new());
}
read_rows(&mut reader)
}
/// The rows for a session whose database could not be opened for writing:
/// what a read-only connection still gives, with the legacy files staged over
/// it and left in place (upstream's `load_known_destinations_legacy`).
@@ -2119,6 +2127,51 @@ mod tests {
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn a_shared_instance_client_reads_a_running_owners_rows() {
let dir = temp_dir();
let mut owner = actor_on(&dir);
owner
.recent_announces
.insert([5; 16], entry([5; 16], Some(1000.0), false));
owner.kd.pending.mark_dirty([5; 16]);
owner.flush_known_destinations();
settle(&mut owner);
// The owner keeps its database open, the rows still in its WAL.
let client = client_on(&dir);
assert!(client.recent_announces.contains_key(&[5; 16]));
drop(client);
owner.on_shutdown();
let _ = std::fs::remove_dir_all(dir);
}
/// An owner binds the shared instance first, so a client can open the file
/// while the owner's schema is not yet committed.
#[test]
fn a_shared_instance_client_finds_no_schema_before_the_owner_commits_one() {
let dir = temp_dir();
let known = dir.join("known_destinations.db");
// What the owner's open leaves until its first write.
std::fs::write(&known, b"").unwrap();
assert!(read_owner_rows(&known).unwrap().is_empty());
let owner = rusqlite::Connection::open(&known).unwrap();
owner
.execute_batch(
"PRAGMA journal_mode=WAL; BEGIN IMMEDIATE; CREATE TABLE known_destinations (x)",
)
.unwrap();
assert!(read_owner_rows(&known).unwrap().is_empty());
owner.execute_batch("ROLLBACK").unwrap();
drop(owner);
// A schema without the table is still a fault.
rusqlite::Connection::open(&known)
.unwrap()
.execute_batch("CREATE TABLE other (x)")
.unwrap();
assert!(read_owner_rows(&known).is_err());
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn a_shared_instance_client_writes_nothing() {
let dir = temp_dir();
+8
View File
@@ -324,6 +324,14 @@ impl SqliteKnownDestinations {
})
}
/// True while the file holds no schema, before its owner first commits one.
pub(crate) fn is_uninitialized(&self) -> Result<bool, StoreError> {
let tables: i64 = db(self
.conn
.query_row("SELECT count(*) FROM sqlite_master", [], |r| r.get(0)))?;
Ok(tables == 0)
}
/// The public key stored for `destination`, whatever its length: upstream
/// recalls a row whose key does not load, which this crate's table skips.
pub fn stored_public_key(&self, destination: &DestHash) -> Result<Option<Vec<u8>>, StoreError> {