Skip to main content

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_all = "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) => alloy_serde::quantity::serialize(&x, serializer),
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    #[cfg(feature = "serde")]
989    fn block_number_or_tag_serialization() {
990        let number = BlockNumberOrTag::Number(0);
991        assert_eq!(serde_json::to_string(&number).unwrap(), "\"0x0\"");
992
993        let number = BlockNumberOrTag::Number(16);
994        assert_eq!(serde_json::to_string(&number).unwrap(), "\"0x10\"");
995
996        let latest = BlockNumberOrTag::Latest;
997        assert_eq!(serde_json::to_string(&latest).unwrap(), "\"latest\"");
998
999        let pending = BlockNumberOrTag::Pending;
1000        assert_eq!(serde_json::to_string(&pending).unwrap(), "\"pending\"");
1001    }
1002
1003    #[test]
1004    fn block_id_as_u64() {
1005        assert_eq!(BlockId::number(123).as_u64(), Some(123));
1006        assert_eq!(BlockId::number(0).as_u64(), Some(0));
1007        assert_eq!(BlockId::earliest().as_u64(), None);
1008        assert_eq!(BlockId::latest().as_u64(), None);
1009        assert_eq!(BlockId::pending().as_u64(), None);
1010        assert_eq!(BlockId::safe().as_u64(), None);
1011        assert_eq!(BlockId::hash(BlockHash::ZERO).as_u64(), None);
1012        assert_eq!(BlockId::hash_canonical(BlockHash::ZERO).as_u64(), None);
1013    }
1014
1015    #[test]
1016    #[cfg(feature = "serde")]
1017    fn can_parse_eip1898_block_ids() {
1018        let num = serde_json::json!(
1019            { "blockNumber": "0x0" }
1020        );
1021        let id = serde_json::from_value::<BlockId>(num).unwrap();
1022        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Number(0u64)));
1023
1024        let num = serde_json::json!(
1025            { "blockNumber": "pending" }
1026        );
1027        let id = serde_json::from_value::<BlockId>(num).unwrap();
1028        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Pending));
1029
1030        let num = serde_json::json!(
1031            { "blockNumber": "latest" }
1032        );
1033        let id = serde_json::from_value::<BlockId>(num).unwrap();
1034        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Latest));
1035
1036        let num = serde_json::json!(
1037            { "blockNumber": "finalized" }
1038        );
1039        let id = serde_json::from_value::<BlockId>(num).unwrap();
1040        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Finalized));
1041
1042        let num = serde_json::json!(
1043            { "blockNumber": "safe" }
1044        );
1045        let id = serde_json::from_value::<BlockId>(num).unwrap();
1046        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Safe));
1047
1048        let num = serde_json::json!(
1049            { "blockNumber": "earliest" }
1050        );
1051        let id = serde_json::from_value::<BlockId>(num).unwrap();
1052        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Earliest));
1053
1054        let num = serde_json::json!("0x0");
1055        let id = serde_json::from_value::<BlockId>(num).unwrap();
1056        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Number(0u64)));
1057
1058        let num = serde_json::json!("pending");
1059        let id = serde_json::from_value::<BlockId>(num).unwrap();
1060        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Pending));
1061
1062        let num = serde_json::json!("latest");
1063        let id = serde_json::from_value::<BlockId>(num).unwrap();
1064        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Latest));
1065
1066        let num = serde_json::json!("finalized");
1067        let id = serde_json::from_value::<BlockId>(num).unwrap();
1068        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Finalized));
1069
1070        let num = serde_json::json!("safe");
1071        let id = serde_json::from_value::<BlockId>(num).unwrap();
1072        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Safe));
1073
1074        let num = serde_json::json!("earliest");
1075        let id = serde_json::from_value::<BlockId>(num).unwrap();
1076        assert_eq!(id, BlockId::Number(BlockNumberOrTag::Earliest));
1077
1078        let num = serde_json::json!(
1079            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" }
1080        );
1081        let id = serde_json::from_value::<BlockId>(num).unwrap();
1082        assert_eq!(
1083            id,
1084            BlockId::Hash(
1085                "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
1086                    .parse::<B256>()
1087                    .unwrap()
1088                    .into()
1089            )
1090        );
1091    }
1092
1093    #[test]
1094    fn display_rpc_block_hash() {
1095        let hash = RpcBlockHash::from_hash(HASH, Some(true));
1096
1097        assert_eq!(
1098            hash.to_string(),
1099            "canonical hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1100        );
1101
1102        let hash = RpcBlockHash::from_hash(HASH, None);
1103
1104        assert_eq!(
1105            hash.to_string(),
1106            "hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1107        );
1108    }
1109
1110    #[test]
1111    fn display_block_id() {
1112        let id = BlockId::hash(HASH);
1113
1114        assert_eq!(
1115            id.to_string(),
1116            "hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1117        );
1118
1119        let id = BlockId::hash_canonical(HASH);
1120
1121        assert_eq!(
1122            id.to_string(),
1123            "canonical hash 0x1a15e3c30cf094a99826869517b16d185d45831d3a494f01030b0001a9d3ebb9"
1124        );
1125
1126        let id = BlockId::number(100000);
1127
1128        assert_eq!(id.to_string(), "0x186a0");
1129
1130        let id = BlockId::latest();
1131
1132        assert_eq!(id.to_string(), "latest");
1133
1134        let id = BlockId::safe();
1135
1136        assert_eq!(id.to_string(), "safe");
1137
1138        let id = BlockId::finalized();
1139
1140        assert_eq!(id.to_string(), "finalized");
1141
1142        let id = BlockId::earliest();
1143
1144        assert_eq!(id.to_string(), "earliest");
1145
1146        let id = BlockId::pending();
1147
1148        assert_eq!(id.to_string(), "pending");
1149    }
1150
1151    #[test]
1152    fn test_block_number_or_tag() {
1153        // Test Latest variant
1154        let latest = BlockNumberOrTag::Latest;
1155        assert_eq!(latest.as_number(), None);
1156        assert!(latest.is_latest());
1157        assert!(!latest.is_number());
1158        assert!(!latest.is_finalized());
1159        assert!(!latest.is_safe());
1160        assert!(!latest.is_pending());
1161        assert!(!latest.is_earliest());
1162
1163        // Test Finalized variant
1164        let finalized = BlockNumberOrTag::Finalized;
1165        assert_eq!(finalized.as_number(), None);
1166        assert!(finalized.is_finalized());
1167        assert!(!finalized.is_latest());
1168        assert!(!finalized.is_number());
1169        assert!(!finalized.is_safe());
1170        assert!(!finalized.is_pending());
1171        assert!(!finalized.is_earliest());
1172
1173        // Test Safe variant
1174        let safe = BlockNumberOrTag::Safe;
1175        assert_eq!(safe.as_number(), None);
1176        assert!(safe.is_safe());
1177        assert!(!safe.is_latest());
1178        assert!(!safe.is_number());
1179        assert!(!safe.is_finalized());
1180        assert!(!safe.is_pending());
1181        assert!(!safe.is_earliest());
1182
1183        // Test Earliest variant
1184        let earliest = BlockNumberOrTag::Earliest;
1185        assert_eq!(earliest.as_number(), None);
1186        assert!(earliest.is_earliest());
1187        assert!(!earliest.is_latest());
1188        assert!(!earliest.is_number());
1189        assert!(!earliest.is_finalized());
1190        assert!(!earliest.is_safe());
1191        assert!(!earliest.is_pending());
1192
1193        // Test Pending variant
1194        let pending = BlockNumberOrTag::Pending;
1195        assert_eq!(pending.as_number(), None);
1196        assert!(pending.is_pending());
1197        assert!(!pending.is_latest());
1198        assert!(!pending.is_number());
1199        assert!(!pending.is_finalized());
1200        assert!(!pending.is_safe());
1201        assert!(!pending.is_earliest());
1202
1203        // Test Number variant
1204        let number = BlockNumberOrTag::Number(42);
1205        assert_eq!(number.as_number(), Some(42));
1206        assert!(number.is_number());
1207        assert!(!number.is_latest());
1208        assert!(!number.is_finalized());
1209        assert!(!number.is_safe());
1210        assert!(!number.is_pending());
1211        assert!(!number.is_earliest());
1212    }
1213
1214    #[test]
1215    fn test_block_number_or_tag_from() {
1216        // Test conversion from u64
1217        let num = 100u64;
1218        let block: BlockNumberOrTag = num.into();
1219        assert_eq!(block, BlockNumberOrTag::Number(100));
1220
1221        // Test conversion from U64
1222        let num = U64::from(200);
1223        let block: BlockNumberOrTag = num.into();
1224        assert_eq!(block, BlockNumberOrTag::Number(200));
1225    }
1226
1227    #[test]
1228    fn test_block_id() {
1229        let hash = BlockHash::random();
1230
1231        // Block hash
1232        let block_id_hash = BlockId::hash(hash);
1233        assert_eq!(block_id_hash.as_block_hash(), Some(hash));
1234        assert!(block_id_hash.is_hash());
1235        assert!(!block_id_hash.is_number());
1236        assert!(!block_id_hash.is_latest());
1237        assert!(!block_id_hash.is_pending());
1238        assert!(!block_id_hash.is_safe());
1239        assert!(!block_id_hash.is_finalized());
1240        assert!(!block_id_hash.is_earliest());
1241
1242        // Block number
1243        let block_id_number = BlockId::number(123);
1244        assert_eq!(block_id_number.as_u64(), Some(123));
1245        assert!(block_id_number.is_number());
1246        assert!(!block_id_number.is_hash());
1247        assert!(!block_id_number.is_latest());
1248        assert!(!block_id_number.is_pending());
1249        assert!(!block_id_number.is_safe());
1250        assert!(!block_id_number.is_finalized());
1251        assert!(!block_id_number.is_earliest());
1252
1253        // Latest block
1254        let block_latest = BlockId::latest();
1255        assert!(block_latest.is_latest());
1256        assert!(!block_latest.is_number());
1257        assert!(!block_latest.is_hash());
1258        assert!(!block_latest.is_pending());
1259        assert!(!block_latest.is_safe());
1260        assert!(!block_latest.is_finalized());
1261        assert!(!block_latest.is_earliest());
1262
1263        // Pending block
1264        let block_pending = BlockId::pending();
1265        assert!(block_pending.is_pending());
1266        assert!(!block_pending.is_latest());
1267        assert!(!block_pending.is_number());
1268        assert!(!block_pending.is_hash());
1269        assert!(!block_pending.is_safe());
1270        assert!(!block_pending.is_finalized());
1271        assert!(!block_pending.is_earliest());
1272
1273        // Safe block
1274        let block_safe = BlockId::safe();
1275        assert!(block_safe.is_safe());
1276        assert!(!block_safe.is_latest());
1277        assert!(!block_safe.is_number());
1278        assert!(!block_safe.is_hash());
1279        assert!(!block_safe.is_pending());
1280        assert!(!block_safe.is_finalized());
1281        assert!(!block_safe.is_earliest());
1282
1283        // Finalized block
1284        let block_finalized = BlockId::finalized();
1285        assert!(block_finalized.is_finalized());
1286        assert!(!block_finalized.is_latest());
1287        assert!(!block_finalized.is_number());
1288        assert!(!block_finalized.is_hash());
1289        assert!(!block_finalized.is_pending());
1290        assert!(!block_finalized.is_safe());
1291        assert!(!block_finalized.is_earliest());
1292
1293        // Earliest block
1294        let block_earliest = BlockId::earliest();
1295        assert!(block_earliest.is_earliest());
1296        assert!(!block_earliest.is_latest());
1297        assert!(!block_earliest.is_number());
1298        assert!(!block_earliest.is_hash());
1299        assert!(!block_earliest.is_pending());
1300        assert!(!block_earliest.is_safe());
1301        assert!(!block_earliest.is_finalized());
1302
1303        // Default block
1304        assert!(BlockId::default().is_latest());
1305        assert!(!BlockId::default().is_number());
1306        assert!(!BlockId::default().is_hash());
1307        assert!(!BlockId::default().is_pending());
1308        assert!(!BlockId::default().is_safe());
1309        assert!(!BlockId::default().is_finalized());
1310        assert!(!BlockId::default().is_earliest());
1311    }
1312
1313    #[test]
1314    fn test_u64_to_block_id() {
1315        // Simple u64
1316        let num: u64 = 123;
1317        let block_id: BlockId = num.into();
1318
1319        match block_id {
1320            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 123),
1321            _ => panic!("Expected BlockId::Number with 123"),
1322        }
1323
1324        // Big integer U64
1325        let num: U64 = U64::from(456);
1326        let block_id: BlockId = num.into();
1327
1328        match block_id {
1329            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 456),
1330            _ => panic!("Expected BlockId::Number with 456"),
1331        }
1332
1333        // u64 as HashOrNumber
1334        let num: u64 = 789;
1335        let block_id: BlockId = HashOrNumber::Number(num).into();
1336
1337        match block_id {
1338            BlockId::Number(BlockNumberOrTag::Number(n)) => assert_eq!(n, 789),
1339            _ => panic!("Expected BlockId::Number with 789"),
1340        }
1341    }
1342
1343    #[test]
1344    fn test_block_number_or_tag_to_block_id() {
1345        let block_number_or_tag = BlockNumberOrTag::Pending;
1346        let block_id: BlockId = block_number_or_tag.into();
1347
1348        match block_id {
1349            BlockId::Number(BlockNumberOrTag::Pending) => {}
1350            _ => panic!("Expected BlockId::Number with Pending"),
1351        }
1352    }
1353
1354    #[test]
1355    fn test_hash_or_number_to_block_id_hash() {
1356        // B256 wrapped in HashOrNumber
1357        let hash: B256 = B256::random();
1358        let block_id: BlockId = HashOrNumber::Hash(hash).into();
1359
1360        match block_id {
1361            BlockId::Hash(rpc_block_hash) => assert_eq!(rpc_block_hash.block_hash, hash),
1362            _ => panic!("Expected BlockId::Hash"),
1363        }
1364
1365        // Simple B256
1366        let hash: B256 = B256::random();
1367        let block_id: BlockId = hash.into();
1368
1369        match block_id {
1370            BlockId::Hash(rpc_block_hash) => assert_eq!(rpc_block_hash.block_hash, hash),
1371            _ => panic!("Expected BlockId::Hash"),
1372        }
1373
1374        // Tuple with B256 and canonical flag
1375        let hash: B256 = B256::random();
1376        let block_id: BlockId = (hash, Some(true)).into();
1377
1378        match block_id {
1379            BlockId::Hash(rpc_block_hash) => {
1380                assert_eq!(rpc_block_hash.block_hash, hash);
1381                assert_eq!(rpc_block_hash.require_canonical, Some(true));
1382            }
1383            _ => panic!("Expected BlockId::Hash with canonical flag"),
1384        }
1385    }
1386
1387    #[test]
1388    fn test_hash_or_number_as_number() {
1389        // Test with a number
1390        let hash_or_number = HashOrNumber::Number(123);
1391        assert_eq!(hash_or_number.as_number(), Some(123));
1392
1393        // Test with a hash
1394        let hash = B256::random();
1395        let hash_or_number = HashOrNumber::Hash(hash);
1396        assert_eq!(hash_or_number.as_number(), None);
1397    }
1398
1399    #[test]
1400    fn test_hash_or_number_as_hash() {
1401        // Test with a hash
1402        let hash = B256::random();
1403        let hash_or_number = HashOrNumber::Hash(hash);
1404        assert_eq!(hash_or_number.as_hash(), Some(hash));
1405
1406        // Test with a number
1407        let hash_or_number = HashOrNumber::Number(456);
1408        assert_eq!(hash_or_number.as_hash(), None);
1409    }
1410
1411    #[test]
1412    fn test_hash_or_number_conversions() {
1413        // Test conversion from B256
1414        let hash = B256::random();
1415        let hash_or_number: HashOrNumber = hash.into();
1416        assert_eq!(hash_or_number, HashOrNumber::Hash(hash));
1417
1418        // Test conversion from &B256
1419        let hash_ref: HashOrNumber = (&hash).into();
1420        assert_eq!(hash_ref, HashOrNumber::Hash(hash));
1421
1422        // Test conversion from u64
1423        let number: u64 = 123;
1424        let hash_or_number: HashOrNumber = number.into();
1425        assert_eq!(hash_or_number, HashOrNumber::Number(number));
1426
1427        // Test conversion from U64
1428        let u64_value = U64::from(456);
1429        let hash_or_number: HashOrNumber = u64_value.into();
1430        assert_eq!(hash_or_number, HashOrNumber::Number(u64_value.to::<u64>()));
1431
1432        // Test conversion from RpcBlockHash (assuming RpcBlockHash is convertible to B256)
1433        let rpc_block_hash = RpcBlockHash { block_hash: hash, require_canonical: Some(true) };
1434        let hash_or_number: HashOrNumber = rpc_block_hash.into();
1435        assert_eq!(hash_or_number, HashOrNumber::Hash(hash));
1436    }
1437
1438    #[test]
1439    fn test_hash_or_number_rlp_roundtrip_hash() {
1440        // Test case: encoding and decoding a B256 hash
1441        let original_hash = B256::random();
1442        let hash_or_number: HashOrNumber = HashOrNumber::Hash(original_hash);
1443
1444        // Encode the HashOrNumber
1445        let mut buf = Vec::new();
1446        hash_or_number.encode(&mut buf);
1447
1448        // Decode the encoded bytes
1449        let decoded: HashOrNumber = HashOrNumber::decode(&mut &buf[..]).expect("Decoding failed");
1450
1451        // Assert that the decoded value matches the original
1452        assert_eq!(decoded, hash_or_number);
1453    }
1454
1455    #[test]
1456    fn test_hash_or_number_rlp_roundtrip_u64() {
1457        // Test case: encoding and decoding a u64 number
1458        let original_number: u64 = 12345;
1459        let hash_or_number: HashOrNumber = HashOrNumber::Number(original_number);
1460
1461        // Encode the HashOrNumber
1462        let mut buf = Vec::new();
1463        hash_or_number.encode(&mut buf);
1464
1465        // Decode the encoded bytes
1466        let decoded: HashOrNumber = HashOrNumber::decode(&mut &buf[..]).expect("Decoding failed");
1467
1468        // Assert that the decoded value matches the original
1469        assert_eq!(decoded, hash_or_number);
1470    }
1471
1472    #[test]
1473    fn test_numhash() {
1474        let number: u64 = 42;
1475        let hash = B256::random();
1476
1477        let num_hash = NumHash::new(number, hash);
1478
1479        // Validate the initial values
1480        assert_eq!(num_hash.number, number);
1481        assert_eq!(num_hash.hash, hash);
1482
1483        // Test into_components
1484        assert_eq!(num_hash.into_components(), (number, hash));
1485    }
1486
1487    #[test]
1488    fn test_numhash_matches_block_or_num() {
1489        let number: u64 = 42;
1490        let hash = B256::random();
1491
1492        let num_hash = NumHash::new(number, hash);
1493
1494        // Test matching by hash
1495        let block_hash = HashOrNumber::Hash(hash);
1496        assert!(num_hash.matches_block_or_num(&block_hash));
1497
1498        // Test matching by number
1499        let block_number = HashOrNumber::Number(number);
1500        assert!(num_hash.matches_block_or_num(&block_number));
1501
1502        // Test non-matching by different hash
1503        let different_hash = B256::random();
1504        let non_matching_hash = HashOrNumber::Hash(different_hash);
1505        assert!(!num_hash.matches_block_or_num(&non_matching_hash));
1506
1507        // Test non-matching by different number
1508        let different_number: u64 = 43;
1509        let non_matching_number = HashOrNumber::Number(different_number);
1510        assert!(!num_hash.matches_block_or_num(&non_matching_number));
1511    }
1512
1513    #[test]
1514    fn test_numhash_conversions() {
1515        // From a tuple (u64, B256)
1516        let number: u64 = 42;
1517        let hash = B256::random();
1518
1519        let num_hash_from_tuple: NumHash = (number, hash).into();
1520
1521        assert_eq!(num_hash_from_tuple.number, number);
1522        assert_eq!(num_hash_from_tuple.hash, hash);
1523
1524        // From a reversed tuple (B256, u64)
1525        let number: u64 = 42;
1526        let hash = B256::random();
1527
1528        let num_hash_from_reversed_tuple: NumHash = (hash, number).into();
1529
1530        assert_eq!(num_hash_from_reversed_tuple.number, number);
1531        assert_eq!(num_hash_from_reversed_tuple.hash, hash);
1532    }
1533
1534    #[test]
1535    #[cfg(feature = "serde")]
1536    fn test_block_id_from_str() {
1537        // Valid hexadecimal block ID (with 0x prefix)
1538        let hex_id = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
1539        assert_eq!(
1540            BlockId::from_str(hex_id).unwrap(),
1541            BlockId::Hash(RpcBlockHash::from_hash(B256::from_str(hex_id).unwrap(), None))
1542        );
1543
1544        // Valid tag strings
1545        assert_eq!(BlockId::from_str("latest").unwrap(), BlockNumberOrTag::Latest.into());
1546        assert_eq!(BlockId::from_str("finalized").unwrap(), BlockNumberOrTag::Finalized.into());
1547        assert_eq!(BlockId::from_str("safe").unwrap(), BlockNumberOrTag::Safe.into());
1548        assert_eq!(BlockId::from_str("earliest").unwrap(), BlockNumberOrTag::Earliest.into());
1549        assert_eq!(BlockId::from_str("pending").unwrap(), BlockNumberOrTag::Pending.into());
1550
1551        // Valid numeric string without prefix
1552        let numeric_string = "12345";
1553        let parsed_numeric_string = BlockId::from_str(numeric_string);
1554        assert!(parsed_numeric_string.is_ok());
1555
1556        // Hex interpretation of numeric string
1557        assert_eq!(
1558            BlockId::from_str("0x12345").unwrap(),
1559            BlockId::Number(BlockNumberOrTag::Number(74565))
1560        );
1561
1562        // Invalid non-numeric string
1563        let invalid_string = "invalid_block_id";
1564        let parsed_invalid_string = BlockId::from_str(invalid_string);
1565        assert!(parsed_invalid_string.is_err());
1566    }
1567
1568    /// Check parsing according to EIP-1898.
1569    #[test]
1570    #[cfg(feature = "serde")]
1571    fn can_parse_blockid_u64() {
1572        let num = serde_json::json!(
1573            {"blockNumber": "0xaf"}
1574        );
1575
1576        let id = serde_json::from_value::<BlockId>(num);
1577        assert_eq!(id.unwrap(), BlockId::from(175));
1578    }
1579
1580    #[test]
1581    #[cfg(feature = "serde")]
1582    fn can_parse_block_hash() {
1583        let block_hash =
1584            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1585                .unwrap();
1586        let block_hash_json = serde_json::json!(
1587            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}
1588        );
1589        let id = serde_json::from_value::<BlockId>(block_hash_json).unwrap();
1590        assert_eq!(id, BlockId::from(block_hash,));
1591    }
1592
1593    #[test]
1594    #[cfg(feature = "serde")]
1595    fn can_parse_block_hash_with_canonical() {
1596        let block_hash =
1597            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1598                .unwrap();
1599        let block_id = BlockId::Hash(RpcBlockHash::from_hash(block_hash, Some(true)));
1600        let block_hash_json = serde_json::json!(
1601            { "blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "requireCanonical": true }
1602        );
1603        let id = serde_json::from_value::<BlockId>(block_hash_json).unwrap();
1604        assert_eq!(id, block_id)
1605    }
1606    #[test]
1607    #[cfg(feature = "serde")]
1608    fn can_parse_blockid_tags() {
1609        let tags = [
1610            ("latest", BlockNumberOrTag::Latest),
1611            ("finalized", BlockNumberOrTag::Finalized),
1612            ("safe", BlockNumberOrTag::Safe),
1613            ("pending", BlockNumberOrTag::Pending),
1614        ];
1615        for (value, tag) in tags {
1616            let num = serde_json::json!({ "blockNumber": value });
1617            let id = serde_json::from_value::<BlockId>(num);
1618            assert_eq!(id.unwrap(), BlockId::from(tag))
1619        }
1620    }
1621    #[test]
1622    #[cfg(feature = "serde")]
1623    fn repeated_keys_is_err() {
1624        let num = serde_json::json!({"blockNumber": 1, "requireCanonical": true, "requireCanonical": false});
1625        assert!(serde_json::from_value::<BlockId>(num).is_err());
1626        let num =
1627            serde_json::json!({"blockNumber": 1, "requireCanonical": true, "blockNumber": 23});
1628        assert!(serde_json::from_value::<BlockId>(num).is_err());
1629    }
1630
1631    /// Serde tests
1632    #[test]
1633    #[cfg(feature = "serde")]
1634    fn serde_blockid_tags() {
1635        let block_ids = [
1636            BlockNumberOrTag::Latest,
1637            BlockNumberOrTag::Finalized,
1638            BlockNumberOrTag::Safe,
1639            BlockNumberOrTag::Pending,
1640        ]
1641        .map(BlockId::from);
1642        for block_id in &block_ids {
1643            let serialized = serde_json::to_string(&block_id).unwrap();
1644            let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1645            assert_eq!(deserialized, *block_id)
1646        }
1647    }
1648
1649    #[test]
1650    #[cfg(feature = "serde")]
1651    fn serde_blockid_number() {
1652        let block_id = BlockId::from(100u64);
1653        let serialized = serde_json::to_string(&block_id).unwrap();
1654        let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1655        assert_eq!(deserialized, block_id)
1656    }
1657
1658    #[test]
1659    #[cfg(feature = "serde")]
1660    fn serde_blockid_hash() {
1661        let block_id = BlockId::from(B256::default());
1662        let serialized = serde_json::to_string(&block_id).unwrap();
1663        let deserialized: BlockId = serde_json::from_str(&serialized).unwrap();
1664        assert_eq!(deserialized, block_id)
1665    }
1666
1667    #[test]
1668    #[cfg(feature = "serde")]
1669    fn serde_blockid_hash_from_str() {
1670        let val = "\"0x898753d8fdd8d92c1907ca21e68c7970abd290c647a202091181deec3f30a0b2\"";
1671        let block_hash: B256 = serde_json::from_str(val).unwrap();
1672        let block_id: BlockId = serde_json::from_str(val).unwrap();
1673        assert_eq!(block_id, BlockId::Hash(block_hash.into()));
1674    }
1675
1676    #[test]
1677    #[cfg(feature = "serde")]
1678    fn serde_rpc_payload_block_tag() {
1679        let payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},"latest"],"id":1,"jsonrpc":"2.0"}"#;
1680        let value: serde_json::Value = serde_json::from_str(payload).unwrap();
1681        let block_id_param = value.pointer("/params/1").unwrap();
1682        let block_id: BlockId = serde_json::from_value::<BlockId>(block_id_param.clone()).unwrap();
1683        assert_eq!(BlockId::Number(BlockNumberOrTag::Latest), block_id);
1684    }
1685
1686    #[test]
1687    #[cfg(feature = "serde")]
1688    fn serde_rpc_payload_block_object() {
1689        let example_payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},{"blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}],"id":1,"jsonrpc":"2.0"}"#;
1690        let value: serde_json::Value = serde_json::from_str(example_payload).unwrap();
1691        let block_id_param = value.pointer("/params/1").unwrap().to_string();
1692        let block_id: BlockId = serde_json::from_str::<BlockId>(&block_id_param).unwrap();
1693        let hash =
1694            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1695                .unwrap();
1696        assert_eq!(BlockId::from(hash), block_id);
1697        let serialized = serde_json::to_string(&BlockId::from(hash)).unwrap();
1698        assert_eq!("{\"blockHash\":\"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3\"}", serialized)
1699    }
1700
1701    #[test]
1702    #[cfg(feature = "serde")]
1703    fn serde_rpc_payload_block_number() {
1704        let example_payload = r#"{"method":"eth_call","params":[{"to":"0xebe8efa441b9302a0d7eaecc277c09d20d684540","data":"0x45848dfc"},{"blockNumber": "0x0"}],"id":1,"jsonrpc":"2.0"}"#;
1705        let value: serde_json::Value = serde_json::from_str(example_payload).unwrap();
1706        let block_id_param = value.pointer("/params/1").unwrap().to_string();
1707        let block_id: BlockId = serde_json::from_str::<BlockId>(&block_id_param).unwrap();
1708        assert_eq!(BlockId::from(0u64), block_id);
1709        let serialized = serde_json::to_string(&BlockId::from(0u64)).unwrap();
1710        assert_eq!("\"0x0\"", serialized)
1711    }
1712
1713    #[test]
1714    #[should_panic]
1715    #[cfg(feature = "serde")]
1716    fn serde_rpc_payload_block_number_duplicate_key() {
1717        let payload = r#"{"blockNumber": "0x132", "blockNumber": "0x133"}"#;
1718        let parsed_block_id = serde_json::from_str::<BlockId>(payload);
1719        parsed_block_id.unwrap();
1720    }
1721
1722    #[test]
1723    #[cfg(feature = "serde")]
1724    fn serde_rpc_payload_block_hash() {
1725        let payload = r#"{"blockHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"}"#;
1726        let parsed = serde_json::from_str::<BlockId>(payload).unwrap();
1727        let expected = BlockId::from(
1728            B256::from_str("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
1729                .unwrap(),
1730        );
1731        assert_eq!(parsed, expected);
1732    }
1733
1734    #[test]
1735    #[cfg(feature = "serde")]
1736    fn serde_blocknumber_non_0xprefix() {
1737        let s = "\"2\"";
1738        let err = serde_json::from_str::<BlockNumberOrTag>(s).unwrap_err();
1739        assert_eq!(err.to_string(), HexStringMissingPrefixError::default().to_string());
1740    }
1741
1742    #[cfg(feature = "serde")]
1743    #[derive(Debug, serde::Deserialize, PartialEq)]
1744    struct TestLenientStruct {
1745        #[serde(deserialize_with = "super::lenient_block_number_or_tag::deserialize")]
1746        block: BlockNumberOrTag,
1747    }
1748
1749    #[test]
1750    #[cfg(feature = "serde")]
1751    fn test_lenient_block_number_or_tag() {
1752        // Test parsing numeric strings
1753        let lenient_struct: TestLenientStruct =
1754            serde_json::from_str(r#"{"block": "0x1"}"#).unwrap();
1755        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(1));
1756
1757        let lenient_struct: TestLenientStruct =
1758            serde_json::from_str(r#"{"block": "123"}"#).unwrap();
1759        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(123));
1760
1761        // Test parsing tags
1762        let lenient_struct: TestLenientStruct =
1763            serde_json::from_str(r#"{"block": "latest"}"#).unwrap();
1764        assert_eq!(lenient_struct.block, BlockNumberOrTag::Latest);
1765
1766        let lenient_struct: TestLenientStruct =
1767            serde_json::from_str(r#"{"block": "finalized"}"#).unwrap();
1768        assert_eq!(lenient_struct.block, BlockNumberOrTag::Finalized);
1769
1770        let lenient_struct: TestLenientStruct =
1771            serde_json::from_str(r#"{"block": "safe"}"#).unwrap();
1772        assert_eq!(lenient_struct.block, BlockNumberOrTag::Safe);
1773
1774        let lenient_struct: TestLenientStruct =
1775            serde_json::from_str(r#"{"block": "earliest"}"#).unwrap();
1776        assert_eq!(lenient_struct.block, BlockNumberOrTag::Earliest);
1777
1778        let lenient_struct: TestLenientStruct =
1779            serde_json::from_str(r#"{"block": "pending"}"#).unwrap();
1780        assert_eq!(lenient_struct.block, BlockNumberOrTag::Pending);
1781
1782        // Test parsing raw numbers (not strings)
1783        let lenient_struct: TestLenientStruct = serde_json::from_str(r#"{"block": 123}"#).unwrap();
1784        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(123));
1785
1786        let lenient_struct: TestLenientStruct = serde_json::from_str(r#"{"block": 0}"#).unwrap();
1787        assert_eq!(lenient_struct.block, BlockNumberOrTag::Number(0));
1788
1789        // Test invalid inputs
1790        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": "invalid"}"#).is_err());
1791        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": null}"#).is_err());
1792        assert!(serde_json::from_str::<TestLenientStruct>(r#"{"block": {}}"#).is_err());
1793    }
1794
1795    #[test]
1796    #[cfg(feature = "serde")]
1797    fn test_lenient_block_number_or_tag_wrapper() {
1798        // Test the LenientBlockNumberOrTag wrapper directly
1799        let block_number: LenientBlockNumberOrTag = serde_json::from_str("\"latest\"").unwrap();
1800        assert_eq!(block_number.0, BlockNumberOrTag::Latest);
1801
1802        let block_number: LenientBlockNumberOrTag = serde_json::from_str("123").unwrap();
1803        assert_eq!(block_number.0, BlockNumberOrTag::Number(123));
1804
1805        let block_number: LenientBlockNumberOrTag = serde_json::from_str("\"0x1\"").unwrap();
1806        assert_eq!(block_number.0, BlockNumberOrTag::Number(1));
1807    }
1808}