Skip to content

Commit

Permalink
Merge branch 'main' into k8s_deploy_script
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Feb 19, 2024
2 parents fb0c345 + 97d1399 commit 3b1256f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
CODEOWNERS @brunoffranca

/node/actors/consensus/ @brunoffranca @moshababo
/node/actors/executor/ @brunoffranca @pompon0 @slowli
/node/actors/network/ @pompon0
/node/actors/sync_blocks/ @slowli

/node/libs/concurrency/ @pompon0
/node/libs/crypto/ @brunoffranca
/node/libs/protobuf/ @pompon0
/node/libs/protobuf_build/ @pompon0
/node/libs/storage/ @slowli
/node/libs/protobuf_build/ @pompon0
2 changes: 2 additions & 0 deletions node/libs/protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
pub mod build;
pub mod proto;
mod proto_fmt;
pub mod repr;
pub use repr::{read_required_repr, ProtoRepr};
pub mod serde;
mod std_conv;
pub mod testonly;
Expand Down
33 changes: 33 additions & 0 deletions node/libs/protobuf/src/repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Trait for defining proto conversion for external types.

use crate::build::prost_reflect::ReflectMessage;
use anyhow::Context as _;

/// Trait reverse to `zksync_protobuf::ProtoFmt` for cases where
/// you would like to specify a custom proto encoding for an externally defined type.
pub trait ProtoRepr: ReflectMessage + Default {
/// The externally defined type associated with the proto Self.
type Type;
/// Converts proto Self to `Type`.
fn read(&self) -> anyhow::Result<Self::Type>;
/// Converts `Type` to proto Self.
fn build(this: &Self::Type) -> Self;
}

/// Parses a required proto field.
pub fn read_required_repr<P: ProtoRepr>(field: &Option<P>) -> anyhow::Result<P::Type> {
field.as_ref().context("missing field")?.read()
}

/// Encodes a proto message.
/// Currently it outputs a canonical encoding, but `decode` accepts
/// non-canonical encoding as well.
pub fn encode<P: ProtoRepr>(msg: &P::Type) -> Vec<u8> {
let msg = P::build(msg);
super::canonical_raw(&msg.encode_to_vec(), &msg.descriptor()).unwrap()
}

/// Decodes a proto message.
pub fn decode<P: ProtoRepr>(bytes: &[u8]) -> anyhow::Result<P::Type> {
P::read(&P::decode(bytes)?)
}
27 changes: 27 additions & 0 deletions node/libs/protobuf/src/testonly/gen_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Util types for generating random values in tests.

use rand::Rng;

/// Generator of random values.
pub struct GenValue<'a, R: Rng> {
/// Underlying RNG.
pub rng: &'a mut R,
/// Generate values 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<'a, R: Rng> GenValue<'a, R> {
/// Generates a random value of type `C`.
pub fn gen<C: RandomValue>(&mut self) -> C {
C::sample(self)
}
}

/// Types that can be used to generate a random instance.
pub trait RandomValue {
/// Generates a random value.
fn sample(g: &mut GenValue<impl Rng>) -> Self;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Testonly utilities.

pub mod gen_value;
use super::{canonical, canonical_raw, decode, encode, read_fields, ProtoFmt, Wire};
pub use gen_value::*;
use prost::Message as _;
use prost_reflect::ReflectMessage as _;
use rand::{
Expand Down

0 comments on commit 3b1256f

Please sign in to comment.