Skip to content

Commit

Permalink
move decode logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Dec 21, 2024
1 parent a5b38bc commit 86182b6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
data/
optimism/
8 changes: 3 additions & 5 deletions bin/host/src/eigenda_fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ 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());

// 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;

// proxy could just return the original blob
let mut padded_eigenda_blob = vec![0u8; data_size as usize];
padded_eigenda_blob[..eigenda_blob.len()].copy_from_slice(eigenda_blob.as_ref());

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

for i in 0..blob_length {
Expand Down Expand Up @@ -213,10 +213,8 @@ where
// proof to be done
kv_write_lock.set(
PreimageKey::new(*blob_key_hash, PreimageKeyType::GlobalGeneric).into(),
[1,2,3].to_vec(),
[1, 2, 3].to_vec(),
)?;


} else {
panic!("Invalid hint type: {hint_type}. FetcherWithEigenDASupport.prefetch only supports EigenDACommitment hints.");
}
Expand Down
41 changes: 3 additions & 38 deletions crates/eigenda/src/eigenda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
//! [DataAvailabilityProvider] trait for the EigenDA protocol.
use crate::eigenda_blobs::EigenDABlobSource;
use crate::traits::EigenDABlobProvider;
use crate::errors::CodecError;
use crate::traits::EigenDABlobProvider;

use alloc::{boxed::Box, fmt::Debug};
use alloy_primitives::Bytes;
use bytes::buf::Buf;
use async_trait::async_trait;
use kona_derive::{
errors::{PipelineEncodingError, PipelineError, PipelineErrorKind},
sources::EthereumDataSource,
traits::{BlobProvider, ChainProvider, DataAvailabilityProvider},
types::PipelineResult,
errors::{PipelineErrorKind, PipelineError, PipelineEncodingError},
};
use op_alloy_protocol::BlockInfo;
use rust_kzg_bn254::helpers::remove_empty_byte_from_padded_bytes_unchecked;

/// A factory for creating an Ethereum data source provider.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -66,12 +64,7 @@ where
// just dump all the data out
info!(target: "eth-datasource", "next item {:?}", item);

let padded_eigenda_blob = self.eigenda_source.next(&item).await?;
info!(target: "eigenda-datasource", "eigenda_source_result {:?}", padded_eigenda_blob);

// get the actual blob as encoded inside blob
let eigenda_blob = self.default_decode_blob(padded_eigenda_blob)?;

let eigenda_blob = self.eigenda_source.next(&item).await?;
Ok(eigenda_blob)
}

Expand All @@ -80,31 +73,3 @@ where
self.ethereum_source.clear();
}
}

impl<C, B, A> EigenDADataSource<C, B, A>
where
C: ChainProvider + Send + Sync + Clone + Debug,
B: BlobProvider + Send + Sync + Clone + Debug,
A: EigenDABlobProvider + Send + Sync + Clone + Debug,
{
// https://github.com/Layr-Labs/eigenda/blob/1345e77c8a91fed8e5e33f02c3e32c9ed9921670/api/clients/codecs/default_blob_codec.go#L44
fn default_decode_blob(&self, padded_eigenda_blob: Bytes) -> PipelineResult<Bytes> {
if padded_eigenda_blob.len() < 32 {
// ToDo format error better
//return Err(PipelineErrorKind::Temporary(PipelineError::BadEncoding(PipelineEncodingError::SpanBatchError(()))));
unimplemented!()
}

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

let content_size = padded_eigenda_blob.slice(2..6).get_u32();
info!(target: "eigenda-datasource", "content_size {:?}", content_size);
let codec_data = padded_eigenda_blob.slice(32..);

let blob_content = remove_empty_byte_from_padded_bytes_unchecked(codec_data.as_ref());
let blob_content: Bytes = blob_content.into();

Ok(blob_content.slice(..content_size as usize))
}

}
28 changes: 25 additions & 3 deletions crates/eigenda/src/eigenda_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use alloy_primitives::Bytes;
use bytes::buf::Buf;

use kona_derive::errors::BlobDecodingError;

use rust_kzg_bn254::helpers;

#[derive(Default, Clone, Debug)]
/// Represents the data structure for EigenDA Blob.
pub struct EigenDABlobData {
Expand All @@ -13,9 +16,28 @@ 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> {
// where we can implement zero bytes etc.
info!(target: "eigenda-blobdata", "decode {} {:?}", self.blob.len(), self.blob.clone());
Ok(self.blob.clone())
if self.blob.len() < 32 {
// ToDo format error better
//return Err(PipelineErrorKind::Temporary(PipelineError::BadEncoding(PipelineEncodingError::SpanBatchError(()))));
unimplemented!()
}

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();

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

Expand Down
2 changes: 1 addition & 1 deletion crates/eigenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ mod certificate;
pub use certificate::BlobInfo;

mod errors;
pub use errors::CodecError;
pub use errors::CodecError;
14 changes: 8 additions & 6 deletions crates/proof/src/eigenda_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::boxed::Box;
use alloc::sync::Arc;
use alloy_primitives::{keccak256, Bytes};
use async_trait::async_trait;
use hokulea_eigenda::{EigenDABlobProvider, BlobInfo};
use hokulea_eigenda::{BlobInfo, EigenDABlobProvider};
use kona_preimage::{CommsClient, PreimageKey, PreimageKeyType};

use kona_proof::errors::OracleProviderError;
Expand Down Expand Up @@ -50,17 +50,16 @@ impl<T: CommsClient + Sync + Send> EigenDABlobProvider for OracleEigenDAProvider
.await
.map_err(OracleProviderError::Preimage)?;


let mut blob: Vec<u8> = vec![0; cert_blob_info.blob_header.data_length as usize];

// 96 because our g1 commitment has 64 bytes in v1
let mut field_element_key = [0u8; 96];

// ToDo data_length should be power of 2, proxy should have returned it with dividing 32
let data_length = cert_blob_info.blob_header.data_length as u64 / 32;

info!("cert_blob_info.blob_header.data_length {:?}", data_length);

field_element_key[..32].copy_from_slice(&cert_blob_info.blob_header.commitment.x);
field_element_key[32..64].copy_from_slice(&cert_blob_info.blob_header.commitment.y);
for i in 0..data_length {
Expand All @@ -69,14 +68,17 @@ impl<T: CommsClient + Sync + Send> EigenDABlobProvider for OracleEigenDAProvider
let mut field_element = [0u8; 32];
self.oracle
.get_exact(
PreimageKey::new(*keccak256(field_element_key), PreimageKeyType::GlobalGeneric),
PreimageKey::new(
*keccak256(field_element_key),
PreimageKeyType::GlobalGeneric,
),
&mut field_element,
)
.await
.map_err(OracleProviderError::Preimage)?;
blob[(i as usize) << 5..(i as usize + 1) << 5].copy_from_slice(field_element.as_ref());
}

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

Ok(blob.into())
Expand Down

0 comments on commit 86182b6

Please sign in to comment.