-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathstructs.rs
172 lines (147 loc) · 5.28 KB
/
structs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Smoldot
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Type definitions that implement the [`serde::Serialize`] and [`serde::Deserialize`] traits and
//! that match the chain specs JSON file structure.
//!
//! The main type is [`ClientSpec`].
use super::light_sync_state::LightSyncState;
use alloc::{boxed::Box, collections::BTreeMap, format, string::String, vec::Vec};
use fnv::FnvBuildHasher;
use hashbrown::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(super) struct ClientSpec {
pub(super) name: String,
pub(super) id: String,
#[serde(default)]
pub(super) chain_type: ChainType,
#[serde(default)]
// TODO: make use of this
pub(super) code_substitutes: HashMap<String, HexString, fnv::FnvBuildHasher>,
pub(super) boot_nodes: Vec<String>,
pub(super) telemetry_endpoints: Option<Vec<(String, u8)>>,
pub(super) protocol_id: Option<String>,
pub(super) properties: Option<Box<serde_json::value::RawValue>>,
// TODO: make use of this
pub(super) fork_blocks: Option<Vec<(u64, HashHexString)>>,
// TODO: make use of this
pub(super) bad_blocks: Option<HashSet<HashHexString, FnvBuildHasher>>,
// Unused but for some reason still part of the chain specs.
pub(super) consensus_engine: (),
pub(super) genesis: Genesis,
pub(super) light_sync_state: Option<LightSyncState>,
#[serde(flatten)]
pub(super) parachain: Option<ChainSpecParachain>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub(super) struct ChainSpecParachain {
pub(super) relay_chain: String,
pub(super) para_id: u32,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub(super) enum ChainType {
Development,
Local,
Live,
Custom(String),
}
impl Default for ChainType {
fn default() -> Self {
Self::Live
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(super) enum Genesis {
Raw(RawGenesis),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(super) struct RawGenesis {
pub(super) top: BTreeMap<HexString, HexString>,
pub(super) children_default: BTreeMap<HexString, ChildRawStorage>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct HexString(pub(super) Vec<u8>);
impl core::borrow::Borrow<[u8]> for HexString {
fn borrow(&self) -> &[u8] {
&self.0
}
}
impl serde::Serialize for HexString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
format!("0x{}", hex::encode(&self.0[..])).serialize(serializer)
}
}
impl<'a> serde::Deserialize<'a> for HexString {
fn deserialize<D>(deserializer: D) -> Result<HexString, D::Error>
where
D: serde::Deserializer<'a>,
{
let string = String::deserialize(deserializer)?;
if !string.starts_with("0x") {
return Err(serde::de::Error::custom(
"hexadecimal string doesn't start with 0x",
));
}
let bytes = hex::decode(&string[2..]).map_err(serde::de::Error::custom)?;
Ok(HexString(bytes))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(super) struct ChildRawStorage {
pub(super) child_info: Vec<u8>,
pub(super) child_type: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(super) struct HashHexString(pub(super) [u8; 32]);
impl serde::Serialize for HashHexString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
format!("0x{}", hex::encode(&self.0[..])).serialize(serializer)
}
}
impl<'a> serde::Deserialize<'a> for HashHexString {
fn deserialize<D>(deserializer: D) -> Result<HashHexString, D::Error>
where
D: serde::Deserializer<'a>,
{
let string = String::deserialize(deserializer)?;
if !string.starts_with("0x") {
return Err(serde::de::Error::custom("hash doesn't start with 0x"));
}
let bytes = hex::decode(&string[2..]).map_err(serde::de::Error::custom)?;
if bytes.len() != 32 {
return Err(serde::de::Error::invalid_length(
bytes.len(),
&"a 32 bytes hash",
));
}
let mut out = [0; 32];
out.copy_from_slice(&bytes);
Ok(HashHexString(out))
}
}