Skip to main content

nautilus_blockchain/data/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use alloy::primitives::B256;
17
18pub mod client;
19pub mod core;
20pub mod subscription;
21
22// An event without an observed hash carries no constraint and always matches.
23pub(crate) fn block_hash_matches(observed: Option<B256>, cached: B256) -> bool {
24    observed.is_none_or(|hash| hash == cached)
25}
26
27#[cfg(test)]
28mod tests {
29    use std::str::FromStr;
30
31    use rstest::rstest;
32
33    use super::*;
34
35    fn fixture_hash(hash: &str) -> B256 {
36        B256::from_str(hash).expect("fixture block hash must be valid")
37    }
38
39    #[rstest]
40    #[case(
41        None,
42        "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
43        true
44    )]
45    #[case(
46        Some("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
47        "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
48        true
49    )]
50    #[case(
51        Some("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
52        "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
53        true
54    )]
55    #[case(
56        Some("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
57        "0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
58        true
59    )]
60    #[case(
61        Some("0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"),
62        "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
63        false
64    )]
65    fn test_block_hash_matches(
66        #[case] observed: Option<&str>,
67        #[case] cached: &str,
68        #[case] expected: bool,
69    ) {
70        let observed = observed.map(fixture_hash);
71        assert_eq!(block_hash_matches(observed, fixture_hash(cached)), expected);
72    }
73}