stratus/eth/primitives/
block_filter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::fmt::Display;

use display_json::DebugAsJson;

use super::PointInTime;
use crate::alias::JsonValue;
use crate::eth::primitives::BlockNumber;
use crate::eth::primitives::Hash;

#[derive(DebugAsJson, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, Hash)]
#[cfg_attr(test, derive(fake::Dummy))]
pub enum BlockFilter {
    /// Information from the last mined block.
    #[default]
    Latest,

    /// Information from the block being mined.
    Pending,

    /// Information from the first block.
    Earliest,

    /// Retrieve a block by its hash.
    Hash(Hash),

    /// Retrieve a block by its number.
    Number(BlockNumber),
}

impl Display for BlockFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BlockFilter::Latest => write!(f, "latest"),
            BlockFilter::Pending => write!(f, "pending"),
            BlockFilter::Earliest => write!(f, "earliest"),
            BlockFilter::Hash(block_hash) => write!(f, "{block_hash}"),
            BlockFilter::Number(block_number) => write!(f, "{block_number}"),
        }
    }
}

impl From<PointInTime> for BlockFilter {
    fn from(point_in_time: PointInTime) -> Self {
        match point_in_time {
            PointInTime::Mined => Self::Latest,
            PointInTime::Pending => Self::Pending,
            PointInTime::MinedPast(number) => Self::Number(number),
        }
    }
}

// -----------------------------------------------------------------------------
// Serialization / Deserilization
// -----------------------------------------------------------------------------

impl<'de> serde::Deserialize<'de> for BlockFilter {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = JsonValue::deserialize(deserializer)?;
        match value {
            // default
            JsonValue::Null => Ok(Self::Latest),

            // number
            serde_json::Value::Number(number) => match number.as_u64() {
                Some(number) => Ok(Self::Number(BlockNumber::from(number))),
                None => Err(serde::de::Error::custom("block filter must be zero or a positive integer")),
            },

            // string
            serde_json::Value::String(value) => {
                match value.as_str() {
                    // parse special keywords
                    "latest" | "Latest" => Ok(Self::Latest),
                    "pending" | "Pending" => Ok(Self::Pending),
                    "earliest" | "Earliest" => Ok(Self::Earliest),

                    // parse hash (64: H256 without 0x prefix; 66: H256 with 0x prefix)
                    s if s.len() == 64 || s.len() == 66 => {
                        let hash: Hash = s.parse().map_err(serde::de::Error::custom)?;
                        Ok(Self::Hash(hash))
                    }
                    // parse number
                    s => {
                        let number: BlockNumber = s.parse().map_err(serde::de::Error::custom)?;
                        Ok(Self::Number(number))
                    }
                }
            }

            serde_json::Value::Object(map) => {
                if map.len() != 1 {
                    return Err(serde::de::Error::custom("value was an object with an unexpected number of fields"));
                }
                let Some((key, value)) = map.iter().next() else {
                    return Err(serde::de::Error::custom("value was an object with no fields"));
                };
                let Some(value_str) = value.as_str() else {
                    return Err(serde::de::Error::custom("value was an object with non-str fields"));
                };
                match key.as_str() {
                    "Hash" => {
                        let hash: Hash = value_str.parse().map_err(serde::de::Error::custom)?;
                        Ok(Self::Hash(hash))
                    }
                    "Number" => {
                        let number: BlockNumber = value_str.parse().map_err(serde::de::Error::custom)?;
                        Ok(Self::Number(number))
                    }
                    _ => Err(serde::de::Error::custom(
                        "value was an object but its field was neither \"Hash\" nor \"Number\"",
                    )),
                }
            }

            // unhandled type
            _ => Err(serde::de::Error::custom("block filter must be a string or integer")),
        }
    }
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::eth::primitives::*;

    #[test]
    fn serde_block_number_with_latest() {
        let json = json!("latest");
        assert_eq!(serde_json::from_value::<BlockFilter>(json).unwrap(), BlockFilter::Latest);
    }

    #[test]
    fn serde_block_number_with_number() {
        let json = json!("0x2");
        assert_eq!(serde_json::from_value::<BlockFilter>(json).unwrap(), BlockFilter::Number(2usize.into()));
    }
}