Skip to content

Commit

Permalink
Improve test assertions on SseBlobSidecars.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Oct 10, 2023
1 parent 934a3fc commit 39cc2fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
20 changes: 6 additions & 14 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2811,13 +2811,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {

if let Some(event_handler) = self.event_handler.as_ref() {
if event_handler.has_blob_sidecar_subscribers() {
event_handler.register(EventKind::BlobSidecar(SseBlobSidecar {
block_root: blob.block_root(),
index: blob.index(),
slot: blob.slot(),
kzg_commitment: blob.kzg_commitment(),
versioned_hash: blob.kzg_commitment().calculate_versioned_hash(),
}));
event_handler.register(EventKind::BlobSidecar(SseBlobSidecar::from_blob_sidecar(
blob.as_blob(),
)));
}
}

Expand Down Expand Up @@ -2848,13 +2844,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if let Some(event_handler) = self.event_handler.as_ref() {
if event_handler.has_blob_sidecar_subscribers() {
for blob in blobs.iter().filter_map(|maybe_blob| maybe_blob.as_ref()) {
event_handler.register(EventKind::BlobSidecar(SseBlobSidecar {
block_root: blob.block_root,
index: blob.index,
slot: blob.slot,
kzg_commitment: blob.kzg_commitment,
versioned_hash: blob.kzg_commitment.calculate_versioned_hash(),
}));
event_handler.register(EventKind::BlobSidecar(
SseBlobSidecar::from_blob_sidecar(blob),
));
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions beacon_node/beacon_chain/tests/events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use beacon_chain::blob_verification::GossipVerifiedBlob;
use beacon_chain::test_utils::BeaconChainHarness;
use bls::Signature;
use eth2::types::EventKind;
use eth2::types::{EventKind, SseBlobSidecar};
use rand::rngs::StdRng;
use rand::SeedableRng;
use std::marker::PhantomData;
Expand Down Expand Up @@ -37,14 +37,16 @@ async fn blob_sidecar_event_on_process_gossip_blob() {
_phantom: PhantomData,
};
let gossip_verified_blob = GossipVerifiedBlob::__assumed_valid(signed_sidecar);
let expected_sse_blobs = SseBlobSidecar::from_blob_sidecar(gossip_verified_blob.as_blob());

let _ = harness
.chain
.process_gossip_blob(gossip_verified_blob)
.await
.unwrap();

let sidecar_event = blob_event_receiver.try_recv().unwrap();
assert!(matches!(sidecar_event, EventKind::BlobSidecar(..)));
assert_eq!(sidecar_event, EventKind::BlobSidecar(expected_sse_blobs));
}

/// Verifies that a blob event is emitted when blobs are received via RPC.
Expand Down Expand Up @@ -73,18 +75,25 @@ async fn blob_sidecar_event_on_process_rpc_blobs() {
index: 1,
..BlobSidecar::random_valid(&mut rng, kzg).unwrap()
});
let blobs = FixedBlobSidecarList::from(vec![Some(blob_1.clone()), Some(blob_2)]);
let blobs = FixedBlobSidecarList::from(vec![Some(blob_1.clone()), Some(blob_2.clone())]);
let expected_sse_blobs = vec![
SseBlobSidecar::from_blob_sidecar(blob_1.as_ref()),
SseBlobSidecar::from_blob_sidecar(blob_2.as_ref()),
];

let _ = harness
.chain
.process_rpc_blobs(blob_1.slot, blob_1.block_root, blobs)
.await
.unwrap();

let mut events: Vec<EventKind<E>> = vec![];
let mut sse_blobs: Vec<SseBlobSidecar> = vec![];
while let Ok(sidecar_event) = blob_event_receiver.try_recv() {
assert!(matches!(sidecar_event, EventKind::BlobSidecar(..)));
events.push(sidecar_event);
if let EventKind::BlobSidecar(sse_blob_sidecar) = sidecar_event {
sse_blobs.push(sse_blob_sidecar);
} else {
panic!("`BlobSidecar` event kind expected.");
}
}
assert!(events.len() == 2);
assert_eq!(sse_blobs, expected_sse_blobs);
}
12 changes: 12 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,18 @@ pub struct SseBlobSidecar {
pub versioned_hash: VersionedHash,
}

impl SseBlobSidecar {
pub fn from_blob_sidecar<E: EthSpec>(blob_sidecar: &BlobSidecar<E>) -> SseBlobSidecar {
SseBlobSidecar {
block_root: blob_sidecar.block_root,
index: blob_sidecar.index,
slot: blob_sidecar.slot,
kzg_commitment: blob_sidecar.kzg_commitment,
versioned_hash: blob_sidecar.kzg_commitment.calculate_versioned_hash(),
}
}
}

#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct SseFinalizedCheckpoint {
pub block: Hash256,
Expand Down

0 comments on commit 39cc2fe

Please sign in to comment.