From db4e9dd87010753c1202f43f67411d52dedfbf64 Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sun, 31 Jul 2022 05:09:59 +0400 Subject: [PATCH] use unified gen_id function --- bin/tau/taud/src/task_info.rs | 5 +++-- bin/tau/taud/src/util.rs | 5 ----- src/raft/consensus.rs | 6 +++--- src/raft/mod.rs | 8 -------- src/util/mod.rs | 6 ++++++ 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/bin/tau/taud/src/task_info.rs b/bin/tau/taud/src/task_info.rs index 54b554d69..709226c31 100644 --- a/bin/tau/taud/src/task_info.rs +++ b/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 { // generate ref_id - let ref_id = random_ref_id(); + let ref_id = gen_id(30); let created_at = Timestamp::current_time(); diff --git a/bin/tau/taud/src/util.rs b/bin/tau/taud/src/util.rs index 03983a439..b60d6ced3 100644 --- a/bin/tau/taud/src/util.rs +++ b/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 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) { diff --git a/src/raft/consensus.rs b/src/raft/consensus.rs index 0e6e5b079..01821f2fd 100644 --- a/src/raft/consensus.rs +++ b/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 Raft { 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 } diff --git a/src/raft/mod.rs b/src/raft/mod.rs index 6d17e6814..a12775045 100644 --- a/src/raft/mod.rs +++ b/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( map: Arc>>, @@ -51,9 +49,3 @@ async fn p2p_send_loop(receiver: async_channel::Receiver, p2p: net::P2pP } } } - -fn gen_id() -> NodeId { - let timestamp = Utc::now().timestamp(); - let hash: String = blake3::hash(×tamp.to_be_bytes()).to_hex().to_string(); - NodeId(hash) -} diff --git a/src/util/mod.rs b/src/util/mod.rs index dfd35d819..d49677d57 100644 --- a/src/util/mod.rs +++ b/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() +}