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

feature: beacon sidecar iterator #718

Merged
merged 4 commits into from
May 10, 2024
Merged
Changes from all 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
53 changes: 49 additions & 4 deletions crates/rpc-types-beacon/src/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
use crate::header::Header;
use alloy_eips::eip4844::{Blob, Bytes48};
use alloy_eips::eip4844::{Blob, BlobTransactionSidecar, Bytes48};
use alloy_primitives::{Bytes, B256};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::vec::IntoIter;

/// Bundle of blobs for a given block
#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BeaconBlobBundle {
/// Vec of individual blob data
pub data: Vec<BlobData>,
data: Vec<BlobData>,
}

/// Yields an iterator for BlobData
impl IntoIterator for BeaconBlobBundle {
type Item = BlobData;
type IntoIter = IntoIter<BlobData>;

fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}

/// Intermediate type for BlobTransactionSidecar matching
#[derive(Debug, Clone)]
pub struct SidecarIterator {
JackG-eth marked this conversation as resolved.
Show resolved Hide resolved
JackG-eth marked this conversation as resolved.
Show resolved Hide resolved
pub iter: IntoIter<BlobData>,
JackG-eth marked this conversation as resolved.
Show resolved Hide resolved
}

impl Iterator for SidecarIterator {
type Item = BlobData;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

impl SidecarIterator {
pub fn new(bundle: BeaconBlobBundle) -> Self {
SidecarIterator { iter: bundle.into_iter() }
}

/// Returns a BlobTransactionSidecar of len num_hashes.
pub fn next_sidecar(&mut self, num_hashes: usize) -> Option<BlobTransactionSidecar> {
let mut blobs = Vec::with_capacity(num_hashes);
let mut commitments = Vec::with_capacity(num_hashes);
let mut proofs = Vec::with_capacity(num_hashes);
for _ in 0..num_hashes {
let next = self.next()?;
blobs.push(*next.blob);
commitments.push(next.kzg_commitment);
proofs.push(next.kzg_proof);
}
Some(BlobTransactionSidecar { blobs, commitments, proofs })
}
}

/// Individual Blob data that belongs to a 4844 transaction.
Expand All @@ -32,7 +77,7 @@ pub struct BlobData {
pub kzg_commitment_inclusion_proof: Vec<B256>,
}

// Helper function to deserialize boxed blobs
/// Helper function to deserialize boxed blobs
fn deserialize_blob<'de, D>(deserializer: D) -> Result<Box<Blob>, D::Error>
where
D: serde::de::Deserializer<'de>,
Expand All @@ -48,7 +93,7 @@ where
mod tests {
use super::*;

// Should deserialise json containing 6 blobs
/// Should deserialise json containing 6 blobs
#[test]
fn serde_sidecar_bundle() {
let s = include_str!("examples/sidecar.json");
Expand Down
Loading