impl hash correctly for the blob wrapper

This commit is contained in:
realbigsean 2022-12-23 12:52:26 -05:00
parent 5e11edc612
commit d09523802b
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
2 changed files with 8 additions and 15 deletions

View File

@ -1,3 +1,4 @@
use derivative::Derivative;
use crate::test_utils::TestRandom;
use crate::{Blob, EthSpec, Hash256, SignedBeaconBlock, SignedRoot, Slot};
use kzg::KzgProof;
@ -10,9 +11,10 @@ use tree_hash_derive::TreeHash;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(
Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, PartialEq, Default, TestRandom,
Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, Default, TestRandom, Derivative
)]
#[serde(bound = "T: EthSpec")]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
pub struct BlobsSidecar<T: EthSpec> {
pub beacon_block_root: Hash256,
pub beacon_block_slot: Slot,

View File

@ -5,6 +5,7 @@ use ssz::{Decode, DecodeError};
use ssz_derive::{Decode, Encode};
use std::sync::Arc;
use tree_hash_derive::TreeHash;
use derivative::Derivative;
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, PartialEq)]
#[serde(bound = "T: EthSpec")]
@ -13,8 +14,8 @@ pub struct SignedBeaconBlockAndBlobsSidecarDecode<T: EthSpec> {
pub blobs_sidecar: BlobsSidecar<T>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash, PartialEq)]
#[serde(bound = "T: EthSpec")]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash, Derivative)]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
pub struct SignedBeaconBlockAndBlobsSidecar<T: EthSpec> {
pub beacon_block: Arc<SignedBeaconBlock<T>>,
pub blobs_sidecar: Arc<BlobsSidecar<T>>,
@ -34,7 +35,8 @@ impl<T: EthSpec> SignedBeaconBlockAndBlobsSidecar<T> {
}
/// A wrapper over a [`SignedBeaconBlock`] or a [`SignedBeaconBlockAndBlobsSidecar`].
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Derivative)]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
pub enum BlockWrapper<T: EthSpec> {
Block(Arc<SignedBeaconBlock<T>>),
BlockAndBlob(SignedBeaconBlockAndBlobsSidecar<T>),
@ -105,17 +107,6 @@ impl<T: EthSpec> BlockWrapper<T> {
}
}
// FIXME(sean): probably needs to be changed. This is needed because SignedBeaconBlockAndBlobsSidecar
// does not implement Hash
impl<T: EthSpec> std::hash::Hash for BlockWrapper<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
BlockWrapper::Block(block) => block.hash(state),
BlockWrapper::BlockAndBlob(block_and_blob) => block_and_blob.beacon_block.hash(state),
}
}
}
impl<T: EthSpec> From<SignedBeaconBlock<T>> for BlockWrapper<T> {
fn from(block: SignedBeaconBlock<T>) -> Self {
BlockWrapper::Block(Arc::new(block))