-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client/host): Oracle-backed Blob fetcher
Implements the oracle-backed blob fetcher for the client program
- Loading branch information
Showing
10 changed files
with
247 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Contains the concrete implementation of the [BlobProvider] trait for the client program. | ||
use crate::{CachingOracle, HintType, HINT_WRITER}; | ||
use alloc::{boxed::Box, sync::Arc, vec::Vec}; | ||
use alloy_consensus::Blob; | ||
use alloy_eips::eip4844::FIELD_ELEMENTS_PER_BLOB; | ||
use alloy_primitives::keccak256; | ||
use async_trait::async_trait; | ||
use kona_derive::{ | ||
traits::BlobProvider, | ||
types::{BlobProviderError, IndexedBlobHash}, | ||
}; | ||
use kona_preimage::{HintWriterClient, PreimageKey, PreimageKeyType, PreimageOracleClient}; | ||
use kona_primitives::BlockInfo; | ||
|
||
/// An oracle-backed blob provider. | ||
#[derive(Debug)] | ||
pub struct OracleBlobProvider { | ||
oracle: Arc<CachingOracle>, | ||
} | ||
|
||
impl OracleBlobProvider { | ||
/// Constructs a new `OracleBlobProvider`. | ||
pub fn new(oracle: Arc<CachingOracle>) -> Self { | ||
Self { oracle } | ||
} | ||
|
||
/// Retrieves a blob from the oracle. | ||
/// | ||
/// ## Takes | ||
/// - `block_ref`: The block reference. | ||
/// - `blob_hash`: The blob hash. | ||
/// | ||
/// ## Returns | ||
/// - `Ok(blob)`: The blob. | ||
/// - `Err(e)`: The blob could not be retrieved. | ||
async fn get_blob( | ||
&self, | ||
block_ref: &BlockInfo, | ||
blob_hash: &IndexedBlobHash, | ||
) -> Result<Blob, BlobProviderError> { | ||
let mut blob_req_meta = [0u8; 48]; | ||
blob_req_meta[0..32].copy_from_slice(blob_hash.hash.as_ref()); | ||
blob_req_meta[32..40].copy_from_slice((blob_hash.index as u64).to_be_bytes().as_ref()); | ||
blob_req_meta[40..48].copy_from_slice(block_ref.timestamp.to_be_bytes().as_ref()); | ||
|
||
// Send a hint for the blob commitment and field elements. | ||
HINT_WRITER.write(&HintType::L1Blob.encode_with(&[blob_req_meta.as_ref()])).await?; | ||
|
||
// Fetch the blob commitment. | ||
let mut commitment = [0u8; 48]; | ||
self.oracle | ||
.get_exact(PreimageKey::new(*blob_hash.hash, PreimageKeyType::Sha256), &mut commitment) | ||
.await?; | ||
|
||
// Reconstruct the blob from the 4096 field elements. | ||
let mut blob = Blob::default(); | ||
let mut field_element_key = [0u8; 80]; | ||
field_element_key[..48].copy_from_slice(commitment.as_ref()); | ||
for i in 0..FIELD_ELEMENTS_PER_BLOB { | ||
field_element_key[72..].copy_from_slice(i.to_be_bytes().as_ref()); | ||
|
||
let mut field_element = [0u8; 32]; | ||
self.oracle | ||
.get_exact( | ||
PreimageKey::new(*keccak256(field_element_key), PreimageKeyType::Blob), | ||
&mut field_element, | ||
) | ||
.await?; | ||
blob[(i as usize) << 5..(i as usize + 1) << 5].copy_from_slice(field_element.as_ref()); | ||
} | ||
|
||
Ok(blob) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl BlobProvider for OracleBlobProvider { | ||
async fn get_blobs( | ||
&mut self, | ||
block_ref: &BlockInfo, | ||
blob_hashes: &[IndexedBlobHash], | ||
) -> Result<Vec<Blob>, BlobProviderError> { | ||
let mut blobs = Vec::with_capacity(blob_hashes.len()); | ||
for hash in blob_hashes { | ||
blobs.push(self.get_blob(block_ref, hash).await?); | ||
} | ||
Ok(blobs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
//! Contains the L1 constructs of the client program. | ||
mod blob_provider; | ||
pub use blob_provider::OracleBlobProvider; | ||
|
||
mod chain_provider; | ||
pub use chain_provider::OracleL1ChainProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.