hacker-challenge-sgx-general/src/grpc/mod.rs
Valentyn Faychuk f256e5e44f
bounceback optimisation
Signed-off-by: Valentyn Faychuk <valy@detee.ltd>
2024-12-23 00:15:26 +02:00

64 lines
2.0 KiB
Rust

pub mod client;
pub mod server;
use crate::{datastore::NodeInfo, NodeUpdate};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub mod challenge {
tonic::include_proto!("challenge");
}
#[derive(Clone, PartialEq)]
pub struct InternalNodeUpdate {
pub sender_ip: String,
pub update: NodeUpdate,
}
impl From<(String, NodeInfo)> for NodeUpdate {
fn from((ip, info): (String, NodeInfo)) -> Self {
NodeUpdate {
ip: ip.to_string(),
started_at: Some(prost_types::Timestamp::from(info.started_at)),
keepalive: Some(prost_types::Timestamp::from(info.keepalive)),
mint_requests: info.mint_requests,
mints: info.mints,
mratls_conns: info.mratls_conns,
quote_attacks: info.net_attacks,
public: info.public,
restarts: info.restarts,
disk_attacks: info.disk_attacks,
}
}
}
impl From<NodeUpdate> for (String, NodeInfo) {
fn from(val: NodeUpdate) -> Self {
let ip = val.ip;
let started_at: SystemTime = match val.started_at {
Some(ts) => {
let duration = Duration::new(ts.seconds as u64, ts.nanos as u32);
UNIX_EPOCH.checked_add(duration).unwrap_or(SystemTime::now())
}
None => SystemTime::now(),
};
let keepalive: SystemTime = match val.keepalive {
Some(ts) => {
let duration = Duration::new(ts.seconds as u64, ts.nanos as u32);
UNIX_EPOCH.checked_add(duration).unwrap_or(SystemTime::now())
}
None => SystemTime::now(),
};
let self_info = NodeInfo {
started_at,
keepalive,
mint_requests: val.mint_requests,
mints: val.mints,
mratls_conns: val.mratls_conns,
net_attacks: val.quote_attacks,
public: val.public,
restarts: val.restarts,
disk_attacks: val.disk_attacks,
};
(ip, self_info)
}
}