瀏覽代碼

[net] Modify Event pruning debugging

Change approach to dynamically calculate Genesis rather than storing it
in the EventGraph struct.

Add error-level debug messages in the case when a peer requests an
outdated Event and the node responds without an error. This indicates
that our Dag contains outdated events, indicating that the previous
prune failed.
y 2 年之前
父節點
當前提交
bf5d77c31f
共有 2 個文件被更改,包括 28 次插入10 次删除
  1. 0 6
      src/event_graph/mod.rs
  2. 28 4
      src/event_graph/proto.rs

+ 0 - 6
src/event_graph/mod.rs

@@ -89,7 +89,6 @@ pub struct EventGraph {
     /// inserted into the DAG
     pub event_sub: SubscriberPtr<Event>,
     days_rotation: u64,
-    genesis: Event,
 }
 
 impl EventGraph {
@@ -117,7 +116,6 @@ impl EventGraph {
             prune_task: OnceCell::new(),
             event_sub,
             days_rotation,
-            genesis: current_genesis.clone(),
         });
 
         // Check if we have it in our DAG.
@@ -156,10 +154,6 @@ impl EventGraph {
         self.days_rotation
     }
 
-    pub fn genesis(&self) -> Event {
-        self.genesis.clone()
-    }
-
     async fn _handle_stop(&self, sled_db: sled::Db) {
         info!(target: "event_graph::_handle_stop()", "[EVENTGRAPH] Prune task stopped, flushing sled");
         sled_db.flush_async().await.unwrap();

+ 28 - 4
src/event_graph/proto.rs

@@ -30,7 +30,7 @@ use darkfi_serial::{async_trait, deserialize_async, SerialDecodable, SerialEncod
 use log::{debug, error, trace, warn};
 use smol::Executor;
 
-use super::{Event, EventGraphPtr, NULL_ID};
+use super::{Event, EventGraph, EventGraphPtr, NULL_ID};
 use crate::{impl_p2p_message, net::*, system::timeout::timeout, Error, Result};
 
 /// Malicious behaviour threshold. If the threshold is reached, we will
@@ -142,12 +142,20 @@ impl ProtocolEventGraph {
                  target: "event_graph::protocol::handle_event_put()",
                  "Got EventPut: {} [{}]", event.id(), self.channel.address(),
             );
-            // Check if event is older than the previous rotation period
-            if event.timestamp < self.event_graph.genesis().timestamp {
+            // Check if the event is older than the genesis event. If so, we should
+            // not include it in our Dag.
+            // The genesis event marks the last time the Dag has been pruned of old
+            // events. The pruning interval is defined by the days_rotation field
+            // of [`EventGraph`].
+            // TODO it would be better to store/cache this instead of calculating
+            // on every broadcast/relay.
+            let genesis_timestamp =
+                EventGraph::generate_genesis(self.event_graph.days_rotation()).timestamp;
+            if event.timestamp < genesis_timestamp {
                 debug!(
                     target: "event_graph::protocol::handle_event_put()",
                     "Event {} is older than genesis. Event timestamp: `{}`. Genesis timestamp: `{}`",
-                event.id(), event.timestamp, self.event_graph.genesis().timestamp
+                event.id(), event.timestamp, genesis_timestamp
                 );
             }
 
@@ -362,6 +370,22 @@ impl ProtocolEventGraph {
             let event = self.event_graph.dag.get(event_id.as_bytes()).unwrap().unwrap();
             let event: Event = deserialize_async(&event).await.unwrap();
 
+            // Check if the event is older than the genesis event. If so, something
+            // has gone wrong. The event should have been pruned during the last
+            // rotation.
+            // TODO it would be better to store/cache this instead of calculating
+            // on every broadcast/relay.
+            let genesis_timestamp =
+                EventGraph::generate_genesis(self.event_graph.days_rotation()).timestamp;
+            if event.timestamp < genesis_timestamp {
+                error!(
+                    target: "event_graph::protocol::handle_event_req()",
+                    "Requested event {} is older than previous rotation period. It should have been pruned.
+                Event timestamp: `{}`. Genesis timestamp: `{}`",
+                event.id(), event.timestamp, genesis_timestamp
+                );
+            }
+
             // Now let's get the upper level of event IDs. When we reply, we could
             // get requests for those IDs as well.
             let mut bcast_ids = self.event_graph.broadcasted_ids.write().await;