mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-04-23 16:38:04 +00:00
wrap assigning local/global IDs into struct to avoid things getting out of sync
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use std::{collections::HashMap, hash::Hash};
|
||||
use serde::{Serialize,Deserialize};
|
||||
|
||||
#[derive(Clone,Copy,Debug,Hash,PartialEq,Eq,Serialize,Deserialize)]
|
||||
pub struct Id(usize);
|
||||
|
||||
impl std::convert::From<Id> for usize {
|
||||
fn from(id: Id) -> usize {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
/// A struct that allows you to assign ID to an arbitrary set of
|
||||
/// details (so long as they are Eq+Hash+Clone), and then access
|
||||
/// the assigned ID given those details or access the details given
|
||||
/// the ID.
|
||||
#[derive(Debug)]
|
||||
pub struct AssignId<Details> {
|
||||
current_id: Id,
|
||||
from_details: HashMap<Details, Id>,
|
||||
from_id: HashMap<Id, Details>
|
||||
}
|
||||
|
||||
impl <Details> AssignId<Details> where Details: Eq + Hash + Clone {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current_id: Id(0),
|
||||
from_details: HashMap::new(),
|
||||
from_id: HashMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assign_id(&mut self, details: Details) -> Id {
|
||||
let this_id = self.current_id;
|
||||
self.current_id.0 += 1;
|
||||
|
||||
self.from_details.insert(details.clone(), this_id);
|
||||
self.from_id.insert(this_id, details);
|
||||
|
||||
this_id
|
||||
}
|
||||
|
||||
pub fn get_details(&mut self, id: Id) -> Option<&Details> {
|
||||
self.from_id.get(&id)
|
||||
}
|
||||
|
||||
pub fn get_id(&mut self, details: &Details) -> Option<Id> {
|
||||
self.from_details.get(details).map(|id| *id)
|
||||
}
|
||||
|
||||
pub fn remove_by_id(&mut self, id: Id) -> Option<Details> {
|
||||
if let Some(details) = self.from_id.remove(&id) {
|
||||
self.from_details.remove(&details);
|
||||
Some(details)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_by_details(&mut self, details: &Details) -> Option<Id> {
|
||||
if let Some(id) = self.from_details.remove(&details) {
|
||||
self.from_id.remove(&id);
|
||||
Some(id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
*self = AssignId::new()
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,15 @@ use std::net::IpAddr;
|
||||
|
||||
use crate::node::Payload;
|
||||
use crate::types::{NodeDetails};
|
||||
use crate::assign_id::Id;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The shard-local ID of a given node, where a single connection
|
||||
/// might send data on behalf of more than one chain.
|
||||
pub type LocalId = u64;
|
||||
pub type LocalId = Id;
|
||||
|
||||
/// A global ID assigned to messages from each different pair of ConnId+LocalId.
|
||||
pub type GlobalId = u64;
|
||||
pub type GlobalId = Id;
|
||||
|
||||
/// Message sent from the shard to the backend core
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
|
||||
@@ -3,4 +3,5 @@ pub mod internal_messages;
|
||||
pub mod types;
|
||||
pub mod util;
|
||||
pub mod json;
|
||||
pub mod log_level;
|
||||
pub mod log_level;
|
||||
pub mod assign_id;
|
||||
Reference in New Issue
Block a user