-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlib.rs
399 lines (347 loc) · 13.2 KB
/
lib.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Pallet for setting the Partner Chain validators using inherent data
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]
pub mod migrations;
/// [`pallet_session`] and [`pallet_session_validator_management`] integration.
#[cfg(feature = "pallet-session-compat")]
pub mod pallet_session_support;
#[cfg(feature = "pallet-session-compat")]
pub mod session_manager;
pub use pallet::*;
#[cfg(any(test, feature = "mock"))]
pub mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
pub use sp_session_validator_management::CommitteeMember;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use log::{info, warn};
use sidechain_domain::byte_string::SizedByteString;
use sidechain_domain::{MainchainAddress, PolicyId};
use sp_core::blake2_256;
use sp_runtime::traits::{MaybeSerializeDeserialize, One, Zero};
use sp_session_validator_management::*;
use sp_std::fmt::Display;
use sp_std::{ops::Add, vec, vec::Vec};
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::constant]
type MaxValidators: Get<u32>;
type AuthorityId: Member
+ Parameter
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ Ord
+ Into<Self::AccountId>;
type AuthorityKeys: Parameter + Member + MaybeSerializeDeserialize + Ord + MaxEncodedLen;
type AuthoritySelectionInputs: Parameter;
type ScEpochNumber: Parameter
+ MaxEncodedLen
+ Zero
+ Display
+ Add
+ One
+ Default
+ Ord
+ Copy
+ From<u64>
+ Into<u64>;
type CommitteeMember: Parameter
+ Member
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ CommitteeMember<AuthorityId = Self::AuthorityId, AuthorityKeys = Self::AuthorityKeys>;
fn select_authorities(
input: Self::AuthoritySelectionInputs,
sidechain_epoch: Self::ScEpochNumber,
) -> Option<BoundedVec<Self::CommitteeMember, Self::MaxValidators>>;
fn current_epoch_number() -> Self::ScEpochNumber;
/// Weight functions needed for pallet_session_validator_management.
type WeightInfo: WeightInfo;
}
#[pallet::event]
pub enum Event<T: Config> {}
use frame_support::{BoundedVec, CloneNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxValidators))]
pub struct CommitteeInfo<ScEpochNumber: Clone, CommitteeMember: Clone, MaxValidators> {
pub epoch: ScEpochNumber,
pub committee: BoundedVec<CommitteeMember, MaxValidators>,
}
impl<ScEpochNumber, CommitteeMember, MaxValidators> Default
for CommitteeInfo<ScEpochNumber, CommitteeMember, MaxValidators>
where
CommitteeMember: Clone,
ScEpochNumber: Clone + Zero,
{
fn default() -> Self {
Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
}
}
#[pallet::storage]
pub type CurrentCommittee<T: Config> = StorageValue<
_,
CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>,
ValueQuery,
>;
#[pallet::storage]
pub type NextCommittee<T: Config> = StorageValue<
_,
CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>,
OptionQuery,
>;
#[pallet::storage]
pub type MainChainScriptsConfiguration<T: Config> =
StorageValue<_, MainChainScripts, ValueQuery>;
#[pallet::error]
pub enum Error<T> {
InvalidEpoch,
UnnecessarySetCall,
}
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub initial_authorities: Vec<T::CommitteeMember>,
pub main_chain_scripts: MainChainScripts,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let initial_authorities = BoundedVec::truncate_from(self.initial_authorities.clone());
let committee_info =
CommitteeInfo { epoch: T::ScEpochNumber::zero(), committee: initial_authorities };
CurrentCommittee::<T>::put(committee_info);
MainChainScriptsConfiguration::<T>::put(self.main_chain_scripts.clone());
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
// Only reason for this hook is to set the genesis committee as the committee for first block's epoch.
// If it wouldn't be set, the should_end_session() function would return true at the 2nd block,
// thus denying handover phase to genesis committee, which would break the chain. With this hook,
// should_end_session() returns true at 1st block and changes committee to the same one, thus allowing
// handover phase to happen. After having proper chain initialization procedure this probably won't be needed anymore.
// Note: If chain is started during handover phase, it will wait until new epoch to produce the first block.
fn on_initialize(block_nr: BlockNumberFor<T>) -> Weight {
if block_nr.is_one() {
CurrentCommittee::<T>::mutate(|committee| {
committee.epoch = T::current_epoch_number();
});
T::DbWeight::get().reads_writes(2, 1)
} else {
Weight::zero()
}
}
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
/// Responsible for calling `Call:set()` on each block by the block author, if the validator list changed
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
if NextCommittee::<T>::exists() {
None
} else {
let for_epoch_number = CurrentCommittee::<T>::get().epoch + One::one();
let (authority_selection_inputs, selection_inputs_hash) =
Self::inherent_data_to_authority_selection_inputs(data);
if let Some(validators) =
T::select_authorities(authority_selection_inputs, for_epoch_number)
{
Some(Call::set { validators, for_epoch_number, selection_inputs_hash })
} else {
let current_committee = CurrentCommittee::<T>::get();
let current_committee_epoch = current_committee.epoch;
warn!("Committee for epoch {for_epoch_number} is the same as for epoch {current_committee_epoch}");
let validators = current_committee.committee;
Some(Call::set { validators, for_epoch_number, selection_inputs_hash })
}
}
}
// TODO make this call run by every full node, so it can be relied upon for ensuring that the block is correct
fn check_inherent(call: &Self::Call, data: &InherentData) -> Result<(), Self::Error> {
let (validators_param, for_epoch_number_param, call_selection_inputs_hash) = match call
{
Call::set { ref validators, ref for_epoch_number, ref selection_inputs_hash } => {
(validators, for_epoch_number, selection_inputs_hash)
},
_ => return Ok(()),
};
let (authority_selection_inputs, computed_selection_inputs_hash) =
Self::inherent_data_to_authority_selection_inputs(data);
let validators =
T::select_authorities(authority_selection_inputs, *for_epoch_number_param)
.unwrap_or_else(|| {
// Proposed block should keep the same committee if calculation of new one was impossible.
// This is code is executed before the committee rotation, so the NextCommittee should be used.
let committee_info = NextCommittee::<T>::get()
// Needed only for verification of the block no 1, before any `set` call is executed.
.unwrap_or_else(CurrentCommittee::<T>::get);
committee_info.committee
});
if *validators_param != validators {
if *call_selection_inputs_hash == computed_selection_inputs_hash {
return Err(InherentError::InvalidValidatorsMatchingHash(
computed_selection_inputs_hash,
));
} else {
return Err(InherentError::InvalidValidatorsHashMismatch(
computed_selection_inputs_hash,
call_selection_inputs_hash.clone(),
));
}
}
Ok(())
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::set { .. })
}
fn is_inherent_required(_: &InherentData) -> Result<Option<Self::Error>, Self::Error> {
if !NextCommittee::<T>::exists() {
Ok(Some(InherentError::CommitteeNeedsToBeStoredOneEpochInAdvance)) // change error
} else {
Ok(None)
}
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// 'for_epoch_number' parameter is needed only for validation purposes, because we need to make sure that
/// check_inherent uses the same epoch_number as was used to create inherent data.
/// Alternative approach would be to put epoch number inside InherentData. However, sidechain
/// epoch number is set in Runtime, thus, inherent data provider doesn't have to know about it.
/// On top of that, the latter approach is slightly more complicated to code.
#[pallet::call_index(0)]
#[pallet::weight((
T::WeightInfo::set(validators.len() as u32),
DispatchClass::Mandatory
))]
pub fn set(
origin: OriginFor<T>,
validators: BoundedVec<T::CommitteeMember, T::MaxValidators>,
for_epoch_number: T::ScEpochNumber,
selection_inputs_hash: SizedByteString<32>,
) -> DispatchResult {
ensure_none(origin)?;
let expected_epoch_number = CurrentCommittee::<T>::get().epoch + One::one();
ensure!(for_epoch_number == expected_epoch_number, Error::<T>::InvalidEpoch);
let len = validators.len();
info!("💼 Storing committee of size {len} for epoch {for_epoch_number}, input data hash: {}", selection_inputs_hash.to_hex_string());
NextCommittee::<T>::put(CommitteeInfo {
epoch: for_epoch_number,
committee: validators,
});
Ok(())
}
/// Changes the main chain scripts used for committee rotation.
///
/// This extrinsic must be run either using `sudo` or some other chain governance mechanism.
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::set(1))]
pub fn set_main_chain_scripts(
origin: OriginFor<T>,
committee_candidate_address: MainchainAddress,
d_parameter_policy_id: PolicyId,
permissioned_candidates_policy_id: PolicyId,
) -> DispatchResult {
ensure_root(origin)?;
let new_scripts = MainChainScripts {
committee_candidate_address,
d_parameter_policy_id,
permissioned_candidates_policy_id,
};
MainChainScriptsConfiguration::<T>::put(new_scripts);
Ok(())
}
}
impl<T: Config> Pallet<T> {
pub fn get_next_unset_epoch_number() -> T::ScEpochNumber {
NextCommittee::<T>::get()
.map(|next_committee| next_committee.epoch + One::one())
.unwrap_or(CurrentCommittee::<T>::get().epoch + One::one())
}
pub fn get_current_authority(index: usize) -> Option<T::AuthorityId> {
CurrentCommittee::<T>::get()
.committee
.get(index)
.map(|authority| authority.authority_id())
.clone()
}
pub fn get_current_authority_round_robin(index: usize) -> Option<T::CommitteeMember> {
let committee = CurrentCommittee::<T>::get().committee;
if committee.is_empty() {
return None;
}
committee.get(index % committee.len() as usize).cloned()
}
pub fn current_committee_storage(
) -> CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators> {
CurrentCommittee::<T>::get()
}
pub fn next_committee_storage(
) -> Option<CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>> {
NextCommittee::<T>::get()
}
/// This function's result should be always defined after inherent call of 1st block of each epoch
pub fn next_committee() -> Option<BoundedVec<T::AuthorityId, T::MaxValidators>> {
Some(BoundedVec::truncate_from(
NextCommittee::<T>::get()?
.committee
.into_iter()
.map(|member| member.authority_id())
.collect::<Vec<T::AuthorityId>>(),
))
}
fn inherent_data_to_authority_selection_inputs(
data: &InherentData,
) -> (T::AuthoritySelectionInputs, SizedByteString<32>) {
let decoded_data = data
.get_data::<T::AuthoritySelectionInputs>(&INHERENT_IDENTIFIER)
.expect("Validator inherent data not correctly encoded")
.expect("Validator inherent data must be provided");
let data_hash = SizedByteString(blake2_256(&decoded_data.encode()));
(decoded_data, data_hash)
}
pub fn calculate_committee(
authority_selection_inputs: T::AuthoritySelectionInputs,
sidechain_epoch: T::ScEpochNumber,
) -> Option<Vec<T::CommitteeMember>> {
T::select_authorities(authority_selection_inputs, sidechain_epoch).map(|c| c.to_vec())
}
pub fn rotate_committee_to_next_epoch() -> Option<Vec<T::CommitteeMember>> {
let next_committee = NextCommittee::<T>::take()?;
CurrentCommittee::<T>::put(next_committee.clone());
let validators = next_committee.committee.to_vec();
let len = validators.len();
info!(
"Committee rotated: Returning {len} validators, stored in epoch {}",
next_committee.epoch
);
Some(validators)
}
pub fn get_current_committee() -> (T::ScEpochNumber, Vec<T::CommitteeMember>) {
let committee_info = CurrentCommittee::<T>::get();
(committee_info.epoch, committee_info.committee.to_vec())
}
pub fn get_next_committee() -> Option<(T::ScEpochNumber, Vec<T::CommitteeMember>)> {
let committee_info = NextCommittee::<T>::get()?;
Some((committee_info.epoch, committee_info.committee.to_vec()))
}
pub fn get_main_chain_scripts() -> MainChainScripts {
MainChainScriptsConfiguration::<T>::get()
}
}
}