Skip to content

Commit

Permalink
Prepare logic to create a detached view.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Oct 26, 2023
1 parent e0f3184 commit fe5c673
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion workspace/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ either = { version = "1.8", optional = true }

[dependencies.sos-sdk]
version = "0.5.19"
#path = "../sdk"
path = "../sdk"

[dependencies.sos-migrate]
version = "0.5.14"
Expand Down
45 changes: 43 additions & 2 deletions workspace/net/src/client/user/user_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ use sos_sdk::{
AuthenticatedUser, DelegatedPassphrase, ExtractFilesLocation,
ImportedAccount, LocalAccounts, Login, NewAccount, RestoreOptions,
},
commit::CommitHash,
crypto::AccessKey,
decode, encode,
events::{
AuditData, AuditEvent, AuditProvider, Event, EventKind, ReadEvent,
WriteEvent,
WriteEvent, EventReducer,
},
mpc::generate_keypair,
search::{DocumentCount, SearchIndex},
signer::ecdsa::Address,
storage::{AppPaths, UserPaths},
vault::{
secret::{Secret, SecretData, SecretId, SecretMeta, SecretType},
Summary, Vault, VaultAccess, VaultId, VaultWriter,
Gatekeeper, Summary, Vault, VaultAccess, VaultId, VaultWriter,
},
vfs::{self, File},
Timestamp,
Expand Down Expand Up @@ -56,6 +57,20 @@ use sos_migrate::{

use super::{file_manager::FileProgress, search_index::UserIndex};

/// Read-only view of a vault created from a specific
/// event log commit.
pub struct DetachedView {
search: Arc<RwLock<SearchIndex>>,
keeper: Gatekeeper,
}

impl DetachedView {
/// Read-only access to the vault.
pub fn keeper(&self) -> &Gatekeeper {
&self.keeper
}
}

/// Options used when creating, deleting and updating
/// secrets.
#[derive(Default)]
Expand Down Expand Up @@ -1599,4 +1614,30 @@ impl UserStorage {

Ok((account, owner))
}

/// Create a detached view of an event log until a
/// particular commit.
///
/// This is useful for time travel; browsing the event
/// history at a particular point in time.
pub async fn create_detached_view(
&self,
summary: Summary,
commit: CommitHash,
) -> Result<DetachedView> {
let cache = self.storage.cache();
let (log_file, _) = cache
.get(summary.id())
.ok_or_else(|| Error::CacheNotAvailable(*summary.id()))?;

let vault = EventReducer::new_until_commit(commit)
.reduce(log_file).await?
.build().await?;

let search = Arc::new(RwLock::new(SearchIndex::new()));
let mut keeper = Gatekeeper::new(vault, Some(Arc::clone(&search)));
keeper.create_search_index().await?;

Ok(DetachedView { search, keeper })
}
}
8 changes: 8 additions & 0 deletions workspace/sdk/src/events/log/reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl<'a> EventReducer<'a> {
Default::default()
}

/// Create a new reducer until a commit.
pub fn new_until_commit(until: CommitHash) -> Self {
Self {
until_commit: Some(until),
..Default::default()
}
}

/// Split a vault into a truncated vault and a collection
/// of events that represent the vault.
///
Expand Down

0 comments on commit fe5c673

Please sign in to comment.