Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/lib/src/chain_spec/tests.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2019-2022  Parity Technologies (UK) Ltd.
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::{Bootnode, ChainSpec, CheckpointToChainInformationError};
21
22
#[test]
23
1
fn can_decode_polkadot_genesis() {
24
1
    let spec = &include_bytes!("./tests/example.json")[..];
25
1
    let specs = ChainSpec::from_json_bytes(spec).unwrap();
26
1
    assert_eq!(specs.id(), "polkadot");
27
28
    // code_substitutes field
29
1
    assert_eq!(specs.client_spec.code_substitutes.get(&1), None);
30
1
    assert!(specs.client_spec.code_substitutes.get(&5203203).is_some());
31
32
    // bootnodes field
33
1
    assert_eq!(
34
1
        specs.boot_nodes().collect::<Vec<_>>(),
35
1
        vec![
36
1
            Bootnode::Parsed {
37
1
                multiaddr: "/dns4/p2p.cc1-0.polkadot.network/tcp/30100".into(),
38
1
                peer_id: vec![
39
1
                    0, 36, 8, 1, 18, 32, 71, 154, 61, 188, 212, 39, 215, 192, 217, 22, 168, 87,
40
1
                    162, 148, 234, 176, 0, 195, 4, 31, 109, 123, 175, 185, 26, 169, 218, 92, 192,
41
1
                    0, 126, 111
42
1
                ]
43
1
            },
44
1
            Bootnode::Parsed {
45
1
                multiaddr: "/dns4/cc1-1.parity.tech/tcp/30333".into(),
46
1
                peer_id: vec![
47
1
                    0, 36, 8, 1, 18, 32, 82, 103, 22, 131, 223, 29, 166, 147, 119, 199, 217, 185,
48
1
                    69, 70, 87, 73, 165, 110, 224, 141, 138, 44, 217, 75, 191, 55, 156, 212, 204,
49
1
                    41, 11, 59
50
1
                ]
51
1
            },
52
1
            Bootnode::UnrecognizedFormat("/some/wrong/multiaddress")
53
1
        ]
54
1
    );
55
1
}
56
57
#[test]
58
1
fn relay_chain_para_id_either_both_present_or_absent() {
59
1
    ChainSpec::from_json_bytes(
60
1
        r#"{
61
1
            "name": "Test",
62
1
            "id": "test",
63
1
            "bootNodes": [],
64
1
            "genesis": {
65
1
              "raw": {
66
1
                "top": {},
67
1
                "childrenDefault": {}
68
1
              }
69
1
            }
70
1
          }
71
1
          "#,
72
1
    )
73
1
    .unwrap();
74
1
75
1
    ChainSpec::from_json_bytes(
76
1
        r#"{
77
1
            "name": "Test",
78
1
            "id": "test",
79
1
            "bootNodes": [],
80
1
            "relay_chain": "foo",
81
1
            "para_id": 1,
82
1
            "genesis": {
83
1
              "raw": {
84
1
                "top": {},
85
1
                "childrenDefault": {}
86
1
              }
87
1
            }
88
1
          }
89
1
          "#,
90
1
    )
91
1
    .unwrap();
92
1
93
1
    ChainSpec::from_json_bytes(
94
1
        r#"{
95
1
            "name": "Test",
96
1
            "id": "test",
97
1
            "bootNodes": [],
98
1
            "relayChain": "foo",
99
1
            "paraId": 1,
100
1
            "genesis": {
101
1
              "raw": {
102
1
                "top": {},
103
1
                "childrenDefault": {}
104
1
              }
105
1
            }
106
1
          }
107
1
          "#,
108
1
    )
109
1
    .unwrap();
110
1
111
1
    assert!(ChainSpec::from_json_bytes(
112
1
        r#"{
113
1
            "name": "Test",
114
1
            "id": "test",
115
1
            "bootNodes": [],
116
1
            "relayChain": "foo",
117
1
            "genesis": {
118
1
              "raw": {
119
1
                "top": {},
120
1
                "childrenDefault": {}
121
1
              }
122
1
            }
123
1
          }
124
1
          "#,
125
1
    )
126
1
    .is_err());
127
128
1
    assert!(ChainSpec::from_json_bytes(
129
1
        r#"{
130
1
            "name": "Test",
131
1
            "id": "test",
132
1
            "bootNodes": [],
133
1
            "paraId": 1,
134
1
            "genesis": {
135
1
              "raw": {
136
1
                "top": {},
137
1
                "childrenDefault": {}
138
1
              }
139
1
            }
140
1
          }
141
1
          "#,
142
1
    )
143
1
    .is_err());
144
1
}
145
146
#[test]
147
1
fn issue_598() {
148
1
    // Regression test for a panic.
149
1
    let chain_spec = ChainSpec::from_json_bytes(include_bytes!("./tests/issue-598.json")).unwrap();
150
1
    assert!(
matches!0
(
151
1
        chain_spec
152
1
            .light_sync_state()
153
1
            .unwrap()
154
1
            .to_chain_information(),
155
        Err(CheckpointToChainInformationError::GenesisBlockCheckpoint)
156
    ));
157
1
}