event-graph: Find and fill the unreferenced tips on EventGraph::new()

This commit is contained in:
parazyd
2023-09-07 11:14:07 +02:00
parent b184cfb5b8
commit a81f8f096d
2 changed files with 28 additions and 12 deletions

View File

@@ -66,8 +66,6 @@ pub struct EventGraph {
dag: sled::Tree,
/// The set of unreferenced DAG tips
unreferenced_tips: RwLock<HashSet<blake3::Hash>>,
/// The last event ID inserted into the DAG
last_event: RwLock<blake3::Hash>,
/// A `HashSet` containg event IDs and their 1-level parents.
/// These come from the events we've sent out using `EventPut`.
/// They are used with `EventReq` to decide if we should reply
@@ -90,16 +88,9 @@ impl EventGraph {
) -> Result<EventGraphPtr> {
let dag = sled_db.open_tree(dag_tree_name)?;
let unreferenced_tips = RwLock::new(HashSet::new());
let last_event = RwLock::new(NULL_ID);
let broadcasted_ids = RwLock::new(HashSet::new());
let self_ = Arc::new(Self {
p2p,
dag: dag.clone(),
unreferenced_tips,
last_event,
broadcasted_ids,
});
let self_ = Arc::new(Self { p2p, dag: dag.clone(), unreferenced_tips, broadcasted_ids });
// Create the current genesis event based on the `days_rotation`
let current_genesis = Self::generate_genesis(days_rotation);
@@ -112,6 +103,9 @@ impl EventGraph {
self_.dag_insert(&current_genesis).await?;
}
// Find the unreferenced tips in the current DAG state.
*self_.unreferenced_tips.write().await = self_.find_unreferenced_tips().await;
Ok(self_)
}
@@ -186,11 +180,31 @@ impl EventGraph {
drop(bcast_ids);
self.dag.insert(event_id.as_bytes(), s_event).unwrap();
*self.last_event.write().await = event_id;
Ok(event_id)
}
/// Find the unreferenced tips in the current DAG state.
async fn find_unreferenced_tips(&self) -> HashSet<blake3::Hash> {
// First get all the event IDs
let mut tips = HashSet::new();
for iter_elem in self.dag.iter() {
let (id, _) = iter_elem.unwrap();
let id = blake3::Hash::from_bytes((&id as &[u8]).try_into().unwrap());
tips.insert(id);
}
for iter_elem in self.dag.iter() {
let (_, event) = iter_elem.unwrap();
let event: Event = deserialize_async(&event).await.unwrap();
for parent in event.parents.iter() {
tips.remove(parent);
}
}
tips
}
/// Get the current set of unreferenced tips in the DAG.
async fn get_unreferenced_tips(&self) -> [blake3::Hash; N_EVENT_PARENTS] {
let unreferenced_tips = self.unreferenced_tips.read().await;

View File

@@ -112,8 +112,10 @@ async fn eventgraph_propagation_real(ex: Arc<Executor<'static>>) {
EventGraph::new(p2p.clone(), &sled_db, "dag", 1, ex.clone()).await.unwrap();
let event_graph_ = event_graph.clone();
// Take the last sled item since there's only 1
if genesis_event_id == NULL_ID {
genesis_event_id = *event_graph.last_event.read().await;
let (id, _) = event_graph.dag.last().unwrap().unwrap();
genesis_event_id = blake3::Hash::from_bytes((&id as &[u8]).try_into().unwrap());
}
// Register the P2P protocols