소스 검색

use unified gen_id function

ghassmo 4 년 전
부모
커밋
db4e9dd870
5개의 변경된 파일12개의 추가작업 그리고 18개의 파일을 삭제
  1. 3 2
      bin/tau/taud/src/task_info.rs
  2. 0 5
      bin/tau/taud/src/util.rs
  3. 3 3
      src/raft/consensus.rs
  4. 0 8
      src/raft/mod.rs
  5. 6 0
      src/util/mod.rs

+ 3 - 2
bin/tau/taud/src/task_info.rs

@@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
 
 use darkfi::util::{
     file::{load_json_file, save_json_file},
+    gen_id,
     serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Timestamp,
 };
@@ -15,7 +16,7 @@ use darkfi::util::{
 use crate::{
     error::{TaudError, TaudResult},
     month_tasks::MonthTasks,
-    util::{find_free_id, random_ref_id},
+    util::find_free_id,
 };
 
 #[derive(Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq, Eq)]
@@ -84,7 +85,7 @@ impl TaskInfo {
         dataset_path: &Path,
     ) -> TaudResult<Self> {
         // generate ref_id
-        let ref_id = random_ref_id();
+        let ref_id = gen_id(30);
 
         let created_at = Timestamp::current_time();
 

+ 0 - 5
bin/tau/taud/src/util.rs

@@ -2,7 +2,6 @@ use std::path::PathBuf;
 
 use fxhash::FxHashMap;
 use log::info;
-use rand::{distributions::Alphanumeric, thread_rng, Rng};
 
 use darkfi::Result;
 
@@ -48,10 +47,6 @@ pub fn parse_workspaces(config_file: &PathBuf) -> Result<FxHashMap<String, Works
     Ok(ret)
 }
 
-pub fn random_ref_id() -> String {
-    thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect()
-}
-
 pub fn find_free_id(task_ids: &[u32]) -> u32 {
     for i in 1.. {
         if !task_ids.contains(&i) {

+ 3 - 3
src/raft/consensus.rs

@@ -14,14 +14,14 @@ use rand::{rngs::OsRng, Rng, RngCore};
 use crate::{
     net,
     util::{
-        self,
+        self, gen_id,
         serial::{deserialize, serialize, Decodable, Encodable},
     },
     Error, Result,
 };
 
 use super::{
-    gen_id, p2p_send_loop,
+    p2p_send_loop,
     primitives::{
         BroadcastMsgRequest, Channel, Log, LogRequest, LogResponse, Logs, MapLength, NetMsg,
         NetMsgMethod, NodeId, NodeIdMsg, Role, Sender, VoteRequest, VoteResponse,
@@ -85,7 +85,7 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
         let id = match datastore.id.get_last()? {
             Some(_id) => _id,
             None => {
-                let id = gen_id();
+                let id = NodeId(gen_id(30));
                 datastore.id.insert(&id)?;
                 id
             }

+ 0 - 8
src/raft/mod.rs

@@ -20,8 +20,6 @@ pub use primitives::NetMsg;
 pub use protocol_raft::ProtocolRaft;
 pub use settings::RaftSettings;
 
-use primitives::NodeId;
-
 // Auxilary function to periodically prun items, based on when they were received.
 async fn prune_map<T: Clone + Eq + std::hash::Hash>(
     map: Arc<Mutex<fxhash::FxHashMap<T, i64>>>,
@@ -51,9 +49,3 @@ async fn p2p_send_loop(receiver: async_channel::Receiver<NetMsg>, p2p: net::P2pP
         }
     }
 }
-
-fn gen_id() -> NodeId {
-    let timestamp = Utc::now().timestamp();
-    let hash: String = blake3::hash(&timestamp.to_be_bytes()).to_hex().to_string();
-    NodeId(hash)
-}

+ 6 - 0
src/util/mod.rs

@@ -19,3 +19,9 @@ pub use net_name::NetworkName;
 pub use parse::{decode_base10, encode_base10};
 pub use path::{expand_path, join_config_path, load_keypair_to_str};
 pub use time::{check_clock, unix_timestamp, NanoTimestamp, Timestamp};
+
+use rand::{distributions::Alphanumeric, thread_rng, Rng};
+
+pub fn gen_id(len: usize) -> String {
+    thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
+}