Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Move staging db to Rust core instead of in host app

Leendert de Borst607294c42f35
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
index c820b91e5..eb8fda01d 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
@@ -9,7 +9,6 @@ import net.aliasvault.app.vaultstore.storageprovider.StorageProvider
import net.aliasvault.app.webapi.WebApiService
import org.json.JSONArray
import org.json.JSONObject
-import uniffi.aliasvault_core.SqliteMemoryDatabase
import uniffi.aliasvault_core.VaultSyncSession
import java.net.SocketTimeoutException
@@ -38,9 +37,6 @@ class VaultSyncEngine(
private const val DERIVATION_PARAMS_STATE_KEY = "encryptionKeyDerivationParams"
}
- /** The engine staging database used for internal sync and merge operations. */
- private var staging: SqliteMemoryDatabase? = null
-
/** Whether the live vault was written to since the last store went through. */
private var localMutated = false
private var runLog = VaultSyncRunLog("")
@@ -73,7 +69,6 @@ class VaultSyncEngine(
log.engine { session.resume(responseJson, bytes) }
}
} finally {
- closeStaging()
discardLocalDatabaseIfNeeded()
session.destroy()
log.finish(success, storageProvider)
@@ -120,7 +115,7 @@ class VaultSyncEngine(
return Pair(handle(kind, command), null)
}
return try {
- Pair(JSONObject(), exportDatabase(command.optString("db")))
+ Pair(JSONObject(), vaultStore.database.export())
} catch (e: Exception) {
Pair(errorResponse(kind, e), null)
}
@@ -139,20 +134,13 @@ class VaultSyncEngine(
setState(command.optString("key"), null)
JSONObject()
}
- "dbOpen" -> {
- openStaging()
- JSONObject()
- }
"dbQuery" -> {
val params = (command.optJSONArray("params") ?: JSONArray()).toString()
- JSONObject().put("rows", JSONArray(database(command.optString("db")).query(command.optString("sql"), params)))
+ JSONObject().put("rows", JSONArray(vaultStore.database.connection().query(command.optString("sql"), params)))
}
"dbExec" -> {
- val name = command.optString("db")
- database(name).exec((command.optJSONArray("statements") ?: JSONArray()).toString())
- if (name == "local") {
- localMutated = true
- }
+ vaultStore.database.connection().exec((command.optJSONArray("statements") ?: JSONArray()).toString())
+ localMutated = true
JSONObject()
}
"vaultStore" -> storeVault(command)
@@ -172,15 +160,6 @@ class VaultSyncEngine(
}
}
- /**
- * Serialize a database to SQLite bytes.
- */
- private fun exportDatabase(name: String): ByteArray = when (name) {
- "local" -> vaultStore.database.export()
- "staging" -> stagingDatabase().export()
- else -> error("Unknown database $name")
- }
-
/**
* The `{ "error": ... }` response for a failed command.
*/
@@ -303,27 +282,6 @@ class VaultSyncEngine(
// region SQLite
- private fun database(name: String): SqliteMemoryDatabase = when (name) {
- "local" -> vaultStore.database.connection()
- "staging" -> stagingDatabase()
- else -> error("Unknown database $name")
- }
-
- private fun stagingDatabase(): SqliteMemoryDatabase = staging ?: error("The staging database is not open")
-
- /**
- * Open the staging database in memory.
- */
- private fun openStaging() {
- closeStaging()
- staging = SqliteMemoryDatabase.withLatestSchema()
- }
-
- private fun closeStaging() {
- staging?.destroy()
- staging = null
- }
-
/**
* Reload the stored vault when the run left changes in the live database that no store persisted, so the
* vault the app reads from is the one on disk rather than a half-finished sync.
diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
index 5158c9617..eca4d7c0e 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
@@ -26,8 +26,6 @@ public final class VaultSyncEngine {
private let vaultStore: VaultStore
private let webApiService: WebApiService
- /// The engine's staging database, held in the Rust core's memory.
- private var staging: SqliteMemoryDatabase?
/// Whether the live vault was written to since the last store went through.
private var localMutated = false
private var runLog = VaultSyncRunLog(operation: "")
@@ -37,10 +35,6 @@ public final class VaultSyncEngine {
self.webApiService = webApiService
}
- deinit {
- closeStaging()
- }
-
/// Run one engine operation (`fullSync`, `statusCheck`, `migrationStatus`, `migrateManifest`, `resolveVaultKey`, or a
/// sharing operation) and return its result.
public func run(operation: String, forcePull: Bool = false, encryptionKey: String? = nil, sharing: [String: Any]? = nil) async throws -> [String: Any] {
@@ -49,7 +43,6 @@ public final class VaultSyncEngine {
var finalResult: [String: Any]?
let session = try VaultSyncSession(requestJson: try buildRequest(operation: operation, forcePull: forcePull, encryptionKey: encryptionKey, sharing: sharing))
defer {
- closeStaging()
discardLocalDatabaseIfNeeded()
log.finish(result: finalResult, userDefaults: vaultStore.userDefaults)
}
@@ -120,7 +113,7 @@ public final class VaultSyncEngine {
return (await handle(kind: kind, command: command), nil)
}
do {
- return ([:], try exportDatabase(named: command["db"] as? String ?? ""))
+ return ([:], try vaultStore.exportDatabase())
} catch {
return (Self.errorResponse(error), nil)
}
@@ -137,19 +130,12 @@ public final class VaultSyncEngine {
case "stateRemove":
setState(nil, forKey: command["key"] as? String ?? "")
return [:]
- case "dbOpen":
- try openStaging()
- return [:]
case "dbQuery":
- let db = try database(named: command["db"] as? String ?? "")
- let rowsJson = try db.query(sql: command["sql"] as? String ?? "", paramsJson: try Self.serializeJson(command["params"] ?? []))
+ let rowsJson = try localDatabase().query(sql: command["sql"] as? String ?? "", paramsJson: try Self.serializeJson(command["params"] ?? []))
return ["rows": try Self.parseJsonArray(rowsJson)]
case "dbExec":
- let name = command["db"] as? String ?? ""
- try database(named: name).exec(statementsJson: try Self.serializeJson(command["statements"] ?? []))
- if name == "local" {
- localMutated = true
- }
+ try localDatabase().exec(statementsJson: try Self.serializeJson(command["statements"] ?? []))
+ localMutated = true
return [:]
case "vaultStore":
return try storeVault(command)
@@ -169,18 +155,6 @@ public final class VaultSyncEngine {
}
}
- /// Serialize a database to SQLite bytes.
- private func exportDatabase(named name: String) throws -> Data {
- switch name {
- case "local":
- return try vaultStore.exportDatabase()
- case "staging":
- return try database(named: "staging").export()
- default:
- throw AppError.unknownError(message: "Unknown database \(name)")
- }
- }
-
/// The `{ "error": ... }` response for a failed command.
private static func errorResponse(_ error: Error) -> [String: Any] {
if let appError = error as? AppError {
@@ -299,31 +273,12 @@ public final class VaultSyncEngine {
// MARK: - SQLite
- private func database(named name: String) throws -> SqliteMemoryDatabase {
- switch name {
- case "local":
- guard let connection = vaultStore.dbConnection else {
- throw AppError.unknownError(message: "The vault is not unlocked")
- }
- return connection
- case "staging":
- guard let staging = staging else {
- throw AppError.unknownError(message: "The staging database is not open")
- }
- return staging
- default:
- throw AppError.unknownError(message: "Unknown database \(name)")
+ /// The live vault database the `db*` commands address.
+ private func localDatabase() throws -> SqliteMemoryDatabase {
+ guard let connection = vaultStore.dbConnection else {
+ throw AppError.unknownError(message: "The vault is not unlocked")
}
- }
-
- /// Open a fresh staging database in memory with the current client schema.
- private func openStaging() throws {
- closeStaging()
- staging = try SqliteMemoryDatabase.withLatestSchema()
- }
-
- private func closeStaging() {
- staging = nil
+ return connection
}
/// Reload the stored vault when the run left changes in the live database that no store persisted, so the
diff --git a/core/client/src/sync/VaultSyncEngine.ts b/core/client/src/sync/VaultSyncEngine.ts
index 5809d3704..83fcf1ed8 100644
--- a/core/client/src/sync/VaultSyncEngine.ts
+++ b/core/client/src/sync/VaultSyncEngine.ts
@@ -3,8 +3,6 @@
* against the host. The per-operation wrappers that call this live in VaultSync.
*/
-import { VaultSqlGenerator } from '@aliasvault/vault';
-
import { NetworkError } from '../api/errors/NetworkError';
import { RequestTimeoutError } from '../api/errors/RequestTimeoutError';
import { WebApiService } from '../api/WebApiService';
@@ -177,10 +175,9 @@ type EngineCommand =
| { kind: 'stateGet'; key: string }
| { kind: 'stateSet'; key: string; value: JsonValue }
| { kind: 'stateRemove'; key: string }
- | { kind: 'dbOpen'; db: string }
- | { kind: 'dbQuery'; db: string; sql: string; params: JsonValue[] }
- | { kind: 'dbExec'; db: string; statements: EngineSqlStatement[] }
- | { kind: 'dbExport'; db: string }
+ | { kind: 'dbQuery'; sql: string; params: JsonValue[] }
+ | { kind: 'dbExec'; statements: EngineSqlStatement[] }
+ | { kind: 'dbExport' }
| { kind: 'vaultStore'; encryptedBlob: string; markDirty: boolean; encryptionKey?: string; expectedMutationSeq?: number; revision?: number }
| { kind: 'vaultLoad' }
| { kind: 'markClean'; mutationSeqAtStart: number }
@@ -190,10 +187,6 @@ type EngineCommand =
/** The host's response to one command: the JSON, plus the raw bytes of a `dbExport` or a binary `http` response. */
type HostResponse = { json: JsonValue; bytes: Uint8Array | null };
-/** The database names a command may address. */
-const DB_LOCAL = 'local';
-const DB_STAGING = 'staging';
-
/**
* A cell as the engine binds it: `{ __b64 }` binds a BLOB, booleans bind as integers, objects as their JSON.
* @param value - the JSON parameter
@@ -255,28 +248,6 @@ function stateKey(key: string): StorageKey {
return `local:${key}` as StorageKey;
}
-/** The SQLite bytes of a fresh database on the current client schema. */
-let freshSchemaBytes: Promise<Uint8Array> | null = null;
-
-/**
- * A fresh database on the current client schema.
- */
-async function openFreshSchemaDatabase(): Promise<ISqliteDatabase> {
- freshSchemaBytes ??= (async (): Promise<Uint8Array> => {
- const db = await getPlatform().sqlite.open();
- try {
- db.exec(new VaultSqlGenerator().getCompleteSchemaSql());
- return db.export();
- } finally {
- db.close();
- }
- })().catch((error: unknown) => {
- freshSchemaBytes = null;
- throw error;
- });
- return getPlatform().sqlite.open(await freshSchemaBytes);
-}
-
/**
* Current time in milliseconds.
*/
@@ -288,8 +259,6 @@ function now(): number {
* Carries out one engine run's commands.
*/
class EngineRun {
- private staging: ISqliteDatabase | null = null;
-
/** The open local vault. */
private local: ISqliteDatabase | null = null;
@@ -345,11 +314,9 @@ class EngineRun {
}
/**
- * Release the staging database.
+ * End the run, discarding local changes no store persisted.
*/
public close(): void {
- this.staging?.close();
- this.staging = null;
if (this.localMutated) {
this.host.discardLocalDatabase?.();
}
@@ -369,21 +336,16 @@ class EngineRun {
case 'stateRemove':
await getPlatform().storage.remove(stateKey(command.key));
return {};
- case 'dbOpen':
- await this.openStaging();
- return {};
case 'dbQuery': {
- const rows = (await this.database(command.db)).query(command.sql, command.params.map(toBindValue));
+ const rows = (await this.database()).query(command.sql, command.params.map(toBindValue));
return { rows: rows.map(row => Object.fromEntries(Object.entries(row).map(([column, value]) => [column, toJsonCell(value)]))) };
}
case 'dbExec':
- execInTransaction(await this.database(command.db), command.statements);
- if (command.db === DB_LOCAL) {
- this.localMutated = true;
- }
+ execInTransaction(await this.database(), command.statements);
+ this.localMutated = true;
return {};
case 'dbExport':
- return (await this.database(command.db)).export();
+ return (await this.database()).export();
case 'vaultStore':
return this.storeVault(command);
case 'vaultLoad':
@@ -436,37 +398,11 @@ class EngineRun {
}
/**
- * The database a command addresses.
- * @param name - `local` or `staging`
- */
- private async database(name: string): Promise<ISqliteDatabase> {
- if (name === DB_LOCAL) {
- this.local ??= await this.host.localDatabase();
- return this.local;
- }
- if (name === DB_STAGING) {
- if (!this.staging) {
- throw new Error('The staging database is not open');
- }
- return this.staging;
- }
- throw new Error(`Unknown database ${name}`);
- }
-
- /**
- * Open a fresh staging database.
+ * The local vault the `db*` commands address.
*/
- private async openStaging(): Promise<void> {
- this.staging?.close();
- this.staging = null;
- const db = await openFreshSchemaDatabase();
- try {
- db.exec('PRAGMA foreign_keys = OFF');
- } catch (error) {
- db.close();
- throw error;
- }
- this.staging = db;
+ private async database(): Promise<ISqliteDatabase> {
+ this.local ??= await this.host.localDatabase();
+ return this.local;
}
/**
diff --git a/core/rust/README.md b/core/rust/README.md
index d53af79ee..7b1cc14df 100644
--- a/core/rust/README.md
+++ b/core/rust/README.md
@@ -30,7 +30,7 @@ real SQLite host per device (`tests/test_host.rs`).
### sqlite_host
An in-memory SQLite database (`SqliteMemoryDatabase`) that hosts every client's vault, so no platform
-needs its own SQLite build or writes plaintext to disk.
+needs its own SQLite build.
### credential_matcher
Priority-based credential filtering for autofill. Root domains come from an embedded copy of the
@@ -75,9 +75,7 @@ Only the web app (fetched on page load) builds with the size-optimized `release`
(`extension` profile), iOS and Android (`mobile` profile) build with `opt-level = 3`, which
trades a few megabytes for faster sync, since those ship as a one time download. Both WASM builds write to `core/client/wasm`, so locally the
apps run whichever was built last. Every build bundles SQLite behind
-`SqliteMemoryDatabase`, which hosts every client's vault database in
-memory (the sync engine's staging database included), so all clients run one SQLite build and the phones never
-write plaintext to disk. On wasm32 that SQLite is compiled by clang (sqlite-wasm-rs); macOS needs Homebrew LLVM
+`SqliteMemoryDatabase`, which hosts every client's vault database in memory.
(`brew install llvm`), which `build.sh` finds on its own.
## Testing
diff --git a/core/rust/src/bindings/uniffi_api.rs b/core/rust/src/bindings/uniffi_api.rs
index a7d27c7a7..36001052a 100644
--- a/core/rust/src/bindings/uniffi_api.rs
+++ b/core/rust/src/bindings/uniffi_api.rs
@@ -376,12 +376,6 @@ impl SqliteMemoryDatabase {
Ok(std::sync::Arc::new(Self { inner: MemoryDatabase::from_bytes(&bytes)? }))
}
- /// Open an empty database on the latest vault schema, foreign keys off.
- #[uniffi::constructor]
- pub fn with_latest_schema() -> Result<std::sync::Arc<Self>, VaultError> {
- Ok(std::sync::Arc::new(Self { inner: MemoryDatabase::with_latest_schema()? }))
- }
-
/// Run a SQL script without parameters.
pub fn execute_batch(&self, sql: String) -> Result<(), VaultError> {
self.inner.execute_batch(&sql)
diff --git a/core/rust/src/vault_sync/db.rs b/core/rust/src/vault_sync/db.rs
index 12d8e8054..60b33345d 100644
--- a/core/rust/src/vault_sync/db.rs
+++ b/core/rust/src/vault_sync/db.rs
@@ -1,4 +1,4 @@
-//! The vault's SQLite through the host.
+//! The vault's SQLite: the local vault through the host, the staging database in-process.
use std::collections::HashMap;
@@ -9,7 +9,7 @@ use super::session::Host;
use super::types::{Ack, Command, Db, DbRows, LogLevel};
use super::legacy;
use crate::common::encoding::uuid_from_bytes;
-use crate::sqlite_host::SqlStatement;
+use crate::sqlite_host::{MemoryDatabase, SqlStatement};
use crate::common::timestamp::{now_iso_utc, now_vault_datetime};
use crate::vault_codec::row::{blob_ref_of, inline_bytes};
use crate::vault_codec::{is_skip_table, manifest_scoped_tables, CodecRecord, CodecTableData, MaterializedTables};
@@ -23,28 +23,31 @@ const INSERT_BATCH_ROWS: usize = 400;
const TABLE_COLUMNS: &str = "SELECT m.name AS TableName, p.name AS ColumnName FROM sqlite_master m JOIN pragma_table_info(m.name) p WHERE m.type = 'table' ORDER BY m.name, p.cid";
pub(crate) async fn query(host: &Host, db: Db, sql: &str, params: Vec<Value>) -> SyncResult<Vec<Row>> {
- let response: DbRows = host.call(Command::DbQuery { db, sql: sql.to_string(), params }).await?;
- Ok(response.rows)
+ match db {
+ Db::Local => Ok(host.call::<DbRows>(Command::DbQuery { sql: sql.to_string(), params }).await?.rows),
+ Db::Staging => host.with_staging(|staging| staging.query(sql, &params)),
+ }
}
pub(crate) async fn exec(host: &Host, db: Db, statements: Vec<SqlStatement>) -> SyncResult<()> {
if statements.is_empty() {
return Ok(());
}
- host.call::<Ack>(Command::DbExec { db, statements }).await?;
- Ok(())
-}
-
-/// Open the staging database fresh with the current schema.
-pub(crate) async fn open_staging(host: &Host) -> SyncResult<()> {
- host.call::<Ack>(Command::DbOpen { db: Db::Staging }).await?;
- Ok(())
+ match db {
+ Db::Local => host.call::<Ack>(Command::DbExec { statements }).await.map(|_| ()),
+ Db::Staging => host.with_staging(|staging| staging.exec(&statements)),
+ }
}
/// Serialize a database to SQLite bytes.
pub(crate) async fn export(host: &Host, db: Db) -> SyncResult<Vec<u8>> {
- let (_, bytes) = host.call_with_bytes::<Ack>(Command::DbExport { db }, None).await?;
- bytes.ok_or_else(|| SyncError::Other("host returned no database bytes".to_string()))
+ match db {
+ Db::Local => {
+ let (_, bytes) = host.call_with_bytes::<Ack>(Command::DbExport, None).await?;
+ bytes.ok_or_else(|| SyncError::Other("host returned no database bytes".to_string()))
+ }
+ Db::Staging => host.with_staging(MemoryDatabase::export),
+ }
}
/// A cell as text: strings as they are, null as empty, anything else in its JSON form.
@@ -204,7 +207,7 @@ pub(crate) async fn insert_materialized(host: &Host, materialized: &Materialized
/// A failed insert into the fresh staging database is a row the schema refuses (the vault data), not a storage failure.
fn rows_rejected(table: &str, error: SyncError) -> SyncError {
match error {
- SyncError::Host { message, .. } => SyncError::VaultDataRejected(format!("table {}: {}", table, message)),
+ SyncError::Staging(message) => SyncError::VaultDataRejected(format!("table {}: {}", table, message)),
other => other,
}
}
diff --git a/core/rust/src/vault_sync/errors.rs b/core/rust/src/vault_sync/errors.rs
index 4369a04d6..0cca5238a 100644
--- a/core/rust/src/vault_sync/errors.rs
+++ b/core/rust/src/vault_sync/errors.rs
@@ -51,7 +51,7 @@ pub enum ErrorCode {
/// The host failed a write command (state, database, at-rest blob, dirty flag).
#[serde(rename = "E-602")]
StorageWriteFailed,
- /// The host could not open the staging database.
+ /// The engine's staging database failed.
#[serde(rename = "E-603")]
DatabaseInitFailed,
#[serde(rename = "E-701")]
@@ -156,6 +156,9 @@ pub enum SyncError {
/// LEGACY: the manifest migration cannot run before the sqlite-blob upgrade chain.
#[error("The vault has to walk the legacy upgrade chain first")]
LegacyUpgradePending,
+ /// The engine's in-process staging database failed.
+ #[error("Staging database failed: {0}")]
+ Staging(String),
/// The host reported a failure for a command (named by its wire tag, `dbExec`).
#[error("Host command {command} failed: {message}")]
Host { command: &'static str, message: String },
@@ -210,6 +213,7 @@ impl SyncError {
SyncError::ResyncLimitReached => Failure::Coded(ErrorCode::MergeConflict),
SyncError::UploadRejected(_) | SyncError::MissingBlobs(_) => Failure::Coded(ErrorCode::UploadFailed),
SyncError::LegacyUpgradePending => Failure::Coded(ErrorCode::MigrationCheckFailed),
+ SyncError::Staging(_) => Failure::Coded(ErrorCode::DatabaseInitFailed),
SyncError::Host { command, .. } => Failure::Coded(storage_error_code(command)),
SyncError::Json(_) => Failure::Coded(ErrorCode::SyncResponseInvalid),
SyncError::Core(_) => Failure::Coded(ErrorCode::SyncCodecFailed),
@@ -221,7 +225,6 @@ impl SyncError {
/// The client error code a host failure of the named command maps to.
fn storage_error_code(command: &str) -> ErrorCode {
match command {
- "dbOpen" => ErrorCode::DatabaseInitFailed,
"stateGet" | "dbQuery" | "dbExport" | "vaultLoad" => ErrorCode::StorageReadFailed,
"stateSet" | "stateRemove" | "dbExec" | "vaultStore" | "markClean" => ErrorCode::StorageWriteFailed,
_ => ErrorCode::UnknownError,
@@ -244,7 +247,7 @@ mod tests {
assert_eq!(SyncError::KeyOutOfSync.failure(), Failure::Coded(ErrorCode::KeyOutOfSync));
assert_eq!(serde_json::to_string(&LogoutReason::PasswordChanged).unwrap(), "\"passwordChanged\"");
assert_eq!(SyncError::Auth.failure(), Failure::Logout(LogoutReason::SessionExpired));
- assert_eq!(SyncError::Host { command: "dbOpen", message: String::new() }.failure(), Failure::Coded(ErrorCode::DatabaseInitFailed));
+ assert_eq!(SyncError::Staging(String::new()).failure(), Failure::Coded(ErrorCode::DatabaseInitFailed));
assert_eq!(SyncError::Host { command: "dbExec", message: String::new() }.failure(), Failure::Coded(ErrorCode::StorageWriteFailed));
}
diff --git a/core/rust/src/vault_sync/pull.rs b/core/rust/src/vault_sync/pull.rs
index e685cf8e5..6c43764d8 100644
--- a/core/rust/src/vault_sync/pull.rs
+++ b/core/rust/src/vault_sync/pull.rs
@@ -355,7 +355,7 @@ pub(crate) async fn materialize_to_sqlite(ctx: &mut Ctx, manifests: &[Manifest],
}
// Use a fresh staging database for every materialize.
- db::open_staging(&ctx.host).await?;
+ ctx.host.open_staging()?;
db::insert_materialized(&ctx.host, &materialized, &schema.columns, blob_map).await?;
// Names are not part of a manifest: the ones the vault already shows are kept, the ones this run opened win.
diff --git a/core/rust/src/vault_sync/session.rs b/core/rust/src/vault_sync/session.rs
index fcd960c23..a76fe021d 100644
--- a/core/rust/src/vault_sync/session.rs
+++ b/core/rust/src/vault_sync/session.rs
@@ -12,6 +12,7 @@ use super::engine;
use super::errors::{SyncError, SyncResult};
use super::types::{Ack, Command, LogLevel, SyncRequest};
use crate::common::error::{VaultError, VaultResult};
+use crate::sqlite_host::MemoryDatabase;
/// The exchange point between the engine's future and the host.
#[derive(Default)]
@@ -24,15 +25,31 @@ pub(crate) struct Slot {
pub response_bytes: Option<Vec<u8>>,
}
-/// The engine's handle to the host.
+/// The engine's handle to the host, plus the staging database the engine keeps for itself.
#[derive(Clone)]
pub(crate) struct Host {
slot: Arc<Mutex<Slot>>,
+ staging: Arc<Mutex<Option<MemoryDatabase>>>,
}
impl Host {
pub fn new(slot: Arc<Mutex<Slot>>) -> Self {
- Self { slot }
+ Self { slot, staging: Arc::new(Mutex::new(None)) }
+ }
+
+ /// Replace the staging database with a fresh one on the latest vault schema.
+ pub fn open_staging(&self) -> SyncResult<()> {
+ let mut staging = self.staging.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ *staging = None;
+ *staging = Some(MemoryDatabase::with_latest_schema().map_err(|e| SyncError::Staging(e.to_string()))?);
+ Ok(())
+ }
+
+ /// Run `f` on the open staging database.
+ pub fn with_staging<R>(&self, f: impl FnOnce(&MemoryDatabase) -> VaultResult<R>) -> SyncResult<R> {
+ let staging = self.staging.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ let db = staging.as_ref().ok_or_else(|| SyncError::Other("the staging database is not open".to_string()))?;
+ f(db).map_err(|e| SyncError::Staging(e.to_string()))
}
/// Send a command and wait for the host's typed response. A `{ "error": ... }` response becomes an error.
diff --git a/core/rust/src/vault_sync/state.rs b/core/rust/src/vault_sync/state.rs
index da4a57bd8..9bed0910d 100644
--- a/core/rust/src/vault_sync/state.rs
+++ b/core/rust/src/vault_sync/state.rs
@@ -107,7 +107,7 @@ impl Ctx {
if let Some(schema) = &self.schema {
return Ok(schema.clone());
}
- db::open_staging(&self.host).await?;
+ self.host.open_staging()?;
let schema = SchemaInfo { columns: db::schema_columns(&self.host, Db::Staging).await?, migration_id: db::latest_migration_id(&self.host, Db::Staging).await? };
self.schema = Some(schema.clone());
Ok(schema)
diff --git a/core/rust/src/vault_sync/tests/test_host.rs b/core/rust/src/vault_sync/tests/test_host.rs
index 6228ecd1d..18b473e93 100644
--- a/core/rust/src/vault_sync/tests/test_host.rs
+++ b/core/rust/src/vault_sync/tests/test_host.rs
@@ -12,7 +12,7 @@ use crate::sqlite_host::{self, SqlStatement};
use crate::vault_codec::{self, CodecTableData};
use crate::vault_sync::session::SyncSession;
use crate::vault_sync::state;
-use crate::vault_sync::types::{Command, Db};
+use crate::vault_sync::types::Command;
pub const USERNAME: &str = "tester";
@@ -29,7 +29,6 @@ pub type Responder = Box<dyn Fn(&str, &str, Option<&Value>) -> Option<(u16, Valu
pub struct TestHost {
pub local: Connection,
- pub staging: Option<Connection>,
pub state: HashMap<String, Value>,
pub vault_blob: Option<String>,
/// The key the stored vault is encrypted under; the session key `sync` and `run` hand to the engine.
@@ -97,7 +96,6 @@ impl TestHost {
pub fn new(vault_key: &str) -> Self {
Self {
local: open_schema_db(),
- staging: None,
state: HashMap::new(),
vault_blob: None,
vault_key: vault_key.to_string(),
@@ -223,25 +221,21 @@ impl TestHost {
self.state.remove(&key);
json!({})
}
- Command::DbOpen { .. } => {
- self.staging = Some(open_schema_db());
- json!({})
- }
- Command::DbQuery { db, .. } | Command::DbExec { db, .. } | Command::DbExport { db } if db == Db::Local && self.vault_blob.is_none() => {
+ Command::DbQuery { .. } | Command::DbExec { .. } | Command::DbExport if self.vault_blob.is_none() => {
// Like the app hosts: without a stored vault there is no local database to open.
json!({ "error": "Vault not available" })
}
- Command::DbQuery { db, sql, params } => match query(self.db(db), &sql, &params) {
+ Command::DbQuery { sql, params } => match query(&self.local, &sql, &params) {
Ok(rows) => json!({ "rows": rows }),
Err(error) => json!({ "error": format!("{} ({})", error, sql) }),
},
- Command::DbExec { db, statements } => match exec(self.db(db), &statements) {
+ Command::DbExec { statements } => match exec(&self.local, &statements) {
Ok(()) => json!({}),
Err(error) => json!({ "error": error }),
},
- Command::DbExport { db } => {
+ Command::DbExport => {
// The SQLite file travels as raw bytes, outside the JSON.
- let bytes = self.db(db).serialize(MAIN_DB).unwrap().to_vec();
+ let bytes = self.local.serialize(MAIN_DB).unwrap().to_vec();
session.resume("{}", Some(bytes)).unwrap();
continue;
}
@@ -291,13 +285,6 @@ impl TestHost {
self.local = open_from_bytes(&bytes);
}
- fn db(&mut self, db: Db) -> &Connection {
- match db {
- Db::Local => &self.local,
- Db::Staging => self.staging.as_ref().expect("staging database opened"),
- }
- }
-
/*
* Readers.
*/
diff --git a/core/rust/src/vault_sync/types.rs b/core/rust/src/vault_sync/types.rs
index 945660736..80c93f4fd 100644
--- a/core/rust/src/vault_sync/types.rs
+++ b/core/rust/src/vault_sync/types.rs
@@ -226,14 +226,13 @@ pub enum HttpMethod {
Delete,
}
-/// The databases a command may address.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
-#[serde(rename_all = "lowercase")]
+/// The databases the engine reads and writes.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Db {
- /// The vault the host has open.
+ /// The vault the host has open, reached through the `db*` commands.
Local,
- /// A throwaway database the engine opens for one step and discards: it materializes a pulled vault into it
- /// before that becomes the new local vault, and probes the current client schema by opening a fresh one.
+ /// A throwaway database the engine keeps in-process: it materializes a pulled vault into it before that
+ /// becomes the new local vault, and probes the current client schema by opening a fresh one.
Staging,
}
@@ -273,15 +272,13 @@ pub enum Command {
StateSet { key: String, value: Value },
/// Delete an engine-owned persisted value; response [`Ack`].
StateRemove { key: String },
- /// Open the staging database fresh with the current client schema applied; response [`Ack`].
- DbOpen { db: Db },
- /// Run a SELECT; response [`DbRows`].
- DbQuery { db: Db, sql: String, params: Vec<Value> },
- /// Run statements inside one transaction; response [`Ack`]. A `{ "__b64": ... }` parameter binds a BLOB.
- DbExec { db: Db, statements: Vec<SqlStatement> },
- /// Serialize a database; response [`Ack`], with the SQLite file handed back as raw bytes through
+ /// Run a SELECT on the local vault; response [`DbRows`].
+ DbQuery { sql: String, params: Vec<Value> },
+ /// Run statements on the local vault inside one transaction; response [`Ack`]. A `{ "__b64": ... }` parameter binds a BLOB.
+ DbExec { statements: Vec<SqlStatement> },
+ /// Serialize the local vault; response [`Ack`], with the SQLite file handed back as raw bytes through
/// `SyncSession::resume` (never base64 inside the JSON).
- DbExport { db: Db },
+ DbExport,
/// Persist the at-rest vault blob; response [`StoreOutcome`].
VaultStore {
encrypted_blob: String,
@@ -311,10 +308,9 @@ impl Command {
Command::StateGet { .. } => "stateGet",
Command::StateSet { .. } => "stateSet",
Command::StateRemove { .. } => "stateRemove",
- Command::DbOpen { .. } => "dbOpen",
Command::DbQuery { .. } => "dbQuery",
Command::DbExec { .. } => "dbExec",
- Command::DbExport { .. } => "dbExport",
+ Command::DbExport => "dbExport",
Command::VaultStore { .. } => "vaultStore",
Command::VaultLoad => "vaultLoad",
Command::MarkClean { .. } => "markClean",
diff --git a/core/vault/scripts/generate-vault-sql.cjs b/core/vault/scripts/generate-vault-sql.cjs
index 1510dfba6..a78a767cf 100644
--- a/core/vault/scripts/generate-vault-sql.cjs
+++ b/core/vault/scripts/generate-vault-sql.cjs
@@ -1,7 +1,6 @@
#!/usr/bin/env node
/**
* Writes the complete vault schema from the TypeScript source to a .sql file that the Rust core embeds,
- * so the native apps open a vault on the latest schema through SqliteMemoryDatabase.withLatestSchema().
*/
const fs = require('fs');