forked from MystenLabs/narwhal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_tests.rs
261 lines (234 loc) · 8.87 KB
/
config_tests.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
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
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
// This file contains tests that detect changes in Narwhal configs and parameters.
// If a PR breaks one or more tests here, the PR probably has a real impact
// on a Narwhal configuration file. When test failure happens, the PR should
// be marked as a breaking change and reviewers should be aware of this.
//
// Owners and operators of production configuration files can add themselves to
// .github/CODEOWNERS for the corresponding snapshot tests, so they can get notified
// of changes. PRs that modifies snapshot files should wait for reviews from
// code owners (if any) before merging.
//
// To review snapshot changes, and fix snapshot differences,
// 0. Install cargo-insta
// 1. Run `cargo insta test --review` under `./config`.
// 2. Review, accept or reject changes.
use std::collections::{BTreeMap, HashMap};
use config::{
Committee, ConsensusAPIGrpcParameters, Epoch, Import, Parameters, PrimaryAddresses,
PrometheusMetricsParameters, Stake, WorkerCache,
};
use crypto::PublicKey;
use fastcrypto::traits::KeyPair as _;
use insta::assert_json_snapshot;
use rand::seq::SliceRandom;
use std::{fs::File, io::Write};
use tempfile::tempdir;
use test_utils::{initialize_worker_index_with_port_getter, make_authority_with_port_getter};
#[test]
fn leader_election_rotates_through_all() {
// this committee has equi-sized stakes
let committee = test_utils::committee(None);
let mut leader_counts = HashMap::new();
// We most probably will only call `leader` on even rounds, so let's check this
// still lets us use the whole roster of leaders.
let mut leader_counts_stepping_by_2 = HashMap::new();
for i in 0..100 {
let leader = committee.leader(i);
let leader_stepping_by_2 = committee.leader(i * 2);
*leader_counts.entry(leader).or_insert(0) += 1;
*leader_counts_stepping_by_2
.entry(leader_stepping_by_2)
.or_insert(0) += 1;
}
assert!(leader_counts.values().all(|v| *v >= 20));
assert!(leader_counts_stepping_by_2.values().all(|v| *v >= 20));
}
#[test]
fn update_primary_network_info_test() {
let committee = test_utils::committee(None);
let res = committee
.clone()
.update_primary_network_info(BTreeMap::new())
.unwrap_err();
for err in res {
assert!(matches!(
err,
config::CommitteeUpdateError::MissingFromUpdate(_)
))
}
let committee2 = test_utils::committee(42);
let invalid_new_info = committee2
.authorities
.iter()
.map(|(pk, a)| (pk.clone(), (a.stake, a.primary.clone())))
.collect::<BTreeMap<_, (Stake, PrimaryAddresses)>>();
let res2 = committee
.clone()
.update_primary_network_info(invalid_new_info)
.unwrap_err();
for err in res2 {
// we'll get the two collections reporting missing from each other
assert!(matches!(
err,
config::CommitteeUpdateError::NotInCommittee(_)
| config::CommitteeUpdateError::MissingFromUpdate(_)
))
}
let committee3 = test_utils::committee(None);
let invalid_new_info = committee3
.authorities
.iter()
// change the stake
.map(|(pk, a)| (pk.clone(), (a.stake + 1, a.primary.clone())))
.collect::<BTreeMap<_, (Stake, PrimaryAddresses)>>();
let res2 = committee
.clone()
.update_primary_network_info(invalid_new_info)
.unwrap_err();
for err in res2 {
assert!(matches!(
err,
config::CommitteeUpdateError::DifferentStake(_)
))
}
let committee4 = test_utils::committee(None);
let mut pk_n_stake = Vec::new();
let mut addresses = Vec::new();
committee4.authorities.iter().for_each(|(pk, a)| {
pk_n_stake.push((pk.clone(), a.stake));
addresses.push(a.primary.clone())
});
let mut rng = rand::thread_rng();
addresses.shuffle(&mut rng);
let new_info = pk_n_stake
.into_iter()
.zip(addresses)
.map(|((pk, stk), addr)| (pk, (stk, addr)))
.collect::<BTreeMap<PublicKey, (Stake, PrimaryAddresses)>>();
let mut comm = committee;
let res = comm.update_primary_network_info(new_info.clone());
assert!(res.is_ok());
for (pk, a) in comm.authorities.iter() {
assert_eq!(a.primary, new_info.get(pk).unwrap().1);
}
}
// If one or both of the parameters_xx_matches() tests are broken by a change, the following additional places are
// highly likely needed to be updated as well:
// 1. Docker/validators/parameters.json for starting Narwhal cluster with Docker Compose.
// 2. benchmark/fabfile.py for benchmarking a Narwhal cluster locally.
// 3. Sui configurations & snapshot tests when upgrading Narwhal in Sui to include the change.
#[test]
fn parameters_snapshot_matches() {
// This configuration is load-bearing in the NW benchmarks,
// and in Sui (prod config + shared object bench base). If this test breaks,
// config needs to change in all of these.
// We avoid defaults that randomly bind to a random port
let consensus_api_grpc_parameters = ConsensusAPIGrpcParameters {
socket_addr: "/ip4/127.0.0.1/tcp/8081/http".parse().unwrap(),
..ConsensusAPIGrpcParameters::default()
};
let prometheus_metrics_parameters = PrometheusMetricsParameters {
socket_addr: "/ip4/127.0.0.1/tcp/8081/http".parse().unwrap(),
};
let parameters = Parameters {
consensus_api_grpc: consensus_api_grpc_parameters,
prometheus_metrics: prometheus_metrics_parameters,
..Parameters::default()
};
assert_json_snapshot!("parameters", parameters)
}
#[test]
fn parameters_import_snapshot_matches() {
// GIVEN
let input = r#"{
"header_size": 1000,
"max_header_delay": "100ms",
"gc_depth": 50,
"sync_retry_delay": "5s",
"sync_retry_nodes": 3,
"batch_size": 500000,
"max_batch_delay": "100ms",
"block_synchronizer": {
"certificates_synchronize_timeout": "2s",
"payload_synchronize_timeout": "3_000ms",
"payload_availability_timeout": "4_000ms",
"handler_certificate_deliver_timeout": "1_000ms"
},
"consensus_api_grpc": {
"socket_addr": "/ip4/127.0.0.1/tcp/0/http",
"get_collections_timeout": "5_000ms",
"remove_collections_timeout": "5_000ms"
},
"max_concurrent_requests": 500000,
"prometheus_metrics": {
"socket_addr": "/ip4/127.0.0.1/tcp/0/http"
}
}"#;
// AND temporary file
let dir = tempdir().expect("Couldn't create tempdir");
let file_path = dir.path().join("temp-properties.json");
let mut file = File::create(file_path.clone()).expect("Couldn't create temp file");
// AND write the json context
writeln!(file, "{input}").expect("Couldn't write to file");
// WHEN
let params = Parameters::import(file_path.to_str().unwrap())
.expect("Failed to import given Parameters json");
// THEN
assert_json_snapshot!("parameters_import", params)
}
#[test]
fn commmittee_snapshot_matches() {
// The shape of this configuration is load-bearing in the NW benchmarks,
// and in Sui (prod)
let keys = test_utils::keys(None);
let committee = Committee {
epoch: Epoch::default(),
authorities: keys
.iter()
.map(|kp| {
let mut port = 0;
let increment_port_getter = || {
port += 1;
port
};
(
kp.public().clone(),
make_authority_with_port_getter(increment_port_getter),
)
})
.collect(),
};
// we need authorities to be serialized in order
let mut settings = insta::Settings::clone_current();
settings.set_sort_maps(true);
settings.bind(|| assert_json_snapshot!("committee", committee));
}
#[test]
fn workers_snapshot_matches() {
// The shape of this configuration is load-bearing in the NW benchmarks,
// and in Sui (prod)
let keys = test_utils::keys(None);
let worker_cache = WorkerCache {
epoch: Epoch::default(),
workers: keys
.iter()
.map(|kp| {
let mut port = 0;
let increment_port_getter = || {
port += 1;
port
};
(
kp.public().clone(),
initialize_worker_index_with_port_getter(increment_port_getter),
)
})
.collect(),
};
// we need authorities to be serialized in order
let mut settings = insta::Settings::clone_current();
settings.set_sort_maps(true);
settings.bind(|| assert_json_snapshot!("worker_cache", worker_cache));
}