Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create codec mod #22

Merged
merged 11 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
data/
optimism/
tags
samlaf marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions bin/client/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ run-client-native block_number l1_rpc l1_beacon_rpc l2_rpc rollup_node_rpc rollu
# Move to the workspace root
cd $(git rev-parse --show-toplevel)

rm -rf ./data
mkdir ./data

echo "Running host program with native client program..."
cargo r --bin hokulea-host -- \
--l1-head $L1_HEAD \
Expand Down
31 changes: 10 additions & 21 deletions bin/host/src/eigenda_fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use alloy_provider::ReqwestProvider;
use alloy_rlp::Decodable;
use anyhow::{anyhow, Result};
use core::panic;
use hokulea_eigenda::encode_eigenda_blob;
use hokulea_eigenda::BlobInfo;
use hokulea_eigenda::BLOB_ENCODING_VERSION_0;
use hokulea_proof::hint::{ExtendedHint, ExtendedHintType};
Expand Down Expand Up @@ -141,45 +142,33 @@ where

if hint_type == ExtendedHintType::EigenDACommitment {
let cert = hint_data;
info!(target: "fetcher_with_eigenda_support", "Fetching eigenda commitment cert: {:?}", cert);
trace!(target: "fetcher_with_eigenda_support", "Fetching eigenda commitment cert: {:?}", cert);
// Fetch the blob sidecar from the blob provider.
let rollup_data = self
.eigenda_blob_provider
.fetch_eigenda_blob(&cert)
.await
.map_err(|e| anyhow!("Failed to fetch eigenda blob: {e}"))?;

// Acquire a lock on the key-value store and set the preimages.
let mut kv_write_lock = self.kv_store.write().await;

// the fourth because 0x01010000 in the beginning is metadata
let rollup_data_len = rollup_data.len() as u32;
let item_slice = cert.as_ref();
let cert_blob_info = BlobInfo::decode(&mut &item_slice[4..]).unwrap();

// Todo ensure data_length is always power of 2. Proxy made mistake
// TODO ensure data_length is always power of 2. Proxy made mistake
let data_size = cert_blob_info.blob_header.data_length as u64;
let blob_length: u64 = data_size / 32;
samlaf marked this conversation as resolved.
Show resolved Hide resolved

// encode to become raw blob
let codec_rollup_data = helpers::convert_by_padding_empty_byte(rollup_data.as_ref());
let codec_rollup_data_len = codec_rollup_data.len() as u32;

let mut raw_blob = vec![0u8; data_size as usize];
let raw_blob = encode_eigenda_blob(rollup_data.as_ref());
trace!(target: "fetcher_with_eigenda_support", "Fetching ssize size: {:?} {}", raw_blob.len() , data_size);

if 32 + codec_rollup_data_len as u64 > data_size {
return Err(anyhow!("data size is less than reconstructed data codec_rollup_data_len {} data_size {}", codec_rollup_data_len, data_size));
if raw_blob.len() != data_size as usize {
return Err(
anyhow!("data size from cert does not equal to reconstructed data codec_rollup_data_len {} data_size {}",
raw_blob.len(), data_size));
}

// blob header
// https://github.com/Layr-Labs/eigenda/blob/f8b0d31d65b29e60172507074922668f4ca89420/api/clients/codecs/default_blob_codec.go#L25
// raw blob the immediate data just before taking IFFT
raw_blob[1] = BLOB_ENCODING_VERSION_0;
raw_blob[2..6].copy_from_slice(&rollup_data_len.to_be_bytes());

// encode length as uint32
raw_blob[32..(32 + codec_rollup_data_len as usize)].copy_from_slice(&codec_rollup_data);

// Write all the field elements to the key-value store.
// The preimage oracle key for each field element is the keccak256 hash of
// `abi.encodePacked(cert.KZGCommitment, uint256(i))`
Expand All @@ -189,7 +178,7 @@ where
blob_key[..32].copy_from_slice(cert_blob_info.blob_header.commitment.x.as_ref());
blob_key[32..64].copy_from_slice(cert_blob_info.blob_header.commitment.y.as_ref());

info!("cert_blob_info blob_length {:?}", blob_length);
trace!("cert_blob_info blob_length {:?}", blob_length);

for i in 0..blob_length {
blob_key[88..].copy_from_slice(i.to_be_bytes().as_ref());
Expand Down
96 changes: 96 additions & 0 deletions crates/eigenda/src/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::BLOB_ENCODING_VERSION_0;
use alloc::vec::Vec;
use alloy_primitives::Bytes;
use bytes::buf::Buf;
use kona_derive::errors::BlobDecodingError;
use rust_kzg_bn254::helpers;

/// encoded data into an eigenda blob. The output is always power of 2
samlaf marked this conversation as resolved.
Show resolved Hide resolved
pub fn encode_eigenda_blob(rollup_data: &[u8]) -> Bytes {
let rollup_data_size = rollup_data.len() as u32;

// encode to become raw blob
let codec_rollup_data = helpers::convert_by_padding_empty_byte(rollup_data.as_ref());

let blob_payload_size = codec_rollup_data.len();

let blob_size = blob_payload_size + 32;
let blob_size = blob_size.next_power_of_two();

let mut raw_blob = Vec::<u8>::with_capacity(blob_size as usize);
for i in 0..blob_size {
raw_blob.push(0);
}
samlaf marked this conversation as resolved.
Show resolved Hide resolved

raw_blob[1] = BLOB_ENCODING_VERSION_0;
raw_blob[2..6].copy_from_slice(&rollup_data_size.to_be_bytes());

// encode length as uint32
samlaf marked this conversation as resolved.
Show resolved Hide resolved
raw_blob[32..(32 + blob_payload_size as usize)].copy_from_slice(&codec_rollup_data);

Bytes::from(raw_blob)
}

/// decode data into an eigenda blob
samlaf marked this conversation as resolved.
Show resolved Hide resolved
pub fn decode_eigenda_blob(blob: &Bytes) -> Result<Bytes, BlobDecodingError> {
samlaf marked this conversation as resolved.
Show resolved Hide resolved
if blob.len() < 32 {
return Err(BlobDecodingError::InvalidLength);
}

info!(target: "eigenda-datasource", "padded_eigenda_blob {:?}", blob);

// see https://github.com/Layr-Labs/eigenda/blob/f8b0d31d65b29e60172507074922668f4ca89420/api/clients/codecs/default_blob_codec.go#L44
let content_size = blob.slice(2..6).get_u32();
info!(target: "eigenda-datasource", "content_size {:?}", content_size);

// the first 32 Bytes are reserved as the header field element
let codec_data = blob.slice(32..);

// rust kzg bn254 impl already
let blob_content = helpers::remove_empty_byte_from_padded_bytes_unchecked(codec_data.as_ref());
let blob_content: Bytes = blob_content.into();

if blob_content.len() < content_size as usize {
return Err(BlobDecodingError::InvalidLength);
}
Ok(blob_content.slice(..content_size as usize))
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use alloy_primitives::Bytes;
use kona_derive::errors::BlobDecodingError;

#[test]
fn test_decode_success() {
let content = vec![1, 2, 3, 4];
let data = encode_eigenda_blob(&content);
let data_len = data.len();
assert!(data_len.is_power_of_two());

let result = decode_eigenda_blob(&data);
assert!(result.is_ok());
assert_eq!(result.unwrap(), Bytes::from(content));
}

#[test]
fn test_decode_success_empty() {
let content = vec![];
let data = encode_eigenda_blob(&content);
let result = decode_eigenda_blob(&data);
assert!(result.is_ok());
assert_eq!(result.unwrap(), Bytes::from(content));
}

#[test]
fn test_decode_error_invalid_length() {
let content = vec![1, 2, 3, 4];
let mut data = encode_eigenda_blob(&content);
data.truncate(33);
let result = decode_eigenda_blob(&data);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), BlobDecodingError::InvalidLength);
}
}
4 changes: 2 additions & 2 deletions crates/eigenda/src/eigenda_blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ where
// Otherwise, ignore blob and recurse next.
match next_data.decode() {
Ok(d) => Ok(d),
Err(_) => {
warn!(target: "blob-source", "Failed to decode blob data, skipping");
Err(e) => {
warn!(target: "blob-source", "Failed to decode blob data, skipping {}", e);
panic!()
}
}
Expand Down
27 changes: 3 additions & 24 deletions crates/eigenda/src/eigenda_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::codec;
use alloy_primitives::Bytes;
use bytes::buf::Buf;

use kona_derive::errors::BlobDecodingError;

use rust_kzg_bn254::helpers;
Expand All @@ -16,31 +15,11 @@ impl EigenDABlobData {
/// Decodes the blob into raw byte data.
/// Returns a [BlobDecodingError] if the blob is invalid.
pub(crate) fn decode(&self) -> Result<Bytes, BlobDecodingError> {
if self.blob.len() < 32 {
return Err(BlobDecodingError::InvalidLength);
}

info!(target: "eigenda-datasource", "padded_eigenda_blob {:?}", self.blob);

// see https://github.com/Layr-Labs/eigenda/blob/f8b0d31d65b29e60172507074922668f4ca89420/api/clients/codecs/default_blob_codec.go#L44
let content_size = self.blob.slice(2..6).get_u32();
info!(target: "eigenda-datasource", "content_size {:?}", content_size);

// the first 32 Bytes are reserved as the header field element
let codec_data = self.blob.slice(32..);

// rust kzg bn254 impl already
let blob_content =
helpers::remove_empty_byte_from_padded_bytes_unchecked(codec_data.as_ref());
let blob_content: Bytes = blob_content.into();

if blob_content.len() < content_size as usize {
return Err(BlobDecodingError::InvalidLength);
}
let rollup_blob = codec::decode_eigenda_blob(&self.blob)?;
samlaf marked this conversation as resolved.
Show resolved Hide resolved
// might insert a FFT here,

// take data
Ok(blob_content.slice(..content_size as usize))
Ok(rollup_blob)
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/eigenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub use eigenda_data::EigenDABlobData;
mod certificate;
pub use certificate::BlobInfo;

pub mod codec;
pub use codec::decode_eigenda_blob;
pub use codec::encode_eigenda_blob;

mod constant;
pub use constant::BLOB_ENCODING_VERSION_0;
pub use constant::STALE_GAP;
Loading