forked from o1-labs/mina-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_connectivity_initial_joining.rs
More file actions
234 lines (201 loc) · 9.25 KB
/
basic_connectivity_initial_joining.rs
File metadata and controls
234 lines (201 loc) · 9.25 KB
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
#![allow(warnings)]
use std::{
collections::{BTreeMap, HashMap},
time::Duration,
};
use node::{
event_source::Event,
p2p::{P2pConnectionEvent, P2pEvent},
};
use crate::{node::RustNodeTestingConfig, scenario::ScenarioStep, scenarios::ClusterRunner};
/// Global test that aims to be deterministic.
/// Launch `TOTAL_PEERS` number of nodes with `MAX_PEERS_PER_NODE` is est as the maximum number of peers.
/// Launch a seed node where `TOTAL_PEERS` is set as the maximum number of peers.
/// Run the simulation until the following condition is satisfied:
/// * Each node is connected to a number of peers determined by the `P2pState::min_peers` method.
/// Fail the test if any node exceeds the maximum number of connections.
/// Fail the test if the specified number of steps occur but the condition is not met.
#[derive(documented::Documented, Default, Clone, Copy)]
pub struct MultiNodeBasicConnectivityInitialJoining;
impl MultiNodeBasicConnectivityInitialJoining {
pub async fn run(self, mut runner: ClusterRunner<'_>) {
const TOTAL_PEERS: usize = 20;
const STEPS_PER_PEER: usize = 10;
const EXTRA_STEPS: usize = 2000;
const MAX_PEERS_PER_NODE: usize = 12;
const STEP_DELAY: Duration = Duration::from_millis(200);
let seed_node =
runner.add_rust_node(RustNodeTestingConfig::devnet_default().max_peers(TOTAL_PEERS));
eprintln!("launch Openmina seed node, id: {seed_node}");
let mut nodes = vec![seed_node];
for step in 0..(TOTAL_PEERS * STEPS_PER_PEER + EXTRA_STEPS) {
tokio::time::sleep(STEP_DELAY).await;
if step % STEPS_PER_PEER == 0 && nodes.len() < TOTAL_PEERS {
let node = runner.add_rust_node(
RustNodeTestingConfig::devnet_default()
.max_peers(MAX_PEERS_PER_NODE)
.initial_peers(vec![seed_node.into()]),
);
eprintln!("launch Openmina node, id: {node}, connects to {seed_node}");
nodes.push(node);
}
let mut connection_events = BTreeMap::<_, BTreeMap<_, Vec<String>>>::default();
let mut steps = vec![];
for node_id in &nodes {
let node_id = *node_id;
let this_id = runner.node(node_id).unwrap().state().p2p.my_id();
let node_steps = runner
.node_pending_events(node_id, true)
.unwrap()
.1
.map(|(_, event)| {
match event {
Event::P2p(P2pEvent::Connection(P2pConnectionEvent::Finalized(
peer_id,
result,
))) => connection_events
.entry(this_id)
.or_default()
.entry(*peer_id)
.or_default()
.push(
result
.as_ref()
.err()
.cloned()
.unwrap_or_else(|| "ok".to_owned()),
),
_ => {}
}
ScenarioStep::Event {
node_id,
event: event.to_string(),
}
})
.collect::<Vec<_>>();
steps.extend(node_steps);
}
for step in steps {
runner.exec_step(step).await.unwrap();
}
if nodes.len() < TOTAL_PEERS {
continue;
}
let mut conditions_met = true;
for &node_id in &nodes {
runner
.exec_step(ScenarioStep::AdvanceNodeTime {
node_id,
by_nanos: STEP_DELAY.as_nanos() as _,
})
.await
.unwrap();
runner
.exec_step(ScenarioStep::CheckTimeouts { node_id })
.await
.unwrap();
let node = runner.node(node_id).expect("node must exist");
let p2p = &node.state().p2p;
let ready_peers = p2p.ready_peers_iter().count();
// each node connected to some peers
conditions_met &=
p2p.ready().is_some() && ready_peers >= node.state().p2p.unwrap().min_peers();
// maximum is not exceeded
let max_peers = if node_id == seed_node {
TOTAL_PEERS
} else {
MAX_PEERS_PER_NODE
};
assert!(ready_peers <= max_peers);
}
if conditions_met {
for (this_id, events) in &connection_events {
for (peer_id, results) in events {
for result in results {
eprintln!("{this_id} <-> {peer_id}: {result}");
}
}
}
let mut total_connections_known = 0;
let mut total_connections_ready = 0;
for &node_id in &nodes {
let node = runner.node(node_id).expect("node must exist");
let p2p = &node.state().p2p;
let ready_peers = p2p.ready_peers_iter().count();
let my_id = p2p.my_id();
let known_peers: usize = node
.state()
.p2p
.ready()
.and_then(|p2p| p2p.network.scheduler.discovery_state())
.map_or(0, |discovery_state| {
discovery_state
.routing_table
.closest_peers(&my_id.try_into().unwrap())
.count()
});
let state_machine_peers = if cfg!(feature = "p2p-webrtc") {
ready_peers
} else {
ready_peers.max(known_peers)
};
total_connections_ready += ready_peers;
total_connections_known += state_machine_peers;
eprintln!("node {} has {ready_peers} peers", p2p.my_id(),);
}
// TODO: calculate per peer
if let Some(debugger) = runner.debugger() {
tokio::time::sleep(Duration::from_secs(10)).await;
let connections = debugger
.connections_raw(0)
.map(|(id, c)| (id, (c.info.addr, c.info.fd, c.info.pid, c.incoming)))
.collect::<HashMap<_, _>>();
// dbg
for (id, cn) in &connections {
eprintln!("{id}: {}", serde_json::to_string(cn).unwrap());
}
// dbg
for (id, msg) in debugger.messages(0, "") {
eprintln!("{id}: {}", serde_json::to_string(&msg).unwrap());
}
// TODO: fix debugger returns timeout
let connections = debugger
.connections()
.filter_map(|id| Some((id, connections.get(&id)?.clone())))
.collect::<HashMap<_, _>>();
let incoming = connections.iter().filter(|(_, (_, _, _, i))| *i).count();
let outgoing = connections.len() - incoming;
eprintln!(
"debugger seen {incoming} incoming connections and {outgoing} outgoing connections",
);
let state_machine_peers = if cfg!(feature = "p2p-webrtc") {
total_connections_ready
} else {
total_connections_ready.max(total_connections_known)
};
assert_eq!(
incoming + outgoing,
state_machine_peers,
"debugger must see the same number of connections as the state machine"
);
} else {
eprintln!("no debugger, run test with --use-debugger for additional check");
}
eprintln!("success");
return;
}
}
for node_id in &nodes {
let node = runner.node(*node_id).expect("node must exist");
let p2p: &node::p2p::P2pState = &node.state().p2p.unwrap();
let ready_peers = p2p.ready_peers_iter().count();
// each node connected to some peers
println!("must hold {ready_peers} >= {}", p2p.min_peers());
}
// for node_id in nodes {
// let node = runner.node(node_id).expect("node must exist");
// println!("{node_id:?} - p2p state: {:#?}", &node.state().p2p);
// }
assert!(false);
}
}