-
Notifications
You must be signed in to change notification settings - Fork 690
/
Copy pathtest_rejoin.rs
225 lines (207 loc) · 8 KB
/
test_rejoin.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
//! Tests whether nodes can leave and rejoin the consensus.
#[cfg(test)]
#[cfg(feature = "expensive_tests")]
mod test {
use std::process::Command;
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::AccountId;
use testlib::node::{create_nodes, sample_queryable_node, sample_two_nodes, Node, NodeConfig};
use testlib::test_helpers::{heavy_test, wait, wait_for_catchup};
fn warmup() {
Command::new("cargo")
.args(&["build", "-p", "near"])
.spawn()
.expect("warmup failed")
.wait()
.unwrap();
}
// DISCLAIMER. These tests are very heavy and somehow manage to interfere with each other.
// If you add multiple tests and they start failing consider splitting it into several *.rs files
// to ensure they are not run in parallel.
fn send_transaction(
nodes: &Vec<Arc<RwLock<dyn Node>>>,
account_names: &Vec<AccountId>,
nonces: &Vec<u64>,
from: usize,
to: usize,
) {
let k = sample_queryable_node(nodes);
nodes[k]
.read()
.unwrap()
.add_transaction(SignedTransaction::send_money(
nonces[from],
account_names[from].clone(),
account_names[to].clone(),
&*nodes[from].read().unwrap().signer(),
1000,
nodes[from].read().unwrap().user().get_best_block_hash().unwrap(),
))
.unwrap();
}
fn test_kill_1(num_nodes: usize, num_trials: usize, test_prefix: &str) {
warmup();
// Start all nodes, crash node#2, proceed, restart node #2 but crash node #3
let crash1 = 2;
let crash2 = 3;
let nodes = create_nodes(num_nodes, test_prefix);
// Convert some of the thread nodes into processes.
let nodes: Vec<_> = nodes
.into_iter()
.map(|node_cfg| {
if rand::random::<bool>() {
if let NodeConfig::Thread(cfg) = node_cfg {
NodeConfig::Process(cfg)
} else {
unimplemented!()
}
} else {
node_cfg
}
})
.collect();
let nodes: Vec<_> = nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect();
let account_names: Vec<_> =
nodes.iter().map(|node| node.read().unwrap().account_id().unwrap()).collect();
for i in 0..num_nodes {
nodes[i].write().unwrap().start();
}
let mut expected_balances = vec![0; num_nodes];
let mut nonces = vec![1; num_nodes];
for i in 0..num_nodes {
nonces[i] = nodes[0]
.read()
.unwrap()
.get_access_key_nonce_for_signer(&account_names[i])
.unwrap()
+ 1;
let account = nodes[0].read().unwrap().view_account(&account_names[i]).unwrap();
expected_balances[i] = account.amount;
}
let trial_duration = 60000;
for trial in 0..num_trials {
println!("TRIAL #{}", trial);
if trial % 10 == 3 {
println!("Killing node {}", crash1);
nodes[crash1].write().unwrap().kill();
thread::sleep(Duration::from_secs(2));
}
if trial % 10 == 6 {
println!("Restarting node {}", crash1);
nodes[crash1].write().unwrap().start();
wait_for_catchup(&nodes);
println!("Killing node {}", crash2);
nodes[crash2].write().unwrap().kill();
}
if trial % 10 == 9 {
println!("Restarting node {}", crash2);
nodes[crash2].write().unwrap().start();
}
let (i, j) = sample_two_nodes(num_nodes);
send_transaction(&nodes, &account_names, &nonces, i, j);
nonces[i] += 1;
expected_balances[i] -= 1000;
expected_balances[j] += 1000;
let t = sample_queryable_node(&nodes);
wait(
|| {
let amt = nodes[t].read().unwrap().view_balance(&account_names[j]).unwrap();
expected_balances[j] <= amt
},
1000,
trial_duration,
);
}
}
fn test_kill_2(num_nodes: usize, num_trials: usize, test_prefix: &str) {
warmup();
// Start all nodes, crash nodes 2 and 3, restart node 2, proceed, restart node 3
let (crash1, crash2) = (2, 3);
let nodes = create_nodes(num_nodes, test_prefix);
// Convert some of the thread nodes into processes.
let nodes: Vec<_> = nodes
.into_iter()
.map(|node_cfg| {
if rand::random::<bool>() {
if let NodeConfig::Thread(cfg) = node_cfg {
NodeConfig::Process(cfg)
} else {
unimplemented!()
}
} else {
node_cfg
}
})
.collect();
let nodes: Vec<Arc<RwLock<dyn Node>>> =
nodes.into_iter().map(|cfg| Node::new_sharable(cfg)).collect();
let account_names: Vec<_> =
nodes.iter().map(|node| node.read().unwrap().account_id().unwrap()).collect();
for i in 0..num_nodes {
nodes[i].write().unwrap().start();
}
let mut expected_balances = vec![0; num_nodes];
let mut nonces = vec![1; num_nodes];
for i in 0..num_nodes {
let account = nodes[i].read().unwrap().view_account(&account_names[i]).unwrap();
nonces[i] = 1 + nodes[i]
.read()
.unwrap()
.get_access_key_nonce_for_signer(&account_names[i])
.unwrap();
expected_balances[i] = account.amount;
}
let trial_duration = 20000;
for trial in 0..num_trials {
println!("TRIAL #{}", trial);
let (i, j) = sample_two_nodes(num_nodes);
if trial % 5 == 2 {
// Here we kill two nodes, make sure transactions stop going through,
// then restart one of the nodes
println!("Killing nodes {}, {}", crash1, crash2);
nodes[crash1].write().unwrap().kill();
nodes[crash2].write().unwrap().kill();
send_transaction(&nodes, &account_names, &nonces, i, j);
thread::sleep(Duration::from_secs(2));
let t = sample_queryable_node(&nodes);
assert_eq!(
nodes[t].read().unwrap().view_balance(&account_names[j]).unwrap(),
expected_balances[j]
);
println!("Restarting node {}", crash1);
nodes[crash1].write().unwrap().start();
} else {
send_transaction(&nodes, &account_names, &nonces, i, j);
if trial % 5 == 4 {
// Restart the second of the nodes killed earlier
println!("Restarting node {}", crash2);
nodes[crash2].write().unwrap().start();
}
}
nonces[i] += 1;
expected_balances[i] -= 100;
expected_balances[j] += 100;
let t = sample_queryable_node(&nodes);
wait(
|| {
let amt = nodes[t].read().unwrap().view_balance(&account_names[j]).unwrap();
expected_balances[j] <= amt
},
1000,
trial_duration,
);
}
}
#[test]
fn test_4_20_kill1() {
heavy_test(|| test_kill_1(4, 10, "4_10_kill1"));
}
#[test]
#[ignore]
fn test_4_20_kill2() {
heavy_test(|| test_kill_2(4, 5, "4_10_kill2"));
}
}