Skip to content

Commit

Permalink
retrieved encode.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Mar 27, 2024
1 parent c59f57f commit 0d0a762
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions node/libs/utils/src/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Utilites for testing encodings.

Check warning on line 1 in node/libs/utils/src/encode.rs

View workflow job for this annotation

GitHub Actions / typos

"Utilites" should be "Utilities".
use rand::{
distributions::{Alphanumeric, DistString, Distribution},
Rng,
};
use zksync_concurrency::net;

/// Distribution for testing encodings.
pub struct EncodeDist {
/// Generate configs with only required fields.
pub required_only: bool,
/// Generate decimal fractions for f64
/// to avoid rounding errors of decimal encodings.
pub decimal_fractions: bool,
}

impl EncodeDist {
/// Returns a small non-empty range if `required_only` is false.
/// Returns an empty range otherwise.
pub fn sample_range(&self, rng: &mut (impl Rng + ?Sized)) -> std::ops::Range<usize> {
if self.required_only {
0..0
} else {
0..rng.gen_range(5..10)
}
}
}

impl Distribution<std::net::SocketAddr> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> std::net::SocketAddr {
std::net::SocketAddr::new(std::net::IpAddr::from(rng.gen::<[u8; 16]>()), rng.gen())
}
}

impl Distribution<net::Host> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> net::Host {
Distribution::<std::net::SocketAddr>::sample(self, rng).into()
}
}

impl Distribution<String> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> String {
let n = self.sample_range(rng).len();
Alphanumeric.sample_string(rng, n)
}
}

impl<T> Distribution<Option<T>> for EncodeDist
where
EncodeDist: Distribution<T>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<T> {
self.sample_range(rng).map(|_| self.sample(rng)).next()
}
}

impl Distribution<f64> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
if self.decimal_fractions {
const PRECISION: usize = 1000000;
#[allow(clippy::float_arithmetic)]
return rng.gen_range(0..PRECISION) as f64 / PRECISION as f64;
}
rng.gen()
}
}
2 changes: 1 addition & 1 deletion node/libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Crate that holds several small utilities and primitives.
mod encode;
pub mod enum_util;
pub mod pipe;
mod encode;

pub use encode::*;

0 comments on commit 0d0a762

Please sign in to comment.