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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use exonum::blockchain::{ConsensusConfig, GenesisConfig, StoredConfiguration, ValidatorKeys};
use exonum::crypto::{self, CryptoHash};
use exonum::helpers::{Height, Round, ValidatorId};
use exonum::messages::{Precommit, Propose};
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Debug)]
pub struct TestNetwork {
us: TestNode,
validators: Vec<TestNode>,
}
impl TestNetwork {
pub fn new(validator_count: u16) -> Self {
Self::with_our_role(Some(ValidatorId(0)), validator_count)
}
pub fn with_our_role(us: Option<ValidatorId>, validator_count: u16) -> Self {
assert!(
validator_count > 0,
"At least one validator should be present in the network."
);
let validators = (0..validator_count)
.map(ValidatorId)
.map(TestNode::new_validator)
.collect::<Vec<_>>();
let us = if let Some(ValidatorId(id)) = us {
validators[id as usize].clone()
} else {
TestNode::new_auditor()
};
TestNetwork { validators, us }
}
pub fn us(&self) -> &TestNode {
&self.us
}
pub fn validators(&self) -> &[TestNode] {
&self.validators
}
pub fn genesis_config(&self) -> GenesisConfig {
GenesisConfig::new(self.validators.iter().map(TestNode::public_keys))
}
pub fn update<I: IntoIterator<Item = TestNode>>(&mut self, mut us: TestNode, validators: I) {
let validators = validators
.into_iter()
.enumerate()
.map(|(id, mut validator)| {
let validator_id = ValidatorId(id as u16);
validator.change_role(Some(validator_id));
if us.public_keys().consensus_key == validator.public_keys().consensus_key {
us.change_role(Some(validator_id));
}
validator
})
.collect::<Vec<_>>();
self.validators = validators;
self.us.clone_from(&us);
}
pub fn update_configuration(&mut self, config: TestNetworkConfiguration) {
self.update(config.us, config.validators);
}
pub fn service_public_key_of(&self, id: ValidatorId) -> Option<&crypto::PublicKey> {
self.validators().get(id.0 as usize).map(|x| {
&x.service_public_key
})
}
pub fn consensus_public_key_of(&self, id: ValidatorId) -> Option<&crypto::PublicKey> {
self.validators().get(id.0 as usize).map(|x| {
&x.consensus_public_key
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TestNode {
consensus_secret_key: crypto::SecretKey,
consensus_public_key: crypto::PublicKey,
service_secret_key: crypto::SecretKey,
service_public_key: crypto::PublicKey,
validator_id: Option<ValidatorId>,
}
impl TestNode {
pub fn new_auditor() -> Self {
let (consensus_public_key, consensus_secret_key) = crypto::gen_keypair();
let (service_public_key, service_secret_key) = crypto::gen_keypair();
TestNode {
consensus_secret_key,
consensus_public_key,
service_secret_key,
service_public_key,
validator_id: None,
}
}
pub fn new_validator(validator_id: ValidatorId) -> Self {
let (consensus_public_key, consensus_secret_key) = crypto::gen_keypair();
let (service_public_key, service_secret_key) = crypto::gen_keypair();
TestNode {
consensus_secret_key,
consensus_public_key,
service_secret_key,
service_public_key,
validator_id: Some(validator_id),
}
}
pub fn from_parts(
consensus_keypair: (crypto::PublicKey, crypto::SecretKey),
service_keypair: (crypto::PublicKey, crypto::SecretKey),
validator_id: Option<ValidatorId>,
) -> TestNode {
TestNode {
consensus_public_key: consensus_keypair.0,
consensus_secret_key: consensus_keypair.1,
service_public_key: service_keypair.0,
service_secret_key: service_keypair.1,
validator_id,
}
}
pub fn create_propose(
&self,
height: Height,
last_hash: &crypto::Hash,
tx_hashes: &[crypto::Hash],
) -> Propose {
Propose::new(
self.validator_id.expect(
"An attempt to create propose from a non-validator node.",
),
height,
Round::first(),
last_hash,
tx_hashes,
&self.consensus_secret_key,
)
}
pub fn create_precommit(&self, propose: &Propose, block_hash: &crypto::Hash) -> Precommit {
use std::time::SystemTime;
Precommit::new(
self.validator_id.expect(
"An attempt to create propose from a non-validator node.",
),
propose.height(),
propose.round(),
&propose.hash(),
block_hash,
SystemTime::now(),
&self.consensus_secret_key,
)
}
pub fn public_keys(&self) -> ValidatorKeys {
ValidatorKeys {
consensus_key: self.consensus_public_key,
service_key: self.service_public_key,
}
}
pub fn validator_id(&self) -> Option<ValidatorId> {
self.validator_id
}
pub fn change_role(&mut self, role: Option<ValidatorId>) {
self.validator_id = role;
}
pub fn service_keypair(&self) -> (&crypto::PublicKey, &crypto::SecretKey) {
(&self.service_public_key, &self.service_secret_key)
}
}
impl From<TestNode> for ValidatorKeys {
fn from(node: TestNode) -> Self {
node.public_keys()
}
}
#[derive(Debug)]
pub struct TestNetworkConfiguration {
us: TestNode,
validators: Vec<TestNode>,
stored_configuration: StoredConfiguration,
}
impl TestNetworkConfiguration {
pub(crate) fn new(
network: &TestNetwork,
mut stored_configuration: StoredConfiguration,
) -> Self {
let prev_hash = CryptoHash::hash(&stored_configuration);
stored_configuration.previous_cfg_hash = prev_hash;
TestNetworkConfiguration {
us: network.us().clone(),
validators: network.validators().into(),
stored_configuration,
}
}
pub fn us(&self) -> &TestNode {
&self.us
}
pub fn set_us(&mut self, us: TestNode) {
self.us = us;
self.update_our_role();
}
pub fn validators(&self) -> &[TestNode] {
self.validators.as_ref()
}
pub fn consensus_configuration(&self) -> &ConsensusConfig {
&self.stored_configuration.consensus
}
pub fn actual_from(&self) -> Height {
self.stored_configuration.actual_from
}
pub fn set_actual_from(&mut self, actual_from: Height) {
self.stored_configuration.actual_from = actual_from;
}
pub fn set_consensus_configuration(&mut self, consensus: ConsensusConfig) {
self.stored_configuration.consensus = consensus;
}
pub fn set_validators<I>(&mut self, validators: I)
where
I: IntoIterator<Item = TestNode>,
{
self.validators = validators
.into_iter()
.enumerate()
.map(|(idx, mut node)| {
node.change_role(Some(ValidatorId(idx as u16)));
node
})
.collect();
self.stored_configuration.validator_keys = self.validators
.iter()
.cloned()
.map(ValidatorKeys::from)
.collect();
self.update_our_role();
}
pub fn service_config<D>(&self, id: &str) -> D
where
for<'de> D: Deserialize<'de>,
{
let value = self.stored_configuration.services.get(id).expect(
"Unable to find configuration for service",
);
serde_json::from_value(value.clone()).unwrap()
}
pub fn set_service_config<D>(&mut self, id: &str, config: D)
where
D: Serialize,
{
let value = serde_json::to_value(config).unwrap();
self.stored_configuration.services.insert(id.into(), value);
}
pub fn stored_configuration(&self) -> &StoredConfiguration {
&self.stored_configuration
}
fn update_our_role(&mut self) {
let validator_id = self.validators
.iter()
.position(|x| {
x.public_keys().service_key == self.us.service_public_key
})
.map(|x| ValidatorId(x as u16));
self.us.validator_id = validator_id;
}
}