Skip to content

Commit

Permalink
1. Sync: code separation
Browse files Browse the repository at this point in the history
2. Bring Id52Bit
  • Loading branch information
cypherkitty committed Jan 5, 2025
1 parent 31d6b95 commit 0147ab0
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 52 deletions.
2 changes: 2 additions & 0 deletions meta-secret/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ exclude = [
thiserror = "2.0.9"
anyhow = "1.0.95"

derive_more = { version = "1.0.0", features = ["full"] }

# Logging and tracing
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19" }
Expand Down
2 changes: 2 additions & 0 deletions meta-secret/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ target = "x86_64-unknown-linux-gnu"
thiserror.workspace = true
anyhow.workspace = true

derive_more.workspace = true

async-trait.workspace = true
flume.workspace = true
async-mutex.workspace = true
Expand Down
45 changes: 43 additions & 2 deletions meta-secret/core/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ pub fn generate_hash() -> String {
hex::encode(hasher.finalize())
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[wasm_bindgen(getter_with_clone)]
pub struct Id52bit {
pub text: String,
}

impl Id52bit {
pub fn generate() -> Self {
let mut rng = rand::thread_rng();

let random_u64: u64 = rng.gen::<u64>() & 0xFFFFFFFFFFFF;

let hex_num = Self::hex(random_u64);
Self { text: hex_num }
}

pub fn take(&self, n: usize) -> String {
self.text.chars().take(n).collect::<String>()
}

fn hex(n: u64) -> String {
let hex_string = format!("{:x}", n);

// Calculate the length of each part (rounded down)
let part_length = hex_string.len() / 3;

// Split the string into four parts using slicing
let (part1, rest) = hex_string.split_at(part_length);
let (part2, part3) = rest.split_at(part_length);

format!("{}-{}-{}", part1, part2, part3)
}
}

impl IdString for Id52bit {
fn id_str(self) -> String {
self.text
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[wasm_bindgen(getter_with_clone)]
Expand All @@ -44,7 +85,7 @@ impl From<String> for U64IdUrlEnc {
}

impl IdString for U64IdUrlEnc {
fn id_str(&self) -> String {
fn id_str(self) -> String {
self.text.base64_str()
}
}
Expand Down Expand Up @@ -76,7 +117,7 @@ impl From<String> for UuidUrlEnc {
}

impl IdString for UuidUrlEnc {
fn id_str(&self) -> String {
fn id_str(self) -> String {
self.text.base64_str()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub mod fixture {
let state_provider = MetaClientStateProviderFixture::generate();
let dt_fxr = MetaClientDataTransferFixture::generate();

let sync_gateway = SyncGatewayFixture::from(&base);
let sync_gateway = SyncGatewayFixture::from(base);

let client = Arc::new(MetaClientService {
data_transfer: dt_fxr.client.clone(),
Expand Down
43 changes: 40 additions & 3 deletions meta-secret/core/src/node/app/sync/global_index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::node::common::model::device::common::DeviceData;
use crate::node::db::descriptors::global_index_descriptor::GlobalIndexDescriptor;
use crate::node::db::descriptors::object_descriptor::ObjectDescriptor;
use crate::node::db::events::generic_log_event::GenericKvLogEvent;
use crate::node::db::objects::global_index::ClientPersistentGlobalIndex;
use crate::node::db::objects::persistent_object::PersistentObject;
Expand Down Expand Up @@ -38,13 +40,48 @@ impl<Repo: KvLogEventRepo> GlobalIndexDbSync<Repo> {
}
}

impl<Repo: KvLogEventRepo> GlobalIndexDbSync<Repo> {
pub async fn get_gi_request(&self) -> Result<GlobalIndexRequest> {
let gi_free_id = self.p_gi.free_id().await?;
pub struct GlobalIndexDbSyncRequest<Repo: KvLogEventRepo> {
pub p_obj: Arc<PersistentObject<Repo>>,
pub sender: DeviceData,
}

impl<Repo: KvLogEventRepo> GlobalIndexDbSyncRequest<Repo> {
const GI_DESC: ObjectDescriptor = ObjectDescriptor::GlobalIndex(GlobalIndexDescriptor::Index);

/// Get a free global index id, to sync from
pub async fn get(&self) -> Result<GlobalIndexRequest> {
let gi_free_id = self.p_obj.find_free_id_by_obj_desc(Self::GI_DESC).await?;

Ok(GlobalIndexRequest {
sender: self.sender.clone(),
global_index: gi_free_id,
})
}
}

#[cfg(test)]
mod test {
use crate::meta_tests::fixture_util::fixture::FixtureRegistry;
use crate::node::app::sync::global_index::GlobalIndexDbSyncRequest;
use crate::node::db::descriptors::global_index_descriptor::GlobalIndexDescriptor;
use crate::node::db::descriptors::object_descriptor::ToObjectDescriptor;
use crate::node::db::events::object_id::ObjectId;
use anyhow::Result;

#[tokio::test]
async fn test_gi_request() -> Result<()> {
let fixture = FixtureRegistry::empty();

let db_sync_request = GlobalIndexDbSyncRequest {
p_obj: fixture.state.p_obj.client.clone(),
sender: fixture.state.device_creds.client.device,
};

let sync = db_sync_request.get().await?;

let expected_id = ObjectId::unit(GlobalIndexDescriptor::Index.to_obj_desc());
assert_eq!(expected_id, sync.global_index);

Ok(())
}
}
24 changes: 18 additions & 6 deletions meta-secret/core/src/node/app/sync/sync_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;

use tracing::{debug, error, info, instrument};

use crate::node::app::sync::global_index::GlobalIndexDbSync;
use crate::node::app::sync::global_index::{GlobalIndexDbSync, GlobalIndexDbSyncRequest};
use crate::node::common::model::device::common::{DeviceData, DeviceId};
use crate::node::common::model::user::common::UserId;
use crate::node::common::model::user::user_creds::UserCredentials;
Expand Down Expand Up @@ -239,20 +239,32 @@ impl<Repo: KvLogEventRepo> SyncGateway<Repo> {

#[instrument(skip(self))]
async fn download_global_index(&self, sender: DeviceData) -> Result<()> {
let gi_sync = GlobalIndexDbSync::new(self.p_obj.clone(), sender.clone());

let sync_request = gi_sync.get_gi_request().await?.to_sync_request();

let data_sync_request = self.build_sync_request(&sender).await?;

let DataEventsResponse(new_gi_events) = self
.server_dt
.dt
.send_to_service_and_get(DataSyncRequest::SyncRequest(sync_request))
.send_to_service_and_get(data_sync_request)
.await?
.to_data()?;

let gi_sync = GlobalIndexDbSync::new(self.p_obj.clone(), sender.clone());
gi_sync.save(new_gi_events).await
}

pub async fn build_sync_request(&self, sender: &DeviceData) -> Result<DataSyncRequest> {
let data_sync_request = {
let db_sync_request = GlobalIndexDbSyncRequest {
p_obj: self.p_obj.clone(),
sender: sender.clone(),
};
let gi_sync_request = db_sync_request.get().await?;
let sync_request = SyncRequest::from(gi_sync_request);
DataSyncRequest::from(sync_request)
};
Ok(data_sync_request)
}

#[instrument(skip(self))]
async fn sync_shared_secrets(
&self,
Expand Down
4 changes: 2 additions & 2 deletions meta-secret/core/src/node/common/model/device/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct DeviceId(pub U64IdUrlEnc);

#[wasm_bindgen]
impl DeviceId {
pub fn as_str(&self) -> String {
pub fn as_str(self) -> String {
self.0.id_str()
}
}
Expand All @@ -25,7 +25,7 @@ impl DeviceId {

impl Display for DeviceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
write!(f, "{}", self.clone().as_str())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl From<DeviceLink> for DeviceLinkId {
}

impl IdString for DeviceLinkId {
fn id_str(&self) -> String {
fn id_str(self) -> String {
String::try_from(&self.0).unwrap()
}
}
Expand Down
2 changes: 1 addition & 1 deletion meta-secret/core/src/node/common/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl From<ApplicationState> for WasmApplicationState {
}

pub trait IdString {
fn id_str(&self) -> String;
fn id_str(self) -> String;
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions meta-secret/core/src/node/common/model/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct SsDistributionId {
}

impl IdString for SsDistributionId {
fn id_str(&self) -> String {
fn id_str(self) -> String {
[self.receiver.as_str(), self.pass_id.id.id_str()].join("|")
}
}
Expand All @@ -36,7 +36,7 @@ pub struct SsDistributionClaimId {
}

impl IdString for SsDistributionClaimId {
fn id_str(&self) -> String {
fn id_str(self) -> String {
[self.id.0.clone(), self.pass_id.id.id_str()].join("|")
}
}
Expand All @@ -49,7 +49,7 @@ pub struct SsDistributionClaimDbId {
}

impl IdString for SsDistributionClaimDbId {
fn id_str(&self) -> String {
fn id_str(self) -> String {
[self.distribution_id.id_str(), self.claim_id.id_str()].join("|")
}
}
Expand Down
7 changes: 7 additions & 0 deletions meta-secret/core/src/node/common/model/vault/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::node::common::model::user::common::{UserData, UserDataMember, UserDat
use crate::node::common::model::vault::vault_data::{VaultData, WasmVaultData};
use std::fmt::Display;
use wasm_bindgen::prelude::wasm_bindgen;
use crate::crypto::utils::Id52bit;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -31,7 +32,13 @@ impl Display for VaultName {
}
}

#[wasm_bindgen]
impl VaultName {
pub fn generate() -> Self {
let id_str = Id52bit::generate().text;
Self(id_str)
}

pub fn test() -> VaultName {
VaultName::from("q")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ impl ObjectType for SharedSecretDescriptor {
impl SharedSecretDescriptor {
pub fn as_id_str(&self) -> String {
match self {
SharedSecretDescriptor::SsDistribution(event_id) => event_id.id_str(),
SharedSecretDescriptor::SsDistribution(event_id) => event_id.clone().id_str(),
SharedSecretDescriptor::SsLog(vault_name) => vault_name.to_string(),
SharedSecretDescriptor::SsDeviceLog(device_id) => device_id.to_string(),
SharedSecretDescriptor::SsDistributionStatus(id) => id.id_str(),
SharedSecretDescriptor::SsClaim(db_id) => db_id.id_str(),
SharedSecretDescriptor::SsDistributionStatus(id) => id.clone().id_str(),
SharedSecretDescriptor::SsClaim(db_id) => db_id.clone().id_str(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion meta-secret/core/src/node/server/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::node::common::model::device::common::DeviceData;
use crate::node::common::model::user::common::UserData;
use crate::node::db::events::object_id::ObjectId;
use crate::node::db::objects::persistent_vault::VaultTail;
use derive_more::From;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, From, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SyncRequest {
GlobalIndex(GlobalIndexRequest),
Expand Down
Loading

0 comments on commit 0147ab0

Please sign in to comment.