Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(protobuf): relocate types from zksync_protobuf_config (BFT-407) #66

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
moshababo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading