Răsfoiți Sursa

event_graph: Test static genesis and minor cleanup

x 1 lună în urmă
părinte
comite
c44d25ea15

+ 3 - 3
bin/darkirc/src/crypto/rln.rs

@@ -130,9 +130,9 @@ impl RlnIdentity {
         Some(m)
     }
 
-    /// Build a [`RegistrationBlob`] suitable for broadcast as a
-    /// `StaticPut`. The proving key comes from the EventGraph's
-    /// shared `ZkKeys` cache.
+    // /// Build a [`RegistrationBlob`] suitable for broadcast as a
+    // /// `StaticPut`. The proving key comes from the EventGraph's
+    // /// shared `ZkKeys` cache.
     // pub fn create_registration(&self, eg: &EventGraphPtr) -> Result<RegistrationBlob> {
     //     let zkbin = ZkBinary::decode(RLN2_REGISTER_ZKBIN, false)?;
 

+ 4 - 4
bin/darkirc/src/irc/services/nickserv.rs

@@ -532,11 +532,11 @@ impl NickServ {
                     format!("Use `SET {account_name}` to make this the active identity."),
                 ));
             }
-            return Ok(replies)
+            Ok(replies)
         } else {
             let replies =
                 vec![notice(nick, format!("Failed to register account \"{account_name}\""))];
-            return Ok(replies)
+            Ok(replies)
         }
         // Apply the registration through the canonical pipeline:
         //
@@ -792,7 +792,7 @@ impl NickServ {
         // any non-empty arg, so a fat-fingered "SLASH alice yes"
         // doesn't go through.
         match tokens.next() {
-            Some(t) if t == "CONFIRM" => {}
+            Some("CONFIRM") => {}
             _ => {
                 return Ok(notices(
                     nick,
@@ -873,7 +873,7 @@ impl NickServ {
         let slash_pk = evgr.zk_keys.load_slash_pk()?;
         let (proof, root) = {
             let mut id_state = evgr.identity_state.write().await;
-            create_slash_proof(identity_secret_hash, &mut *id_state, &slash_pk)?
+            create_slash_proof(identity_secret_hash, &mut id_state, &slash_pk)?
         };
 
         let slash_blob = SlashBlob { proof, identity_secret_hash, merkle_root: root };

+ 10 - 6
src/event_graph/mod.rs

@@ -1672,7 +1672,7 @@ impl EventGraph {
 
     async fn static_new(sled_db: &sled::Db, config: &EventGraphConfig) -> Result<sled::Tree> {
         let tree = sled_db.open_tree("static-dag")?;
-        let genesis = generate_genesis(&EventGraphConfig { hours_rotation: 0, ..config.clone() });
+        let genesis = generate_static_genesis(config);
         let mut ov = SledTreeOverlay::new(&tree);
         ov.insert(genesis.id().as_bytes(), &serialize_async(&genesis).await).unwrap();
 
@@ -2291,7 +2291,7 @@ impl EventGraph {
 
                 // Accepting only genesis registeration, reject every
                 // other account.
-                return StaticEventCheck::Rejected;
+                StaticEventCheck::Rejected
                 /*
                 #[allow(unreachable_code)]
                 let reg: RegistrationBlob = match deserialize_async_partial(blob).await {
@@ -2374,14 +2374,18 @@ impl EventGraph {
         }
     }
 
-    /// Insert proof-less genesis registration events commitments into
-    /// the static DAG, called once at startup after the genesis event
-    /// itself is inserted. Idempotent - skips any commitment already
-    /// present in the identity tree.
+    /// Insert proof-less pregenerated identity commitments into the
+    /// static DAG, called once at startup after the static genesis
+    /// event itself is inserted. Idempotent - skips any commitment
+    /// already present in the identity tree.
     pub async fn bootstrap_genesis_identities(&self) -> Result<()> {
+        // Deterministic for premade identities.
         let genesis_event =
             generate_genesis(&EventGraphConfig { hours_rotation: 0, ..self.config.clone() });
         let genesis_id = genesis_event.id();
+        if !self.static_dag.contains_key(genesis_id.as_bytes())? {
+            return Err(Error::Custom("static DAG genesis missing during bootstrap".into()))
+        }
 
         let genesis_commitments = genesis_commitments();
         for commitment in genesis_commitments.iter() {

+ 8 - 11
src/event_graph/test_helpers.rs

@@ -109,20 +109,17 @@ fn shared_zk_keys() -> Arc<crate::event_graph::rln::ZkKeys> {
 }
 
 pub async fn make_eg() -> EventGraphPtr {
+    make_eg_with_config(test_config()).await
+}
+
+/// Construct an [`EventGraph`] with a caller-provided test config.
+pub async fn make_eg_with_config(config: EventGraphConfig) -> EventGraphPtr {
     let ex = Arc::new(Executor::new());
     let p2p = P2p::new(Settings::default(), ex.clone()).await.unwrap();
     let sled_db = sled::Config::new().temporary(true).open().unwrap();
-    EventGraph::with_zk_keys(
-        p2p,
-        sled_db,
-        "/tmp".into(),
-        false,
-        test_config(),
-        shared_zk_keys(),
-        ex,
-    )
-    .await
-    .unwrap()
+    EventGraph::with_zk_keys(p2p, sled_db, "/tmp".into(), false, config, shared_zk_keys(), ex)
+        .await
+        .unwrap()
 }
 
 /// Number of nodes a `make_network` call brings up.

+ 43 - 3
src/event_graph/tests_rln.rs

@@ -19,7 +19,7 @@
 use std::{sync::Arc, time::UNIX_EPOCH};
 
 use darkfi_sdk::{crypto::poseidon_hash, pasta::pallas};
-use darkfi_serial::serialize_async;
+use darkfi_serial::{deserialize_async, serialize_async};
 use sled_overlay::sled;
 use smol::Executor;
 
@@ -31,9 +31,11 @@ use crate::{
             MAX_MSG_LIMIT, RLN_EPOCH_LEN, RLN_GENESIS,
         },
         test_helpers::{
-            make_eg, make_network, run_multi_node_test, shutdown_network, TestIdentity,
+            make_eg, make_eg_with_config, make_network, run_multi_node_test, shutdown_network,
+            TestIdentity,
         },
-        Event, EventGraphPtr, NULL_PARENTS,
+        util::generate_genesis,
+        Event, EventGraphConfig, EventGraphPtr, NULL_ID, NULL_PARENTS,
     },
     system::sleep,
     zk::Proof,
@@ -349,6 +351,44 @@ fn synthesize_placeholder_proof() -> Proof {
     Proof::new(vec![])
 }
 
+#[test]
+fn rln_bootstrapped_identities_parent_static_genesis() {
+    smol::block_on(async {
+        let config = EventGraphConfig {
+            hours_rotation: 1,
+            ..crate::event_graph::test_helpers::test_config()
+        };
+        let eg = make_eg_with_config(config).await;
+        let static_genesis =
+            generate_genesis(&EventGraphConfig { hours_rotation: 0, ..eg.config.clone() });
+        let static_genesis_id = static_genesis.id();
+        let rotating_genesis_id = eg.current_genesis.read().await.id();
+
+        assert_ne!(
+            static_genesis_id, rotating_genesis_id,
+            "rotating test config must expose the old static-parent bug",
+        );
+        assert!(eg.static_dag.contains_key(static_genesis_id.as_bytes()).unwrap());
+
+        let mut bootstrapped = 0usize;
+        for item in eg.static_dag.iter() {
+            let (_, bytes) = item.unwrap();
+            let ev: Event = deserialize_async(&bytes).await.unwrap();
+            if ev.header.parents == NULL_PARENTS {
+                continue
+            }
+
+            bootstrapped += 1;
+            assert_eq!(ev.header.layer, 1);
+            assert_eq!(ev.header.parents[0], static_genesis_id);
+            assert!(ev.header.parents[1..].iter().all(|p| *p == NULL_ID));
+            assert!(eg.static_dag.contains_key(ev.header.parents[0].as_bytes()).unwrap());
+        }
+
+        assert!(bootstrapped > 0, "expected pregenerated identities to be bootstrapped");
+    })
+}
+
 async fn make_static_event(content: &[u8], eg: &EventGraphPtr) -> Event {
     use crate::event_graph::event::Header;
     let timestamp = eg.current_genesis.read().await.header.timestamp;