Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple event log references from sync feature #393

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions crates/net/src/client/account/network_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use sos_sdk::{
AccountStatistics, ArchiveFilter, Document, DocumentCount,
DocumentView, QueryFilter, SearchIndex,
},
AccessOptions, ClientStorage,
AccessOptions, ClientStorage, StorageEventLogs,
},
sync::{Origin, SyncOptions, SyncStorage, UpdateSet},
sync::{Origin, SyncOptions, UpdateSet},
vault::{
secret::{Secret, SecretId, SecretMeta, SecretRow},
Summary, Vault, VaultId,
Expand Down Expand Up @@ -760,14 +760,14 @@ impl Account for NetworkAccount {
&mut self,
) -> Result<HashMap<Summary, (AccountEvent, u64, u64)>> {
let result = {
let mut account = self.account.lock().await;
account.compact_account().await?
let mut account = self.account.lock().await;
account.compact_account().await?
};

let identity = {
let log = self.identity_log().await?;
let reader = log.read().await;
Some(reader.diff(None).await?)
let log = self.identity_log().await?;
let reader = log.read().await;
Some(reader.diff(None).await?)
};

// Prepare event logs for the folders that
Expand Down Expand Up @@ -810,8 +810,8 @@ impl Account for NetworkAccount {
folder: &Summary,
) -> Result<(AccountEvent, u64, u64)> {
let result = {
let mut account = self.account.lock().await;
account.compact_folder(folder).await?
let mut account = self.account.lock().await;
account.compact_folder(folder).await?
};

// Prepare event logs for the folders that
Expand All @@ -825,7 +825,10 @@ impl Account for NetworkAccount {

// Force update the folders on remote servers
let sync_options: SyncOptions = Default::default();
let updates = UpdateSet { identity: None, folders };
let updates = UpdateSet {
identity: None,
folders,
};

let sync_error = self.force_update(&updates, &sync_options).await;
if let Some(sync_error) = sync_error {
Expand All @@ -848,14 +851,14 @@ impl Account for NetworkAccount {
new_key: AccessKey,
) -> Result<()> {
{
let mut account = self.account.lock().await;
account.change_folder_password(folder, new_key).await?;
let mut account = self.account.lock().await;
account.change_folder_password(folder, new_key).await?;
}

let identity = {
let log = self.identity_log().await?;
let reader = log.read().await;
Some(reader.diff(None).await?)
let log = self.identity_log().await?;
let reader = log.read().await;
Some(reader.diff(None).await?)
};

// Prepare event logs for the folders that
Expand Down
16 changes: 10 additions & 6 deletions crates/net/src/client/account/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::client::{NetworkAccount, RemoteSync, SyncError};
use async_trait::async_trait;
use sos_sdk::{
events::{AccountEventLog, FolderEventLog},
storage::StorageEventLogs,
sync::{
Origin, SyncClient, SyncOptions, SyncStatus, SyncStorage, UpdateSet,
},
Expand Down Expand Up @@ -170,12 +171,7 @@ impl RemoteSync for NetworkAccount {
}

#[async_trait]
impl SyncStorage for NetworkAccount {
async fn sync_status(&self) -> Result<SyncStatus> {
let account = self.account.lock().await;
account.sync_status().await
}

impl StorageEventLogs for NetworkAccount {
async fn identity_log(&self) -> Result<Arc<RwLock<FolderEventLog>>> {
let account = self.account.lock().await;
account.identity_log().await
Expand Down Expand Up @@ -211,3 +207,11 @@ impl SyncStorage for NetworkAccount {
account.folder_log(id).await
}
}

#[async_trait]
impl SyncStorage for NetworkAccount {
async fn sync_status(&self) -> Result<SyncStatus> {
let account = self.account.lock().await;
account.sync_status().await
}
}
103 changes: 73 additions & 30 deletions crates/sdk/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ use crate::{
decode, encode,
events::{
AccountEvent, AccountEventLog, Event, EventKind, EventLogExt,
FolderReducer, ReadEvent, WriteEvent,
FolderEventLog, FolderReducer, ReadEvent, WriteEvent,
},
identity::{AccountRef, FolderKeys, Identity, PublicIdentity},
signer::ecdsa::{Address, BoxedEcdsaSigner},
storage::AccountPack,
storage::{
search::{DocumentCount, SearchIndex},
AccessOptions, ClientStorage,
AccessOptions, AccountPack, ClientStorage, StorageEventLogs,
},
vault::{
secret::{Secret, SecretId, SecretMeta, SecretRow, SecretType},
Gatekeeper, Header, Summary, Vault, VaultId,
BuilderCredentials, VaultBuilder,
BuilderCredentials, Gatekeeper, Header, Summary, Vault, VaultBuilder,
VaultId,
},
vfs, Error, Paths, Result, UtcDateTime,
};
Expand All @@ -37,18 +36,22 @@ use crate::audit::{AuditData, AuditEvent};
use crate::account::archive::{Inventory, RestoreOptions};

#[cfg(feature = "device")]
use crate::device::{
DeviceManager, DevicePublicKey, DeviceSigner, TrustedDevice,
use crate::{
device::{DeviceManager, DevicePublicKey, DeviceSigner, TrustedDevice},
events::DeviceEventLog,
};

#[cfg(feature = "device")]
use indexmap::IndexSet;

#[cfg(feature = "files")]
use crate::events::FileEventLog;

#[cfg(feature = "search")]
use crate::storage::search::*;

#[cfg(feature = "sync")]
use crate::sync::{SyncError, SyncStorage};
use crate::sync::SyncError;

#[cfg(all(feature = "files", feature = "sync"))]
use crate::storage::files::Transfers;
Expand All @@ -57,19 +60,17 @@ use crate::storage::files::Transfers;
use crate::{account::security_report::*, zxcvbn::Entropy};

#[cfg(feature = "migrate")]
use crate::{
migrate::{
export::PublicExport,
import::{
csv::{
bitwarden::BitwardenCsv, chrome::ChromePasswordCsv,
dashlane::DashlaneCsvZip, firefox::FirefoxPasswordCsv,
macos::MacPasswordCsv, one_password::OnePasswordCsv,
},
ImportFormat, ImportTarget,
use crate::migrate::{
export::PublicExport,
import::{
csv::{
bitwarden::BitwardenCsv, chrome::ChromePasswordCsv,
dashlane::DashlaneCsvZip, firefox::FirefoxPasswordCsv,
macos::MacPasswordCsv, one_password::OnePasswordCsv,
},
Convert,
ImportFormat, ImportTarget,
},
Convert,
};

use tracing::{span, Level};
Expand Down Expand Up @@ -430,7 +431,6 @@ pub trait Account {
) -> std::result::Result<CommitState, Self::Error>;

/// Compact the identity folder and all user folders.
#[cfg(feature = "sync")]
async fn compact_account(
&mut self,
) -> std::result::Result<
Expand Down Expand Up @@ -659,7 +659,6 @@ pub trait Account {
///
/// This is used for destructive operations that rewrite the identity
/// folder such as changing the cipher or account password.
#[cfg(feature = "sync")]
async fn import_identity_folder(
&mut self,
vault: Vault,
Expand Down Expand Up @@ -1596,11 +1595,8 @@ impl Account for LocalAccount {
let secret_data = SecretRow::new(*key, meta, secret);
output.create_secret(&secret_data).await?;
}

#[cfg(feature = "sync")]
{
self.import_identity_folder(output.into()).await?;
}

self.import_identity_folder(output.into()).await?;

// Login again so in-memory data is up to date
let identity_vault_path = self.paths().identity_vault();
Expand Down Expand Up @@ -1808,8 +1804,7 @@ impl Account for LocalAccount {
let reader = storage.read().await;
Ok(reader.commit_state(summary).await?)
}

#[cfg(feature = "sync")]

async fn compact_account(
&mut self,
) -> Result<HashMap<Summary, (AccountEvent, u64, u64)>> {
Expand Down Expand Up @@ -2478,8 +2473,7 @@ impl Account for LocalAccount {
marker: std::marker::PhantomData,
})
}

#[cfg(feature = "sync")]

async fn import_identity_folder(
&mut self,
vault: Vault,
Expand Down Expand Up @@ -3081,3 +3075,52 @@ impl Account for LocalAccount {
Ok(account)
}
}

#[async_trait]
impl StorageEventLogs for LocalAccount {
async fn identity_log(&self) -> Result<Arc<RwLock<FolderEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.identity_log))
}

async fn account_log(&self) -> Result<Arc<RwLock<AccountEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.account_log))
}

#[cfg(feature = "device")]
async fn device_log(&self) -> Result<Arc<RwLock<DeviceEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.device_log))
}

#[cfg(feature = "files")]
async fn file_log(&self) -> Result<Arc<RwLock<FileEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.file_log))
}

async fn folder_identifiers(&self) -> Result<Vec<VaultId>> {
let storage = self.storage().await?;
let storage = storage.read().await;
let summaries = storage.list_folders().to_vec();
Ok(summaries.iter().map(|s| *s.id()).collect())
}

async fn folder_log(
&self,
id: &VaultId,
) -> Result<Arc<RwLock<FolderEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
let folder = storage
.cache()
.get(id)
.ok_or(Error::CacheNotAvailable(*id))?;
Ok(folder.event_log())
}
}
7 changes: 2 additions & 5 deletions crates/sdk/src/account/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,8 @@ impl LocalAccount {
false,
)
.await?;

#[cfg(feature = "sync")]
{
self.import_identity_folder(vault).await?;
}

self.import_identity_folder(vault).await?;
};

Ok(())
Expand Down
60 changes: 4 additions & 56 deletions crates/sdk/src/account/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::{
account::{Account, LocalAccount},
commit::{CommitState, Comparison},
decode,
events::{
AccountEvent, AccountEventLog, EventLogExt, FolderEventLog, LogEvent,
},
events::{AccountEvent, EventLogExt, LogEvent},
storage::StorageEventLogs,
sync::{
AccountDiff, CheckedPatch, FolderDiff, FolderMergeOptions, Merge,
SyncStatus, SyncStorage,
Expand All @@ -14,17 +13,12 @@ use crate::{
};
use async_trait::async_trait;
use indexmap::IndexMap;
use std::sync::Arc;
use tokio::sync::RwLock;

#[cfg(feature = "device")]
use crate::{
events::{DeviceEventLog, DeviceReducer},
sync::DeviceDiff,
};
use crate::{events::DeviceReducer, sync::DeviceDiff};

#[cfg(feature = "files")]
use crate::{events::FileEventLog, sync::FileDiff};
use crate::sync::FileDiff;

#[async_trait]
impl Merge for LocalAccount {
Expand Down Expand Up @@ -376,50 +370,4 @@ impl SyncStorage for LocalAccount {
folders,
})
}

async fn identity_log(&self) -> Result<Arc<RwLock<FolderEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.identity_log))
}

async fn account_log(&self) -> Result<Arc<RwLock<AccountEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.account_log))
}

#[cfg(feature = "device")]
async fn device_log(&self) -> Result<Arc<RwLock<DeviceEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.device_log))
}

#[cfg(feature = "files")]
async fn file_log(&self) -> Result<Arc<RwLock<FileEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
Ok(Arc::clone(&storage.file_log))
}

async fn folder_identifiers(&self) -> Result<Vec<VaultId>> {
let storage = self.storage().await?;
let storage = storage.read().await;
let summaries = storage.list_folders().to_vec();
Ok(summaries.iter().map(|s| *s.id()).collect())
}

async fn folder_log(
&self,
id: &VaultId,
) -> Result<Arc<RwLock<FolderEventLog>>> {
let storage = self.storage().await?;
let storage = storage.read().await;
let folder = storage
.cache()
.get(id)
.ok_or(Error::CacheNotAvailable(*id))?;
Ok(folder.event_log())
}
}
Loading