use unified gen_id function

This commit is contained in:
ghassmo
2022-07-31 05:09:59 +04:00
parent f73217d966
commit db4e9dd870
5 changed files with 12 additions and 18 deletions

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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()
}