This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtests.rs
200 lines (169 loc) · 6.38 KB
/
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
// Copyright 2017-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 module.
#![cfg(test)]
use primitives::{testing, traits::OnFinalize};
use primitives::traits::Header;
use runtime_io::with_externalities;
use crate::mock::{Grandpa, System, new_test_ext};
use system::{EventRecord, Phase};
use crate::{RawLog, RawEvent};
use codec::{Decode, Encode};
use super::*;
#[test]
fn authorities_change_logged() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default());
Grandpa::schedule_change(vec![(4, 1), (5, 1), (6, 1)], 0, None).unwrap();
System::note_finished_extrinsics();
Grandpa::on_finalize(1);
let header = System::finalize();
assert_eq!(header.digest, testing::Digest {
logs: vec![
RawLog::AuthoritiesChangeSignal(0, vec![(4, 1), (5, 1), (6, 1)]).into(),
],
});
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
event: RawEvent::NewAuthorities(vec![(4, 1), (5, 1), (6, 1)]).into(),
topics: vec![],
},
]);
});
}
#[test]
fn authorities_change_logged_after_delay() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default());
Grandpa::schedule_change(vec![(4, 1), (5, 1), (6, 1)], 1, None).unwrap();
Grandpa::on_finalize(1);
let header = System::finalize();
assert_eq!(header.digest, testing::Digest {
logs: vec![
RawLog::AuthoritiesChangeSignal(1, vec![(4, 1), (5, 1), (6, 1)]).into(),
],
});
// no change at this height.
assert_eq!(System::events(), vec![]);
System::initialize(&2, &header.hash(), &Default::default());
System::note_finished_extrinsics();
Grandpa::on_finalize(2);
let _header = System::finalize();
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
event: RawEvent::NewAuthorities(vec![(4, 1), (5, 1), (6, 1)]).into(),
topics: vec![],
},
]);
});
}
#[test]
fn cannot_schedule_change_when_one_pending() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default());
Grandpa::schedule_change(vec![(4, 1), (5, 1), (6, 1)], 1, None).unwrap();
assert!(Grandpa::pending_change().is_some());
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_err());
Grandpa::on_finalize(1);
let header = System::finalize();
System::initialize(&2, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_some());
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_err());
Grandpa::on_finalize(2);
let header = System::finalize();
System::initialize(&3, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_none());
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_ok());
Grandpa::on_finalize(3);
let _header = System::finalize();
});
}
#[test]
fn new_decodes_from_old() {
let old = OldStoredPendingChange {
scheduled_at: 5u32,
delay: 100u32,
next_authorities: vec![(1u64, 5), (2u64, 10), (3u64, 2)],
};
let encoded = old.encode();
let new = StoredPendingChange::<u32, u64>::decode(&mut &encoded[..]).unwrap();
assert!(new.forced.is_none());
assert_eq!(new.scheduled_at, old.scheduled_at);
assert_eq!(new.delay, old.delay);
assert_eq!(new.next_authorities, old.next_authorities);
}
#[test]
fn dispatch_forced_change() {
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
System::initialize(&1, &Default::default(), &Default::default());
Grandpa::schedule_change(
vec![(4, 1), (5, 1), (6, 1)],
5,
Some(0),
).unwrap();
assert!(Grandpa::pending_change().is_some());
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, Some(0)).is_err());
Grandpa::on_finalize(1);
let mut header = System::finalize();
for i in 2..7 {
System::initialize(&i, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().unwrap().forced.is_some());
assert_eq!(Grandpa::next_forced(), Some(11));
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_err());
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, Some(0)).is_err());
Grandpa::on_finalize(i);
header = System::finalize();
}
// change has been applied at the end of block 6.
// add a normal change.
{
System::initialize(&7, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_none());
assert_eq!(Grandpa::grandpa_authorities(), vec![(4, 1), (5, 1), (6, 1)]);
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_ok());
Grandpa::on_finalize(7);
header = System::finalize();
}
// run the normal change.
{
System::initialize(&8, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_some());
assert_eq!(Grandpa::grandpa_authorities(), vec![(4, 1), (5, 1), (6, 1)]);
assert!(Grandpa::schedule_change(vec![(5, 1)], 1, None).is_err());
Grandpa::on_finalize(8);
header = System::finalize();
}
// normal change applied. but we can't apply a new forced change for some
// time.
for i in 9..11 {
System::initialize(&i, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_none());
assert_eq!(Grandpa::grandpa_authorities(), vec![(5, 1)]);
assert_eq!(Grandpa::next_forced(), Some(11));
assert!(Grandpa::schedule_change(vec![(5, 1), (6, 1)], 5, Some(0)).is_err());
Grandpa::on_finalize(i);
header = System::finalize();
}
{
System::initialize(&11, &header.hash(), &Default::default());
assert!(Grandpa::pending_change().is_none());
assert!(Grandpa::schedule_change(vec![(5, 1), (6, 1), (7, 1)], 5, Some(0)).is_ok());
assert_eq!(Grandpa::next_forced(), Some(21));
Grandpa::on_finalize(11);
header = System::finalize();
}
let _ = header;
});
}