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
|
//! Tests of whole nodes on the simulated network (`transport::sim`): several nodes in
//! one process, built and started as `main` does (`node`), talking through the
//! Mycelium wire encoding.
//!
//! The clock is real (see `transport::sim` for why), so the nodes run with a short
//! keepalive and tests wait for conditions with a timeout instead of advancing time.
//! Timeouts are generous: a test fails only if something doesn't happen at all.
//!
//! `RUST_LOG=continuum=debug cargo test simtest -- --nocapture` shows the nodes' logs
//! (all nodes interleaved; messages name peers by ID).
mod timing;
use std::collections::HashSet;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use automerge::{AutoCommit, ChangeHash};
use serde_json::Value as JsonValue;
use tokio::time::Instant;
use crate::document::replica::Epoch;
use crate::document::store;
use crate::files::Files;
use crate::node::{self, Node, NodeConfig};
use crate::state::{AppState, PeerState};
use crate::swarm::bootstrap::{self, Start};
use crate::transport::sim::SimNet;
/// Keepalive of simulated nodes: dormancy after 1 s, probes of dormant peers every
/// 2.5 s (`Timing::from_keepalive`). Sync reply timeouts and heals don't scale with it
/// (10 s and 20 s at least).
pub(crate) const KEEPALIVE: Duration = Duration::from_millis(250);
/// A simulated swarm: the net and the nodes started on it.
pub(crate) struct Sim {
pub(crate) net: SimNet,
/// Every node started, for the description of a failed wait.
nodes: std::sync::Mutex<Vec<(String, AppState)>>,
}
impl Sim {
pub(crate) fn new(seed: u64) -> Self {
init_logging();
Self {
net: SimNet::new(seed),
nodes: Default::default(),
}
}
/// Starts node `id` with the test defaults: builds it, starts its workers, and
/// bootstraps it in the background (through `seeds`, other nodes' IDs). Returns at
/// once; [`wait_ready`] waits for it.
pub(crate) fn start(&self, id: &str, seeds: &[&str], start: Start) -> AppState {
self.start_with(id, seeds, start, |_| {})
}
/// [`start`](Self::start), with the configuration adjusted first.
pub(crate) fn start_with(
&self,
id: &str,
seeds: &[&str],
start: Start,
adjust: impl FnOnce(&mut NodeConfig),
) -> AppState {
let mut config = NodeConfig {
transport: self.net.transport(id),
initial_epoch: Epoch::GENESIS,
initial_doc: AutoCommit::new(),
earlier_actors: Vec::new(),
keepalive: KEEPALIVE,
neighbours_k: 8,
alias: Some(id.to_owned()),
electorate_window: Duration::from_secs(30 * 24 * 3600),
blob_chunk_bytes: 64 * 1024,
swarm_key: None,
protocol_writers: HashSet::new(),
files: Arc::new(Files::default()),
state_file: None,
document_file: None,
config_report: JsonValue::Null,
};
adjust(&mut config);
let state = Node::new(config).spawn_workers();
self.nodes
.lock()
.unwrap()
.push((id.to_owned(), state.clone()));
let seeds: Vec<String> = seeds.iter().map(|seed| (*seed).to_owned()).collect();
let bootstrapping = state.clone();
let id = id.to_owned();
tokio::spawn(async move {
if let Err(error) = bootstrap::bootstrap(&bootstrapping, &seeds, start).await {
panic!("bootstrapping {id} failed: {error:#}");
}
node::spawn_ready_workers(&bootstrapping);
});
state
}
}
/// Logs to the test output if `RUST_LOG` is set.
fn init_logging() {
if std::env::var_os("RUST_LOG").is_some() {
let _ = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_test_writer()
.compact()
.try_init();
}
}
impl Sim {
/// Waits until `condition` holds, checking every 20 ms. After `timeout`, panics with
/// a description of every node (`describe`).
pub(crate) async fn wait_until<F, Fut>(&self, what: &str, timeout: Duration, mut condition: F)
where
F: FnMut() -> Fut,
Fut: Future<Output = bool>,
{
let deadline = Instant::now() + timeout;
loop {
if condition().await {
return;
}
if Instant::now() >= deadline {
panic!(
"timed out after {timeout:?} waiting until {what}\n{}",
self.describe().await
);
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
}
/// Waits until every node is ready.
pub(crate) async fn wait_ready(&self, nodes: &[&AppState], timeout: Duration) {
self.wait_until("every node is ready", timeout, move || async move {
for node in nodes {
if !node.is_ready().await {
return false;
}
}
true
})
.await;
}
/// Waits until every node has the same heads.
pub(crate) async fn wait_converged(&self, nodes: &[&AppState], timeout: Duration) {
self.wait_until(
"every node has the same heads",
timeout,
move || async move {
let first = heads(nodes[0]).await;
for node in &nodes[1..] {
if heads(node).await != first {
return false;
}
}
true
},
)
.await;
}
/// Every node's state as far as sync and liveness go, and what the net counted.
pub(crate) async fn describe(&self) -> String {
use std::fmt::Write;
let nodes = self.nodes.lock().unwrap().clone();
let now = Instant::now();
let ago = |at: Option<Instant>| at.map(|at| now.saturating_duration_since(at));
let mut out = format!("net: {:?}\n", self.net.counts());
for (id, node) in &nodes {
let heads: Vec<String> = heads(node)
.await
.iter()
.map(|head| head.to_string()[..8].to_owned())
.collect();
let _ = writeln!(
out,
"node {id}: ready={} epoch={} heads={heads:?} initial_sync_done={}",
node.is_ready().await,
node.local_epoch(),
node.initial_sync_done
.load(std::sync::atomic::Ordering::Acquire),
);
let _ = writeln!(
out,
" view: {:?}",
*node
.neighbours
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
);
let peers: Vec<_> = node
.peer_states
.read()
.await
.iter()
.map(|(peer, state)| (peer.clone(), Arc::clone(state)))
.collect();
for (peer, peer_state) in peers {
let p = peer_state.lock().await;
let _ = writeln!(
out,
" peer {peer}: neighbour={} leads={} standing={:?} liveness={:?} hello={:?} \
reports_ready={:?} epoch={:?} last_seen_ago={:?} last_sync_rx_ago={:?} \
unreachable={} their_heads={:?} in_flight={} sending={} reply_due_in={:?} \
retry_at={:?}",
crate::swarm::topology::is_neighbour(node, &peer),
p.leads,
p.standing,
p.liveness,
p.hello_state,
p.reports_ready,
p.epoch,
ago(p.last_seen),
ago(p.last_sync_rx),
p.is_unreachable(&node.timing, now),
p.sync.state.their_heads.as_ref().map(|heads| heads
.iter()
.map(|head| head.to_string()[..8].to_owned())
.collect::<Vec<_>>()),
p.sync.state.in_flight,
p.sync.sending.is_some(),
p.sync
.reply
.as_ref()
.map(|wait| wait.deadline.saturating_duration_since(now)),
p.sync.retry_at.map(|at| at.saturating_duration_since(now)),
);
}
}
out
}
}
fn path(path: &str) -> Vec<String> {
path.split('/').map(str::to_owned).collect()
}
/// Writes `value` at `path` (`a/b/c`) as one change, as the API does.
pub(crate) async fn write(node: &AppState, at: &str, value: JsonValue) {
let at = path(at);
node.mutate_doc(move |doc| store::write_path(doc, &at, &value))
.await
.expect("write");
}
/// The value at `path` (`a/b/c`), if any.
pub(crate) async fn read(node: &AppState, at: &str) -> Option<JsonValue> {
store::read_path(&node.doc.read().await.doc, &path(at)).expect("read")
}
pub(crate) async fn heads(node: &AppState) -> Vec<ChangeHash> {
let mut heads = node.doc.read().await.heads().to_vec();
heads.sort_unstable();
heads
}
/// Whether `node` has `peer` among its neighbours as computed from the member list.
/// Before that list is known everyone counts as a neighbour, so this waits for it.
pub(crate) fn has_neighbour(node: &AppState, peer: &str) -> bool {
node.neighbours
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.is_some()
&& crate::swarm::topology::is_neighbour(node, peer)
}
/// Something about `node`'s state for `peer`; None if it doesn't know the peer.
pub(crate) async fn peer<T>(
node: &AppState,
peer: &str,
f: impl FnOnce(&PeerState) -> T,
) -> Option<T> {
let peer_state = node.get_peer_state(peer).await?;
let peer_state = peer_state.lock().await;
Some(f(&peer_state))
}
|