From b94b6fbeae11791c026509ae172ff213b4daa85e Mon Sep 17 00:00:00 2001 From: parazyd Date: Sun, 12 Nov 2023 16:30:06 +0100 Subject: [PATCH] eventgraph: Initialize prune task through OnceCell, and only when wanted --- src/event_graph/mod.rs | 44 +++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/event_graph/mod.rs b/src/event_graph/mod.rs index 269c2a4b41a3..ab7324fcc039 100644 --- a/src/event_graph/mod.rs +++ b/src/event_graph/mod.rs @@ -19,10 +19,7 @@ use std::{ cmp::Ordering, collections::{HashMap, HashSet, VecDeque}, - sync::{ - atomic::{AtomicBool, Ordering::SeqCst}, - Arc, - }, + sync::Arc, time::UNIX_EPOCH, }; @@ -31,7 +28,7 @@ use darkfi_serial::{deserialize_async, serialize_async}; use log::{debug, error, info}; use num_bigint::BigUint; use smol::{ - lock::{Mutex, RwLock}, + lock::{OnceCell, RwLock}, Executor, }; @@ -86,10 +83,8 @@ pub struct EventGraph { /// or not. Additionally it is also used when we broadcast the /// `TipRep` message telling peers about our unreferenced tips. broadcasted_ids: RwLock>, - /// Marker telling us if we consider the DAG synced - dag_synced: AtomicBool, /// DAG Pruning Task - prune_task: Mutex>, + prune_task: OnceCell, /// Event subscriber, this notifies whenever an event is /// inserted into the DAG pub event_sub: SubscriberPtr, @@ -115,8 +110,7 @@ impl EventGraph { dag: dag.clone(), unreferenced_tips, broadcasted_ids, - dag_synced: AtomicBool::new(false), - prune_task: Mutex::new(None), + prune_task: OnceCell::new(), event_sub, }); @@ -138,18 +132,20 @@ impl EventGraph { *self_.unreferenced_tips.write().await = self_.find_unreferenced_tips().await; // Spawn the DAG pruning task - let self__ = self_.clone(); - let prune_task = StoppableTask::new(); - *self_.prune_task.lock().await = Some(prune_task.clone()); - - prune_task.clone().start( - self_.clone().dag_prune(days_rotation), - |_| async move { - self__.clone()._handle_stop(sled_db).await; - }, - Error::DetachedTaskStopped, - ex.clone(), - ); + if days_rotation > 0 { + let self__ = self_.clone(); + let prune_task = StoppableTask::new(); + let _ = self_.prune_task.set(prune_task.clone()).await; + + prune_task.clone().start( + self_.clone().dag_prune(days_rotation), + |_| async move { + self__.clone()._handle_stop(sled_db).await; + }, + Error::DetachedTaskStopped, + ex.clone(), + ); + } Ok(self_) } @@ -404,7 +400,6 @@ impl EventGraph { } info!(target: "event_graph::dag_sync()", "[EVENTGRAPH] DAG synced successfully!"); - self.dag_synced.store(true, SeqCst); Ok(()) } @@ -417,9 +412,6 @@ impl EventGraph { debug!(target: "event_graph::dag_prune()", "Spawned background DAG pruning task"); loop { - if days_rotation == 0 { - return Ok(()) - } // Find the next rotation timestamp: let next_rotation = next_rotation_timestamp(INITIAL_GENESIS, days_rotation);