fix test_new_repo_default

pull/19/head
Niko PLP 7 months ago
parent f349d4a748
commit 4cdcf305ce
  1. 1
      ng-repo/Cargo.toml
  2. 9
      ng-repo/src/repo.rs
  3. 14
      ng-repo/src/store.rs
  4. 14
      ng-repo/src/types.rs
  5. 3
      ng-verifier/Cargo.toml
  6. 89
      ng-verifier/src/verifier.rs

@ -18,6 +18,7 @@ maintenance = { status = "actively-developed" }
[features]
server_log_output = []
testing = []
[dependencies]
blake3 = "1.3.1"

@ -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<u8>) -> RepositoryV0 {
@ -82,7 +83,7 @@ pub struct Repo {
pub signer: Option<SignerCap>,
pub members: HashMap<Digest, UserInfo>,
pub store: Box<Store>,
pub store: Arc<Store>,
}
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<Store>) -> Self {
pub fn new_with_perms(perms: &[PermissionV0], store: Arc<Store>) -> 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>,
store: Arc<Store>,
) -> Self {
let mut members = HashMap::new();
let permissions = HashMap::from_iter(

@ -80,7 +80,7 @@ impl Store {
}
pub fn create_repo_default(
self: Box<Self>,
self: Arc<Self>,
creator: &UserId,
creator_priv_key: &PrivKey,
) -> Result<(Repo, Vec<(Commit, Vec<Digest>)>), NgError> {
@ -443,14 +443,14 @@ impl Store {
}
}
#[cfg(test)]
#[allow(deprecated)]
pub fn dummy_public_v0() -> Box<Self> {
#[cfg(any(test, feature = "testing"))]
pub fn dummy_public_v0() -> Arc<Self> {
use crate::block_storage::HashMapBlockStorage;
let store_repo = StoreRepo::dummy_public_v0();
let store_readcap = ReadCap::dummy();
//let storage = Box::new() as Box<dyn BlockStorage + Send + Sync>;
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<Self> {
#[cfg(any(test, feature = "testing"))]
pub fn dummy_with_key(repo_pubkey: PubKey) -> Arc<Self> {
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<dyn BlockStorage + Send + Sync>;
Box::new(Self::new(
Arc::new(Self::new(
store_repo,
store_readcap,
Arc::new(RwLock::new(HashMapBlockStorage::new()))

@ -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))
}

@ -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"] }

@ -53,7 +53,7 @@ pub struct Verifier {
peer_id: PubKey,
max_reserved_seq_num: u64,
last_reservation: SystemTime,
stores: HashMap<OverlayId, Store>,
stores: HashMap<OverlayId, Arc<Store>>,
repos: HashMap<RepoId, Repo>,
}
@ -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<RwLock<Box<dyn BlockStorage + Send + Sync + 'static>>>;
let block_storage = Arc::new(std::sync::RwLock::new(HashMapBlockStorage::new()))
as Arc<std::sync::RwLock<dyn BlockStorage + Send + Sync>>;
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<Store> {
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<Store>) {
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<Event, NgError> {
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<BlockId>,
//topic_id: TopicId,
//topic_priv_key: &BranchWriteCapSecret,
store: &Store, // store could be omitted and a store repo ID would be given instead.
) -> Result<Event, NgError> {
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<u64, NgError> {
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<Vec<Event>, 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>,
store_repo: &StoreRepo,
) -> Result<(&'a Repo, Vec<Event>), 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));
}
}

Loading…
Cancel
Save