Skip to content

Commit

Permalink
PR #1928
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasknauth committed May 15, 2024
1 parent 4de7fee commit 62133e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
30 changes: 26 additions & 4 deletions core/lib/basic_types/src/web3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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::<u8>::deserialize(deserializer).map(Bytes)
}
}
}

Expand Down Expand Up @@ -111,6 +119,20 @@ impl<'a> Visitor<'a> for BytesVisitor {
{
self.visit_str(value.as_ref())
}

fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Bytes(value.to_vec()))
}

fn visit_byte_buf<E>(self, value: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Bytes(value))
}
}

// `Log`: from `web3::types::log`
Expand Down
24 changes: 24 additions & 0 deletions core/lib/basic_types/src/web3/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,27 @@ fn block_can_be_deserialized() {
let block: Block<H256> = 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<u8> = 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<u8> = 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 62133e1

Please sign in to comment.