From 62133e15c15bc02b7247be7c9b59a74f6f5c330b Mon Sep 17 00:00:00 2001 From: Thomas Knauth Date: Wed, 15 May 2024 11:10:54 +0000 Subject: [PATCH] PR #1928 --- core/lib/basic_types/src/web3/mod.rs | 30 ++++++++++++++++--- core/lib/basic_types/src/web3/tests.rs | 24 +++++++++++++++ .../src/tee_verifier_input_producer/mod.rs | 2 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/core/lib/basic_types/src/web3/mod.rs b/core/lib/basic_types/src/web3/mod.rs index be785040d2d..6291c22d567 100644 --- a/core/lib/basic_types/src/web3/mod.rs +++ b/core/lib/basic_types/src/web3/mod.rs @@ -61,9 +61,13 @@ impl Serialize for Bytes { where S: Serializer, { - let mut serialized = "0x".to_owned(); - serialized.push_str(&hex::encode(&self.0)); - serializer.serialize_str(serialized.as_ref()) + if serializer.is_human_readable() { + let mut serialized = "0x".to_owned(); + serialized.push_str(&hex::encode(&self.0)); + serializer.serialize_str(serialized.as_ref()) + } else { + self.0.serialize(serializer) + } } } @@ -72,7 +76,11 @@ impl<'a> Deserialize<'a> for Bytes { where D: Deserializer<'a>, { - deserializer.deserialize_identifier(BytesVisitor) + if deserializer.is_human_readable() { + deserializer.deserialize_identifier(BytesVisitor) + } else { + Vec::::deserialize(deserializer).map(Bytes) + } } } @@ -111,6 +119,20 @@ impl<'a> Visitor<'a> for BytesVisitor { { self.visit_str(value.as_ref()) } + + fn visit_bytes(self, value: &[u8]) -> Result + where + E: Error, + { + Ok(Bytes(value.to_vec())) + } + + fn visit_byte_buf(self, value: Vec) -> Result + where + E: Error, + { + Ok(Bytes(value)) + } } // `Log`: from `web3::types::log` diff --git a/core/lib/basic_types/src/web3/tests.rs b/core/lib/basic_types/src/web3/tests.rs index e7c0a0d048a..223c7d6214b 100644 --- a/core/lib/basic_types/src/web3/tests.rs +++ b/core/lib/basic_types/src/web3/tests.rs @@ -83,3 +83,27 @@ fn block_can_be_deserialized() { let block: Block = serde_json::from_str(pre_dencun).unwrap(); assert!(block.excess_blob_gas.is_none()); } + +#[test] +fn test_bytes_serde_bincode() { + let original = Bytes(vec![0, 1, 2, 3, 4]); + let encoded: Vec = bincode::serialize(&original).unwrap(); + let decoded: Bytes = bincode::deserialize(&encoded).unwrap(); + assert_eq!(original, decoded); +} + +#[test] +fn test_bytes_serde_bincode_snapshot() { + let original = Bytes(vec![0, 1, 2, 3, 4]); + let encoded: Vec = vec![5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; + let decoded: Bytes = bincode::deserialize(&encoded).unwrap(); + assert_eq!(original, decoded); +} + +#[test] +fn test_bytes_serde_json() { + let original = Bytes(vec![0, 1, 2, 3, 4]); + let encoded = serde_json::to_string(&original).unwrap(); + let decoded: Bytes = serde_json::from_str(&encoded).unwrap(); + assert_eq!(original, decoded); +} diff --git a/core/lib/zksync_core/src/tee_verifier_input_producer/mod.rs b/core/lib/zksync_core/src/tee_verifier_input_producer/mod.rs index 95cb6d2d085..98a15259a78 100644 --- a/core/lib/zksync_core/src/tee_verifier_input_producer/mod.rs +++ b/core/lib/zksync_core/src/tee_verifier_input_producer/mod.rs @@ -11,12 +11,12 @@ use std::{sync::Arc, time::Instant}; use anyhow::Context; use async_trait::async_trait; -use multivm::zk_evm_latest::ethereum_types::H256; use google_cloud_googleapis::pubsub::v1::PubsubMessage; use google_cloud_pubsub::{ client::{Client, ClientConfig}, publisher::Publisher, }; +use multivm::zk_evm_latest::ethereum_types::H256; use tokio::{runtime::Handle, task::JoinHandle}; use vm_utils::storage::L1BatchParamsProvider; use zksync_dal::{tee_verifier_input_producer_dal::JOB_MAX_ATTEMPT, ConnectionPool, Core, CoreDal};