Newer
Older
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
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the im-online module.
#![cfg(test)]
use super::*;
use crate::mock::*;
use offchain::testing::TestOffchainExt;
use primitives::offchain::OpaquePeerId;
use runtime_io::with_externalities;
use support::{dispatch, assert_noop};
use sr_primitives::testing::UintAuthorityId;
#[test]
fn test_unresponsiveness_slash_fraction() {
// A single case of unresponsiveness is not slashed.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(1, 50),
Perbill::zero(),
);
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(3, 50),
Perbill::from_parts(6000000), // 0.6%
);
// One third offline should be punished around 5%.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(17, 50),
Perbill::from_parts(48000000), // 4.8%
);
}
#[test]
fn should_report_offline_validators() {
with_externalities(&mut new_test_ext(), || {
// given
let block = 1;
System::set_block_number(block);
// buffer new validators
Session::rotate_session();
// enact the change and buffer another one
let validators = vec![1, 2, 3, 4, 5, 6];
VALIDATORS.with(|l| *l.borrow_mut() = Some(validators.clone()));
Session::rotate_session();
// when
// we end current session and start the next one
Session::rotate_session();
// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 2,
validator_set_count: 3,
offenders: vec![
(1, 1),
(2, 2),
(3, 3),
],
})
]);
// should not report when heartbeat is sent
for (idx, v) in validators.into_iter().take(4).enumerate() {
let _ = heartbeat(block, 3, idx as u32, v.into()).unwrap();
}
Session::rotate_session();
// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 3,
validator_set_count: 6,
offenders: vec![
(5, 5),
(6, 6),
],
})
]);
});
}
fn heartbeat(
block_number: u64,
session_index: u32,
authority_index: u32,
id: UintAuthorityId,
) -> dispatch::Result {
let heartbeat = Heartbeat {
block_number,
network_state: OpaqueNetworkState {
peer_id: OpaquePeerId(vec![1]),
external_addresses: vec![],
},
session_index,
authority_index,
};
let signature = id.sign(&heartbeat.encode()).unwrap();
ImOnline::heartbeat(
Origin::system(system::RawOrigin::None),
heartbeat,
signature
)
}
#[test]
fn should_mark_online_validator_when_heartbeat_is_received() {
with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
assert_eq!(Session::validators(), Vec::<u64>::new());
// enact the change and buffer another one
advance_session();
assert_eq!(Session::current_index(), 2);
assert_eq!(Session::validators(), vec![1, 2, 3]);
assert!(!ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(!ImOnline::is_online_in_current_session(2));
// when
let _ = heartbeat(1, 2, 0, 1.into()).unwrap();
// then
assert!(ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(!ImOnline::is_online_in_current_session(2));
// and when
let _ = heartbeat(1, 2, 2, 3.into()).unwrap();
// then
assert!(ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(ImOnline::is_online_in_current_session(2));
});
}
#[test]
fn late_heartbeat_should_fail() {
with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 4, 4, 5, 6]));
assert_eq!(Session::validators(), Vec::<u64>::new());
// enact the change and buffer another one
advance_session();
assert_eq!(Session::current_index(), 2);
assert_eq!(Session::validators(), vec![1, 2, 3]);
// when
assert_noop!(heartbeat(1, 3, 0, 1.into()), "Outdated heartbeat received.");
assert_noop!(heartbeat(1, 1, 0, 1.into()), "Outdated heartbeat received.");
});
}
#[test]
fn should_generate_heartbeats() {
let mut ext = new_test_ext();
let (offchain, state) = TestOffchainExt::new();
ext.set_offchain_externalities(offchain);
with_externalities(&mut ext, || {
// given
let block = 1;
System::set_block_number(block);
// buffer new validators
Session::rotate_session();
// enact the change and buffer another one
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
Session::rotate_session();
// when
UintAuthorityId::set_all_keys(vec![0, 1, 2]);
ImOnline::offchain(2);
// then
let transaction = state.write().transactions.pop().unwrap();
// All validators have `0` as their session key, so we generate 3 transactions.
assert_eq!(state.read().transactions.len(), 2);
// check stuff about the transaction.
let ex: Extrinsic = Decode::decode(&mut &*transaction).unwrap();
let heartbeat = match ex.1 {
crate::mock::Call::ImOnline(crate::Call::heartbeat(h, _)) => h,
e => panic!("Unexpected call: {:?}", e),
};
assert_eq!(heartbeat, Heartbeat {
block_number: 2,
network_state: runtime_io::network_state().unwrap(),
session_index: 2,
authority_index: 2,
});
});
}