diff --git a/node/libs/protobuf/examples/conformance_test/main.rs b/node/libs/protobuf/examples/conformance_test/main.rs index 947e30d7..5d36c8d8 100644 --- a/node/libs/protobuf/examples/conformance_test/main.rs +++ b/node/libs/protobuf/examples/conformance_test/main.rs @@ -18,7 +18,10 @@ mod proto; /// Decodes a generated proto message from json for arbitrary ReflectMessage. fn decode_json_proto(json: &str) -> anyhow::Result { let mut deserializer = serde_json::Deserializer::from_str(json); - let proto: T = zksync_protobuf::serde::Deserialize{deny_unknown_fields:true}.proto(&mut deserializer)?; + let proto: T = zksync_protobuf::serde::Deserialize { + deny_unknown_fields: true, + } + .proto(&mut deserializer)?; deserializer.end()?; Ok(proto) } @@ -26,7 +29,9 @@ fn decode_json_proto(json: &str) -> anyhow::Result< /// Encodes a generated proto message to json for arbitrary ReflectMessage. fn encode_json_proto(proto: &T) -> String { let mut serializer = serde_json::Serializer::pretty(vec![]); - zksync_protobuf::serde::Serialize{}.proto(proto, &mut serializer).unwrap(); + zksync_protobuf::serde::Serialize + .proto(proto, &mut serializer) + .unwrap(); String::from_utf8(serializer.into_inner()).unwrap() } diff --git a/node/libs/protobuf/src/serde.rs b/node/libs/protobuf/src/serde.rs index 5df680b1..413b7932 100644 --- a/node/libs/protobuf/src/serde.rs +++ b/node/libs/protobuf/src/serde.rs @@ -3,30 +3,12 @@ //! therefore it is suitable for version control. //! WARNING: Currently this serde implementation uses reflection, //! so it is not very efficient. -use crate::{ProtoFmt,ProtoRepr}; +use crate::{ProtoFmt, ProtoRepr}; use prost::Message as _; use prost_reflect::ReflectMessage; -/* -/// ProtoFmt wrapper which implements serde Serialize/Deserialize. -#[derive(Debug, Clone)] -pub struct Serde(pub T); - -impl serde::Serialize for Serde { - fn serialize(&self, s: S) -> Result { - serialize(&self.0, s) - } -} - -impl<'de, T: ProtoFmt, const X :Deserialize> serde::Deserialize<'de> for Serde { - fn deserialize>(d: D) -> Result { - Ok(Self(X.deserialize(d)?)) - } -} -*/ - /// Serialization options. -pub struct Serialize {} +pub struct Serialize; impl Serialize { /// Serializes ReflectMessage. @@ -40,7 +22,11 @@ impl Serialize { } /// Serializes ProtoFmt. - pub fn proto_fmt(&self, x: &T, s: S) -> Result { + pub fn proto_fmt( + &self, + x: &T, + s: S, + ) -> Result { self.proto(&x.build(), s) } @@ -59,21 +45,25 @@ impl Serialize { } /// Serializes ProtoRepr. - pub fn proto_repr(&self, x: &T::Type, s: S) -> Result { + pub fn proto_repr( + &self, + x: &T::Type, + s: S, + ) -> Result { self.proto(&T::build(x), s) } /// Serializes ProtoRepr to json. pub fn proto_repr_to_json(&self, x: &T::Type) -> String { let mut s = serde_json::Serializer::pretty(vec![]); - self.proto_repr::(x, &mut s).unwrap(); + self.proto_repr::(x, &mut s).unwrap(); String::from_utf8(s.into_inner()).unwrap() } /// Serializes ProtoRepr to yaml pub fn proto_repr_to_yaml(&self, x: &T::Type) -> String { let mut s = serde_yaml::Serializer::new(vec![]); - self.proto_repr::(x, &mut s).unwrap(); + self.proto_repr::(x, &mut s).unwrap(); String::from_utf8(s.into_inner().unwrap()).unwrap() } } @@ -83,21 +73,29 @@ impl Serialize { pub struct Deserialize { /// true => returns an error when an unknown field is found. /// false => silently ignores unknown fields. - pub deny_unknown_fields: bool + pub deny_unknown_fields: bool, } impl Deserialize { /// Implementation of serde::Deserialize for arbitrary ReflectMessage with deny_unknown_fields option - pub fn proto<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>(&self, d: D) -> Result { + pub fn proto<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>( + &self, + d: D, + ) -> Result { let mut p = T::default(); - let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(self.deny_unknown_fields); - let msg = prost_reflect::DynamicMessage::deserialize_with_options(p.descriptor(), d, &options)?; + let options = + prost_reflect::DeserializeOptions::new().deny_unknown_fields(self.deny_unknown_fields); + let msg = + prost_reflect::DynamicMessage::deserialize_with_options(p.descriptor(), d, &options)?; p.merge(msg.encode_to_vec().as_slice()).unwrap(); Ok(p) } /// Implementation of serde::Deserialize for arbitrary ProtoFmt. - pub fn proto_fmt<'de, T: ProtoFmt, D: serde::Deserializer<'de>>(&self, d: D) -> Result { + pub fn proto_fmt<'de, T: ProtoFmt, D: serde::Deserializer<'de>>( + &self, + d: D, + ) -> Result { T::read(&self.proto(d)?).map_err(serde::de::Error::custom) } @@ -115,20 +113,25 @@ impl Deserialize { } /// Implementation of serde::Deserialize for arbitrary ProtoFmt. - pub fn proto_repr<'de, T: ProtoRepr, D: serde::Deserializer<'de>>(&self, d: D) -> Result { - self.proto::(d)?.read().map_err(serde::de::Error::custom) + pub fn proto_repr<'de, T: ProtoRepr, D: serde::Deserializer<'de>>( + &self, + d: D, + ) -> Result { + self.proto::(d)? + .read() + .map_err(serde::de::Error::custom) } /// Deserializes ProtoRepr from json. pub fn proto_repr_from_json(&self, json: &str) -> anyhow::Result { let mut d = serde_json::Deserializer::from_str(json); - let p = self.proto_repr::(&mut d)?; + let p = self.proto_repr::(&mut d)?; d.end()?; Ok(p) } /// Deserializes ProtoRepr from yaml. pub fn proto_repr_from_yaml(&self, yaml: &str) -> anyhow::Result { - Ok(self.proto_repr::(serde_yaml::Deserializer::from_str(yaml))?) + Ok(self.proto_repr::(serde_yaml::Deserializer::from_str(yaml))?) } } diff --git a/node/libs/protobuf/src/testonly/mod.rs b/node/libs/protobuf/src/testonly/mod.rs index afdebdca..2896acbf 100644 --- a/node/libs/protobuf/src/testonly/mod.rs +++ b/node/libs/protobuf/src/testonly/mod.rs @@ -1,5 +1,9 @@ //! Testonly utilities. -use super::{serde::{Serialize,Deserialize}, canonical, canonical_raw, decode, encode, read_fields, ProtoFmt, ProtoRepr, Wire}; +use super::{ + canonical, canonical_raw, decode, encode, read_fields, + serde::{Deserialize, Serialize}, + ProtoFmt, ProtoRepr, Wire, +}; use prost::Message as _; use prost_reflect::ReflectMessage; use rand::{ @@ -114,24 +118,34 @@ fn decode_proto(bytes: &[u8]) -> anyhow::Result { fn encode_json(msg: &X::Type) -> String { let mut s = serde_json::Serializer::pretty(vec![]); - Serialize{}.proto(&X::build(msg), &mut s).unwrap(); + Serialize.proto(&X::build(msg), &mut s).unwrap(); String::from_utf8(s.into_inner()).unwrap() } fn decode_json(json: &str) -> anyhow::Result { let mut d = serde_json::Deserializer::from_str(json); - X::read(&Deserialize{deny_unknown_fields:true}.proto(&mut d)?) + X::read( + &Deserialize { + deny_unknown_fields: true, + } + .proto(&mut d)?, + ) } fn encode_yaml(msg: &X::Type) -> String { let mut s = serde_yaml::Serializer::new(vec![]); - Serialize{}.proto(&X::build(msg), &mut s).unwrap(); + Serialize.proto(&X::build(msg), &mut s).unwrap(); String::from_utf8(s.into_inner().unwrap()).unwrap() } fn decode_yaml(yaml: &str) -> anyhow::Result { let d = serde_yaml::Deserializer::from_str(yaml); - X::read(&Deserialize{deny_unknown_fields:true}.proto(d)?) + X::read( + &Deserialize { + deny_unknown_fields: true, + } + .proto(d)?, + ) } /// Wrapper for `ProtoRepr`, implementing ProtoConv; diff --git a/node/tools/src/bin/localnet_config.rs b/node/tools/src/bin/localnet_config.rs index 39b14256..90e603c8 100644 --- a/node/tools/src/bin/localnet_config.rs +++ b/node/tools/src/bin/localnet_config.rs @@ -11,7 +11,7 @@ use std::{ }; use zksync_consensus_roles::{node, validator}; use zksync_consensus_tools::config; -use zksync_protobuf::serde::{Serialize}; +use zksync_protobuf::serde::Serialize; /// Command line arguments. #[derive(Debug, Parser)] @@ -111,7 +111,7 @@ fn main() -> anyhow::Result<()> { .context("fs::set_permissions()")?; let config_path = root.join("config.json"); - fs::write(&config_path, Serialize{}.proto_fmt_to_json(&cfg)).context("fs::write()")?; + fs::write(&config_path, Serialize.proto_fmt_to_json(&cfg)).context("fs::write()")?; fs::set_permissions(&config_path, Permissions::from_mode(0o600)) .context("fs::set_permissions()")?; } diff --git a/node/tools/src/config.rs b/node/tools/src/config.rs index cff8a7cb..be36dc56 100644 --- a/node/tools/src/config.rs +++ b/node/tools/src/config.rs @@ -1,7 +1,6 @@ //! Node configuration. use crate::{proto, store}; use anyhow::{anyhow, Context as _}; -use serde_json::ser::Formatter; use std::{ collections::{HashMap, HashSet}, fs, io, @@ -42,29 +41,6 @@ fn read_optional_secret_text(text: &Option) -> anyhow::Resul /// Ports for the nodes to listen on kubernetes pod. pub const NODES_PORT: u16 = 3054; -/// Decodes serde type from json. -pub fn decode_json(json: &str) -> anyhow::Result { - let mut d = serde_json::Deserializer::from_str(json); - let p = T::deserialize(&mut d)?; - d.end()?; - Ok(p) -} - -/// Encodes a generated proto message to json for arbitrary ProtoFmt. -pub fn encode_json(x: &T) -> String { - let s = serde_json::Serializer::pretty(vec![]); - encode_with_serializer(x, s) -} - -/// Encodes a generated proto message for arbitrary ProtoFmt with provided serializer. -pub(crate) fn encode_with_serializer( - x: &T, - mut serializer: serde_json::Serializer, F>, -) -> String { - T::serialize(x, &mut serializer).unwrap(); - String::from_utf8(serializer.into_inner()).unwrap() -} - /// Pair of (public key, host addr) for a gossip network node. #[derive(Debug, Clone)] pub struct NodeAddr { diff --git a/node/tools/src/k8s.rs b/node/tools/src/k8s.rs index 002a41b0..29ab0961 100644 --- a/node/tools/src/k8s.rs +++ b/node/tools/src/k8s.rs @@ -18,7 +18,7 @@ use kube::{ use std::{collections::BTreeMap, net::SocketAddr, time::Duration}; use tokio::time; use tracing::log::info; -use zksync_protobuf::serde::{Serialize}; +use zksync_protobuf::serde::Serialize; /// Docker image name for consensus nodes. const DOCKER_IMAGE_NAME: &str = "consensus-node"; @@ -335,7 +335,7 @@ fn is_pod_running(pod: &Pod) -> bool { fn get_cli_args(consensus_node: &ConsensusNode) -> Vec { vec![ "--config".to_string(), - Serialize{}.proto_fmt_to_json(&consensus_node.config), + Serialize.proto_fmt_to_json(&consensus_node.config), ] } diff --git a/node/tools/src/main.rs b/node/tools/src/main.rs index e7c7c3de..bf79a9bd 100644 --- a/node/tools/src/main.rs +++ b/node/tools/src/main.rs @@ -32,7 +32,10 @@ impl Cli { None => fs::read_to_string(&self.config_path)?, }; Ok(config::Configs { - app: Deserialize{deny_unknown_fields:true}.proto_fmt_from_json(&json)?, + app: Deserialize { + deny_unknown_fields: true, + } + .proto_fmt_from_json(&json)?, database: self.database.clone(), }) }