Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/lib/src/trie/prefix_proof/tests.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2023  Pierre Krieger
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
#![cfg(test)]
19
20
use super::{prefix_scan, Config, ResumeOutcome};
21
22
// TODO: more tests
23
24
#[test]
25
1
fn regression_test_174() {
26
1
    let test_data = serde_json::from_str::<TestData>(include_str!("./test.json")).unwrap();
27
1
28
1
    let mut prefix_scan = prefix_scan(Config {
29
1
        prefix: &test_data.prefix.0,
30
1
        trie_root_hash: <[u8; 32]>::try_from(&test_data.trie_root_hash.0[..]).unwrap(),
31
1
        full_storage_values_required: true,
32
1
    });
33
34
6
    for proof in test_data.proofs {
35
6
        match prefix_scan.resume_all_keys(&proof.0) {
36
5
            Ok(ResumeOutcome::InProgress(scan)) => {
37
5
                prefix_scan = scan;
38
5
                continue;
39
            }
40
1
            Ok(ResumeOutcome::Success { mut entries, .. }) => {
41
1
                let mut expected = test_data
42
1
                    .expected_entries
43
1
                    .into_iter()
44
922
                    .map(|e| e.0)
45
1
                    .collect::<Vec<_>>();
46
1
                expected.sort();
47
6.48k
                entries.sort_by(|(key1, _), (key2, _)| key1.cmp(&key2));
48
1
                assert_eq!(
49
922
                    entries.into_iter().map(|(key, _)| key).collect::<Vec<_>>(),
50
1
                    expected
51
1
                );
52
1
                return;
53
            }
54
0
            Err((_, err)) => panic!("{err:?}"),
55
        }
56
    }
57
1
}
58
59
9
#[derive(serde::Serialize, 
s5
erde::Deserialize)]
_RINvXs_NvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtBa_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB5_7___FieldB1j_11deserializeINtNtCscu7pqq74Vb8_10serde_json2de6MapKeyNtNtB2K_4read7StrReadEEBg_
Line
Count
Source
59
4
#[derive(serde::Serialize, serde::Deserialize)]
Unexecuted instantiation: _RINvXs0_NvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtBb_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB6_9___VisitorNtB1m_7Visitor9visit_seqINtNtCscu7pqq74Vb8_10serde_json2de9SeqAccessNtNtB2U_4read7StrReadEEBh_
_RINvXs0_NvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtBb_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB6_9___VisitorNtB1m_7Visitor9visit_mapINtNtCscu7pqq74Vb8_10serde_json2de9MapAccessNtNtB2U_4read7StrReadEEBh_
Line
Count
Source
59
5
#[derive(serde::Serialize, 
s1
erde::Deserialize)]
_RINvXNvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtB8_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB3_14___FieldVisitorNtB1j_7Visitor9visit_strNtNtCscu7pqq74Vb8_10serde_json5error5ErrorEBe_
Line
Count
Source
59
4
#[derive(serde::Serialize, serde::Deserialize)]
Unexecuted instantiation: _RNvXs0_NvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtBa_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB5_9___VisitorNtB1l_7Visitor9expecting
Unexecuted instantiation: _RNvXNvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtB7_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB2_14___FieldVisitorNtB1i_7Visitor9expecting
Unexecuted instantiation: _RINvXNvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtB8_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB3_14___FieldVisitorNtB1j_7Visitor9visit_u64pEBe_
Unexecuted instantiation: _RINvXNvXNvNtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testss_1__NtB8_8TestDataNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeNtB3_14___FieldVisitorNtB1j_7Visitor11visit_bytespEBe_
60
struct TestData {
61
    prefix: HexString,
62
    trie_root_hash: HexString,
63
    proofs: Vec<HexString>,
64
    expected_entries: Vec<HexString>,
65
}
66
67
#[derive(Clone, PartialEq, Eq, Hash)]
68
struct HexString(Vec<u8>);
69
70
impl serde::Serialize for HexString {
71
0
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72
0
    where
73
0
        S: serde::Serializer,
74
0
    {
75
0
        serde::Serialize::serialize(&("0x".to_string() + &hex::encode(&self.0)), serializer)
76
0
    }
77
}
78
79
impl<'a> serde::Deserialize<'a> for HexString {
80
930
    fn deserialize<D>(deserializer: D) -> Result<HexString, D::Error>
81
930
    where
82
930
        D: serde::Deserializer<'a>,
83
930
    {
84
930
        let string = String::deserialize(deserializer)
?0
;
85
86
930
        if string.is_empty() {
87
0
            return Ok(HexString(Vec::new()));
88
930
        }
89
930
90
930
        if !string.starts_with("0x") {
91
0
            return Err(serde::de::Error::custom(
92
0
                "hexadecimal string doesn't start with 0x",
93
0
            ));
94
930
        }
95
96
930
        let bytes = hex::decode(&string[2..]).map_err(serde::de::Error::custom)
?0
;
97
930
        Ok(HexString(bytes))
98
930
    }
Unexecuted instantiation: _RINvXs_NtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testsNtB5_9HexStringNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeINtNvNtNtB1e_9___private2de13missing_field24MissingFieldDeserializerNtNtCscu7pqq74Vb8_10serde_json5error5ErrorEEBb_
_RINvXs_NtNtNtCsN16ciHI6Qf_7smoldot4trie12prefix_proof5testsNtB5_9HexStringNtNtCsf0yC2YK6bpM_5serde2de11Deserialize11deserializeQINtNtCscu7pqq74Vb8_10serde_json2de12DeserializerNtNtB27_4read7StrReadEEBb_
Line
Count
Source
80
930
    fn deserialize<D>(deserializer: D) -> Result<HexString, D::Error>
81
930
    where
82
930
        D: serde::Deserializer<'a>,
83
930
    {
84
930
        let string = String::deserialize(deserializer)
?0
;
85
86
930
        if string.is_empty() {
87
0
            return Ok(HexString(Vec::new()));
88
930
        }
89
930
90
930
        if !string.starts_with("0x") {
91
0
            return Err(serde::de::Error::custom(
92
0
                "hexadecimal string doesn't start with 0x",
93
0
            ));
94
930
        }
95
96
930
        let bytes = hex::decode(&string[2..]).map_err(serde::de::Error::custom)
?0
;
97
930
        Ok(HexString(bytes))
98
930
    }
99
}