Skip to content

Commit

Permalink
fix(derive): impl sidecar fetching and fix c-kzg online feat flag
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 17, 2024
1 parent ace6571 commit 664968f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
9 changes: 6 additions & 3 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ miniz_oxide = { version = "0.7.2" }
lru = "0.12.3"
spin = { version = "0.9.8", features = ["mutex"] }
c-kzg = { version = "1.0.0", default-features = false, optional = true }
sha2 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false, optional = true }

# `serde` feature dependencies
serde = { version = "1.0.197", default-features = false, features = ["derive"], optional = true }
Expand All @@ -46,11 +46,14 @@ alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "e3f2f07",

[features]
default = ["serde", "k256"]
serde = ["dep:serde", "alloy-primitives/serde", "alloy-consensus/serde", "op-alloy-consensus/serde", "c-kzg/serde"]
serde = ["dep:serde", "alloy-primitives/serde", "alloy-consensus/serde", "op-alloy-consensus/serde"]
k256 = ["alloy-primitives/k256", "alloy-consensus/k256", "op-alloy-consensus/k256"]
online = [
"dep:c-kzg",
"dep:sha2",
"dep:alloy-provider",
"dep:alloy-transport-http",
"dep:reqwest",
"alloy-consensus/serde"
"alloy-consensus/serde",
"c-kzg/serde",
]
6 changes: 3 additions & 3 deletions crates/derive/src/online/beacon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::types::{
APIConfigResponse, APIGenesisResponse, APIGetBlobSidecarsResponse, IndexedBlobHash,
};
use alloc::{boxed::Box, string::String, vec::Vec};
use alloc::{boxed::Box, string::String};
use alloy_provider::Provider;
use alloy_transport_http::Http;
use async_trait::async_trait;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait BeaconClient {
&self,
fetch_all_sidecars: bool,
slot: u64,
hashes: Vec<IndexedBlobHash>,
hashes: &[IndexedBlobHash],
) -> anyhow::Result<APIGetBlobSidecarsResponse>;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ impl<T: Provider<Http<Client>> + Send> BeaconClient for OnlineBeaconClient<T> {
&self,
fetch_all_sidecars: bool,
slot: u64,
hashes: Vec<IndexedBlobHash>,
hashes: &[IndexedBlobHash],
) -> anyhow::Result<APIGetBlobSidecarsResponse> {
let method = alloc::format!("{}{}", SIDECARS_METHOD_PREFIX, slot);
self.inner
Expand Down
24 changes: 19 additions & 5 deletions crates/derive/src/online/blob_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl From<anyhow::Error> for OnlineBlobProviderError {
pub struct OnlineBlobProvider<T: Provider<Http<Client>>, B: BeaconClient, S: SlotDerivation> {
/// The inner Ethereum JSON-RPC provider.
_inner: T,
/// Whether to fetch all sidecars.
fetch_all_sidecars: bool,
/// The Beacon API client.
beacon_client: B,
/// Beacon Genesis time used for the time to slot conversion.
Expand All @@ -78,11 +80,19 @@ impl<T: Provider<Http<Client>>, B: BeaconClient, S: SlotDerivation> OnlineBlobPr
/// provided.
pub fn new(
_inner: T,
fetch_all_sidecars: bool,
beacon_client: B,
genesis_time: Option<u64>,
slot_interval: Option<u64>,
) -> Self {
Self { _inner, beacon_client, genesis_time, slot_interval, _slot_derivation: PhantomData }
Self {
_inner,
fetch_all_sidecars,
beacon_client,
genesis_time,
slot_interval,
_slot_derivation: PhantomData,
}
}

/// Loads the beacon genesis and config spec
Expand All @@ -102,10 +112,14 @@ impl<T: Provider<Http<Client>>, B: BeaconClient, S: SlotDerivation> OnlineBlobPr
/// Fetches blob sidecars for the given slot and blob hashes.
pub async fn fetch_sidecars(
&self,
_slot: u64,
_hashes: &[IndexedBlobHash],
slot: u64,
hashes: &[IndexedBlobHash],
) -> Result<Vec<APIBlobSidecar>, OnlineBlobProviderError> {
unimplemented!("fetching blob sidecars is not implemented");
self.beacon_client
.beacon_blob_side_cars(self.fetch_all_sidecars, slot, hashes)
.await
.map(|r| r.data)
.map_err(|e| e.into())
}

/// Fetches blob sidecars that were confirmed in the specified L1 block with the given indexed
Expand Down Expand Up @@ -250,7 +264,7 @@ mod tests {
let (provider, _anvil) = spawn_anvil();
let beacon_client = MockBeaconClient::default();
let mut blob_provider: OnlineBlobProvider<_, _, SimpleSlotDerivation> =
OnlineBlobProvider::new(provider, beacon_client, None, None);
OnlineBlobProvider::new(provider, true, beacon_client, None, None);
let block_ref = BlockInfo::default();
let blob_hashes = Vec::new();
let result = blob_provider.get_blob_sidecars(&block_ref, &blob_hashes).await;
Expand Down
4 changes: 2 additions & 2 deletions crates/derive/src/online/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::BeaconClient;
use crate::types::{
APIConfigResponse, APIGenesisResponse, APIGetBlobSidecarsResponse, IndexedBlobHash,
};
use alloc::{boxed::Box, string::String, vec::Vec};
use alloc::{boxed::Box, string::String};
use alloy_node_bindings::{Anvil, AnvilInstance};
use alloy_provider::{network::Ethereum, ReqwestProvider};
use alloy_rpc_client::RpcClient;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl BeaconClient for MockBeaconClient {
&self,
_fetch_all_sidecars: bool,
_slot: u64,
_hashes: Vec<IndexedBlobHash>,
_hashes: &[IndexedBlobHash],
) -> anyhow::Result<APIGetBlobSidecarsResponse> {
self.blob_sidecars.clone().ok_or_else(|| anyhow::anyhow!("blob_sidecars not set"))
}
Expand Down
2 changes: 2 additions & 0 deletions crates/derive/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub use payload::{
L2ExecutionPayload, L2ExecutionPayloadEnvelope, PAYLOAD_MEM_FIXED_COST, PAYLOAD_TX_MEM_OVERHEAD,
};

#[cfg(feature = "online")]
mod kzg;
#[cfg(feature = "online")]
pub use kzg::{G1Points, G2Points, G1_POINTS, G2_POINTS};

mod block;
Expand Down
10 changes: 9 additions & 1 deletion crates/derive/src/types/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! Contains sidecar types for blobs.
use crate::types::{Blob, G1_POINTS, G2_POINTS};
use crate::types::Blob;
#[cfg(feature = "online")]
use crate::types::{G1_POINTS, G2_POINTS};
use alloc::{string::String, vec::Vec};
use alloy_primitives::FixedBytes;
#[cfg(feature = "online")]
use c_kzg::{Bytes48, KzgProof, KzgSettings};
#[cfg(feature = "online")]
use sha2::{Digest, Sha256};
#[cfg(feature = "online")]
use tracing::warn;

/// KZG Proof Size
Expand All @@ -14,6 +19,7 @@ pub const KZG_PROOF_SIZE: usize = 48;
pub const KZG_COMMITMENT_SIZE: usize = 48;

/// The versioned hash version for KZG.
#[cfg(feature = "online")]
pub(crate) const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;

/// A blob sidecar.
Expand All @@ -34,6 +40,7 @@ pub struct BlobSidecar {

impl BlobSidecar {
/// Verifies the blob kzg proof.
#[cfg(feature = "online")]
pub fn verify_blob_kzg_proof(&self) -> anyhow::Result<bool> {
let how = |e: c_kzg::Error| anyhow::anyhow!(e);
let blob = c_kzg::Blob::from_bytes(self.blob.as_slice()).map_err(how)?;
Expand All @@ -50,6 +57,7 @@ impl BlobSidecar {
}

/// `VERSIONED_HASH_VERSION_KZG ++ sha256(commitment)[1..]`
#[cfg(feature = "online")]
pub fn to_kzg_versioned_hash(&self) -> [u8; 32] {
let commitment = self.kzg_commitment.as_slice();
let mut hash: [u8; 32] = Sha256::digest(commitment).into();
Expand Down

0 comments on commit 664968f

Please sign in to comment.