From 4cdcf305ce056d0598d7d9b7adf11db16a8ddad8 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Tue, 16 Apr 2024 12:16:40 +0300 Subject: [PATCH] fix test_new_repo_default --- ng-repo/Cargo.toml | 1 + ng-repo/src/repo.rs | 9 ++-- ng-repo/src/store.rs | 14 +++--- ng-repo/src/types.rs | 14 +++--- ng-verifier/Cargo.toml | 3 ++ ng-verifier/src/verifier.rs | 89 +++++++++++++++++++++++-------------- 6 files changed, 78 insertions(+), 52 deletions(-) diff --git a/ng-repo/Cargo.toml b/ng-repo/Cargo.toml index f4b2ce6..058c0df 100644 --- a/ng-repo/Cargo.toml +++ b/ng-repo/Cargo.toml @@ -18,6 +18,7 @@ maintenance = { status = "actively-developed" } [features] server_log_output = [] +testing = [] [dependencies] blake3 = "1.3.1" diff --git a/ng-repo/src/repo.rs b/ng-repo/src/repo.rs index 1b5658c..b3bb8cf 100644 --- a/ng-repo/src/repo.rs +++ b/ng-repo/src/repo.rs @@ -22,6 +22,7 @@ use core::fmt; use std::collections::HashMap; use std::collections::HashSet; +use std::sync::Arc; impl RepositoryV0 { pub fn new(id: &PubKey, metadata: &Vec) -> RepositoryV0 { @@ -82,7 +83,7 @@ pub struct Repo { pub signer: Option, pub members: HashMap, - pub store: Box, + pub store: Arc, } impl fmt::Display for Repo { @@ -102,9 +103,9 @@ impl fmt::Display for Repo { } impl Repo { - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] #[allow(deprecated)] - pub fn new_with_perms(perms: &[PermissionV0], store: Box) -> Self { + pub fn new_with_perms(perms: &[PermissionV0], store: Arc) -> Self { let pub_key = PubKey::nil(); Self::new_with_member(&pub_key, &pub_key, perms, OverlayId::dummy(), store) } @@ -114,7 +115,7 @@ impl Repo { member: &UserId, perms: &[PermissionV0], overlay: OverlayId, - store: Box, + store: Arc, ) -> Self { let mut members = HashMap::new(); let permissions = HashMap::from_iter( diff --git a/ng-repo/src/store.rs b/ng-repo/src/store.rs index ef95168..28ab02f 100644 --- a/ng-repo/src/store.rs +++ b/ng-repo/src/store.rs @@ -80,7 +80,7 @@ impl Store { } pub fn create_repo_default( - self: Box, + self: Arc, creator: &UserId, creator_priv_key: &PrivKey, ) -> Result<(Repo, Vec<(Commit, Vec)>), NgError> { @@ -443,14 +443,14 @@ impl Store { } } - #[cfg(test)] #[allow(deprecated)] - pub fn dummy_public_v0() -> Box { + #[cfg(any(test, feature = "testing"))] + pub fn dummy_public_v0() -> Arc { use crate::block_storage::HashMapBlockStorage; let store_repo = StoreRepo::dummy_public_v0(); let store_readcap = ReadCap::dummy(); //let storage = Box::new() as Box; - Box::new(Self::new( + Arc::new(Self::new( store_repo, store_readcap, Arc::new(RwLock::new(HashMapBlockStorage::new())) @@ -458,13 +458,13 @@ impl Store { )) } - #[cfg(test)] - pub fn dummy_with_key(repo_pubkey: PubKey) -> Box { + #[cfg(any(test, feature = "testing"))] + pub fn dummy_with_key(repo_pubkey: PubKey) -> Arc { use crate::block_storage::HashMapBlockStorage; let store_repo = StoreRepo::dummy_with_key(repo_pubkey); let store_readcap = ReadCap::dummy(); //let storage = Box::new() as Box; - Box::new(Self::new( + Arc::new(Self::new( store_repo, store_readcap, Arc::new(RwLock::new(HashMapBlockStorage::new())) diff --git a/ng-repo/src/types.rs b/ng-repo/src/types.rs index 3b325b3..7b602dd 100644 --- a/ng-repo/src/types.rs +++ b/ng-repo/src/types.rs @@ -95,7 +95,7 @@ impl SymKey { pub fn nil() -> Self { SymKey::ChaCha20Key([0; 32]) } - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy() -> Self { SymKey::ChaCha20Key([0; 32]) } @@ -222,7 +222,7 @@ impl PrivKey { PrivKey::Ed25519PrivKey([0u8; 32]) } - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy() -> PrivKey { PrivKey::Ed25519PrivKey([0u8; 32]) } @@ -394,7 +394,7 @@ pub struct BlockRef { } impl BlockId { - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy() -> Self { Digest::Blake3Digest32([0u8; 32]) } @@ -406,7 +406,7 @@ impl BlockId { } impl BlockRef { - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy() -> Self { BlockRef { id: Digest::Blake3Digest32([0u8; 32]), @@ -543,7 +543,7 @@ impl OverlayId { let store_id = serde_bare::to_vec(store_id).unwrap(); OverlayId::Outer((&store_id).into()) } - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy() -> OverlayId { OverlayId::Outer(Digest::dummy()) } @@ -667,13 +667,13 @@ impl StoreRepo { }, } } - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] #[allow(deprecated)] pub fn dummy_public_v0() -> Self { let store_pubkey = PubKey::nil(); StoreRepo::V0(StoreRepoV0::PublicStore(store_pubkey)) } - #[cfg(test)] + #[cfg(any(test, feature = "testing"))] pub fn dummy_with_key(repo_pubkey: PubKey) -> Self { StoreRepo::V0(StoreRepoV0::PublicStore(repo_pubkey)) } diff --git a/ng-verifier/Cargo.toml b/ng-verifier/Cargo.toml index 41a0dc0..e6d22b0 100644 --- a/ng-verifier/Cargo.toml +++ b/ng-verifier/Cargo.toml @@ -34,3 +34,6 @@ web-time = "0.2.0" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] ng-storage-rocksdb = { path = "../ng-storage-rocksdb", version = "0.1.0" } getrandom = "0.2.7" + +[dev-dependencies] +ng-repo = { path = "../ng-repo", version = "0.1.0", features = ["testing"] } \ No newline at end of file diff --git a/ng-verifier/src/verifier.rs b/ng-verifier/src/verifier.rs index ce15fcf..0e7a47b 100644 --- a/ng-verifier/src/verifier.rs +++ b/ng-verifier/src/verifier.rs @@ -53,7 +53,7 @@ pub struct Verifier { peer_id: PubKey, max_reserved_seq_num: u64, last_reservation: SystemTime, - stores: HashMap, + stores: HashMap>, repos: HashMap, } @@ -67,16 +67,17 @@ impl fmt::Debug for Verifier { impl Verifier { #[cfg(test)] pub fn new_dummy() -> Self { + use ng_repo::block_storage::HashMapBlockStorage; let (peer_priv_key, peer_id) = generate_keypair(); - let block_storage = Arc::new(RwLock::new(HashMapBlockStorage::new())) - as Arc>>; + let block_storage = Arc::new(std::sync::RwLock::new(HashMapBlockStorage::new())) + as Arc>; Verifier { config: VerifierConfig { config_type: VerifierConfigType::Memory, user_master_key: [0; 32], peer_priv_key, user_priv_key: PrivKey::random_ed(), - private_store_read_cap: ObjectRef::dummy(), + private_store_read_cap: ObjectRef::nil(), }, connected_server_id: None, graph_dataset: None, @@ -91,9 +92,9 @@ impl Verifier { } } - pub fn get_store(&mut self, store_repo: &StoreRepo) -> &mut Store { + pub fn get_store(&mut self, store_repo: &StoreRepo) -> Arc { let overlay_id = store_repo.overlay_id_for_storage_purpose(); - if self.stores.get(&overlay_id).is_none() { + let store = self.stores.entry(overlay_id).or_insert_with(|| { // FIXME: get store_readcap from user storage let store_readcap = ReadCap::nil(); let store = Store::new( @@ -108,11 +109,18 @@ impl Verifier { ), ); //self.stores.insert(overlay_id, store); - let store = self.stores.entry(overlay_id).or_insert(store); - store - } else { - self.stores.get_mut(&overlay_id).unwrap() + //let store = self.stores.entry(overlay_id).or_insert(store); + Arc::new(store) + }); + Arc::clone(store) + } + + pub fn add_store(&mut self, store: Arc) { + let overlay_id = store.get_store_repo().overlay_id_for_storage_purpose(); + if self.stores.contains_key(&overlay_id) { + return; } + self.stores.insert(overlay_id, store); } pub(crate) fn new_event( @@ -124,13 +132,29 @@ impl Verifier { //topic_id: TopicId, //topic_priv_key: &BranchWriteCapSecret, store: &Store, // store could be omitted and a store repo ID would be given instead. + ) -> Result { + if self.last_seq_num + 1 >= self.max_reserved_seq_num { + self.reserve_more(1)?; + } + self.new_event_(commit, additional_blocks, store) + } + + fn new_event_( + &mut self, + //publisher: &PrivKey, + //seq: &mut u64, + commit: &Commit, + additional_blocks: &Vec, + //topic_id: TopicId, + //topic_priv_key: &BranchWriteCapSecret, + store: &Store, // store could be omitted and a store repo ID would be given instead. ) -> Result { let topic_id = TopicId::nil(); // should be fetched from user storage, based on the Commit.branch let topic_priv_key = BranchWriteCapSecret::nil(); // should be fetched from user storage, based on repoId found in user storage (search by branchId) - let seq = self.last_seq_number()?; + self.last_seq_num += 1; Event::new( &self.config.peer_priv_key, - seq, + self.last_seq_num, commit, additional_blocks, topic_id, @@ -140,7 +164,7 @@ impl Verifier { } pub(crate) fn last_seq_number(&mut self) -> Result { - if self.last_seq_num - 1 >= self.max_reserved_seq_num { + if self.last_seq_num + 1 >= self.max_reserved_seq_num { self.reserve_more(1)?; } self.last_seq_num += 1; @@ -154,24 +178,13 @@ impl Verifier { ) -> Result, NgError> { let missing_count = events.len() as i64 - self.available_seq_nums() as i64; // this is reducing the capacity of reserver_seq_num by half (cast from u64 to i64) - // but we will never reach situation where so many seq_nums are reserved, neither such a big list of events to processs + // but we will never reach situation where so many seq_nums are reserved, neither such a big list of events to process if missing_count >= 0 { self.reserve_more(missing_count as u64 + 1)?; } let mut res = vec![]; for event in events { - let topic_id = TopicId::nil(); // should be fetched from user storage, based on the Commit.branch - let topic_priv_key = BranchWriteCapSecret::nil(); // should be fetched from user storage, based on repoId found in user storage (search by branchId) - self.last_seq_num += 1; - let event = Event::new( - &self.config.peer_priv_key, - self.last_seq_num, - &event.0, - &event.1, - topic_id, - &topic_priv_key, - store, - )?; + let event = self.new_event_(&event.0, &event.1, store)?; res.push(event); } Ok(res) @@ -192,6 +205,11 @@ impl Verifier { fn take_some_peer_last_seq_numbers(&mut self, qty: u16) -> Result<(), NgError> { // TODO the magic + self.max_reserved_seq_num += qty as u64; + log_debug!( + "reserving more seq_nums {qty}. now at {}", + self.max_reserved_seq_num + ); Ok(()) } @@ -259,14 +277,16 @@ impl Verifier { &'a mut self, creator: &UserId, creator_priv_key: &PrivKey, - //store_repo: &StoreRepo, - store: Box, + store_repo: &StoreRepo, ) -> Result<(&'a Repo, Vec), NgError> { - //let store = self.get_store(store_repo); + let store = self.get_store(store_repo); let (repo, proto_events) = store.create_repo_default(creator, creator_priv_key)?; - //repo.store = Some(store); let events = self.new_events(proto_events, &repo.store)?; + // let mut events = vec![]; + // for event in proto_events { + // events.push(self.new_event(&event.0, &event.1, &repo.store)?); + // } let repo_ref = self.repos.entry(repo.id).or_insert(repo); Ok((repo_ref, events)) @@ -278,6 +298,7 @@ mod test { use crate::types::*; use crate::verifier::*; use ng_repo::log::*; + use ng_repo::store::Store; #[test] pub fn test_new_repo_default() { @@ -287,12 +308,12 @@ mod test { let publisher_peer = PeerId::Forwarded(publisher_pubkey); let store = Store::dummy_public_v0(); - + let store_repo = store.get_store_repo().clone(); let mut verifier = Verifier::new_dummy(); - //let store = verifier.get_store(store_repo); + verifier.add_store(store); let (repo, events) = verifier - .new_repo_default(&creator_pub_key, &creator_priv_key, store) + .new_repo_default(&creator_pub_key, &creator_priv_key, &store_repo) .expect("new_default"); log_debug!("REPO OBJECT {}", repo); @@ -304,6 +325,6 @@ mod test { i += 1; } - assert_eq!(verifier.last_seq_number(), 6); + assert_eq!(verifier.last_seq_number(), Ok(6)); } }