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
use crate::{configuration, paras::*, shared::Pallet as ParasShared};
use frame_system::RawOrigin;
use primitives::v2::{HeadData, Id as ParaId, ValidationCode, ValidatorId, ValidatorIndex};
use sp_application_crypto::RuntimeAppPublic;
const SESSION_INDEX: SessionIndex = 1;
const VALIDATOR_NUM: usize = 800;
const CAUSES_NUM: usize = 100;
fn validation_code() -> ValidationCode {
ValidationCode(vec![0])
}
fn old_validation_code() -> ValidationCode {
ValidationCode(vec![1])
}
pub fn prepare_inclusion_bench<T>() -> (PvfCheckStatement, ValidatorSignature)
where
T: Config + shared::Config,
{
initialize::<T>();
initialize_pvf_active_vote::<T>(VoteCause::Onboarding);
let stmt_n_sig = generate_statements::<T>(VoteOutcome::Accept).next().unwrap();
stmt_n_sig
}
pub fn prepare_finalization_bench<T>(
cause: VoteCause,
outcome: VoteOutcome,
) -> (PvfCheckStatement, ValidatorSignature)
where
T: Config + shared::Config,
{
initialize::<T>();
initialize_pvf_active_vote::<T>(cause);
let mut stmts = generate_statements::<T>(outcome).collect::<Vec<_>>();
assert!(stmts.len() > 2);
let stmt_n_sig = stmts.pop().unwrap();
for (stmt, sig) in stmts {
let r = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, sig);
assert!(r.is_ok());
}
stmt_n_sig
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum VoteCause {
Onboarding,
Upgrade,
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum VoteOutcome {
Accept,
Reject,
}
fn initialize<T>()
where
T: Config + shared::Config,
{
let validators = (0..VALIDATOR_NUM)
.map(|_| <ValidatorId as RuntimeAppPublic>::generate_pair(None))
.collect::<Vec<_>>();
let mut config = configuration::Pallet::<T>::config();
config.pvf_checking_enabled = true;
configuration::Pallet::<T>::force_set_active_config(config.clone());
ParasShared::<T>::set_active_validators_ascending(validators.clone());
ParasShared::<T>::set_session_index(SESSION_INDEX);
}
fn initialize_pvf_active_vote<T>(vote_cause: VoteCause)
where
T: Config + shared::Config,
{
for i in 0..CAUSES_NUM {
let id = ParaId::from(i as u32);
if vote_cause == VoteCause::Upgrade {
let old_validation_code = old_validation_code();
let validation_code = validation_code();
let mut parachains = ParachainsCache::new();
Pallet::<T>::initialize_para_now(
&mut parachains,
id,
&ParaGenesisArgs {
parachain: true,
genesis_head: HeadData(vec![1, 2, 3, 4]),
validation_code: old_validation_code,
},
);
drop(parachains);
Pallet::<T>::schedule_code_upgrade(
id,
validation_code,
1u32.into(),
&configuration::Pallet::<T>::config(),
);
} else {
let r = Pallet::<T>::schedule_para_initialize(
id,
ParaGenesisArgs {
parachain: true,
genesis_head: HeadData(vec![1, 2, 3, 4]),
validation_code: validation_code(),
},
);
assert!(r.is_ok());
}
}
}
fn generate_statements<T>(
vote_outcome: VoteOutcome,
) -> impl Iterator<Item = (PvfCheckStatement, ValidatorSignature)>
where
T: Config + shared::Config,
{
let validators = ParasShared::<T>::active_validator_keys();
let required_votes = primitives::v2::supermajority_threshold(validators.len());
(0..required_votes).map(move |validator_index| {
let stmt = PvfCheckStatement {
accept: vote_outcome == VoteOutcome::Accept,
subject: validation_code().hash(),
session_index: SESSION_INDEX,
validator_index: ValidatorIndex(validator_index as u32),
};
let signature = validators[validator_index].sign(&stmt.signing_payload()).unwrap();
(stmt, signature)
})
}