Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 22 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 24 additions & 53 deletions core/lib/via_da_clients/src/celestia/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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]
Expand All @@ -86,22 +60,21 @@ impl DataAvailabilityClient for CelestiaClient {
_batch_number: u32,
data: Vec<u8>,
) -> Result<types::DispatchResponse, types::DAError> {
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
Expand Down Expand Up @@ -137,11 +110,10 @@ impl DataAvailabilityClient for CelestiaClient {
blob_id: &str,
) -> Result<Option<types::InclusionData>, 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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions core/lib/via_da_clients/src/common.rs
Original file line number Diff line number Diff line change
@@ -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))
}
Loading