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
173
174
175
176
177
178
179
use serde::de::Error;
use serde_json::{self, Error as JsonError};
use std::collections::{BTreeMap, HashSet};
use storage::StorageValue;
use events::Milliseconds;
use crypto::{hash, PublicKey, Hash};
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ValidatorKeys {
#[doc(hidden)]
pub consensus_key: PublicKey,
pub service_key: PublicKey,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StoredConfiguration {
pub previous_cfg_hash: Hash,
pub actual_from: u64,
pub validator_keys: Vec<ValidatorKeys>,
pub consensus: ConsensusConfig,
pub services: BTreeMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ConsensusConfig {
pub round_timeout: Milliseconds,
pub status_timeout: Milliseconds,
pub peers_timeout: Milliseconds,
pub propose_timeout: Milliseconds,
pub txs_block_limit: u32,
}
impl Default for ConsensusConfig {
fn default() -> Self {
ConsensusConfig {
round_timeout: 3000,
propose_timeout: 500,
status_timeout: 5000,
peers_timeout: 10000,
txs_block_limit: 1000,
}
}
}
impl StoredConfiguration {
pub fn try_serialize(&self) -> Result<Vec<u8>, JsonError> {
serde_json::to_vec(&self)
}
pub fn try_deserialize(serialized: &[u8]) -> Result<StoredConfiguration, JsonError> {
let config: StoredConfiguration = serde_json::from_slice(serialized)?;
{
let mut keys = HashSet::with_capacity(config.validator_keys.len() * 2);
for k in &config.validator_keys {
keys.insert(k.consensus_key);
keys.insert(k.service_key);
}
if keys.len() != config.validator_keys.len() * 2 {
return Err(JsonError::custom(
"Duplicated keys are found: each consensus and service key must be unique",
));
}
}
Ok(config)
}
}
impl StorageValue for StoredConfiguration {
fn into_bytes(self) -> Vec<u8> {
self.try_serialize().unwrap()
}
fn from_bytes(v: ::std::borrow::Cow<[u8]>) -> Self {
StoredConfiguration::try_deserialize(v.as_ref()).unwrap()
}
fn hash(&self) -> Hash {
let vec_bytes = self.try_serialize().unwrap();
hash(&vec_bytes)
}
}
#[cfg(test)]
mod tests {
use toml;
use crypto::{Seed, gen_keypair_from_seed};
use super::*;
#[test]
fn stored_configuration_toml() {
let original = create_test_configuration();
let toml = toml::to_string(&original).unwrap();
let deserialized: StoredConfiguration = toml::from_str(&toml).unwrap();
assert_eq!(original, deserialized);
}
#[test]
fn stored_configuration_serialize_deserialize() {
let configuration = create_test_configuration();
serialize_deserialize(&configuration);
}
#[test]
#[should_panic(expected = "Duplicated keys are found")]
fn stored_configuration_duplicated_keys() {
let mut configuration = create_test_configuration();
configuration.validator_keys.push(ValidatorKeys {
consensus_key: PublicKey::zero(),
service_key: PublicKey::zero(),
});
serialize_deserialize(&configuration);
}
fn create_test_configuration() -> StoredConfiguration {
let validator_keys = (1..4)
.map(|i| {
ValidatorKeys {
consensus_key: gen_keypair_from_seed(&Seed::new([i; 32])).0,
service_key: gen_keypair_from_seed(&Seed::new([i * 10; 32])).0,
}
})
.collect();
StoredConfiguration {
previous_cfg_hash: Hash::zero(),
actual_from: 42,
validator_keys,
consensus: ConsensusConfig::default(),
services: BTreeMap::new(),
}
}
fn serialize_deserialize(configuration: &StoredConfiguration) {
let serialized = configuration.try_serialize().unwrap();
let _ = StoredConfiguration::try_deserialize(&serialized).unwrap();
}
}