Skip to content

Commit

Permalink
refactor(unionlabs-primitives): rename Hash -> FixedBytes (#3435)
Browse files Browse the repository at this point in the history
finishes #3418
  • Loading branch information
benluelo authored Jan 2, 2025
2 parents 602ccb1 + 166a0e3 commit 8ee8bfd
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 116 deletions.
6 changes: 3 additions & 3 deletions lib/beacon-api-types/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use unionlabs::{
bls::BlsSignature,
primitives::{Hash, H256},
primitives::{FixedBytes, H256},
};
#[cfg(feature = "ssz")]
use {
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct BeaconBlockBodySsz<
pub sync_aggregate: SyncAggregateSsz<C>,
pub execution_payload: ExecutionPayloadSsz<C>,
pub bls_to_execution_changes: List<SignedBlsToExecutionChange, C::MAX_BLS_TO_EXECUTION_CHANGES>,
pub blob_kzg_commitments: List<Hash<48>, C::MAX_BLOB_COMMITMENTS_PER_BLOCK>,
pub blob_kzg_commitments: List<FixedBytes<48>, C::MAX_BLOB_COMMITMENTS_PER_BLOCK>,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -72,5 +72,5 @@ pub struct BeaconBlockBody {
pub sync_aggregate: SyncAggregate,
pub execution_payload: ExecutionPayload,
pub bls_to_execution_changes: Vec<SignedBlsToExecutionChange>,
pub blob_kzg_commitments: Vec<Hash<48>>,
pub blob_kzg_commitments: Vec<FixedBytes<48>>,
}
54 changes: 27 additions & 27 deletions lib/beacon-api-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use core::{

use hex_literal::hex;
use typenum::{NonZero, Unsigned};
use unionlabs::primitives::Hash;
use unionlabs::primitives::FixedBytes;

pub use crate::{
attestation::Attestation, attestation_data::AttestationData,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub use crate::{
serde(transparent)
)]
#[cfg_attr(feature = "ssz", derive(ssz::Ssz), ssz(transparent))]
pub struct Version(pub Hash<4>);
pub struct Version(pub FixedBytes<4>);

/// <https://github.com/ethereum/consensus-specs/blob/087e7378b44f327cdad4549304fc308613b780c3/specs/phase0/beacon-chain.md#custom-types>
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
Expand All @@ -106,7 +106,7 @@ pub struct Version(pub Hash<4>);
serde(transparent)
)]
#[cfg_attr(feature = "ssz", derive(ssz::Ssz), ssz(transparent))]
pub struct DomainType(pub Hash<4>);
pub struct DomainType(pub FixedBytes<4>);

/// <https://github.com/ethereum/consensus-specs/blob/087e7378b44f327cdad4549304fc308613b780c3/specs/phase0/beacon-chain.md#custom-types>
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
Expand All @@ -116,7 +116,7 @@ pub struct DomainType(pub Hash<4>);
serde(transparent)
)]
#[cfg_attr(feature = "ssz", derive(ssz::Ssz), ssz(transparent))]
pub struct ForkDigest(pub Hash<4>);
pub struct ForkDigest(pub FixedBytes<4>);

/// <https://github.com/ethereum/consensus-specs/blob/087e7378b44f327cdad4549304fc308613b780c3/specs/phase0/beacon-chain.md#custom-types>
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
Expand All @@ -126,23 +126,23 @@ pub struct ForkDigest(pub Hash<4>);
serde(transparent)
)]
#[cfg_attr(feature = "ssz", derive(ssz::Ssz), ssz(transparent))]
pub struct Domain(pub Hash<32>);
pub struct Domain(pub FixedBytes<32>);

/// <https://github.com/ethereum/consensus-specs/blob/087e7378b44f327cdad4549304fc308613b780c3/specs/phase0/beacon-chain.md#domain-types>
#[rustfmt::skip]
impl DomainType {
pub const BEACON_PROPOSER: Self = Self(Hash::new(hex!("00000000")));
pub const BEACON_ATTESTER: Self = Self(Hash::new(hex!("01000000")));
pub const RANDAO: Self = Self(Hash::new(hex!("02000000")));
pub const DEPOSIT: Self = Self(Hash::new(hex!("03000000")));
pub const VOLUNTARY_EXIT: Self = Self(Hash::new(hex!("04000000")));
pub const SELECTION_PROOF: Self = Self(Hash::new(hex!("05000000")));
pub const AGGREGATE_AND_PROOF: Self = Self(Hash::new(hex!("06000000")));
pub const SYNC_COMMITTEE: Self = Self(Hash::new(hex!("07000000")));
pub const SYNC_COMMITTEE_SELECTION_PROOF: Self = Self(Hash::new(hex!("08000000")));
pub const CONTRIBUTION_AND_PROOF: Self = Self(Hash::new(hex!("09000000")));
pub const BLS_TO_EXECUTION_CHANGE: Self = Self(Hash::new(hex!("0A000000")));
pub const APPLICATION_MASK: Self = Self(Hash::new(hex!("00000001")));
pub const BEACON_PROPOSER: Self = Self(FixedBytes::new(hex!("00000000")));
pub const BEACON_ATTESTER: Self = Self(FixedBytes::new(hex!("01000000")));
pub const RANDAO: Self = Self(FixedBytes::new(hex!("02000000")));
pub const DEPOSIT: Self = Self(FixedBytes::new(hex!("03000000")));
pub const VOLUNTARY_EXIT: Self = Self(FixedBytes::new(hex!("04000000")));
pub const SELECTION_PROOF: Self = Self(FixedBytes::new(hex!("05000000")));
pub const AGGREGATE_AND_PROOF: Self = Self(FixedBytes::new(hex!("06000000")));
pub const SYNC_COMMITTEE: Self = Self(FixedBytes::new(hex!("07000000")));
pub const SYNC_COMMITTEE_SELECTION_PROOF: Self = Self(FixedBytes::new(hex!("08000000")));
pub const CONTRIBUTION_AND_PROOF: Self = Self(FixedBytes::new(hex!("09000000")));
pub const BLS_TO_EXECUTION_CHANGE: Self = Self(FixedBytes::new(hex!("0A000000")));
pub const APPLICATION_MASK: Self = Self(FixedBytes::new(hex!("00000001")));
}

/// Minimal config.
Expand Down Expand Up @@ -412,24 +412,24 @@ pub struct Config {
pub const MAINNET: Config = Config {
preset: preset::MAINNET,
fork_parameters: ForkParameters {
genesis_fork_version: Version(Hash::new([0, 0, 0, 0])),
genesis_fork_version: Version(FixedBytes::new([0, 0, 0, 0])),
genesis_slot: 0,

altair: Fork {
version: Version(Hash::new([1, 0, 0, 0])),
version: Version(FixedBytes::new([1, 0, 0, 0])),
epoch: 74_240,
},
bellatrix: Fork {
version: Version(Hash::new([2, 0, 0, 0])),
version: Version(FixedBytes::new([2, 0, 0, 0])),
epoch: 144_896,
},
capella: Fork {
version: Version(Hash::new([3, 0, 0, 0])),
version: Version(FixedBytes::new([3, 0, 0, 0])),
epoch: 194_048,
},
// TODO: enabled march 13th 2024
deneb: Fork {
version: Version(Hash::new([4, 0, 0, 0])),
version: Version(FixedBytes::new([4, 0, 0, 0])),
epoch: u64::MAX,
},
},
Expand All @@ -439,27 +439,27 @@ pub const MAINNET: Config = Config {
pub const MINIMAL: Config = Config {
preset: preset::MINIMAL,
fork_parameters: ForkParameters {
genesis_fork_version: Version(Hash::new([0, 0, 0, 1])),
genesis_fork_version: Version(FixedBytes::new([0, 0, 0, 1])),
genesis_slot: 0,

altair: Fork {
version: Version(Hash::new([1, 0, 0, 1])),
version: Version(FixedBytes::new([1, 0, 0, 1])),
epoch: 0,
},

bellatrix: Fork {
version: Version(Hash::new([2, 0, 0, 1])),
version: Version(FixedBytes::new([2, 0, 0, 1])),
epoch: 0,
},

capella: Fork {
version: Version(Hash::new([3, 0, 0, 1])),
version: Version(FixedBytes::new([3, 0, 0, 1])),
epoch: 0,
},

// NOTE: dummy data
deneb: Fork {
version: Version(Hash::new([4, 0, 0, 1])),
version: Version(FixedBytes::new([4, 0, 0, 1])),
epoch: 0,
},
},
Expand Down
2 changes: 1 addition & 1 deletion lib/ssz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ macro_rules! option_unwrap {
pub(crate) use option_unwrap;

impl<const BYTES: usize, E: unionlabs_primitives::encoding::Encoding> Ssz
for unionlabs_primitives::Hash<BYTES, E>
for unionlabs_primitives::FixedBytes<BYTES, E>
where
[u8; BYTES]: Ssz,
{
Expand Down
10 changes: 5 additions & 5 deletions lib/unionlabs-primitives/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{cmp::Ordering, fmt, marker::PhantomData, ops::Deref, str::FromStr};

use crate::{
encoding::{Encoding, HexPrefixed},
hash::FixedBytesError,
fixed_bytes::FixedBytesError,
};

pub struct Bytes<E: Encoding = HexPrefixed> {
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<E: Encoding> IntoIterator for Bytes<E> {
}

impl<EBytes: Encoding, EHash: Encoding, const BYTES: usize> TryFrom<Bytes<EBytes>>
for crate::hash::Hash<BYTES, EHash>
for crate::fixed_bytes::FixedBytes<BYTES, EHash>
{
type Error = FixedBytesError;

Expand Down Expand Up @@ -252,10 +252,10 @@ impl<E: Encoding, const N: usize> From<[u8; N]> for Bytes<E> {
}
}

impl<EBytes: Encoding, EHash: Encoding, const N: usize> From<crate::hash::Hash<N, EHash>>
for Bytes<EBytes>
impl<EBytes: Encoding, EHash: Encoding, const N: usize>
From<crate::fixed_bytes::FixedBytes<N, EHash>> for Bytes<EBytes>
{
fn from(value: crate::hash::Hash<N, EHash>) -> Self {
fn from(value: crate::fixed_bytes::FixedBytes<N, EHash>) -> Self {
Self::new(value.get().as_slice().to_owned())
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/unionlabs-primitives/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<primitive_types::H160> for crate::H160 {

#[cfg(feature = "generic-array-compat")]
impl<E: crate::encoding::Encoding, const BYTES: usize>
From<generic_array::GenericArray<u8, typenum::U<BYTES>>> for crate::Hash<BYTES, E>
From<generic_array::GenericArray<u8, typenum::U<BYTES>>> for crate::FixedBytes<BYTES, E>
where
typenum::Const<BYTES>: typenum::ToUInt<Output: generic_array::ArrayLength<u8>>,
{
Expand All @@ -42,12 +42,12 @@ where
}

#[cfg(feature = "generic-array-compat")]
impl<E: crate::encoding::Encoding, const BYTES: usize> From<crate::Hash<BYTES, E>>
impl<E: crate::encoding::Encoding, const BYTES: usize> From<crate::FixedBytes<BYTES, E>>
for generic_array::GenericArray<u8, typenum::U<BYTES>>
where
typenum::Const<BYTES>: typenum::ToUInt<Output: generic_array::ArrayLength<u8>>,
{
fn from(arr: crate::Hash<BYTES, E>) -> Self {
fn from(arr: crate::FixedBytes<BYTES, E>) -> Self {
generic_array::GenericArray::<u8, typenum::U<BYTES>>::from_slice(arr.get()).to_owned()
}
}
Expand All @@ -56,7 +56,7 @@ where
impl<EBytes: crate::encoding::Encoding> TryFrom<crate::Bytes<EBytes>>
for alloy_primitives::Address
{
type Error = crate::hash::FixedBytesError;
type Error = crate::fixed_bytes::FixedBytesError;

fn try_from(value: crate::Bytes<EBytes>) -> Result<Self, Self::Error> {
<crate::H160>::try_from(value).map(Self::from)
Expand Down Expand Up @@ -86,18 +86,18 @@ impl<E: crate::encoding::Encoding> From<crate::H160<E>> for alloy_primitives::Ad

#[cfg(feature = "alloy-primitives-compat")]
impl<E: crate::encoding::Encoding, const BYTES: usize> From<alloy_primitives::FixedBytes<BYTES>>
for crate::Hash<BYTES, E>
for crate::FixedBytes<BYTES, E>
{
fn from(value: alloy_primitives::FixedBytes<BYTES>) -> Self {
value.0.into()
}
}

#[cfg(feature = "alloy-primitives-compat")]
impl<E: crate::encoding::Encoding, const BYTES: usize> From<crate::Hash<BYTES, E>>
impl<E: crate::encoding::Encoding, const BYTES: usize> From<crate::FixedBytes<BYTES, E>>
for alloy_primitives::FixedBytes<BYTES>
{
fn from(value: crate::Hash<BYTES, E>) -> Self {
fn from(value: crate::FixedBytes<BYTES, E>) -> Self {
value.get().into()
}
}
Expand Down
Loading

0 comments on commit 8ee8bfd

Please sign in to comment.