alloy_eips/
eip1898.rs

1//! [EIP-1898]: https://eips.ethereum.org/EIPS/eip-1898
2
3use alloy_primitives::{hex::FromHexError, ruint::ParseError, BlockHash, B256, U64};
4use alloy_rlp::{bytes, Decodable, Encodable, Error as RlpError};
5use core::{
6    fmt::{self, Formatter},
7    num::ParseIntError,
8    str::FromStr,
9};
10
11/// A helper struct to store the block number/hash and its parent hash.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
15#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
16pub struct BlockWithParent {
17    /// Parent hash.
18    pub parent: B256,
19    /// Block number/hash.
20    pub block: BlockNumHash,
21}
22
23impl BlockWithParent {
24    /// Creates a new [`BlockWithParent`] instance.
25    pub const fn new(parent: B256, block: BlockNumHash) -> Self {
26        Self { parent, block }
27    }
28}
29
30/// A block hash which may have a boolean `requireCanonical` field.
31///
32/// - If false, a RPC call should raise if a block matching the hash is not found.
33/// - If true, a RPC call should additionally raise if the block is not in the canonical chain.
34///
35/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md#specification>
36#[derive(Clone, Copy, PartialEq, Eq)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize))]
38#[cfg_attr(feature = "serde", serde(rename = "camelCase"))]
39pub struct RpcBlockHash {
40    /// A block hash
41    pub block_hash: BlockHash,
42    /// Whether the block must be a canonical block
43    pub require_canonical: Option<bool>,
44}
45
46impl RpcBlockHash {
47    /// Returns a [`RpcBlockHash`] from a [`B256`].
48    #[doc(alias = "from_block_hash")]
49    pub const fn from_hash(block_hash: B256, require_canonical: Option<bool>) -> Self {
50        Self { block_hash, require_canonical }
51    }
52}
53
54impl From<B256> for RpcBlockHash {
55    fn from(value: B256) -> Self {
56        Self::from_hash(value, None)
57    }
58}
59
60impl From<RpcBlockHash> for B256 {
61    fn from(value: RpcBlockHash) -> Self {
62        value.block_hash
63    }
64}
65
66impl AsRef<B256> for RpcBlockHash {
67    fn as_ref(&self) -> &B256 {
68        &self.block_hash
69    }
70}
71
72impl fmt::Display for RpcBlockHash {
73    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
74        let Self { block_hash, require_canonical } = self;
75        if *require_canonical == Some(true) {
76            f.write_str("canonical ")?;
77        }
78        write!(f, "hash {block_hash}")
79    }
80}
81
82impl fmt::Debug for RpcBlockHash {
83    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
84        match self.require_canonical {
85            Some(require_canonical) => f
86                .debug_struct("RpcBlockHash")
87                .field("block_hash", &self.block_hash)
88                .field("require_canonical", &require_canonical)
89                .finish(),
90            None => fmt::Debug::fmt(&self.block_hash, f),
91        }
92    }
93}
94
95/// A block Number (or tag - "latest", "earliest", "pending")
96///
97/// This enum allows users to specify a block in a flexible manner.
98#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
99pub enum BlockNumberOrTag {
100    /// Latest block
101    #[default]
102    Latest,
103    /// Finalized block accepted as canonical
104    Finalized,
105    /// Safe head block
106    Safe,
107    /// Earliest block (genesis)
108    Earliest,
109    /// Pending block (not yet part of the blockchain)
110    Pending,
111    /// Block by number from canonical chain
112    Number(u64),
113}
114
115impl BlockNumberOrTag {
116    /// Returns the numeric block number if explicitly set
117    pub const fn as_number(&self) -> Option<u64> {
118        match *self {
119            Self::Number(num) => Some(num),
120            _ => None,
121        }
122    }
123
124    /// Returns `true` if a numeric block number is set
125    pub const fn is_number(&self) -> bool {
126        matches!(self, Self::Number(_))
127    }
128
129    /// Returns `true` if it's "latest"
130    pub const fn is_latest(&self) -> bool {
131        matches!(self, Self::Latest)
132    }
133
134    /// Returns `true` if it's "finalized"
135    pub const fn is_finalized(&self) -> bool {
136        matches!(self, Self::Finalized)
137    }
138
139    /// Returns `true` if it's "safe"
140    pub const fn is_safe(&self) -> bool {
141        matches!(self, Self::Safe)
142    }
143
144    /// Returns `true` if it's "pending"
145    pub const fn is_pending(&self) -> bool {
146        matches!(self, Self::Pending)
147    }
148
149    /// Returns `true` if it's "earliest"
150    pub const fn is_earliest(&self) -> bool {
151        matches!(self, Self::Earliest)
152    }
153}
154
155impl From<u64> for BlockNumberOrTag {
156    fn from(num: u64) -> Self {
157        Self::Number(num)
158    }
159}
160
161impl From<U64> for BlockNumberOrTag {
162    fn from(num: U64) -> Self {
163        num.to::<u64>().into()
164    }
165}
166
167#[cfg(feature = "serde")]
168impl serde::Serialize for BlockNumberOrTag {
169    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
170    where
171        S: serde::Serializer,
172    {
173        match *self {
174            Self::Number(x) => serializer.serialize_str(&format!("0x{x:x}")),
175            Self::Latest => serializer.serialize_str("latest"),
176            Self::Finalized => serializer.serialize_str("finalized"),
177            Self::Safe => serializer.serialize_str("safe"),
178            Self::Earliest => serializer.serialize_str("earliest"),
179            Self::Pending => serializer.serialize_str("pending"),
180        }
181    }
182}
183
184#[cfg(feature = "serde")]
185impl<'de> serde::Deserialize<'de> for BlockNumberOrTag {
186    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
187    where
188        D: serde::Deserializer<'de>,
189    {
190        let s = alloc::string::String::deserialize(deserializer)?;
191        s.parse().map_err(serde::de::Error::custom)
192    }
193}
194
195impl FromStr for BlockNumberOrTag {
196    type Err = ParseBlockNumberError;
197
198    fn from_str(s: &str) -> Result<Self, Self::Err> {
199        Ok(match s.to_lowercase().as_str() {
200            "latest" => Self::Latest,
201            "finalized" => Self::Finalized,
202            "safe" => Self::Safe,
203            "earliest" => Self::Earliest,
204            "pending" => Self::Pending,
205            s => {
206                if let Some(hex_val) = s.strip_prefix("0x") {
207                    u64::from_str_radix(hex_val, 16)?.into()
208                } else {
209                    return Err(HexStringMissingPrefixError::default().into());
210                }
211            }
212        })
213    }
214}
215
216impl fmt::Display for BlockNumberOrTag {
217    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218        match *self {
219            Self::Number(x) => write!(f, "0x{x:x}"),
220            Self::Latest => f.pad("latest"),
221            Self::Finalized => f.pad("finalized"),
222            Self::Safe => f.pad("safe"),
223            Self::Earliest => f.pad("earliest"),
224            Self::Pending => f.pad("pending"),
225        }
226    }
227}
228
229impl fmt::Debug for BlockNumberOrTag {
230    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231        fmt::Display::fmt(self, f)
232    }
233}
234
235/// This is a helper type to allow for lenient parsing of block numbers.
236///
237/// The eth json-rpc spec requires quantities to be hex encoded, which [`BlockNumberOrTag`] strictly
238/// enforces.
239///
240/// This type can be used if you want to allow for lenient parsing of block numbers.
241#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
242#[cfg_attr(feature = "serde", derive(serde::Serialize))]
243#[cfg_attr(feature = "serde", serde(transparent))]
244pub struct LenientBlockNumberOrTag(BlockNumberOrTag);
245
246impl LenientBlockNumberOrTag {
247    /// Creates a new [`LenientBlockNumberOrTag`] from a [`BlockNumberOrTag`].
248    pub const fn new(block_number_or_tag: BlockNumberOrTag) -> Self {
249        Self(block_number_or_tag)
250    }
251
252    /// Returns the inner [`BlockNumberOrTag`].
253    pub const fn into_inner(self) -> BlockNumberOrTag {
254        self.0
255    }
256}
257
258impl From<LenientBlockNumberOrTag> for BlockNumberOrTag {
259    fn from(value: LenientBlockNumberOrTag) -> Self {
260        value.0
261    }
262}
263impl From<BlockNumberOrTag> for LenientBlockNumberOrTag {
264    fn from(value: BlockNumberOrTag) -> Self {
265        Self(value)
266    }
267}
268
269impl From<LenientBlockNumberOrTag> for BlockId {
270    fn from(value: LenientBlockNumberOrTag) -> Self {
271        value.into_inner().into()
272    }
273}
274
275/// A module that deserializes either a BlockNumberOrTag, or a simple number.
276#[cfg(feature = "serde")]
277pub mod lenient_block_number_or_tag {
278    use super::{BlockNumberOrTag, LenientBlockNumberOrTag};
279    use core::fmt;
280    use serde::{
281        de::{self, Visitor},
282        Deserialize, Deserializer,
283    };
284
285    /// Following the spec the block parameter is either:
286    ///
287    /// > HEX String - an integer block number
288    /// > Integer - a block number
289    /// > String "latest" - for the latest mined block
290    /// > String "finalized" - for the finalized block
291    /// > String "safe" - for the safe head block
292    /// > String "earliest" for the earliest/genesis block
293    /// > String "pending" - for the pending state/transactions
294    ///
295    /// and with EIP-1898:
296    /// > blockNumber: QUANTITY - a block number
297    /// > blockHash: DATA - a block hash
298    ///
299    /// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md>
300    ///
301    /// EIP-1898 does not all calls that use `BlockNumber` like `eth_getBlockByNumber` and doesn't
302    /// list raw integers as supported.
303    pub fn deserialize<'de, D>(deserializer: D) -> Result<BlockNumberOrTag, D::Error>
304    where
305        D: Deserializer<'de>,
306    {
307        LenientBlockNumberOrTag::deserialize(deserializer).map(Into::into)
308    }
309
310    impl<'de> Deserialize<'de> for LenientBlockNumberOrTag {
311        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
312        where
313            D: Deserializer<'de>,
314        {
315            struct LenientBlockNumberVisitor;
316
317            impl<'de> Visitor<'de> for LenientBlockNumberVisitor {
318                type Value = LenientBlockNumberOrTag;
319
320                fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
321                    formatter.write_str("a block number or tag, or a u64")
322                }
323
324                fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
325                where
326                    E: de::Error,
327                {
328                    Ok(LenientBlockNumberOrTag(v.into()))
329                }
330
331                fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
332                where
333                    E: de::Error,
334                {
335                    // Attempt to parse as a numeric string
336                    if let Ok(num) = v.parse::<u64>() {
337                        return Ok(LenientBlockNumberOrTag(BlockNumberOrTag::Number(num)));
338                    }
339
340                    BlockNumberOrTag::deserialize(de::value::StrDeserializer::<E>::new(v))
341                        .map(LenientBlockNumberOrTag)
342                        .map_err(de::Error::custom)
343                }
344            }
345
346            deserializer.deserialize_any(LenientBlockNumberVisitor)
347        }
348    }
349}
350
351/// Error thrown when parsing a [BlockNumberOrTag] from a string.
352#[derive(Debug)]
353pub enum ParseBlockNumberError {
354    /// Failed to parse hex value
355    ParseIntErr(ParseIntError),
356    /// Failed to parse hex value
357    ParseErr(ParseError),
358    /// Block numbers should be 0x-prefixed
359    MissingPrefix(HexStringMissingPrefixError),
360}
361
362/// Error variants when parsing a [BlockNumberOrTag]
363impl core::error::Error for ParseBlockNumberError {
364    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
365        match self {
366            Self::ParseIntErr(err) => Some(err),
367            Self::MissingPrefix(err) => Some(err),
368            Self::ParseErr(_) => None,
369        }
370    }
371}
372
373impl fmt::Display for ParseBlockNumberError {
374    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
375        match self {
376            Self::ParseIntErr(err) => write!(f, "{err}"),
377            Self::ParseErr(err) => write!(f, "{err}"),
378            Self::MissingPrefix(err) => write!(f, "{err}"),
379        }
380    }
381}
382
383impl From<ParseIntError> for ParseBlockNumberError {
384    fn from(err: ParseIntError) -> Self {
385        Self::ParseIntErr(err)
386    }
387}
388
389impl From<ParseError> for ParseBlockNumberError {
390    fn from(err: ParseError) -> Self {
391        Self::ParseErr(err)
392    }
393}
394
395impl From<HexStringMissingPrefixError> for ParseBlockNumberError {
396    fn from(err: HexStringMissingPrefixError) -> Self {
397        Self::MissingPrefix(err)
398    }
399}
400
401/// Thrown when a 0x-prefixed hex string was expected
402#[derive(Clone, Copy, Debug, Default)]
403#[non_exhaustive]
404pub struct HexStringMissingPrefixError;
405
406impl fmt::Display for HexStringMissingPrefixError {
407    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
408        f.write_str("hex string without 0x prefix")
409    }
410}
411
412impl core::error::Error for HexStringMissingPrefixError {}
413
414/// A Block Identifier.
415/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md>
416#[derive(Clone, Copy, PartialEq, Eq)]
417pub enum BlockId {
418    /// A block hash and an optional bool that defines if it's canonical
419    Hash(RpcBlockHash),
420    /// A block number
421    Number(BlockNumberOrTag),
422}
423
424impl BlockId {
425    /// Returns the block hash if it is [BlockId::Hash]
426    pub const fn as_block_hash(&self) -> Option<BlockHash> {
427        match self {
428            Self::Hash(hash) => Some(hash.block_hash),
429            Self::Number(_) => None,
430        }
431    }
432
433    /// Returns the block number if it is [`BlockId::Number`] and not a tag
434    pub const fn as_u64(&self) -> Option<u64> {
435        match self {
436            Self::Number(x) => x.as_number(),
437            _ => None,
438        }
439    }
440
441    /// Returns true if this is [BlockNumberOrTag::Latest]
442    pub const fn is_latest(&self) -> bool {
443        matches!(self, Self::Number(BlockNumberOrTag::Latest))
444    }
445
446    /// Returns true if this is [BlockNumberOrTag::Pending]
447    pub const fn is_pending(&self) -> bool {
448        matches!(self, Self::Number(BlockNumberOrTag::Pending))
449    }
450
451    /// Returns true if this is [BlockNumberOrTag::Safe]
452    pub const fn is_safe(&self) -> bool {
453        matches!(self, Self::Number(BlockNumberOrTag::Safe))
454    }
455
456    /// Returns true if this is [BlockNumberOrTag::Finalized]
457    pub const fn is_finalized(&self) -> bool {
458        matches!(self, Self::Number(BlockNumberOrTag::Finalized))
459    }
460
461    /// Returns true if this is [BlockNumberOrTag::Earliest]
462    pub const fn is_earliest(&self) -> bool {
463        matches!(self, Self::Number(BlockNumberOrTag::Earliest))
464    }
465
466    /// Returns true if this is [BlockNumberOrTag::Number]
467    pub const fn is_number(&self) -> bool {
468        matches!(self, Self::Number(BlockNumberOrTag::Number(_)))
469    }
470    /// Returns true if this is [BlockId::Hash]
471    pub const fn is_hash(&self) -> bool {
472        matches!(self, Self::Hash(_))
473    }
474
475    /// Creates a new "pending" tag instance.
476    pub const fn pending() -> Self {
477        Self::Number(BlockNumberOrTag::Pending)
478    }
479
480    /// Creates a new "latest" tag instance.
481    pub const fn latest() -> Self {
482        Self::Number(BlockNumberOrTag::Latest)
483    }
484
485    /// Creates a new "earliest" tag instance.
486    pub const fn earliest() -> Self {
487        Self::Number(BlockNumberOrTag::Earliest)
488    }
489
490    /// Creates a new "finalized" tag instance.
491    pub const fn finalized() -> Self {
492        Self::Number(BlockNumberOrTag::Finalized)
493    }
494
495    /// Creates a new "safe" tag instance.
496    pub const fn safe() -> Self {
497        Self::Number(BlockNumberOrTag::Safe)
498    }
499
500    /// Creates a new block number instance.
501    pub const fn number(num: u64) -> Self {
502        Self::Number(BlockNumberOrTag::Number(num))
503    }
504
505    /// Create a new block hash instance.
506    pub const fn hash(block_hash: BlockHash) -> Self {
507        Self::Hash(RpcBlockHash { block_hash, require_canonical: None })
508    }
509
510    /// Create a new block hash instance that requires the block to be canonical.
511    pub const fn hash_canonical(block_hash: BlockHash) -> Self {
512        Self::Hash(RpcBlockHash { block_hash, require_canonical: Some(true) })
513    }
514}
515
516impl Default for BlockId {
517    fn default() -> Self {
518        BlockNumberOrTag::Latest.into()
519    }
520}
521
522impl From<u64> for BlockId {
523    fn from(num: u64) -> Self {
524        BlockNumberOrTag::Number(num).into()
525    }
526}
527
528impl From<U64> for BlockId {
529    fn from(value: U64) -> Self {
530        value.to::<u64>().into()
531    }
532}
533
534impl From<BlockNumberOrTag> for BlockId {
535    fn from(num: BlockNumberOrTag) -> Self {
536        Self::Number(num)
537    }
538}
539
540impl From<HashOrNumber> for BlockId {
541    fn from(block: HashOrNumber) -> Self {
542        match block {
543            HashOrNumber::Hash(hash) => hash.into(),
544            HashOrNumber::Number(num) => num.into(),
545        }
546    }
547}
548
549impl From<B256> for BlockId {
550    fn from(block_hash: B256) -> Self {
551        RpcBlockHash { block_hash, require_canonical: None }.into()
552    }
553}
554
555impl From<(B256, Option<bool>)> for BlockId {
556    fn from(hash_can: (B256, Option<bool>)) -> Self {
557        RpcBlockHash { block_hash: hash_can.0, require_canonical: hash_can.1 }.into()
558    }
559}
560
561impl From<RpcBlockHash> for BlockId {
562    fn from(value: RpcBlockHash) -> Self {
563        Self::Hash(value)
564    }
565}
566
567#[cfg(feature = "serde")]
568impl serde::Serialize for BlockId {
569    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
570    where
571        S: serde::Serializer,
572    {
573        use serde::ser::SerializeStruct;
574
575        match self {
576            Self::Hash(RpcBlockHash { block_hash, require_canonical }) => {
577                let mut s = serializer.serialize_struct("BlockIdEip1898", 1)?;
578                s.serialize_field("blockHash", block_hash)?;
579                if let Some(require_canonical) = require_canonical {
580                    s.serialize_field("requireCanonical", require_canonical)?;
581                }
582                s.end()
583            }
584            Self::Number(num) => num.serialize(serializer),
585        }
586    }
587}
588
589#[cfg(feature = "serde")]
590impl<'de> serde::Deserialize<'de> for BlockId {
591    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
592    where
593        D: serde::Deserializer<'de>,
594    {
595        struct BlockIdVisitor;
596
597        impl<'de> serde::de::Visitor<'de> for BlockIdVisitor {
598            type Value = BlockId;
599
600            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
601                formatter.write_str("Block identifier following EIP-1898")
602            }
603
604            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
605            where
606                E: serde::de::Error,
607            {
608                // Since there is no way to clearly distinguish between a DATA parameter and a QUANTITY parameter. A str is therefore deserialized into a Block Number: <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md>
609                // However, since the hex string should be a QUANTITY, we can safely assume that if the len is 66 bytes, it is in fact a hash, ref <https://github.com/ethereum/go-ethereum/blob/ee530c0d5aa70d2c00ab5691a89ab431b73f8165/rpc/types.go#L184-L184>
610                if v.len() == 66 {
611                    Ok(v.parse::<B256>().map_err(serde::de::Error::custom)?.into())
612                } else {
613                    // quantity hex string or tag
614                    Ok(BlockId::Number(v.parse().map_err(serde::de::Error::custom)?))
615                }
616            }
617
618            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
619            where
620                A: serde::de::MapAccess<'de>,
621            {
622                let mut number = None;
623                let mut block_hash = None;
624                let mut require_canonical = None;
625                while let Some(key) = map.next_key::<alloc::string::String>()? {
626                    match key.as_str() {
627                        "blockNumber" => {
628                            if number.is_some() || block_hash.is_some() {
629                                return Err(serde::de::Error::duplicate_field("blockNumber"));
630                            }
631                            if require_canonical.is_some() {
632                                return Err(serde::de::Error::custom(
633                                    "Non-valid require_canonical field",
634                                ));
635                            }
636                            number = Some(map.next_value::<BlockNumberOrTag>()?)
637                        }
638                        "blockHash" => {
639                            if number.is_some() || block_hash.is_some() {
640                                return Err(serde::de::Error::duplicate_field("blockHash"));
641                            }
642
643                            block_hash = Some(map.next_value::<B256>()?);
644                        }
645                        "requireCanonical" => {
646                            if number.is_some() || require_canonical.is_some() {
647                                return Err(serde::de::Error::duplicate_field("requireCanonical"));
648                            }
649
650                            require_canonical = Some(map.next_value::<bool>()?)
651                        }
652                        key => {
653                            return Err(serde::de::Error::unknown_field(
654                                key,
655                                &["blockNumber", "blockHash", "requireCanonical"],
656                            ))
657                        }
658                    }
659                }
660
661                #[expect(clippy::option_if_let_else)]
662                if let Some(number) = number {
663                    Ok(number.into())
664                } else if let Some(block_hash) = block_hash {
665                    Ok((block_hash, require_canonical).into())
666                } else {
667                    Err(serde::de::Error::custom(
668                        "Expected `blockNumber` or `blockHash` with `requireCanonical` optionally",
669                    ))
670                }
671            }
672        }
673
674        deserializer.deserialize_any(BlockIdVisitor)
675    }
676}
677
678impl fmt::Display for BlockId {
679    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
680        match self {
681            Self::Hash(hash) => hash.fmt(f),
682            Self::Number(num) => num.fmt(f),
683        }
684    }
685}
686
687impl fmt::Debug for BlockId {
688    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
689        match self {
690            Self::Hash(hash) => hash.fmt(f),
691            Self::Number(num) => num.fmt(f),
692        }
693    }
694}
695
696/// Error thrown when parsing a [BlockId] from a string.
697#[derive(Debug)]
698pub enum ParseBlockIdError {
699    /// Failed to parse a block id from a number.
700    ParseIntError(ParseIntError),
701    /// Failed to parse hex number
702    ParseError(ParseError),
703    /// Failed to parse a block id as a hex string.
704    FromHexError(FromHexError),
705}
706
707impl fmt::Display for ParseBlockIdError {
708    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
709        match self {
710            Self::ParseIntError(err) => write!(f, "{err}"),
711            Self::ParseError(err) => write!(f, "{err}"),
712            Self::FromHexError(err) => write!(f, "{err}"),
713        }
714    }
715}
716
717impl core::error::Error for ParseBlockIdError {
718    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
719        match self {
720            Self::ParseIntError(err) => Some(err),
721            Self::FromHexError(err) => Some(err),
722            Self::ParseError(_) => None,
723        }
724    }
725}
726
727impl From<ParseIntError> for ParseBlockIdError {
728    fn from(err: ParseIntError) -> Self {
729        Self::ParseIntError(err)
730    }
731}
732
733impl From<FromHexError> for ParseBlockIdError {
734    fn from(err: FromHexError) -> Self {
735        Self::FromHexError(err)
736    }
737}
738
739impl FromStr for BlockId {
740    type Err = ParseBlockIdError;
741    fn from_str(s: &str) -> Result<Self, Self::Err> {
742        if s.starts_with("0x") {
743            return match s.len() {
744                66 => B256::from_str(s).map(Into::into).map_err(ParseBlockIdError::FromHexError),
745                _ => U64::from_str(s).map(Into::into).map_err(ParseBlockIdError::ParseError),
746            };
747        }
748
749        match s {
750            "latest" | "finalized" | "safe" | "earliest" | "pending" => {
751                Ok(BlockNumberOrTag::from_str(s).unwrap().into())
752            }
753            _ => s.parse::<u64>().map_err(ParseBlockIdError::ParseIntError).map(Into::into),
754        }
755    }
756}
757
758/// A number and a hash.
759#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
760#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
761#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
762pub struct NumHash {
763    /// The number
764    pub number: u64,
765    /// The hash.
766    pub hash: B256,
767}
768
769/// Block number and hash of the forked block.
770pub type ForkBlock = NumHash;
771
772/// A block number and a hash
773pub type BlockNumHash = NumHash;
774
775impl NumHash {
776    /// Creates a new `NumHash` from a number and hash.
777    pub const fn new(number: u64, hash: B256) -> Self {
778        Self { number, hash }
779    }
780
781    /// Consumes `Self` and returns the number and hash
782    pub const fn into_components(self) -> (u64, B256) {
783        (self.number, self.hash)
784    }
785
786    /// Returns whether or not the block matches the given [HashOrNumber].
787    pub fn matches_block_or_num(&self, block: &HashOrNumber) -> bool {
788        match block {
789            HashOrNumber::Hash(hash) => self.hash == *hash,
790            HashOrNumber::Number(number) => self.number == *number,
791        }
792    }
793}
794
795impl From<(u64, B256)> for NumHash {
796    fn from(val: (u64, B256)) -> Self {
797        Self { number: val.0, hash: val.1 }
798    }
799}
800
801impl From<(B256, u64)> for NumHash {
802    fn from(val: (B256, u64)) -> Self {
803        Self { hash: val.0, number: val.1 }
804    }
805}
806
807/// Either a hash _or_ a block number
808#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
809#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
810#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
811pub enum HashOrNumber {
812    /// The hash
813    Hash(B256),
814    /// The number
815    Number(u64),
816}
817
818/// A block hash _or_ a block number
819pub type BlockHashOrNumber = HashOrNumber;
820
821// === impl HashOrNumber ===
822
823impl HashOrNumber {
824    /// Returns the block number if it is a [`HashOrNumber::Number`].
825    #[inline]
826    pub const fn as_number(self) -> Option<u64> {
827        match self {
828            Self::Hash(_) => None,
829            Self::Number(num) => Some(num),
830        }
831    }
832
833    /// Returns the block hash if it is a [`HashOrNumber::Hash`].
834    #[inline]
835    pub const fn as_hash(self) -> Option<B256> {
836        match self {
837            Self::Hash(hash) => Some(hash),
838            Self::Number(_) => None,
839        }
840    }
841}
842
843impl From<B256> for HashOrNumber {
844    fn from(value: B256) -> Self {
845        Self::Hash(value)
846    }
847}
848
849impl From<&B256> for HashOrNumber {
850    fn from(value: &B256) -> Self {
851        (*value).into()
852    }
853}
854
855impl From<u64> for HashOrNumber {
856    fn from(value: u64) -> Self {
857        Self::Number(value)
858    }
859}
860
861impl From<U64> for HashOrNumber {
862    fn from(value: U64) -> Self {
863        value.to::<u64>().into()
864    }
865}
866
867impl From<RpcBlockHash> for HashOrNumber {
868    fn from(value: RpcBlockHash) -> Self {
869        Self::Hash(value.into())
870    }
871}
872
873/// Allows for RLP encoding of either a hash or a number
874impl Encodable for HashOrNumber {
875    fn encode(&self, out: &mut dyn bytes::BufMut) {
876        match self {
877            Self::Hash(block_hash) => block_hash.encode(out),
878            Self::Number(block_number) => block_number.encode(out),
879        }
880    }
881    fn length(&self) -> usize {
882        match self {
883            Self::Hash(block_hash) => block_hash.length(),
884            Self::Number(block_number) => block_number.length(),
885        }
886    }
887}
888
889/// Allows for RLP decoding of a hash or number
890impl Decodable for HashOrNumber {
891    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
892        let header: u8 = *buf.first().ok_or(RlpError::InputTooShort)?;
893        // if the byte string is exactly 32 bytes, decode it into a Hash
894        // 0xa0 = 0x80 (start of string) + 0x20 (32, length of string)
895        if header == 0xa0 {
896            // strip the first byte, parsing the rest of the string.
897            // If the rest of the string fails to decode into 32 bytes, we'll bubble up the
898            // decoding error.
899            Ok(B256::decode(buf)?.into())
900        } else {
901            // a block number when encoded as bytes ranges from 0 to any number of bytes - we're
902            // going to accept numbers which fit in less than 64 bytes.
903            // Any data larger than this which is not caught by the Hash decoding should error and
904            // is considered an invalid block number.
905            Ok(u64::decode(buf)?.into())
906        }
907    }
908}
909
910impl fmt::Display for HashOrNumber {
911    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
912        match self {
913            Self::Hash(hash) => write!(f, "{hash}"),
914            Self::Number(num) => write!(f, "{num}"),
915        }
916    }
917}
918
919/// Error thrown when parsing a [HashOrNumber] from a string.
920#[derive(Debug)]
921pub struct ParseBlockHashOrNumberError {
922    input: alloc::string::String,
923    parse_int_error: ParseIntError,
924    hex_error: alloy_primitives::hex::FromHexError,
925}
926
927impl fmt::Display for ParseBlockHashOrNumberError {
928    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
929        write!(
930            f,
931            "failed to parse {:?} as a number: {} or hash: {}",
932            self.input, self.parse_int_error, self.hex_error
933        )
934    }
935}
936
937impl core::error::Error for ParseBlockHashOrNumberError {}
938
939impl FromStr for HashOrNumber {
940    type Err = ParseBlockHashOrNumberError;
941
942    fn from_str(s: &str) -> Result<Self, Self::Err> {
943        use alloc::string::ToString;
944
945        match u64::from_str(s) {
946            Ok(val) => Ok(val.into()),
947            Err(parse_int_error) => match B256::from_str(s) {
948                Ok(val) => Ok(val.into()),
949                Err(hex_error) => Err(ParseBlockHashOrNumberError {
950                    input: s.to_string(),
951                    parse_int_error,
952                    hex_error,
953                }),
954            },
955        }
956    }
957}
958
959#[cfg(test)]
960mod tests {
961    use super::*;
962    use alloc::{string::ToString, vec::Vec};
963    use alloy_primitives::b256;
964
965    const HASH: B256 = b256!("1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9");
966
967    #[test]
968    fn block_id_from_str() {
969        assert_eq!("0x0".parse::<BlockId>().unwrap(), BlockId::number(0));
970        assert_eq!("0x24A931".parse::<BlockId>().unwrap(), BlockId::number(2402609));
971        assert_eq!(
972            "0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
973                .parse::<BlockId>()
974                .unwrap(),
975            HASH.into()
976        );
977    }
978
979    #[test]
980    #[cfg(feature = "serde")]
981    fn compact_block_number_serde() {
982        let num: BlockNumberOrTag = 1u64.into();
983        let serialized = serde_json::to_string(&num).unwrap();
984        assert_eq!(serialized, "\"0x1\"");
985    }
986
987    #[test]
988    fn block_id_as_u64() {
989        assert_eq!(BlockId::number(123).as_u64(), Some(123));
990        assert_eq!(BlockId::number(0).as_u64(), Some(0));
991        assert_eq!(BlockId::earliest().as_u64(), None);
992        assert_eq!(BlockId::latest().as_u64(), None);
993        assert_eq!(BlockId::pending().as_u64(), None);
994        assert_eq!(BlockId::safe().as_u64(), None);
995        assert_eq!(BlockId::hash(BlockHash::ZERO).as_u64(), None);
996        assert_eq!(BlockId::hash_canonical(BlockHash::ZERO).as_u64(), None);
997    }
998
999    #[test]
1000    #[cfg(feature = "serde")]
1001    fn can_parse_eip1898_block_ids() {
1002        let num = serde_json::json!(
1003            { "blockNumber": "0x0" }
1004        );
1005        let id = serde_json::from_value::<BlockId>(num).unwrap();
1006        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Number(0u64)));
1007
1008        let num = serde_json::json!(
1009            { "blockNumber": "pending" }
1010        );
1011        let id = serde_json::from_value::<BlockId>(num).unwrap();
1012        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Pending));
1013
1014        let num = serde_json::json!(
1015            { "blockNumber": "latest" }
1016        );
1017        let id = serde_json::from_value::<BlockId>(num).unwrap();
1018        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Latest));
1019
1020        let num = serde_json::json!(
1021            { "blockNumber": "finalized" }
1022        );
1023        let id = serde_json::from_value::<BlockId>(num).unwrap();
1024        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Finalized));
1025
1026        let num = serde_json::json!(
1027            { "blockNumber": "safe" }
1028        );
1029        let id = serde_json::from_value::<BlockId>(num).unwrap();
1030        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Safe));
1031
1032        let num = serde_json::json!(
1033            { "blockNumber": "earliest" }
1034        );
1035        let id = serde_json::from_value::<BlockId>(num).unwrap();
1036        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Earliest));
1037
1038        let num = serde_json::json!("0x0");
1039        let id = serde_json::from_value::<BlockId>(num).unwrap();
1040        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Number(0u64)));
1041
1042        let num = serde_json::json!("pending");
1043        let id = serde_json::from_value::<BlockId>(num).unwrap();
1044        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Pending));
1045
1046        let num = serde_json::json!("latest");
1047        let id = serde_json::from_value::<BlockId>(num).unwrap();
1048        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Latest));
1049
1050        let num = serde_json::json!("finalized");
1051        let id = serde_json::from_value::<BlockId>(num).unwrap();
1052        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Finalized));
1053
1054        let num = serde_json::json!("safe");
1055        let id = serde_json::from_value::<BlockId>(num).unwrap();
1056        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Safe));
1057
1058        let num = serde_json::json!("earliest");
1059        let id = serde_json::from_value::<BlockId>(num).unwrap();
1060        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Earliest));
1061
1062        let num = serde_json::json!(
1063            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" }
1064        );
1065        let id = serde_json::from_value::<BlockId>(num).unwrap();
1066        assert_eq!(
1067            id,
1068            BlockId::Hash(
1069                "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
1070                    .parse::<B256>()
1071                    .unwrap()
1072                    .into()
1073            )
1074        );
1075    }
1076
1077    #[test]
1078    fn display_rpc_block_hash() {
1079        let hash = RpcBlockHash::from_hash(HASH, Some(true));
1080
1081        assert_eq!(
1082            hash.to_string(),
1083            "canonical hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1084        );
1085
1086        let hash = RpcBlockHash::from_hash(HASH, None);
1087
1088        assert_eq!(
1089            hash.to_string(),
1090            "hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1091        );
1092    }
1093
1094    #[test]
1095    fn display_block_id() {
1096        let id = BlockId::hash(HASH);
1097
1098        assert_eq!(
1099            id.to_string(),
1100            "hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1101        );
1102
1103        let id = BlockId::hash_canonical(HASH);
1104
1105        assert_eq!(
1106            id.to_string(),
1107            "canonical hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1108        );
1109
1110        let id = BlockId::number(100000);
1111
1112        assert_eq!(id.to_string(), "0x186a0");
1113
1114        let id = BlockId::latest();
1115
1116        assert_eq!(id.to_string(), "latest");
1117
1118        let id = BlockId::safe();
1119
1120        assert_eq!(id.to_string(), "safe");
1121
1122        let id = BlockId::finalized();
1123
1124        assert_eq!(id.to_string(), "finalized");
1125
1126        let id = BlockId::earliest();
1127
1128        assert_eq!(id.to_string(), "earliest");
1129
1130        let id = BlockId::pending();
1131
1132        assert_eq!(id.to_string(), "pending");
1133    }
1134
1135    #[test]
1136    fn test_block_number_or_tag() {
1137        // Test Latest variant
1138        let latest = BlockNumberOrTag::Latest;
1139        assert_eq!(latest.as_number(), None);
1140        assert!(latest.is_latest());
1141        assert!(!latest.is_number());
1142        assert!(!latest.is_finalized());
1143        assert!(!latest.is_safe());
1144        assert!(!latest.is_pending());
1145        assert!(!latest.is_earliest());
1146
1147        // Test Finalized variant
1148        let finalized = BlockNumberOrTag::Finalized;
1149        assert_eq!(finalized.as_number(), None);
1150        assert!(finalized.is_finalized());
1151        assert!(!finalized.is_latest());
1152        assert!(!finalized.is_number());
1153        assert!(!finalized.is_safe());
1154        assert!(!finalized.is_pending());
1155        assert!(!finalized.is_earliest());
1156
1157        // Test Safe variant
1158        let safe = BlockNumberOrTag::Safe;
1159        assert_eq!(safe.as_number(), None);
1160        assert!(safe.is_safe());
1161        assert!(!safe.is_latest());
1162        assert!(!safe.is_number());
1163        assert!(!safe.is_finalized());
1164        assert!(!safe.is_pending());
1165        assert!(!safe.is_earliest());
1166
1167        // Test Earliest variant
1168        let earliest = BlockNumberOrTag::Earliest;
1169        assert_eq!(earliest.as_number(), None);
1170        assert!(earliest.is_earliest());
1171        assert!(!earliest.is_latest());
1172        assert!(!earliest.is_number());
1173        assert!(!earliest.is_finalized());
1174        assert!(!earliest.is_safe());
1175        assert!(!earliest.is_pending());
1176
1177        // Test Pending variant
1178        let pending = BlockNumberOrTag::Pending;
1179        assert_eq!(pending.as_number(), None);
1180        assert!(pending.is_pending());
1181        assert!(!pending.is_latest());
1182        assert!(!pending.is_number());
1183        assert!(!pending.is_finalized());
1184        assert!(!pending.is_safe());
1185        assert!(!pending.is_earliest());
1186
1187        // Test Number variant
1188        let number = BlockNumberOrTag::Number(42);
1189        assert_eq!(number.as_number(), Some(42));
1190        assert!(number.is_number());
1191        assert!(!number.is_latest());
1192        assert!(!number.is_finalized());
1193        assert!(!number.is_safe());
1194        assert!(!number.is_pending());
1195        assert!(!number.is_earliest());
1196    }
1197
1198    #[test]
1199    fn test_block_number_or_tag_from() {
1200        // Test conversion from u64
1201        let num = 100u64;
1202        let block: BlockNumberOrTag = num.into();
1203        assert_eq!(block, BlockNumberOrTag::Number(100));
1204
1205        // Test conversion from U64
1206        let num = U64::from(200);
1207        let block: BlockNumberOrTag = num.into();
1208        assert_eq!(block, BlockNumberOrTag::Number(200));
1209    }
1210
1211    #[test]
1212    fn test_block_id() {
1213        let hash = BlockHash::random();
1214
1215        // Block hash
1216        let block_id_hash = BlockId::hash(hash);
1217        assert_eq!(block_id_hash.as_block_hash(), Some(hash));
1218        assert!(block_id_hash.is_hash());
1219        assert!(!block_id_hash.is_number());
1220        assert!(!block_id_hash.is_latest());
1221        assert!(!block_id_hash.is_pending());
1222        assert!(!block_id_hash.is_safe());
1223        assert!(!block_id_hash.is_finalized());
1224        assert!(!block_id_hash.is_earliest());
1225
1226        // Block number
1227        let block_id_number = BlockId::number(123);
1228        assert_eq!(block_id_number.as_u64(), Some(123));
1229        assert!(block_id_number.is_number());
1230        assert!(!block_id_number.is_hash());
1231        assert!(!block_id_number.is_latest());
1232        assert!(!block_id_number.is_pending());
1233        assert!(!block_id_number.is_safe());
1234        assert!(!block_id_number.is_finalized());
1235        assert!(!block_id_number.is_earliest());
1236
1237        // Latest block
1238        let block_latest = BlockId::latest();
1239        assert!(block_latest.is_latest());
1240        assert!(!block_latest.is_number());
1241        assert!(!block_latest.is_hash());
1242        assert!(!block_latest.is_pending());
1243        assert!(!block_latest.is_safe());
1244        assert!(!block_latest.is_finalized());
1245        assert!(!block_latest.is_earliest());
1246
1247        // Pending block
1248        let block_pending = BlockId::pending();
1249        assert!(block_pending.is_pending());
1250        assert!(!block_pending.is_latest());
1251        assert!(!block_pending.is_number());
1252        assert!(!block_pending.is_hash());
1253        assert!(!block_pending.is_safe());
1254        assert!(!block_pending.is_finalized());
1255        assert!(!block_pending.is_earliest());
1256
1257        // Safe block
1258        let block_safe = BlockId::safe();
1259        assert!(block_safe.is_safe());
1260        assert!(!block_safe.is_latest());
1261        assert!(!block_safe.is_number());
1262        assert!(!block_safe.is_hash());
1263        assert!(!block_safe.is_pending());
1264        assert!(!block_safe.is_finalized());
1265        assert!(!block_safe.is_earliest());
1266
1267        // Finalized block
1268        let block_finalized = BlockId::finalized();
1269        assert!(block_finalized.is_finalized());
1270        assert!(!block_finalized.is_latest());
1271        assert!(!block_finalized.is_number());
1272        assert!(!block_finalized.is_hash());
1273        assert!(!block_finalized.is_pending());
1274        assert!(!block_finalized.is_safe());
1275        assert!(!block_finalized.is_earliest());
1276
1277        // Earliest block
1278        let block_earliest = BlockId::earliest();
1279        assert!(block_earliest.is_earliest());
1280        assert!(!block_earliest.is_latest());
1281        assert!(!block_earliest.is_number());
1282        assert!(!block_earliest.is_hash());
1283        assert!(!block_earliest.is_pending());
1284        assert!(!block_earliest.is_safe());
1285        assert!(!block_earliest.is_finalized());
1286
1287        // Default block
1288        assert!(BlockId::default().is_latest());
1289        assert!(!BlockId::default().is_number());
1290        assert!(!BlockId::default().is_hash());
1291        assert!(!BlockId::default().is_pending());
1292        assert!(!BlockId::default().is_safe());
1293        assert!(!BlockId::default().is_finalized());
1294        assert!(!BlockId::default().is_earliest());
1295    }
1296
1297    #[test]
1298    fn test_u64_to_block_id() {
1299        // Simple u64
1300        let num: u64 = 123;
1301        let block_id: BlockId = num.into();
1302
1303        match block_id {
1304            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 123),
1305            _ => panic!("Expected BlockId::Number with 123"),
1306        }
1307
1308        // Big integer U64
1309        let num: U64 = U64::from(456);
1310        let block_id: BlockId = num.into();
1311
1312        match block_id {
1313            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 456),
1314            _ => panic!("Expected BlockId::Number with 456"),
1315        }
1316
1317        // u64 as HashOrNumber
1318        let num: u64 = 789;
1319        let block_id: BlockId = HashOrNumber::Number(num).into();
1320
1321        match block_id {
1322            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 789),
1323            _ => panic!("Expected BlockId::Number with 789"),
1324        }
1325    }
1326
1327    #[test]
1328    fn test_block_number_or_tag_to_block_id() {
1329        let block_number_or_tag = BlockNumberOrTag::Pending;
1330        let block_id: BlockId = block_number_or_tag.into();
1331
1332        match block_id {
1333            BlockId::Number(BlockNumberOrTag::Pending) => {}
1334            _ => panic!("Expected BlockId::Number with Pending"),
1335        }
1336    }
1337
1338    #[test]
1339    fn test_hash_or_number_to_block_id_hash() {
1340        // B256 wrapped in HashOrNumber
1341        let hash: B256 = B256::random();
1342        let block_id: BlockId = HashOrNumber::Hash(hash).into();
1343
1344        match block_id {
1345            BlockId::Hash(rpc_block_hash) => assert_eq!(rpc_block_hash.block_hash, hash),
1346            _ => panic!("Expected BlockId::Hash"),
1347        }
1348
1349        // Simple B256
1350        let hash: B256 = B256::random();
1351        let block_id: BlockId = hash.into();
1352
1353        match block_id {
1354            BlockId::Hash(rpc_block_hash) => assert_eq!(rpc_block_hash.block_hash, hash),
1355            _ => panic!("Expected BlockId::Hash"),
1356        }
1357
1358        // Tuple with B256 and canonical flag
1359        let hash: B256 = B256::random();
1360        let block_id: BlockId = (hash, Some(true)).into();
1361
1362        match block_id {
1363            BlockId::Hash(rpc_block_hash) => {
1364                assert_eq!(rpc_block_hash.block_hash, hash);
1365                assert_eq!(rpc_block_hash.require_canonical, Some(true));
1366            }
1367            _ => panic!("Expected BlockId::Hash with canonical flag"),
1368        }
1369    }
1370
1371    #[test]
1372    fn test_hash_or_number_as_number() {
1373        // Test with a number
1374        let hash_or_number = HashOrNumber::Number(123);
1375        assert_eq!(hash_or_number.as_number(), Some(123));
1376
1377        // Test with a hash
1378        let hash = B256::random();
1379        let hash_or_number = HashOrNumber::Hash(hash);
1380        assert_eq!(hash_or_number.as_number(), None);
1381    }
1382
1383    #[test]
1384    fn test_hash_or_number_as_hash() {
1385        // Test with a hash
1386        let hash = B256::random();
1387        let hash_or_number = HashOrNumber::Hash(hash);
1388        assert_eq!(hash_or_number.as_hash(), Some(hash));
1389
1390        // Test with a number
1391        let hash_or_number = HashOrNumber::Number(456);
1392        assert_eq!(hash_or_number.as_hash(), None);
1393    }
1394
1395    #[test]
1396    fn test_hash_or_number_conversions() {
1397        // Test conversion from B256
1398        let hash = B256::random();
1399        let hash_or_number: HashOrNumber = hash.into();
1400        assert_eq!(hash_or_number, HashOrNumber::Hash(hash));
1401
1402        // Test conversion from &B256
1403        let hash_ref: HashOrNumber = (&hash).into();
1404        assert_eq!(hash_ref, HashOrNumber::Hash(hash));
1405
1406        // Test conversion from u64
1407        let number: u64 = 123;
1408        let hash_or_number: HashOrNumber = number.into();
1409        assert_eq!(hash_or_number, HashOrNumber::Number(number));
1410
1411        // Test conversion from U64
1412        let u64_value = U64::from(456);
1413        let hash_or_number: HashOrNumber = u64_value.into();
1414        assert_eq!(hash_or_number, HashOrNumber::Number(u64_value.to::<u64>()));
1415
1416        // Test conversion from RpcBlockHash (assuming RpcBlockHash is convertible to B256)
1417        let rpc_block_hash = RpcBlockHash { block_hash: hash, require_canonical: Some(true) };
1418        let hash_or_number: HashOrNumber = rpc_block_hash.into();
1419        assert_eq!(hash_or_number, HashOrNumber::Hash(hash));
1420    }
1421
1422    #[test]
1423    fn test_hash_or_number_rlp_roundtrip_hash() {
1424        // Test case: encoding and decoding a B256 hash
1425        let original_hash = B256::random();
1426        let hash_or_number: HashOrNumber = HashOrNumber::Hash(original_hash);
1427
1428        // Encode the HashOrNumber
1429        let mut buf = Vec::new();
1430        hash_or_number.encode(&mut buf);
1431
1432        // Decode the encoded bytes
1433        let decoded: HashOrNumber = HashOrNumber::decode(&mut &buf[..]).expect("Decoding failed");
1434
1435        // Assert that the decoded value matches the original
1436        assert_eq!(decoded, hash_or_number);
1437    }
1438
1439    #[test]
1440    fn test_hash_or_number_rlp_roundtrip_u64() {
1441        // Test case: encoding and decoding a u64 number
1442        let original_number: u64 = 12345;
1443        let hash_or_number: HashOrNumber = HashOrNumber::Number(original_number);
1444
1445        // Encode the HashOrNumber
1446        let mut buf = Vec::new();
1447        hash_or_number.encode(&mut buf);
1448
1449        // Decode the encoded bytes
1450        let decoded: HashOrNumber = HashOrNumber::decode(&mut &buf[..]).expect("Decoding failed");
1451
1452        // Assert that the decoded value matches the original
1453        assert_eq!(decoded, hash_or_number);
1454    }
1455
1456    #[test]
1457    fn test_numhash() {
1458        let number: u64 = 42;
1459        let hash = B256::random();
1460
1461        let num_hash = NumHash::new(number, hash);
1462
1463        // Validate the initial values
1464        assert_eq!(num_hash.number, number);
1465        assert_eq!(num_hash.hash, hash);
1466
1467        // Test into_components
1468        assert_eq!(num_hash.into_components(), (number, hash));
1469    }
1470
1471    #[test]
1472    fn test_numhash_matches_block_or_num() {
1473        let number: u64 = 42;
1474        let hash = B256::random();
1475
1476        let num_hash = NumHash::new(number, hash);
1477
1478        // Test matching by hash
1479        let block_hash = HashOrNumber::Hash(hash);
1480        assert!(num_hash.matches_block_or_num(&block_hash));
1481
1482        // Test matching by number
1483        let block_number = HashOrNumber::Number(number);
1484        assert!(num_hash.matches_block_or_num(&block_number));
1485
1486        // Test non-matching by different hash
1487        let different_hash = B256::random();
1488        let non_matching_hash = HashOrNumber::Hash(different_hash);
1489        assert!(!num_hash.matches_block_or_num(&non_matching_hash));
1490
1491        // Test non-matching by different number
1492        let different_number: u64 = 43;
1493        let non_matching_number = HashOrNumber::Number(different_number);
1494        assert!(!num_hash.matches_block_or_num(&non_matching_number));
1495    }
1496
1497    #[test]
1498    fn test_numhash_conversions() {
1499        // From a tuple (u64, B256)
1500        let number: u64 = 42;
1501        let hash = B256::random();
1502
1503        let num_hash_from_tuple: NumHash = (number, hash).into();
1504
1505        assert_eq!(num_hash_from_tuple.number, number);
1506        assert_eq!(num_hash_from_tuple.hash, hash);
1507
1508        // From a reversed tuple (B256, u64)
1509        let number: u64 = 42;
1510        let hash = B256::random();
1511
1512        let num_hash_from_reversed_tuple: NumHash = (hash, number).into();
1513
1514        assert_eq!(num_hash_from_reversed_tuple.number, number);
1515        assert_eq!(num_hash_from_reversed_tuple.hash, hash);
1516    }
1517
1518    #[test]
1519    #[cfg(feature = "serde")]
1520    fn test_block_id_from_str() {
1521        // Valid hexadecimal block ID (with 0x prefix)
1522        let hex_id = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
1523        assert_eq!(
1524            BlockId::from_str(hex_id).unwrap(),
1525            BlockId::Hash(RpcBlockHash::from_hash(B256::from_str(hex_id).unwrap(), None))
1526        );
1527
1528        // Valid tag strings
1529        assert_eq!(BlockId::from_str("latest").unwrap(), BlockNumberOrTag::Latest.into());
1530        assert_eq!(BlockId::from_str("finalized").unwrap(), BlockNumberOrTag::Finalized.into());
1531        assert_eq!(BlockId::from_str("safe").unwrap(), BlockNumberOrTag::Safe.into());
1532        assert_eq!(BlockId::from_str("earliest").unwrap(), BlockNumberOrTag::Earliest.into());
1533        assert_eq!(BlockId::from_str("pending").unwrap(), BlockNumberOrTag::Pending.into());
1534
1535        // Valid numeric string without prefix
1536        let numeric_string = "12345";
1537        let parsed_numeric_string = BlockId::from_str(numeric_string);
1538        assert!(parsed_numeric_string.is_ok());
1539
1540        // Hex interpretation of numeric string
1541        assert_eq!(
1542            BlockId::from_str("0x12345").unwrap(),
1543            BlockId::Number(BlockNumberOrTag::Number(74565))
1544        );
1545
1546        // Invalid non-numeric string
1547        let invalid_string = "invalid_block_id";
1548        let parsed_invalid_string = BlockId::from_str(invalid_string);
1549        assert!(parsed_invalid_string.is_err());
1550    }
1551
1552    /// Check parsing according to EIP-1898.
1553    #[test]
1554    #[cfg(feature = "serde")]
1555    fn can_parse_blockid_u64() {
1556        let num = serde_json::json!(
1557            {"blockNumber": "0xaf"}
1558        );
1559
1560        let id = serde_json::from_value::<BlockId>(num);
1561        assert_eq!(id.unwrap(), BlockId::from(175));
1562    }
1563
1564    #[test]
1565    #[cfg(feature = "serde")]
1566    fn can_parse_block_hash() {
1567        let block_hash =
1568            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1569                .unwrap();
1570        let block_hash_json = serde_json::json!(
1571            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}
1572        );
1573        let id = serde_json::from_value::<BlockId>(block_hash_json).unwrap();
1574        assert_eq!(id, BlockId::from(block_hash,));
1575    }
1576
1577    #[test]
1578    #[cfg(feature = "serde")]
1579    fn can_parse_block_hash_with_canonical() {
1580        let block_hash =
1581            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1582                .unwrap();
1583        let block_id = BlockId::Hash(RpcBlockHash::from_hash(block_hash, Some(true)));
1584        let block_hash_json = serde_json::json!(
1585            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true }
1586        );
1587        let id = serde_json::from_value::<BlockId>(block_hash_json).unwrap();
1588        assert_eq!(id, block_id)
1589    }
1590    #[test]
1591    #[cfg(feature = "serde")]
1592    fn can_parse_blockid_tags() {
1593        let tags = [
1594            ("latest", BlockNumberOrTag::Latest),
1595            ("finalized", BlockNumberOrTag::Finalized),
1596            ("safe", BlockNumberOrTag::Safe),
1597            ("pending", BlockNumberOrTag::Pending),
1598        ];
1599        for (value, tag) in tags {
1600            let num = serde_json::json!({ "blockNumber": value });
1601            let id = serde_json::from_value::<BlockId>(num);
1602            assert_eq!(id.unwrap(), BlockId::from(tag))
1603        }
1604    }
1605    #[test]
1606    #[cfg(feature = "serde")]
1607    fn repeated_keys_is_err() {
1608        let num = serde_json::json!({"blockNumber": 1, "requireCanonical": true, "requireCanonical": false});
1609        assert!(serde_json::from_value::<BlockId>(num).is_err());
1610        let num =
1611            serde_json::json!({"blockNumber": 1, "requireCanonical": true, "blockNumber": 23});
1612        assert!(serde_json::from_value::<BlockId>(num).is_err());
1613    }
1614
1615    /// Serde tests
1616    #[test]
1617    #[cfg(feature = "serde")]
1618    fn serde_blockid_tags() {
1619        let block_ids = [
1620            BlockNumberOrTag::Latest,
1621            BlockNumberOrTag::Finalized,
1622            BlockNumberOrTag::Safe,
1623            BlockNumberOrTag::Pending,
1624        ]
1625        .map(BlockId::from);
1626        for block_id in &block_ids {
1627            let serialized = serde_json::to_string(&block_id).unwrap();
1628            let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1629            assert_eq!(deserialized, *block_id)
1630        }
1631    }
1632
1633    #[test]
1634    #[cfg(feature = "serde")]
1635    fn serde_blockid_number() {
1636        let block_id = BlockId::from(100u64);
1637        let serialized = serde_json::to_string(&block_id).unwrap();
1638        let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1639        assert_eq!(deserialized, block_id)
1640    }
1641
1642    #[test]
1643    #[cfg(feature = "serde")]
1644    fn serde_blockid_hash() {
1645        let block_id = BlockId::from(B256::default());
1646        let serialized = serde_json::to_string(&block_id).unwrap();
1647        let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1648        assert_eq!(deserialized, block_id)
1649    }
1650
1651    #[test]
1652    #[cfg(feature = "serde")]
1653    fn serde_blockid_hash_from_str() {
1654        let val = "\"0x898753d8fdd8d92c1907ca21e68c7970abd290c647a202091181deec3f30a0b2\"";
1655        let block_hash: B256 = serde_json::from_str(val).unwrap();
1656        let block_id: BlockId = serde_json::from_str(val).unwrap();
1657        assert_eq!(block_id, BlockId::Hash(block_hash.into()));
1658    }
1659
1660    #[test]
1661    #[cfg(feature = "serde")]
1662    fn serde_rpc_payload_block_tag() {
1663        let payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},"latest"],"id":1,"jsonrpc":"2.0"}"#;
1664        let value: serde_json::Value = serde_json::from_str(payload).unwrap();
1665        let block_id_param = value.pointer("/params/1").unwrap();
1666        let block_id: BlockId = serde_json::from_value::<BlockId>(block_id_param.clone()).unwrap();
1667        assert_eq!(BlockId::Number(BlockNumberOrTag::Latest), block_id);
1668    }
1669
1670    #[test]
1671    #[cfg(feature = "serde")]
1672    fn serde_rpc_payload_block_object() {
1673        let example_payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},{"blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}],"id":1,"jsonrpc":"2.0"}"#;
1674        let value: serde_json::Value = serde_json::from_str(example_payload).unwrap();
1675        let block_id_param = value.pointer("/params/1").unwrap().to_string();
1676        let block_id: BlockId = serde_json::from_str::<BlockId>(&block_id_param).unwrap();
1677        let hash =
1678            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1679                .unwrap();
1680        assert_eq!(BlockId::from(hash), block_id);
1681        let serialized = serde_json::to_string(&BlockId::from(hash)).unwrap();
1682        assert_eq!("{\"blockHash\":\"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3\"}", serialized)
1683    }
1684
1685    #[test]
1686    #[cfg(feature = "serde")]
1687    fn serde_rpc_payload_block_number() {
1688        let example_payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},{"blockNumber": "0x0"}],"id":1,"jsonrpc":"2.0"}"#;
1689        let value: serde_json::Value = serde_json::from_str(example_payload).unwrap();
1690        let block_id_param = value.pointer("/params/1").unwrap().to_string();
1691        let block_id: BlockId = serde_json::from_str::<BlockId>(&block_id_param).unwrap();
1692        assert_eq!(BlockId::from(0u64), block_id);
1693        let serialized = serde_json::to_string(&BlockId::from(0u64)).unwrap();
1694        assert_eq!("\"0x0\"", serialized)
1695    }
1696
1697    #[test]
1698    #[should_panic]
1699    #[cfg(feature = "serde")]
1700    fn serde_rpc_payload_block_number_duplicate_key() {
1701        let payload = r#"{"blockNumber": "0x132", "blockNumber": "0x133"}"#;
1702        let parsed_block_id = serde_json::from_str::<BlockId>(payload);
1703        parsed_block_id.unwrap();
1704    }
1705
1706    #[test]
1707    #[cfg(feature = "serde")]
1708    fn serde_rpc_payload_block_hash() {
1709        let payload = r#"{"blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}"#;
1710        let parsed = serde_json::from_str::<BlockId>(payload).unwrap();
1711        let expected = BlockId::from(
1712            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1713                .unwrap(),
1714        );
1715        assert_eq!(parsed, expected);
1716    }
1717
1718    #[test]
1719    #[cfg(feature = "serde")]
1720    fn serde_blocknumber_non_0xprefix() {
1721        let s = "\"2\"";
1722        let err = serde_json::from_str::<BlockNumberOrTag>(s).unwrap_err();
1723        assert_eq!(err.to_string(), HexStringMissingPrefixError::default().to_string());
1724    }
1725
1726    #[cfg(feature = "serde")]
1727    #[derive(Debug, serde::Deserialize, PartialEq)]
1728    struct TestLenientStruct {
1729        #[serde(deserialize_with = "super::lenient_block_number_or_tag::deserialize")]
1730        block: BlockNumberOrTag,
1731    }
1732
1733    #[test]
1734    #[cfg(feature = "serde")]
1735    fn test_lenient_block_number_or_tag() {
1736        // Test parsing numeric strings
1737        let lenient_struct: TestLenientStruct =
1738            serde_json::from_str(r#"{"block": "0x1"}"#).unwrap();
1739        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(1));
1740
1741        let lenient_struct: TestLenientStruct =
1742            serde_json::from_str(r#"{"block": "123"}"#).unwrap();
1743        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(123));
1744
1745        // Test parsing tags
1746        let lenient_struct: TestLenientStruct =
1747            serde_json::from_str(r#"{"block": "latest"}"#).unwrap();
1748        assert_eq!(lenient_struct.block, BlockNumberOrTag::Latest);
1749
1750        let lenient_struct: TestLenientStruct =
1751            serde_json::from_str(r#"{"block": "finalized"}"#).unwrap();
1752        assert_eq!(lenient_struct.block, BlockNumberOrTag::Finalized);
1753
1754        let lenient_struct: TestLenientStruct =
1755            serde_json::from_str(r#"{"block": "safe"}"#).unwrap();
1756        assert_eq!(lenient_struct.block, BlockNumberOrTag::Safe);
1757
1758        let lenient_struct: TestLenientStruct =
1759            serde_json::from_str(r#"{"block": "earliest"}"#).unwrap();
1760        assert_eq!(lenient_struct.block, BlockNumberOrTag::Earliest);
1761
1762        let lenient_struct: TestLenientStruct =
1763            serde_json::from_str(r#"{"block": "pending"}"#).unwrap();
1764        assert_eq!(lenient_struct.block, BlockNumberOrTag::Pending);
1765
1766        // Test parsing raw numbers (not strings)
1767        let lenient_struct: TestLenientStruct = serde_json::from_str(r#"{"block": 123}"#).unwrap();
1768        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(123));
1769
1770        let lenient_struct: TestLenientStruct = serde_json::from_str(r#"{"block": 0}"#).unwrap();
1771        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(0));
1772
1773        // Test invalid inputs
1774        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": "invalid"}"#).is_err());
1775        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": null}"#).is_err());
1776        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": {}}"#).is_err());
1777    }
1778
1779    #[test]
1780    #[cfg(feature = "serde")]
1781    fn test_lenient_block_number_or_tag_wrapper() {
1782        // Test the LenientBlockNumberOrTag wrapper directly
1783        let block_number: LenientBlockNumberOrTag = serde_json::from_str("\"latest\"").unwrap();
1784        assert_eq!(block_number.0, BlockNumberOrTag::Latest);
1785
1786        let block_number: LenientBlockNumberOrTag = serde_json::from_str("123").unwrap();
1787        assert_eq!(block_number.0, BlockNumberOrTag::Number(123));
1788
1789        let block_number: LenientBlockNumberOrTag = serde_json::from_str("\"0x1\"").unwrap();
1790        assert_eq!(block_number.0, BlockNumberOrTag::Number(1));
1791    }
1792}