Skip to content

Commit

Permalink
fix(derive): add revm-primitives as a dependency for kzg points
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 17, 2024
1 parent 57635f6 commit 5773554
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 4,274 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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, optional = true }
revm-primitives = { version = "3.1", default-features = false, optional = true }

# `serde` feature dependencies
serde = { version = "1.0.197", default-features = false, features = ["derive"], optional = true }
Expand All @@ -49,11 +50,14 @@ default = ["serde", "k256"]
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:revm-primitives",
"dep:c-kzg",
"dep:sha2",
"dep:alloy-provider",
"dep:alloy-transport-http",
"dep:reqwest",
"alloy-consensus/serde",
"c-kzg/serde",
"revm-primitives/serde",
"revm-primitives/c-kzg",
]
52 changes: 1 addition & 51 deletions crates/derive/src/online/blob_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contains an online implementation of the [BlobProvider] trait.
use crate::{
online::BeaconClient,
online::{blobs_from_sidecars, BeaconClient},
traits::BlobProvider,
types::{APIBlobSidecar, Blob, BlobSidecar, BlockInfo, IndexedBlobHash},
};
Expand Down Expand Up @@ -167,56 +167,6 @@ impl<T: Provider<Http<Client>>, B: BeaconClient, S: SlotDerivation> OnlineBlobPr
}
}

/// Constructs a list of [Blob]s from [BlobSidecar]s and the specified [IndexedBlobHash]es.
pub(crate) fn blobs_from_sidecars(
sidecars: &[BlobSidecar],
hashes: &[IndexedBlobHash],
) -> anyhow::Result<Vec<Blob>> {
if sidecars.len() != hashes.len() {
return Err(anyhow::anyhow!(
"blob sidecars and hashes length mismatch, {} != {}",
sidecars.len(),
hashes.len()
));
}

let mut blobs = Vec::with_capacity(sidecars.len());
for (i, sidecar) in sidecars.iter().enumerate() {
let hash = hashes.get(i).ok_or(anyhow::anyhow!("failed to get blob hash"))?;
if sidecar.index as usize != hash.index {
return Err(anyhow::anyhow!(
"invalid sidecar ordering, blob hash index {} does not match sidecar index {}",
hash.index,
sidecar.index
));
}

// Ensure the blob's kzg commitment hashes to the expected value.
if sidecar.to_kzg_versioned_hash() != hash.hash {
return Err(anyhow::anyhow!(
"expected hash {} for blob at index {} but got {:#?}",
hash.hash,
hash.index,
sidecar.to_kzg_versioned_hash()
));
}

// Confirm blob data is valid by verifying its proof against the commitment
match sidecar.verify_blob_kzg_proof() {
Ok(true) => (),
Ok(false) => {
return Err(anyhow::anyhow!("blob at index {} failed verification", i));
}
Err(e) => {
return Err(anyhow::anyhow!("blob at index {} failed verification: {}", i, e));
}
}

blobs.push(sidecar.blob);
}
Ok(blobs)
}

/// Minimal slot derivation implementation.
#[derive(Debug, Default)]
pub struct SimpleSlotDerivation;
Expand Down
3 changes: 3 additions & 0 deletions crates/derive/src/online/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ pub use alloy_providers::{AlloyChainProvider, AlloyL2ChainProvider};

mod blob_provider;
pub use blob_provider::{OnlineBlobProvider, SimpleSlotDerivation};

mod utils;
pub(crate) use utils::blobs_from_sidecars;
68 changes: 68 additions & 0 deletions crates/derive/src/online/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Contains utilities for online providers.
use crate::types::{Blob, BlobSidecar, IndexedBlobHash};
use alloc::vec::Vec;

/// Constructs a list of [Blob]s from [BlobSidecar]s and the specified [IndexedBlobHash]es.
pub(crate) fn blobs_from_sidecars(
sidecars: &[BlobSidecar],
hashes: &[IndexedBlobHash],
) -> anyhow::Result<Vec<Blob>> {
if sidecars.len() != hashes.len() {
return Err(anyhow::anyhow!(
"blob sidecars and hashes length mismatch, {} != {}",
sidecars.len(),
hashes.len()
));
}

let mut blobs = Vec::with_capacity(sidecars.len());
for (i, sidecar) in sidecars.iter().enumerate() {
let hash = hashes.get(i).ok_or(anyhow::anyhow!("failed to get blob hash"))?;
if sidecar.index as usize != hash.index {
return Err(anyhow::anyhow!(
"invalid sidecar ordering, blob hash index {} does not match sidecar index {}",
hash.index,
sidecar.index
));
}

// Ensure the blob's kzg commitment hashes to the expected value.
if sidecar.to_kzg_versioned_hash() != hash.hash {
return Err(anyhow::anyhow!(
"expected hash {} for blob at index {} but got {:#?}",
hash.hash,
hash.index,
sidecar.to_kzg_versioned_hash()
));
}

// Confirm blob data is valid by verifying its proof against the commitment
match sidecar.verify_blob_kzg_proof() {
Ok(true) => (),
Ok(false) => {
return Err(anyhow::anyhow!("blob at index {} failed verification", i));
}
Err(e) => {
return Err(anyhow::anyhow!("blob at index {} failed verification: {}", i, e));
}
}

blobs.push(sidecar.blob);
}
Ok(blobs)
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::{string::ToString, vec};

#[test]
fn test_blobs_from_sidecars_length_mismatch() {
let sidecars = vec![BlobSidecar::default()];
let hashes = vec![IndexedBlobHash::default(), IndexedBlobHash::default()];
let err = blobs_from_sidecars(&sidecars, &hashes).unwrap_err();
assert_eq!(err.to_string(), "blob sidecars and hashes length mismatch, 1 != 2");
}
}
Binary file removed crates/derive/src/types/kzg/g1_points.bin
Binary file not shown.
Binary file removed crates/derive/src/types/kzg/g2_points.bin
Binary file not shown.
Loading

0 comments on commit 5773554

Please sign in to comment.