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
use crate::{
configuration, paras, scheduler, shared,
util::{take_active_subset, take_active_subset_and_inactive},
};
use frame_support::{
pallet_prelude::*,
traits::{OneSessionHandler, ValidatorSet, ValidatorSetWithIdentification},
};
use primitives::v2::{AssignmentId, AuthorityDiscoveryId, SessionIndex, SessionInfo};
use sp_std::vec::Vec;
pub use pallet::*;
pub mod migration;
#[cfg(test)]
mod tests;
pub type AccountId<T> = <<T as Config>::ValidatorSet as ValidatorSet<
<T as frame_system::Config>::AccountId,
>>::ValidatorId;
pub type IdentificationTuple<T> = (
AccountId<T>,
<<T as Config>::ValidatorSet as ValidatorSetWithIdentification<
<T as frame_system::Config>::AccountId,
>>::Identification,
);
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(migration::STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config:
frame_system::Config
+ configuration::Config
+ shared::Config
+ paras::Config
+ scheduler::Config
+ AuthorityDiscoveryConfig
{
type ValidatorSet: ValidatorSetWithIdentification<Self::AccountId>;
}
#[pallet::storage]
pub(super) type AssignmentKeysUnsafe<T: Config> =
StorageValue<_, Vec<AssignmentId>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn earliest_stored_session)]
pub(crate) type EarliestStoredSession<T: Config> = StorageValue<_, SessionIndex, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn session_info)]
pub(crate) type Sessions<T: Config> = StorageMap<_, Identity, SessionIndex, SessionInfo>;
#[pallet::storage]
#[pallet::getter(fn account_keys)]
pub(crate) type AccountKeys<T: Config> =
StorageMap<_, Identity, SessionIndex, Vec<AccountId<T>>>;
}
pub trait AuthorityDiscoveryConfig {
fn authorities() -> Vec<AuthorityDiscoveryId>;
}
impl<T: pallet_authority_discovery::Config> AuthorityDiscoveryConfig for T {
fn authorities() -> Vec<AuthorityDiscoveryId> {
<pallet_authority_discovery::Pallet<T>>::current_authorities().to_vec()
}
}
impl<T: Config> Pallet<T> {
pub(crate) fn initializer_on_new_session(
notification: &crate::initializer::SessionChangeNotification<T::BlockNumber>,
) {
let config = <configuration::Pallet<T>>::config();
let dispute_period = config.dispute_period;
let validators = notification.validators.clone();
let discovery_keys = <T as AuthorityDiscoveryConfig>::authorities();
let assignment_keys = AssignmentKeysUnsafe::<T>::get();
let active_set = <shared::Pallet<T>>::active_validator_indices();
let validator_groups = <scheduler::Pallet<T>>::validator_groups();
let n_cores = <scheduler::Pallet<T>>::availability_cores().len() as u32;
let zeroth_delay_tranche_width = config.zeroth_delay_tranche_width;
let relay_vrf_modulo_samples = config.relay_vrf_modulo_samples;
let n_delay_tranches = config.n_delay_tranches;
let no_show_slots = config.no_show_slots;
let needed_approvals = config.needed_approvals;
let new_session_index = notification.session_index;
let random_seed = notification.random_seed;
let old_earliest_stored_session = EarliestStoredSession::<T>::get();
let new_earliest_stored_session = new_session_index.saturating_sub(dispute_period);
let new_earliest_stored_session =
core::cmp::max(new_earliest_stored_session, old_earliest_stored_session);
if old_earliest_stored_session != 0 || Sessions::<T>::get(0).is_some() {
for idx in old_earliest_stored_session..new_earliest_stored_session {
Sessions::<T>::remove(&idx);
AccountKeys::<T>::remove(&idx);
}
EarliestStoredSession::<T>::set(new_earliest_stored_session);
} else {
EarliestStoredSession::<T>::set(new_session_index);
}
let account_ids = T::ValidatorSet::validators();
let active_account_ids = take_active_subset(&active_set, &account_ids);
AccountKeys::<T>::insert(&new_session_index, &active_account_ids);
let new_session_info = SessionInfo {
validators, discovery_keys: take_active_subset_and_inactive(&active_set, &discovery_keys),
assignment_keys: take_active_subset(&active_set, &assignment_keys),
validator_groups,
n_cores,
zeroth_delay_tranche_width,
relay_vrf_modulo_samples,
n_delay_tranches,
no_show_slots,
needed_approvals,
active_validator_indices: active_set,
random_seed,
dispute_period,
};
Sessions::<T>::insert(&new_session_index, &new_session_info);
}
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
Weight::zero()
}
pub(crate) fn initializer_finalize() {}
}
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
type Public = AssignmentId;
}
impl<T: pallet_session::Config + Config> OneSessionHandler<T::AccountId> for Pallet<T> {
type Key = AssignmentId;
fn on_genesis_session<'a, I: 'a>(_validators: I)
where
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
{
}
fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, _queued: I)
where
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
{
let assignment_keys: Vec<_> = validators.map(|(_, v)| v).collect();
AssignmentKeysUnsafe::<T>::set(assignment_keys);
}
fn on_disabled(_i: u32) {}
}