Skip to content

Commit 81f980a

Browse files
authored
change: replace abstract ScEpochNumber with domain type in selection pallet (#1068)
1 parent ee125b2 commit 81f980a

File tree

10 files changed

+75
-108
lines changed

10 files changed

+75
-108
lines changed

demo/runtime/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ impl pallet_session_validator_management::Config for Runtime {
343343
type AuthorityId = CrossChainPublic;
344344
type AuthorityKeys = SessionKeys;
345345
type AuthoritySelectionInputs = AuthoritySelectionInputs;
346-
type ScEpochNumber = ScEpochNumber;
347346
type WeightInfo = pallet_session_validator_management::weights::SubstrateWeight<Runtime>;
348347
type CommitteeMember = CommitteeMember<CrossChainPublic, SessionKeys>;
349348
type MainChainScriptsOrigin = EnsureRoot<Self::AccountId>;

demo/runtime/src/mock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ impl pallet_session_validator_management::Config for Test {
165165
type AuthorityId = CrossChainPublic;
166166
type AuthorityKeys = TestSessionKeys;
167167
type AuthoritySelectionInputs = AuthoritySelectionInputs;
168-
type ScEpochNumber = ScEpochNumber;
169168
type CommitteeMember = (Self::AuthorityId, Self::AuthorityKeys);
170169
type MainChainScriptsOrigin = EnsureRoot<Self::AccountId>;
171170

toolkit/committee-selection/pallet/src/lib.rs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133
//! type AuthorityId = CrossChainPublic;
134134
//! type AuthorityKeys = SessionKeys;
135135
//! type AuthoritySelectionInputs = authority_selection_inherents::AuthoritySelectionInputs;
136-
//! type ScEpochNumber = sidechain_domain::ScEpochNumber;
137136
//! type WeightInfo = pallet_session_validator_management::weights::SubstrateWeight<Runtime>;
138137
//! type CommitteeMember = authority_selection_inherents::CommitteeMember<CrossChainPublic, SessionKeys>;
139138
//! type MainChainScriptsOrigin = EnsureRoot<Self::AccountId>;
@@ -262,13 +261,12 @@ pub mod pallet {
262261
use frame_system::pallet_prelude::*;
263262
use log::{info, warn};
264263
use sidechain_domain::byte_string::SizedByteString;
265-
use sidechain_domain::{MainchainAddress, PolicyId};
264+
use sidechain_domain::{MainchainAddress, PolicyId, ScEpochNumber};
266265
use sp_core::blake2_256;
267266
use sp_runtime::traits::{MaybeSerializeDeserialize, One, Zero};
268267
use sp_session_validator_management::*;
269268
use sp_std::collections::btree_set::BTreeSet;
270-
use sp_std::fmt::Display;
271-
use sp_std::{ops::Add, vec::Vec};
269+
use sp_std::vec::Vec;
272270

273271
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
274272

@@ -292,18 +290,6 @@ pub mod pallet {
292290
type AuthorityKeys: Parameter + Member + MaybeSerializeDeserialize + Ord + MaxEncodedLen;
293291
/// Type of input data used by `select_authorities` to select a committee.
294292
type AuthoritySelectionInputs: Parameter;
295-
/// Type of the epoch number used by the partner chain.
296-
type ScEpochNumber: Parameter
297-
+ MaxEncodedLen
298-
+ Zero
299-
+ Display
300-
+ Add
301-
+ One
302-
+ Default
303-
+ Ord
304-
+ Copy
305-
+ From<u64>
306-
+ Into<u64>;
307293

308294
/// Type of committee members returned by `select_authorities`.
309295
type CommitteeMember: Parameter
@@ -319,11 +305,11 @@ pub mod pallet {
319305
/// Should return [None] if selection was impossible for the given input.
320306
fn select_authorities(
321307
input: Self::AuthoritySelectionInputs,
322-
sidechain_epoch: Self::ScEpochNumber,
308+
sidechain_epoch: ScEpochNumber,
323309
) -> Option<BoundedVec<Self::CommitteeMember, Self::MaxValidators>>;
324310

325311
/// Should return the current partner chain epoch.
326-
fn current_epoch_number() -> Self::ScEpochNumber;
312+
fn current_epoch_number() -> ScEpochNumber;
327313

328314
/// Weight functions needed for pallet_session_validator_management.
329315
type WeightInfo: WeightInfo;
@@ -343,27 +329,23 @@ pub mod pallet {
343329
#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
344330
#[scale_info(skip_type_params(MaxValidators))]
345331
/// Committee info type used on-chain.
346-
pub struct CommitteeInfo<ScEpochNumber: Clone, CommitteeMember: Clone, MaxValidators> {
332+
pub struct CommitteeInfo<CommitteeMember: Clone, MaxValidators> {
347333
/// Epoch number the committee is selected for.
348334
pub epoch: ScEpochNumber,
349335
/// List of committee members.
350336
pub committee: BoundedVec<CommitteeMember, MaxValidators>,
351337
}
352338

353-
impl<ScEpochNumber: Clone, CommitteeMember: Clone, MaxValidators>
354-
CommitteeInfo<ScEpochNumber, CommitteeMember, MaxValidators>
355-
{
339+
impl<CommitteeMember: Clone, MaxValidators> CommitteeInfo<CommitteeMember, MaxValidators> {
356340
/// Returns committee info as a pair of epoch number and list of committee members
357341
pub fn as_pair(self) -> (ScEpochNumber, Vec<CommitteeMember>) {
358342
(self.epoch, self.committee.to_vec())
359343
}
360344
}
361345

362-
impl<ScEpochNumber, CommitteeMember, MaxValidators> Default
363-
for CommitteeInfo<ScEpochNumber, CommitteeMember, MaxValidators>
346+
impl<CommitteeMember, MaxValidators> Default for CommitteeInfo<CommitteeMember, MaxValidators>
364347
where
365348
CommitteeMember: Clone,
366-
ScEpochNumber: Clone + Zero,
367349
{
368350
fn default() -> Self {
369351
Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
@@ -375,18 +357,12 @@ pub mod pallet {
375357
StorageValue<_, BoundedBTreeSet<T::AccountId, T::MaxValidators>, ValueQuery>;
376358

377359
#[pallet::storage]
378-
pub type CurrentCommittee<T: Config> = StorageValue<
379-
_,
380-
CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>,
381-
ValueQuery,
382-
>;
360+
pub type CurrentCommittee<T: Config> =
361+
StorageValue<_, CommitteeInfo<T::CommitteeMember, T::MaxValidators>, ValueQuery>;
383362

384363
#[pallet::storage]
385-
pub type NextCommittee<T: Config> = StorageValue<
386-
_,
387-
CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>,
388-
OptionQuery,
389-
>;
364+
pub type NextCommittee<T: Config> =
365+
StorageValue<_, CommitteeInfo<T::CommitteeMember, T::MaxValidators>, OptionQuery>;
390366

391367
/// Stores the stage of handling the inputs change. Used by session manager, to decide
392368
/// if the session should be ended quickly, to speed up using the newly selected committee.
@@ -428,7 +404,7 @@ pub mod pallet {
428404
ProvidedAccounts::<T>::set(provided_accounts.try_into().unwrap());
429405

430406
let committee_info =
431-
CommitteeInfo { epoch: T::ScEpochNumber::zero(), committee: initial_authorities };
407+
CommitteeInfo { epoch: ScEpochNumber::zero(), committee: initial_authorities };
432408
CurrentCommittee::<T>::put(committee_info);
433409
MainChainScriptsConfiguration::<T>::put(self.main_chain_scripts.clone());
434410
}
@@ -545,7 +521,7 @@ pub mod pallet {
545521
pub fn set(
546522
origin: OriginFor<T>,
547523
validators: BoundedVec<T::CommitteeMember, T::MaxValidators>,
548-
for_epoch_number: T::ScEpochNumber,
524+
for_epoch_number: ScEpochNumber,
549525
selection_inputs_hash: SizedByteString<32>,
550526
) -> DispatchResult {
551527
ensure_none(origin)?;
@@ -588,7 +564,7 @@ pub mod pallet {
588564

589565
impl<T: Config> Pallet<T> {
590566
/// Returns epoch number for which next committee hasn't been set yet.
591-
pub fn get_next_unset_epoch_number() -> T::ScEpochNumber {
567+
pub fn get_next_unset_epoch_number() -> ScEpochNumber {
592568
NextCommittee::<T>::get()
593569
.map(|next_committee| next_committee.epoch + One::one())
594570
.unwrap_or(CurrentCommittee::<T>::get().epoch + One::one())
@@ -605,14 +581,13 @@ pub mod pallet {
605581
}
606582

607583
/// Returns current committee from storage.
608-
pub fn current_committee_storage()
609-
-> CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators> {
584+
pub fn current_committee_storage() -> CommitteeInfo<T::CommitteeMember, T::MaxValidators> {
610585
CurrentCommittee::<T>::get()
611586
}
612587

613588
/// Returns next committee from storage.
614589
pub fn next_committee_storage()
615-
-> Option<CommitteeInfo<T::ScEpochNumber, T::CommitteeMember, T::MaxValidators>> {
590+
-> Option<CommitteeInfo<T::CommitteeMember, T::MaxValidators>> {
616591
NextCommittee::<T>::get()
617592
}
618593

@@ -644,7 +619,7 @@ pub mod pallet {
644619
/// Calculates committee using configured `select_authorities` function
645620
pub fn calculate_committee(
646621
authority_selection_inputs: T::AuthoritySelectionInputs,
647-
sidechain_epoch: T::ScEpochNumber,
622+
sidechain_epoch: ScEpochNumber,
648623
) -> Option<Vec<T::CommitteeMember>> {
649624
T::select_authorities(authority_selection_inputs, sidechain_epoch).map(|c| c.to_vec())
650625
}

toolkit/committee-selection/pallet/src/migrations/v0.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use frame_support::pallet_prelude::{OptionQuery, ValueQuery, Zero};
44
use frame_support::{BoundedVec, CloneNoBound, storage_alias};
55
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
66
use scale_info::TypeInfo;
7+
use sidechain_domain::ScEpochNumber;
78

89
#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
910
#[scale_info(skip_type_params(MaxValidators))]
@@ -17,12 +18,11 @@ pub struct LegacyCommitteeInfo<
1718
pub committee: BoundedVec<(AuthorityId, AuthorityKeys), MaxValidators>,
1819
}
1920

20-
impl<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators> Default
21+
impl<AuthorityId, AuthorityKeys, MaxValidators> Default
2122
for LegacyCommitteeInfo<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators>
2223
where
2324
AuthorityId: Clone,
2425
AuthorityKeys: Clone,
25-
ScEpochNumber: Clone + Zero,
2626
{
2727
fn default() -> Self {
2828
Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
@@ -33,7 +33,7 @@ where
3333
pub type CurrentCommittee<T: crate::pallet::Config> = StorageValue<
3434
crate::Pallet<T>,
3535
LegacyCommitteeInfo<
36-
<T as crate::pallet::Config>::ScEpochNumber,
36+
ScEpochNumber,
3737
<T as crate::pallet::Config>::AuthorityId,
3838
<T as crate::pallet::Config>::AuthorityKeys,
3939
<T as crate::pallet::Config>::MaxValidators,
@@ -45,7 +45,7 @@ pub type CurrentCommittee<T: crate::pallet::Config> = StorageValue<
4545
pub type NextCommittee<T: crate::pallet::Config> = StorageValue<
4646
crate::Pallet<T>,
4747
LegacyCommitteeInfo<
48-
<T as crate::pallet::Config>::ScEpochNumber,
48+
ScEpochNumber,
4949
<T as crate::pallet::Config>::AuthorityId,
5050
<T as crate::pallet::Config>::AuthorityKeys,
5151
<T as crate::pallet::Config>::MaxValidators,

toolkit/committee-selection/pallet/src/migrations/v1.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#[cfg(feature = "try-runtime")]
33
extern crate alloc;
44
use frame_support::traits::UncheckedOnRuntimeUpgrade;
5+
use sidechain_domain::ScEpochNumber;
56
#[cfg(feature = "try-runtime")]
67
use {
78
alloc::vec::Vec, parity_scale_codec::Encode, sp_session_validator_management::CommitteeMember,
@@ -30,32 +31,26 @@ where
3031
use sp_runtime::BoundedVec;
3132

3233
let current_committee_v0 = v0::CurrentCommittee::<T>::get();
33-
let current_committee_v1 = crate::pallet::CommitteeInfo::<
34-
T::ScEpochNumber,
35-
T::CommitteeMember,
36-
T::MaxValidators,
37-
> {
38-
epoch: current_committee_v0.epoch,
39-
committee: BoundedVec::truncate_from(
40-
current_committee_v0.committee.into_iter().map(From::from).collect(),
41-
),
42-
};
34+
let current_committee_v1 =
35+
crate::pallet::CommitteeInfo::<T::CommitteeMember, T::MaxValidators> {
36+
epoch: current_committee_v0.epoch,
37+
committee: BoundedVec::truncate_from(
38+
current_committee_v0.committee.into_iter().map(From::from).collect(),
39+
),
40+
};
4341

4442
crate::CurrentCommittee::<T>::put(current_committee_v1);
4543

4644
let Some(next_committee_v0) = v0::NextCommittee::<T>::get() else {
4745
return T::DbWeight::get().reads_writes(2, 1);
4846
};
49-
let next_committee_v1 = crate::pallet::CommitteeInfo::<
50-
T::ScEpochNumber,
51-
T::CommitteeMember,
52-
T::MaxValidators,
53-
> {
54-
epoch: next_committee_v0.epoch,
55-
committee: BoundedVec::truncate_from(
56-
next_committee_v0.committee.into_iter().map(From::from).collect(),
57-
),
58-
};
47+
let next_committee_v1 =
48+
crate::pallet::CommitteeInfo::<T::CommitteeMember, T::MaxValidators> {
49+
epoch: next_committee_v0.epoch,
50+
committee: BoundedVec::truncate_from(
51+
next_committee_v0.committee.into_iter().map(From::from).collect(),
52+
),
53+
};
5954

6055
crate::NextCommittee::<T>::put(next_committee_v1);
6156

@@ -76,15 +71,10 @@ where
7671
use v0::LegacyCommitteeInfo;
7772

7873
let (current_committee_v0, next_committee_v0): (
79-
LegacyCommitteeInfo<
80-
T::ScEpochNumber,
81-
T::AuthorityId,
82-
T::AuthorityKeys,
83-
T::MaxValidators,
84-
>,
74+
LegacyCommitteeInfo<ScEpochNumber, T::AuthorityId, T::AuthorityKeys, T::MaxValidators>,
8575
Option<
8676
LegacyCommitteeInfo<
87-
T::ScEpochNumber,
77+
ScEpochNumber,
8878
T::AuthorityId,
8979
T::AuthorityKeys,
9080
T::MaxValidators,

toolkit/committee-selection/pallet/src/mock.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use frame_support::{
88
};
99
use frame_system::EnsureRoot;
1010
use pallet_balances::AccountData;
11+
use sidechain_domain::ScEpochNumber;
1112
use sidechain_domain::byte_string::SizedByteString;
1213
use sp_core::{H256, blake2_256};
1314
use sp_runtime::{
@@ -22,7 +23,6 @@ type Block = frame_system::mocking::MockBlock<Test>;
2223

2324
type AccountId = u64;
2425
type AuthorityId = u64;
25-
pub type ScEpochNumber = u64;
2626
pub type AuthorityKeys = u64;
2727

2828
sp_runtime::impl_opaque_keys! {
@@ -41,6 +41,7 @@ impl From<u64> for SessionKeys {
4141
#[frame_support::pallet]
4242
pub mod mock_pallet {
4343
use frame_support::pallet_prelude::*;
44+
use sidechain_domain::ScEpochNumber;
4445

4546
#[pallet::pallet]
4647
pub struct Pallet<T>(_);
@@ -49,7 +50,7 @@ pub mod mock_pallet {
4950
pub trait Config: frame_system::Config {}
5051

5152
#[pallet::storage]
52-
pub type CurrentEpoch<T: Config> = StorageValue<_, u64, ValueQuery>;
53+
pub type CurrentEpoch<T: Config> = StorageValue<_, ScEpochNumber, ValueQuery>;
5354
}
5455

5556
// Configure a mock runtime to test the pallet.
@@ -114,19 +115,18 @@ impl pallet::Config for Test {
114115
type AuthorityKeys = AuthorityKeys;
115116
type AuthoritySelectionInputs =
116117
BoundedVec<(Self::AuthorityId, Self::AuthorityKeys), Self::MaxValidators>;
117-
type ScEpochNumber = ScEpochNumber;
118118
type CommitteeMember = (Self::AuthorityId, Self::AuthorityKeys);
119119
type MainChainScriptsOrigin = EnsureRoot<Self::AccountId>;
120120

121121
fn select_authorities(
122122
input: Self::AuthoritySelectionInputs,
123-
_sidechain_epoch: Self::ScEpochNumber,
123+
_sidechain_epoch: ScEpochNumber,
124124
) -> Option<BoundedVec<(Self::AuthorityId, Self::AuthorityKeys), Self::MaxValidators>> {
125125
// This is a good approximation of the real selection algorithm, that returns None iff there are no valid candidates to select from.
126126
if input.is_empty() { None } else { Some(input) }
127127
}
128128

129-
fn current_epoch_number() -> Self::ScEpochNumber {
129+
fn current_epoch_number() -> ScEpochNumber {
130130
current_epoch_number()
131131
}
132132

@@ -243,7 +243,7 @@ pub fn set_validators_directly(
243243
SessionCommitteeManagement::set(
244244
RuntimeOrigin::none(),
245245
BoundedVec::truncate_from(expected_validators),
246-
for_epoch,
246+
for_epoch.into(),
247247
data_hash,
248248
)
249249
}
@@ -267,7 +267,7 @@ pub fn create_inherent_set_validators_call(
267267
<SessionCommitteeManagement as ProvideInherent>::create_inherent(&inherent_data.0)
268268
}
269269

270-
pub(crate) fn current_epoch_number() -> u64 {
270+
pub(crate) fn current_epoch_number() -> ScEpochNumber {
271271
mock_pallet::CurrentEpoch::<Test>::get()
272272
}
273273

0 commit comments

Comments
 (0)