diff --git a/.gitmodules b/.gitmodules index d57651408..6b5e85c84 100644 --- a/.gitmodules +++ b/.gitmodules @@ -6,4 +6,4 @@ [submodule "via-core-ext"] path = via-core-ext url = https://github.com/vianetwork/via-core-ext.git - branch = main + branch = fix/via-in-memory-da-client diff --git a/Cargo.lock b/Cargo.lock index 4023a47d5..243855263 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5640,9 +5640,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.76" +version = "0.3.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65" dependencies = [ "once_cell", "wasm-bindgen", @@ -6226,16 +6226,16 @@ checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" [[package]] name = "libp2p-identity" -version = "0.2.10" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "257b5621d159b32282eac446bed6670c39c7dc68a200a992d8f056afa0066f6d" +checksum = "3104e13b51e4711ff5738caa1fb54467c8604c2e94d607e27745bcf709068774" dependencies = [ "bs58 0.5.1", "hkdf", "multihash", "quick-protobuf", "sha2 0.10.8", - "thiserror 1.0.69", + "thiserror 2.0.6", "tracing", ] @@ -13096,34 +13096,22 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" [[package]] name = "wasm-bindgen" -version = "0.2.99" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60" dependencies = [ "cfg-if", "once_cell", + "rustversion", "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" -dependencies = [ - "bumpalo", - "log", - "proc-macro2 1.0.92", - "quote 1.0.37", - "syn 2.0.90", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.49" +version = "0.4.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" +checksum = "551f88106c6d5e7ccc7cd9a16f312dd3b5d36ea8b4954304657d5dfba115d4a0" dependencies = [ "cfg-if", "js-sys", @@ -13134,9 +13122,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.99" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2" dependencies = [ "quote 1.0.37", "wasm-bindgen-macro-support", @@ -13144,22 +13132,25 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.99" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc" dependencies = [ + "bumpalo", "proc-macro2 1.0.92", "quote 1.0.37", "syn 2.0.90", - "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.99" +version = "0.2.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76" +dependencies = [ + "unicode-ident", +] [[package]] name = "wasm-streams" @@ -13231,9 +13222,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.76" +version = "0.3.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" +checksum = "3a1f95c0d03a47f4ae1f7a64643a6bb97465d9b740f0fa8f90ea33915c99a9a1" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/core/lib/via_da_clients/src/celestia/client.rs b/core/lib/via_da_clients/src/celestia/client.rs index 62d166cf4..f183e1112 100644 --- a/core/lib/via_da_clients/src/celestia/client.rs +++ b/core/lib/via_da_clients/src/celestia/client.rs @@ -6,7 +6,9 @@ use std::{ use anyhow::anyhow; use async_trait::async_trait; use celestia_rpc::{BlobClient, Client, P2PClient}; -use celestia_types::{nmt::Namespace, Blob, Commitment, TxConfig}; +use celestia_types::{ + consts::appconsts::SHARE_VERSION_ZERO, nmt::Namespace, Blob, Commitment, TxConfig, +}; use hex; use zksync_config::configs::via_secrets::ViaDASecrets; pub use zksync_config::ViaCelestiaConfig; @@ -16,6 +18,8 @@ use zksync_types::{ via_da_dispatcher::{deserialize_blob_ids, ViaDaBlob}, }; +use crate::common::{parse_blob_id, VIA_NAME_SPACE_BYTES}; + /// If no value is provided for GasPrice, then this will be serialized to `-1.0` which means the node that /// receives the request will calculate the GasPrice for given blob. const GAS_PRICE: f64 = -1.0; @@ -38,12 +42,7 @@ impl CelestiaClient { // connection test let _info = client.p2p_info().await?; - let namespace_bytes = [b'V', b'I', b'A', 0, 0, 0, 0, 0]; // Pad with zeros to reach 8 bytes - let namespace_bytes: &[u8] = &namespace_bytes; - let namespace = Namespace::new_v0(namespace_bytes).map_err(|error| types::DAError { - error: error.into(), - is_retriable: false, - })?; + let namespace = Namespace::new_v0(&VIA_NAME_SPACE_BYTES)?; Ok(Self { light_node_url: secrets.api_node_url, @@ -52,31 +51,6 @@ impl CelestiaClient { namespace, }) } - - fn parse_blob_id(&self, blob_id: &str) -> anyhow::Result<(Commitment, u64)> { - // [8]byte block height ++ [32]byte commitment - let blob_id_bytes = hex::decode(blob_id).map_err(|error| types::DAError { - error: error.into(), - is_retriable: false, - })?; - - let block_height = - u64::from_be_bytes(blob_id_bytes[..8].try_into().map_err(|_| types::DAError { - error: anyhow!("Failed to convert block height"), - is_retriable: false, - })?); - - let commitment_data: [u8; 32] = - blob_id_bytes[8..40] - .try_into() - .map_err(|_| types::DAError { - error: anyhow!("Failed to convert commitment"), - is_retriable: false, - })?; - let commitment = Commitment(commitment_data); - - Ok((commitment, block_height)) - } } #[async_trait] @@ -86,22 +60,21 @@ impl DataAvailabilityClient for CelestiaClient { _batch_number: u32, data: Vec, ) -> Result { - let share_version = celestia_types::consts::appconsts::SHARE_VERSION_ZERO; - let blob = Blob::new(self.namespace, data.clone()).map_err(|error| types::DAError { error: error.into(), is_retriable: false, })?; - let commitment_result = match Commitment::from_blob(self.namespace, share_version, &data) { - Ok(commit) => commit, - Err(error) => { - return Err(types::DAError { - error: error.into(), - is_retriable: false, - }) - } - }; + let commitment_result = + match Commitment::from_blob(self.namespace, SHARE_VERSION_ZERO, &data) { + Ok(commit) => commit, + Err(error) => { + return Err(types::DAError { + error: error.into(), + is_retriable: false, + }) + } + }; // NOTE: during refactoring add address to the config // we can specify the sender address for the transaction with using TxConfig @@ -137,11 +110,10 @@ impl DataAvailabilityClient for CelestiaClient { blob_id: &str, ) -> Result, types::DAError> { let (commitment, block_height) = - self.parse_blob_id(&blob_id) - .map_err(|error| types::DAError { - error: error.into(), - is_retriable: true, - })?; + parse_blob_id(&blob_id).map_err(|error| types::DAError { + error: error.into(), + is_retriable: true, + })?; let blob = self .inner @@ -177,11 +149,10 @@ impl DataAvailabilityClient for CelestiaClient { for blob_id in blob_ids { let (commitment, block_height) = - self.parse_blob_id(&blob_id) - .map_err(|error| types::DAError { - error: error.into(), - is_retriable: true, - })?; + parse_blob_id(&blob_id).map_err(|error| types::DAError { + error: error.into(), + is_retriable: true, + })?; let blob = self .inner diff --git a/core/lib/via_da_clients/src/common.rs b/core/lib/via_da_clients/src/common.rs new file mode 100644 index 000000000..80dd91b89 --- /dev/null +++ b/core/lib/via_da_clients/src/common.rs @@ -0,0 +1,31 @@ +use anyhow::anyhow; +use celestia_types::Commitment; +use zksync_da_client::types; + +pub const VIA_NAME_SPACE_BYTES: [u8; 8] = [b'V', b'I', b'A', 0, 0, 0, 0, 0]; + +pub(crate) fn parse_blob_id(blob_id: &str) -> anyhow::Result<(Commitment, u64)> { + // [8]byte block height ++ [32]byte commitment + let blob_id_bytes = hex::decode(blob_id).map_err(|error| types::DAError { + error: error.into(), + is_retriable: false, + })?; + + let block_height = + u64::from_be_bytes(blob_id_bytes[..8].try_into().map_err(|_| types::DAError { + error: anyhow!("Failed to convert block height"), + is_retriable: false, + })?); + + let commitment_data: [u8; 32] = + blob_id_bytes[8..40] + .try_into() + .map_err(|_| types::DAError { + error: anyhow!("Failed to convert commitment"), + is_retriable: false, + })?; + + let commitment = Commitment(commitment_data); + + Ok((commitment, block_height)) +} diff --git a/core/lib/via_da_clients/src/external_node/client.rs b/core/lib/via_da_clients/src/external_node/client.rs index 552cf6221..2f06f0988 100644 --- a/core/lib/via_da_clients/src/external_node/client.rs +++ b/core/lib/via_da_clients/src/external_node/client.rs @@ -1,6 +1,7 @@ use std::fmt::Debug; use async_trait::async_trait; +use celestia_types::{consts::appconsts::SHARE_VERSION_ZERO, nmt::Namespace, Commitment}; use zksync_da_client::{ types::{DAError, DispatchResponse, InclusionData}, DataAvailabilityClient, @@ -10,19 +11,84 @@ use zksync_web3_decl::{ namespaces::ViaNamespaceClient, }; +use crate::common::{parse_blob_id, VIA_NAME_SPACE_BYTES}; + /// An implementation of the `DataAvailabilityClient` trait that retrieves data from an external node. /// This client is used as a fallback when Celestia data is not available (e.g., after 30 days). #[derive(Clone)] pub struct ExternalNodeDaClient { client: Box>, + namespace: Namespace, } impl ExternalNodeDaClient { /// Creates a new ExternalNodeDaClient using the provided L2 query client. /// /// The client should be obtained from the node framework's `L2InterfaceResource`. - pub fn new(client: Box>) -> Self { - Self { client } + pub fn new(client: Box>) -> anyhow::Result { + let namespace = Namespace::new_v0(&VIA_NAME_SPACE_BYTES)?; + + Ok(Self { client, namespace }) + } + + pub fn validate_commitment(&self, blob_id: &str, data: &[u8]) -> anyhow::Result<(), DAError> { + let commitment = + Commitment::from_blob(self.namespace, &data, SHARE_VERSION_ZERO).map_err(|error| { + DAError { + error: anyhow!("Error to create commitment: {}", error.to_string()), + is_retriable: false, + } + })?; + + // let commitment_result = + // match Commitment::from_blob(self.namespace, SHARE_VERSION_ZERO, data) { + // Ok(commit) => commit, + // Err(error) => { + // return Err(DAError { + // error: error.into(), + // is_retriable: false, + // }) + // } + // }; + + let blob_id_bytes = hex::decode(blob_id).map_err(|error| DAError { + error: error.into(), + is_retriable: true, + })?; + + // Prepend the block height to the blob_id + // let mut blob_id_with_block_height = Vec::with_capacity(8 + 32); + // blob_id_with_block_height.extend_from_slice(&(0 as u64).to_be_bytes()); + // blob_id_with_block_height.extend_from_slice(&blob_id_bytes); + // let blob_id_str = hex::encode(blob_id_with_block_height.clone()); + + // println!( + // "*****************************: {:?}", + // blob_id_with_block_height + // ); + + let (commitment, _) = parse_blob_id(&blob_id).map_err(|error| DAError { + error: error.into(), + is_retriable: true, + })?; + + println!( + "-----------------------------------------------: {:?}", + commitment.0 + ); + + println!( + "-----------------------------------------------: {:?}", + commitment_result.0 + ); + + if commitment.0 != commitment_result.0 { + return Err(DAError { + error: anyhow::anyhow!("Commitment mismatch"), + is_retriable: false, + }); + } + Ok(()) } } @@ -58,12 +124,30 @@ impl DataAvailabilityClient for ExternalNodeDaClient { error: e.into(), is_retriable: false, })?; + + // self.validate_commitment(blob_id, &data) + // .map_err(|e| DAError { + // error: e.into(), + // is_retriable: false, + // })?; + Ok(Some(InclusionData { data })) } else { let data = hex::decode(&blob.pub_data).map_err(|e| DAError { error: e.into(), is_retriable: false, })?; + println!( + "-----------------------------------------------{:?}", + blob.pub_data + ); + + self.validate_commitment(blob_id, &data) + .map_err(|e| DAError { + error: e.into(), + is_retriable: false, + })?; + Ok(Some(InclusionData { data })) } } diff --git a/core/lib/via_da_clients/src/lib.rs b/core/lib/via_da_clients/src/lib.rs index 10f8474aa..efed51567 100644 --- a/core/lib/via_da_clients/src/lib.rs +++ b/core/lib/via_da_clients/src/lib.rs @@ -3,3 +3,4 @@ pub mod external_node; pub mod fallback; pub mod http; pub mod wiring_layer; +pub mod common; diff --git a/core/lib/via_da_clients/src/wiring_layer.rs b/core/lib/via_da_clients/src/wiring_layer.rs index 798b7393b..9e50156c6 100644 --- a/core/lib/via_da_clients/src/wiring_layer.rs +++ b/core/lib/via_da_clients/src/wiring_layer.rs @@ -69,7 +69,7 @@ impl WiringLayer for ViaDaClientWiringLayer { let mut fallback_client = None; if let Some(query_client_l2) = input.query_client_l2 { // Use the QueryClient from the node framework instead of creating a new HTTP client - fallback_client = Some(Box::new(ExternalNodeDaClient::new(query_client_l2.0)) + fallback_client = Some(Box::new(ExternalNodeDaClient::new(query_client_l2.0)?) as Box); } diff --git a/core/node/via_da_dispatcher/src/da_dispatcher.rs b/core/node/via_da_dispatcher/src/da_dispatcher.rs index eaae0f5bf..5e813ae0d 100644 --- a/core/node/via_da_dispatcher/src/da_dispatcher.rs +++ b/core/node/via_da_dispatcher/src/da_dispatcher.rs @@ -110,6 +110,11 @@ impl ViaDataAvailabilityDispatcher { drop(conn); for batch in batches { + println!( + "----------------------PUBDATA-------------------------: {:?}", + hex::encode(batch.pubdata.clone()) + ); + let chunks: Vec> = batch .pubdata .clone() diff --git a/via-core-ext b/via-core-ext index a5b99648d..d1efb27a3 160000 --- a/via-core-ext +++ b/via-core-ext @@ -1 +1 @@ -Subproject commit a5b99648d58fb844afd8c2df9837cd44db4f9064 +Subproject commit d1efb27a3dae14ec2f33c9b19bfdc52779027542 diff --git a/via_verifier/lib/verifier_dal/src/withdrawals_dal.rs b/via_verifier/lib/verifier_dal/src/withdrawals_dal.rs index b579e4e02..231b85934 100644 --- a/via_verifier/lib/verifier_dal/src/withdrawals_dal.rs +++ b/via_verifier/lib/verifier_dal/src/withdrawals_dal.rs @@ -164,7 +164,7 @@ impl ViaWithdrawalDal<'_, '_> { bridge_withdrawal_id: i64, withdrawal: &WithdrawalRequest, ) -> DalResult<()> { - let result = sqlx::query!( + sqlx::query!( r#" UPDATE via_withdrawals SET bridge_withdrawal_id = $4,