Skip to content

Commit

Permalink
fix(da-clients): add padding to the data within EigenDA blob (matter-…
Browse files Browse the repository at this point in the history
…labs#3203)

## What ❔

Add the padding to the blob data in EigenDA client

## Why ❔

The unpadded larger blobs can't be decoded by the EigenDA disperser.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.
  • Loading branch information
dimazhornyk authored Oct 30, 2024
1 parent 42f177a commit 8ae06b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/node/da_clients/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sha2.workspace = true
prost.workspace = true
bech32.workspace = true
ripemd.workspace = true
tonic = { workspace = true, features = ["tls", "tls-roots", "prost", "codegen"] }
tonic = { workspace = true, features = ["tls-roots", "prost", "codegen"] }
pbjson-types.workspace = true

# Eigen dependencies
Expand Down
31 changes: 30 additions & 1 deletion core/node/da_clients/src/eigen/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct RawEigenClient {
account_id: String,
}

pub(crate) const DATA_CHUNK_SIZE: usize = 32;

impl RawEigenClient {
pub(crate) const BUFFER_SIZE: usize = 1000;

Expand Down Expand Up @@ -55,9 +57,10 @@ impl RawEigenClient {
let (tx, rx) = mpsc::channel(Self::BUFFER_SIZE);

let response_stream = client_clone.disperse_blob_authenticated(ReceiverStream::new(rx));
let padded_data = convert_by_padding_empty_byte(&data);

// 1. send DisperseBlobRequest
self.disperse_data(data, &tx).await?;
self.disperse_data(padded_data, &tx).await?;

// this await is blocked until the first response on the stream, so we only await after sending the `DisperseBlobRequest`
let mut response_stream = response_stream.await?.into_inner();
Expand Down Expand Up @@ -215,3 +218,29 @@ fn get_account_id(secret_key: &SecretKey) -> String {

format!("0x{}", hex)
}

fn convert_by_padding_empty_byte(data: &[u8]) -> Vec<u8> {
let parse_size = DATA_CHUNK_SIZE - 1;

// Calculate the number of chunks
let data_len = (data.len() + parse_size - 1) / parse_size;

// Pre-allocate `valid_data` with enough space for all chunks
let mut valid_data = vec![0u8; data_len * DATA_CHUNK_SIZE];
let mut valid_end = data_len * DATA_CHUNK_SIZE;

for (i, chunk) in data.chunks(parse_size).enumerate() {
let offset = i * DATA_CHUNK_SIZE;
valid_data[offset] = 0x00; // Set first byte of each chunk to 0x00 for big-endian compliance

let copy_end = offset + 1 + chunk.len();
valid_data[offset + 1..copy_end].copy_from_slice(chunk);

if i == data_len - 1 && chunk.len() < parse_size {
valid_end = offset + 1 + chunk.len();
}
}

valid_data.truncate(valid_end);
valid_data
}

0 comments on commit 8ae06b2

Please sign in to comment.