|
|
@ -53,7 +53,7 @@ pub struct Verifier { |
|
|
|
peer_id: PubKey, |
|
|
|
peer_id: PubKey, |
|
|
|
max_reserved_seq_num: u64, |
|
|
|
max_reserved_seq_num: u64, |
|
|
|
last_reservation: SystemTime, |
|
|
|
last_reservation: SystemTime, |
|
|
|
stores: HashMap<OverlayId, Store>, |
|
|
|
stores: HashMap<OverlayId, Arc<Store>>, |
|
|
|
repos: HashMap<RepoId, Repo>, |
|
|
|
repos: HashMap<RepoId, Repo>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -67,16 +67,17 @@ impl fmt::Debug for Verifier { |
|
|
|
impl Verifier { |
|
|
|
impl Verifier { |
|
|
|
#[cfg(test)] |
|
|
|
#[cfg(test)] |
|
|
|
pub fn new_dummy() -> Self { |
|
|
|
pub fn new_dummy() -> Self { |
|
|
|
|
|
|
|
use ng_repo::block_storage::HashMapBlockStorage; |
|
|
|
let (peer_priv_key, peer_id) = generate_keypair(); |
|
|
|
let (peer_priv_key, peer_id) = generate_keypair(); |
|
|
|
let block_storage = Arc::new(RwLock::new(HashMapBlockStorage::new())) |
|
|
|
let block_storage = Arc::new(std::sync::RwLock::new(HashMapBlockStorage::new())) |
|
|
|
as Arc<RwLock<Box<dyn BlockStorage + Send + Sync + 'static>>>; |
|
|
|
as Arc<std::sync::RwLock<dyn BlockStorage + Send + Sync>>; |
|
|
|
Verifier { |
|
|
|
Verifier { |
|
|
|
config: VerifierConfig { |
|
|
|
config: VerifierConfig { |
|
|
|
config_type: VerifierConfigType::Memory, |
|
|
|
config_type: VerifierConfigType::Memory, |
|
|
|
user_master_key: [0; 32], |
|
|
|
user_master_key: [0; 32], |
|
|
|
peer_priv_key, |
|
|
|
peer_priv_key, |
|
|
|
user_priv_key: PrivKey::random_ed(), |
|
|
|
user_priv_key: PrivKey::random_ed(), |
|
|
|
private_store_read_cap: ObjectRef::dummy(), |
|
|
|
private_store_read_cap: ObjectRef::nil(), |
|
|
|
}, |
|
|
|
}, |
|
|
|
connected_server_id: None, |
|
|
|
connected_server_id: None, |
|
|
|
graph_dataset: 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(); |
|
|
|
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
|
|
|
|
// FIXME: get store_readcap from user storage
|
|
|
|
let store_readcap = ReadCap::nil(); |
|
|
|
let store_readcap = ReadCap::nil(); |
|
|
|
let store = Store::new( |
|
|
|
let store = Store::new( |
|
|
@ -108,11 +109,18 @@ impl Verifier { |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
//self.stores.insert(overlay_id, store);
|
|
|
|
//self.stores.insert(overlay_id, store);
|
|
|
|
let store = self.stores.entry(overlay_id).or_insert(store); |
|
|
|
//let store = self.stores.entry(overlay_id).or_insert(store);
|
|
|
|
store |
|
|
|
Arc::new(store) |
|
|
|
} else { |
|
|
|
}); |
|
|
|
self.stores.get_mut(&overlay_id).unwrap() |
|
|
|
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( |
|
|
|
pub(crate) fn new_event( |
|
|
@ -124,13 +132,29 @@ impl Verifier { |
|
|
|
//topic_id: TopicId,
|
|
|
|
//topic_id: TopicId,
|
|
|
|
//topic_priv_key: &BranchWriteCapSecret,
|
|
|
|
//topic_priv_key: &BranchWriteCapSecret,
|
|
|
|
store: &Store, // store could be omitted and a store repo ID would be given instead.
|
|
|
|
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> { |
|
|
|
) -> Result<Event, NgError> { |
|
|
|
let topic_id = TopicId::nil(); // should be fetched from user storage, based on the Commit.branch
|
|
|
|
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 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( |
|
|
|
Event::new( |
|
|
|
&self.config.peer_priv_key, |
|
|
|
&self.config.peer_priv_key, |
|
|
|
seq, |
|
|
|
self.last_seq_num, |
|
|
|
commit, |
|
|
|
commit, |
|
|
|
additional_blocks, |
|
|
|
additional_blocks, |
|
|
|
topic_id, |
|
|
|
topic_id, |
|
|
@ -140,7 +164,7 @@ impl Verifier { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn last_seq_number(&mut self) -> Result<u64, NgError> { |
|
|
|
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.reserve_more(1)?; |
|
|
|
} |
|
|
|
} |
|
|
|
self.last_seq_num += 1; |
|
|
|
self.last_seq_num += 1; |
|
|
@ -154,24 +178,13 @@ impl Verifier { |
|
|
|
) -> Result<Vec<Event>, NgError> { |
|
|
|
) -> Result<Vec<Event>, NgError> { |
|
|
|
let missing_count = events.len() as i64 - self.available_seq_nums() as i64; |
|
|
|
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)
|
|
|
|
// 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 { |
|
|
|
if missing_count >= 0 { |
|
|
|
self.reserve_more(missing_count as u64 + 1)?; |
|
|
|
self.reserve_more(missing_count as u64 + 1)?; |
|
|
|
} |
|
|
|
} |
|
|
|
let mut res = vec![]; |
|
|
|
let mut res = vec![]; |
|
|
|
for event in events { |
|
|
|
for event in events { |
|
|
|
let topic_id = TopicId::nil(); // should be fetched from user storage, based on the Commit.branch
|
|
|
|
let event = self.new_event_(&event.0, &event.1, store)?; |
|
|
|
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, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
res.push(event); |
|
|
|
res.push(event); |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(res) |
|
|
|
Ok(res) |
|
|
@ -192,6 +205,11 @@ impl Verifier { |
|
|
|
fn take_some_peer_last_seq_numbers(&mut self, qty: u16) -> Result<(), NgError> { |
|
|
|
fn take_some_peer_last_seq_numbers(&mut self, qty: u16) -> Result<(), NgError> { |
|
|
|
// TODO the magic
|
|
|
|
// 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(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -259,14 +277,16 @@ impl Verifier { |
|
|
|
&'a mut self, |
|
|
|
&'a mut self, |
|
|
|
creator: &UserId, |
|
|
|
creator: &UserId, |
|
|
|
creator_priv_key: &PrivKey, |
|
|
|
creator_priv_key: &PrivKey, |
|
|
|
//store_repo: &StoreRepo,
|
|
|
|
store_repo: &StoreRepo, |
|
|
|
store: Box<Store>, |
|
|
|
|
|
|
|
) -> Result<(&'a Repo, Vec<Event>), NgError> { |
|
|
|
) -> 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)?; |
|
|
|
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 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); |
|
|
|
let repo_ref = self.repos.entry(repo.id).or_insert(repo); |
|
|
|
Ok((repo_ref, events)) |
|
|
|
Ok((repo_ref, events)) |
|
|
@ -278,6 +298,7 @@ mod test { |
|
|
|
use crate::types::*; |
|
|
|
use crate::types::*; |
|
|
|
use crate::verifier::*; |
|
|
|
use crate::verifier::*; |
|
|
|
use ng_repo::log::*; |
|
|
|
use ng_repo::log::*; |
|
|
|
|
|
|
|
use ng_repo::store::Store; |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
pub fn test_new_repo_default() { |
|
|
|
pub fn test_new_repo_default() { |
|
|
@ -287,12 +308,12 @@ mod test { |
|
|
|
let publisher_peer = PeerId::Forwarded(publisher_pubkey); |
|
|
|
let publisher_peer = PeerId::Forwarded(publisher_pubkey); |
|
|
|
|
|
|
|
|
|
|
|
let store = Store::dummy_public_v0(); |
|
|
|
let store = Store::dummy_public_v0(); |
|
|
|
|
|
|
|
let store_repo = store.get_store_repo().clone(); |
|
|
|
let mut verifier = Verifier::new_dummy(); |
|
|
|
let mut verifier = Verifier::new_dummy(); |
|
|
|
//let store = verifier.get_store(store_repo);
|
|
|
|
verifier.add_store(store); |
|
|
|
|
|
|
|
|
|
|
|
let (repo, events) = verifier |
|
|
|
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"); |
|
|
|
.expect("new_default"); |
|
|
|
|
|
|
|
|
|
|
|
log_debug!("REPO OBJECT {}", repo); |
|
|
|
log_debug!("REPO OBJECT {}", repo); |
|
|
@ -304,6 +325,6 @@ mod test { |
|
|
|
i += 1; |
|
|
|
i += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(verifier.last_seq_number(), 6); |
|
|
|
assert_eq!(verifier.last_seq_number(), Ok(6)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|