Skip to content

Commit

Permalink
chore: swap BlockHashOrNumber alias and struct name (#1270)
Browse files Browse the repository at this point in the history
* chore: swap struct and alias name for BlockHashOrNumber

* chore: update docs
  • Loading branch information
PanGan21 authored Sep 11, 2024
1 parent 41b2fd2 commit 8546ecf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
56 changes: 28 additions & 28 deletions crates/eips/src/eip1898.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,13 @@ impl From<BlockNumberOrTag> for BlockId {
}
}

impl From<BlockHashOrNumber> for BlockId {
fn from(block: BlockHashOrNumber) -> Self {
impl From<HashOrNumber> for BlockId {
fn from(block: HashOrNumber) -> Self {
match block {
BlockHashOrNumber::Hash(hash) => {
HashOrNumber::Hash(hash) => {
Self::Hash(RpcBlockHash { block_hash: hash, require_canonical: None })
}
BlockHashOrNumber::Number(num) => Self::Number(BlockNumberOrTag::Number(num)),
HashOrNumber::Number(num) => Self::Number(BlockNumberOrTag::Number(num)),
}
}
}
Expand Down Expand Up @@ -626,11 +626,11 @@ impl NumHash {
(self.number, self.hash)
}

/// Returns whether or not the block matches the given [BlockHashOrNumber].
pub fn matches_block_or_num(&self, block: &BlockHashOrNumber) -> bool {
/// Returns whether or not the block matches the given [HashOrNumber].
pub fn matches_block_or_num(&self, block: &HashOrNumber) -> bool {
match block {
BlockHashOrNumber::Hash(hash) => self.hash == *hash,
BlockHashOrNumber::Number(number) => self.number == *number,
HashOrNumber::Hash(hash) => self.hash == *hash,
HashOrNumber::Number(number) => self.number == *number,
}
}
}
Expand All @@ -647,24 +647,24 @@ impl From<(B256, u64)> for NumHash {
}
}

/// Either a block hash _or_ a block number
/// Either a hash _or_ a block number
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub enum BlockHashOrNumber {
/// A block hash
pub enum HashOrNumber {
/// The hash
Hash(B256),
/// A block number
/// The number
Number(u64),
}

/// Either a hash _or_ a number
pub type HashOrNumber = BlockHashOrNumber;
/// A block hash _or_ a block number
pub type BlockHashOrNumber = HashOrNumber;

// === impl BlockHashOrNumber ===
// === impl HashOrNumber ===

impl BlockHashOrNumber {
/// Returns the block number if it is a [`BlockHashOrNumber::Number`].
impl HashOrNumber {
/// Returns the block number if it is a [`HashOrNumber::Number`].
#[inline]
pub const fn as_number(self) -> Option<u64> {
match self {
Expand All @@ -674,32 +674,32 @@ impl BlockHashOrNumber {
}
}

impl From<B256> for BlockHashOrNumber {
impl From<B256> for HashOrNumber {
fn from(value: B256) -> Self {
Self::Hash(value)
}
}

impl From<u64> for BlockHashOrNumber {
impl From<u64> for HashOrNumber {
fn from(value: u64) -> Self {
Self::Number(value)
}
}

impl From<U64> for BlockHashOrNumber {
impl From<U64> for HashOrNumber {
fn from(value: U64) -> Self {
value.to::<u64>().into()
}
}

impl From<RpcBlockHash> for BlockHashOrNumber {
impl From<RpcBlockHash> for HashOrNumber {
fn from(value: RpcBlockHash) -> Self {
Self::Hash(value.into())
}
}

/// Allows for RLP encoding of either a block hash or block number
impl Encodable for BlockHashOrNumber {
/// Allows for RLP encoding of either a hash or a number
impl Encodable for HashOrNumber {
fn encode(&self, out: &mut dyn bytes::BufMut) {
match self {
Self::Hash(block_hash) => block_hash.encode(out),
Expand All @@ -714,8 +714,8 @@ impl Encodable for BlockHashOrNumber {
}
}

/// Allows for RLP decoding of a block hash or block number
impl Decodable for BlockHashOrNumber {
/// Allows for RLP decoding of a hash or number
impl Decodable for HashOrNumber {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let header: u8 = *buf.first().ok_or(RlpError::InputTooShort)?;
// if the byte string is exactly 32 bytes, decode it into a Hash
Expand All @@ -736,7 +736,7 @@ impl Decodable for BlockHashOrNumber {
}
}

impl fmt::Display for BlockHashOrNumber {
impl fmt::Display for HashOrNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Hash(hash) => write!(f, "{}", hash),
Expand All @@ -745,7 +745,7 @@ impl fmt::Display for BlockHashOrNumber {
}
}

/// Error thrown when parsing a [BlockHashOrNumber] from a string.
/// Error thrown when parsing a [HashOrNumber] from a string.
#[derive(Debug)]
pub struct ParseBlockHashOrNumberError {
input: alloc::string::String,
Expand All @@ -766,7 +766,7 @@ impl fmt::Display for ParseBlockHashOrNumberError {
#[cfg(feature = "std")]
impl std::error::Error for ParseBlockHashOrNumberError {}

impl FromStr for BlockHashOrNumber {
impl FromStr for HashOrNumber {
type Err = ParseBlockHashOrNumberError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
3 changes: 2 additions & 1 deletion crates/eips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub use eip1559::calc_next_block_base_fee;

pub mod eip1898;
pub use eip1898::{
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, NumHash, RpcBlockHash,
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, HashOrNumber, NumHash,
RpcBlockHash,
};

pub mod eip2718;
Expand Down

0 comments on commit 8546ecf

Please sign in to comment.