use crate::{Endpoint, IncomingConnections};
use bytes::Bytes;
use color_eyre::eyre::Result;
use rand::{distributions::Standard, rngs, Rng, SeedableRng};
use std::{
net::{Ipv4Addr, SocketAddr},
time::Duration,
};
use tiny_keccak::{Hasher, Sha3};
type Digest256 = [u8; 32];
mod common;
#[ctor::ctor]
fn setup() {
color_eyre::install().expect("color_eyre::install() should only be called once");
}
pub(crate) async fn new_endpoint_with_keepalive() -> Result<(Endpoint, IncomingConnections)> {
let ep = Endpoint::builder()
.addr(local_addr())
.keep_alive_interval(Duration::from_secs(5))
.server()?;
Ok(ep)
}
pub(crate) async fn new_endpoint() -> Result<(Endpoint, IncomingConnections)> {
let ep = Endpoint::builder().addr(local_addr()).server()?;
Ok(ep)
}
pub(crate) fn local_addr() -> SocketAddr {
(Ipv4Addr::LOCALHOST, 0).into()
}
pub(crate) fn random_msg(size: usize) -> Bytes {
let random_bytes: Vec<u8> = rngs::SmallRng::from_entropy()
.sample_iter(Standard)
.take(size)
.collect();
Bytes::from(random_bytes)
}
pub(crate) fn hash(bytes: &Bytes) -> Digest256 {
let mut hasher = Sha3::v256();
let mut hash = Digest256::default();
hasher.update(bytes);
hasher.finalize(&mut hash);
hash
}