Skip to content

Commit

Permalink
replace Box with Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
juan518munoz committed Jan 7, 2025
1 parent f99d65a commit e7fb0e5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 73 deletions.
4 changes: 1 addition & 3 deletions core/node/da_clients/src/eigen/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use crate::utils::to_retriable_da_error;
#[async_trait]
pub trait GetBlobData: std::fmt::Debug + Send + Sync {
async fn get_blob_data(&self, input: &str) -> anyhow::Result<Option<Vec<u8>>>;

fn clone_boxed(&self) -> Box<dyn GetBlobData>;
}

/// EigenClient is a client for the Eigen DA service.
Expand All @@ -29,7 +27,7 @@ impl EigenClient {
pub async fn new(
config: EigenConfig,
secrets: EigenSecrets,
get_blob_data: Box<dyn GetBlobData>,
get_blob_data: Arc<dyn GetBlobData>,
) -> anyhow::Result<Self> {
let private_key = SecretKey::from_str(secrets.private_key.0.expose_secret().as_str())
.map_err(|e| anyhow::anyhow!("Failed to parse private key: {}", e))?;
Expand Down
16 changes: 6 additions & 10 deletions core/node/da_clients/src/eigen/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// `cargo test -p zksync_da_clients -- --ignored`
#[cfg(test)]
mod tests {
use std::{str::FromStr, time::Duration};
use std::{str::FromStr, sync::Arc, time::Duration};

use backon::{ConstantBuilder, Retryable};
use serial_test::file_serial;
Expand Down Expand Up @@ -63,10 +63,6 @@ mod tests {
async fn get_blob_data(&self, _input: &'_ str) -> anyhow::Result<Option<Vec<u8>>> {
Ok(None)
}

fn clone_boxed(&self) -> Box<dyn GetBlobData> {
Box::new(self.clone())
}
}

fn test_secrets() -> EigenSecrets {
Expand All @@ -84,7 +80,7 @@ mod tests {
async fn test_non_auth_dispersal() {
let config = EigenConfig::default();
let secrets = test_secrets();
let client = EigenClient::new(config.clone(), secrets, Box::new(MockGetBlobData))
let client = EigenClient::new(config.clone(), secrets, Arc::new(MockGetBlobData))
.await
.unwrap();
let data = vec![1; 20];
Expand Down Expand Up @@ -112,7 +108,7 @@ mod tests {
..EigenConfig::default()
};
let secrets = test_secrets();
let client = EigenClient::new(config.clone(), secrets, Box::new(MockGetBlobData))
let client = EigenClient::new(config.clone(), secrets, Arc::new(MockGetBlobData))
.await
.unwrap();
let data = vec![1; 20];
Expand Down Expand Up @@ -142,7 +138,7 @@ mod tests {
};
let secrets = test_secrets();

let client = EigenClient::new(config.clone(), secrets, Box::new(MockGetBlobData))
let client = EigenClient::new(config.clone(), secrets, Arc::new(MockGetBlobData))
.await
.unwrap();
let data = vec![1; 20];
Expand Down Expand Up @@ -170,7 +166,7 @@ mod tests {
..EigenConfig::default()
};
let secrets = test_secrets();
let client = EigenClient::new(config.clone(), secrets, Box::new(MockGetBlobData))
let client = EigenClient::new(config.clone(), secrets, Arc::new(MockGetBlobData))
.await
.unwrap();
let data = vec![1; 20];
Expand Down Expand Up @@ -199,7 +195,7 @@ mod tests {
..EigenConfig::default()
};
let secrets = test_secrets();
let client = EigenClient::new(config.clone(), secrets, Box::new(MockGetBlobData))
let client = EigenClient::new(config.clone(), secrets, Arc::new(MockGetBlobData))
.await
.unwrap();
let data = vec![1; 20];
Expand Down
22 changes: 5 additions & 17 deletions core/node/da_clients/src/eigen/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{str::FromStr, sync::Arc};

use anyhow::Context;
use secp256k1::{ecdsa::RecoverableSignature, SecretKey};
Expand Down Expand Up @@ -32,25 +32,13 @@ use crate::eigen::{
verifier::VerificationError,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct RawEigenClient {
client: DisperserClient<Channel>,
private_key: SecretKey,
pub config: EigenConfig,
verifier: Verifier,
get_blob_data: Box<dyn GetBlobData>,
}

impl Clone for RawEigenClient {
fn clone(&self) -> Self {
Self {
client: self.client.clone(),
private_key: self.private_key,
config: self.config.clone(),
verifier: self.verifier.clone(),
get_blob_data: self.get_blob_data.clone_boxed(),
}
}
get_blob_data: Arc<dyn GetBlobData>,
}

pub(crate) const DATA_CHUNK_SIZE: usize = 32;
Expand All @@ -61,7 +49,7 @@ impl RawEigenClient {
pub async fn new(
private_key: SecretKey,
config: EigenConfig,
get_blob_data: Box<dyn GetBlobData>,
get_blob_data: Arc<dyn GetBlobData>,
) -> anyhow::Result<Self> {
let endpoint =
Endpoint::from_str(config.disperser_rpc.as_str())?.tls_config(ClientTlsConfig::new())?;
Expand Down Expand Up @@ -94,7 +82,7 @@ impl RawEigenClient {
query_client,
);

let verifier = Verifier::new(verifier_config, Box::new(signing_client))
let verifier = Verifier::new(verifier_config, Arc::new(signing_client))
.await
.context("Failed to create verifier")?;
Ok(RawEigenClient {
Expand Down
24 changes: 4 additions & 20 deletions core/node/da_clients/src/eigen/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::Path};
use std::{collections::HashMap, path::Path, sync::Arc};

use ark_bn254::{Fq, G1Affine};
use ethabi::{encode, ParamType, Token};
Expand All @@ -16,8 +16,6 @@ use super::blob_info::{BatchHeader, BlobHeader, BlobInfo, G1Commitment};

#[async_trait::async_trait]
pub trait VerifierClient: Sync + Send + std::fmt::Debug {
fn clone_boxed(&self) -> Box<dyn VerifierClient>;

/// Returns the current block number.
async fn block_number(&self) -> EnrichedClientResult<U64>;

Expand All @@ -31,10 +29,6 @@ pub trait VerifierClient: Sync + Send + std::fmt::Debug {

#[async_trait::async_trait]
impl VerifierClient for PKSigningClient {
fn clone_boxed(&self) -> Box<dyn VerifierClient> {
Box::new(self.clone())
}

async fn block_number(&self) -> EnrichedClientResult<U64> {
self.as_ref().block_number().await
}
Expand Down Expand Up @@ -92,21 +86,11 @@ pub struct VerifierConfig {
/// Verifier used to verify the integrity of the blob info
/// Kzg is used for commitment verification
/// EigenDA service manager is used to connect to the service manager contract
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Verifier {
kzg: Kzg,
cfg: VerifierConfig,
signing_client: Box<dyn VerifierClient>,
}

impl Clone for Verifier {
fn clone(&self) -> Self {
Self {
kzg: self.kzg.clone(),
cfg: self.cfg.clone(),
signing_client: self.signing_client.clone_boxed(),
}
}
signing_client: Arc<dyn VerifierClient>,
}

impl Verifier {
Expand Down Expand Up @@ -147,7 +131,7 @@ impl Verifier {

pub(crate) async fn new(
cfg: VerifierConfig,
signing_client: Box<dyn VerifierClient>,
signing_client: Arc<dyn VerifierClient>,
) -> Result<Self, VerificationError> {
let srs_points_to_load = cfg.max_blob_size / Self::POINT_SIZE;
let path = Self::save_points(cfg.clone().g1_url, cfg.clone().g2_url).await?;
Expand Down
31 changes: 13 additions & 18 deletions core/node/da_clients/src/eigen/verifier_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
mod test {
use std::{collections::HashMap, str::FromStr};
use std::{collections::HashMap, str::FromStr, sync::Arc};

use url::Url;
use zksync_eth_client::{clients::PKSigningClient, EnrichedClientResult};
Expand Down Expand Up @@ -51,11 +51,6 @@ mod test {

#[async_trait::async_trait]
impl VerifierClient for MockVerifierClient {
fn clone_boxed(&self) -> Box<dyn VerifierClient> {
Box::new(Self {
replies: self.replies.clone(),
})
}

async fn block_number(&self) -> EnrichedClientResult<U64> {
Ok(U64::from(42))
Expand Down Expand Up @@ -95,7 +90,7 @@ mod test {
async fn test_verify_commitment() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let commitment = G1Commitment {
x: vec![
22, 11, 176, 29, 82, 48, 62, 49, 51, 119, 94, 17, 156, 142, 248, 96, 240, 183, 134,
Expand All @@ -117,7 +112,7 @@ mod test {
async fn test_verify_commitment_mocked() {
let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(HashMap::new());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let commitment = G1Commitment {
x: vec![
22, 11, 176, 29, 82, 48, 62, 49, 51, 119, 94, 17, 156, 142, 248, 96, 240, 183, 134,
Expand All @@ -138,7 +133,7 @@ mod test {
async fn test_verify_merkle_proof() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down Expand Up @@ -221,7 +216,7 @@ mod test {
async fn test_verify_merkle_proof_mocked() {
let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(HashMap::new());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down Expand Up @@ -303,7 +298,7 @@ mod test {
async fn test_hash_blob_header() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let blob_header = BlobHeader {
commitment: G1Commitment {
x: vec![
Expand Down Expand Up @@ -342,7 +337,7 @@ mod test {
async fn test_hash_blob_header_mocked() {
let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(HashMap::new());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let blob_header = BlobHeader {
commitment: G1Commitment {
x: vec![
Expand Down Expand Up @@ -380,7 +375,7 @@ mod test {
async fn test_inclusion_proof() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let proof = hex::decode("c455c1ea0e725d7ea3e5f29e9f48be8fc2787bb0a914d5a86710ba302c166ac4f626d76f67f1055bb960a514fb8923af2078fd84085d712655b58a19612e8cd15c3e4ac1cef57acde3438dbcf63f47c9fefe1221344c4d5c1a4943dd0d1803091ca81a270909dc0e146841441c9bd0e08e69ce6168181a3e4060ffacf3627480bec6abdd8d7bb92b49d33f180c42f49e041752aaded9c403db3a17b85e48a11e9ea9a08763f7f383dab6d25236f1b77c12b4c49c5cdbcbea32554a604e3f1d2f466851cb43fe73617b3d01e665e4c019bf930f92dea7394c25ed6a1e200d051fb0c30a2193c459f1cfef00bf1ba6656510d16725a4d1dc031cb759dbc90bab427b0f60ddc6764681924dda848824605a4f08b7f526fe6bd4572458c94e83fbf2150f2eeb28d3011ec921996dc3e69efa52d5fcf3182b20b56b5857a926aa66605808079b4d52c0c0cfe06923fa92e65eeca2c3e6126108e8c1babf5ac522f4d7").unwrap();
let leaf: [u8; 32] =
hex::decode("f6106e6ae4631e68abe0fa898cedbe97dbae6c7efb1b088c5aa2e8b91190ff96")
Expand All @@ -402,7 +397,7 @@ mod test {
async fn test_inclusion_proof_mocked() {
let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(HashMap::new());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let proof = hex::decode("c455c1ea0e725d7ea3e5f29e9f48be8fc2787bb0a914d5a86710ba302c166ac4f626d76f67f1055bb960a514fb8923af2078fd84085d712655b58a19612e8cd15c3e4ac1cef57acde3438dbcf63f47c9fefe1221344c4d5c1a4943dd0d1803091ca81a270909dc0e146841441c9bd0e08e69ce6168181a3e4060ffacf3627480bec6abdd8d7bb92b49d33f180c42f49e041752aaded9c403db3a17b85e48a11e9ea9a08763f7f383dab6d25236f1b77c12b4c49c5cdbcbea32554a604e3f1d2f466851cb43fe73617b3d01e665e4c019bf930f92dea7394c25ed6a1e200d051fb0c30a2193c459f1cfef00bf1ba6656510d16725a4d1dc031cb759dbc90bab427b0f60ddc6764681924dda848824605a4f08b7f526fe6bd4572458c94e83fbf2150f2eeb28d3011ec921996dc3e69efa52d5fcf3182b20b56b5857a926aa66605808079b4d52c0c0cfe06923fa92e65eeca2c3e6126108e8c1babf5ac522f4d7").unwrap();
let leaf: [u8; 32] =
hex::decode("f6106e6ae4631e68abe0fa898cedbe97dbae6c7efb1b088c5aa2e8b91190ff96")
Expand All @@ -423,7 +418,7 @@ mod test {
async fn test_verify_batch() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down Expand Up @@ -531,7 +526,7 @@ mod test {

let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(mock_replies);
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down Expand Up @@ -613,7 +608,7 @@ mod test {
async fn test_verify_security_params() {
let cfg = get_verifier_config();
let signing_client = create_remote_signing_client(cfg.clone());
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down Expand Up @@ -738,7 +733,7 @@ mod test {

let cfg = get_verifier_config();
let signing_client = MockVerifierClient::new(mock_replies);
let verifier = Verifier::new(cfg, Box::new(signing_client)).await.unwrap();
let verifier = Verifier::new(cfg, Arc::new(signing_client)).await.unwrap();
let cert = BlobInfo {
blob_header: BlobHeader {
commitment: G1Commitment {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use zksync_config::{configs::da_client::eigen::EigenSecrets, EigenConfig};
use zksync_da_client::DataAvailabilityClient;
use zksync_da_clients::eigen::{EigenClient, GetBlobData};
Expand Down Expand Up @@ -50,7 +52,7 @@ impl WiringLayer for EigenWiringLayer {
let master_pool = input.master_pool.get().await?;
let get_blob_from_db = GetBlobFromDB { pool: master_pool };
let client: Box<dyn DataAvailabilityClient> = Box::new(
EigenClient::new(self.config, self.secrets, Box::new(get_blob_from_db)).await?,
EigenClient::new(self.config, self.secrets, Arc::new(get_blob_from_db)).await?,
);

Ok(Self::Output {
Expand All @@ -74,8 +76,4 @@ impl GetBlobData for GetBlobFromDB {
.await?;
Ok(batch.map(|b| b.pubdata))
}

fn clone_boxed(&self) -> Box<dyn GetBlobData> {
Box::new(self.clone())
}
}

0 comments on commit e7fb0e5

Please sign in to comment.