mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 09:21:04 +00:00
Refact proposal pallet (#2322)
* Refact proposal pallet * Update runtime/rococo/src/propose_parachain.rs Co-authored-by: Andronik Ordian <write@reusable.software> * Fix * Apply suggestions from code review Co-authored-by: Andronik Ordian <write@reusable.software> * AHH Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Andronik Ordian <write@reusable.software> Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
Generated
+139
-139
File diff suppressed because it is too large
Load Diff
@@ -204,7 +204,7 @@ construct_runtime! {
|
|||||||
Sudo: pallet_sudo::{Module, Call, Storage, Event<T>, Config<T>},
|
Sudo: pallet_sudo::{Module, Call, Storage, Event<T>, Config<T>},
|
||||||
|
|
||||||
// Propose parachain pallet.
|
// Propose parachain pallet.
|
||||||
ProposeParachain: propose_parachain::{Module, Call, Storage, Event},
|
ProposeParachain: propose_parachain::{Module, Call, Storage, Event<T>},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ pub trait Config: pallet_session::Config
|
|||||||
+ runtime_parachains::hrmp::Config
|
+ runtime_parachains::hrmp::Config
|
||||||
{
|
{
|
||||||
/// The overreaching event type.
|
/// The overreaching event type.
|
||||||
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
|
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
|
||||||
|
|
||||||
/// The maximum name length of a parachain.
|
/// The maximum name length of a parachain.
|
||||||
type MaxNameLength: Get<u32>;
|
type MaxNameLength: Get<u32>;
|
||||||
@@ -78,8 +78,6 @@ pub trait Config: pallet_session::Config
|
|||||||
struct Proposal<AccountId, ValidatorId, Balance> {
|
struct Proposal<AccountId, ValidatorId, Balance> {
|
||||||
/// The account that proposed this parachain.
|
/// The account that proposed this parachain.
|
||||||
proposer: AccountId,
|
proposer: AccountId,
|
||||||
/// The validation WASM code of the parachain.
|
|
||||||
validation_code: ValidationCode,
|
|
||||||
/// The genesis head state of the parachain.
|
/// The genesis head state of the parachain.
|
||||||
genesis_head: HeadData,
|
genesis_head: HeadData,
|
||||||
/// The validators for the relay chain provided by the parachain.
|
/// The validators for the relay chain provided by the parachain.
|
||||||
@@ -100,13 +98,15 @@ struct RegisteredParachainInfo<AccountId, ValidatorId> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
decl_event! {
|
decl_event! {
|
||||||
pub enum Event {
|
pub enum Event<T> where ValidatorId = <T as pallet_session::Config>::ValidatorId {
|
||||||
/// A parachain was proposed for registration.
|
/// A parachain was proposed for registration.
|
||||||
ParachainProposed(Vec<u8>, ParaId),
|
ParachainProposed(Vec<u8>, ParaId),
|
||||||
/// A parachain was approved and is scheduled for being activated.
|
/// A parachain was approved and is scheduled for being activated.
|
||||||
ParachainApproved(ParaId),
|
ParachainApproved(ParaId),
|
||||||
/// A parachain was registered and is now running.
|
/// A parachain was registered and is now running.
|
||||||
ParachainRegistered(ParaId),
|
ParachainRegistered(ParaId),
|
||||||
|
/// New validators were added to the set.
|
||||||
|
ValidatorsRegistered(Vec<ValidatorId>),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,6 +141,8 @@ decl_storage! {
|
|||||||
trait Store for Module<T: Config> as ParachainProposer {
|
trait Store for Module<T: Config> as ParachainProposer {
|
||||||
/// All the proposals.
|
/// All the proposals.
|
||||||
Proposals: map hasher(twox_64_concat) ParaId => Option<Proposal<T::AccountId, T::ValidatorId, BalanceOf<T>>>;
|
Proposals: map hasher(twox_64_concat) ParaId => Option<Proposal<T::AccountId, T::ValidatorId, BalanceOf<T>>>;
|
||||||
|
/// The validation WASM code of the parachain.
|
||||||
|
ParachainValidationCode: map hasher(twox_64_concat) ParaId => Option<ValidationCode>;
|
||||||
/// Proposals that are approved.
|
/// Proposals that are approved.
|
||||||
ApprovedProposals: Vec<ParaId>;
|
ApprovedProposals: Vec<ParaId>;
|
||||||
/// Proposals that are scheduled at for a fixed session to be applied.
|
/// Proposals that are scheduled at for a fixed session to be applied.
|
||||||
@@ -149,6 +151,8 @@ decl_storage! {
|
|||||||
ParachainInfo: map hasher(twox_64_concat) ParaId => Option<RegisteredParachainInfo<T::AccountId, T::ValidatorId>>;
|
ParachainInfo: map hasher(twox_64_concat) ParaId => Option<RegisteredParachainInfo<T::AccountId, T::ValidatorId>>;
|
||||||
/// Validators that should be retired, because their Parachain was deregistered.
|
/// Validators that should be retired, because their Parachain was deregistered.
|
||||||
ValidatorsToRetire: Vec<T::ValidatorId>;
|
ValidatorsToRetire: Vec<T::ValidatorId>;
|
||||||
|
/// Validators that should be added.
|
||||||
|
ValidatorsToAdd: Vec<T::ValidatorId>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,10 +205,12 @@ decl_module! {
|
|||||||
ensure!(validation_code.0.starts_with(runtime_common::WASM_MAGIC), Error::<T>::DefinitelyNotWasm);
|
ensure!(validation_code.0.starts_with(runtime_common::WASM_MAGIC), Error::<T>::DefinitelyNotWasm);
|
||||||
|
|
||||||
let active_validators = Session::<T>::validators();
|
let active_validators = Session::<T>::validators();
|
||||||
|
let validators_to_retire = ValidatorsToRetire::<T>::get();
|
||||||
ensure!(
|
ensure!(
|
||||||
validators.iter().all(|v| !active_validators.contains(v)),
|
validators.iter().all(|v| !active_validators.contains(v) || validators_to_retire.contains(v)),
|
||||||
Error::<T>::ValidatorAlreadyRegistered,
|
Error::<T>::ValidatorAlreadyRegistered,
|
||||||
);
|
);
|
||||||
|
|
||||||
Proposals::<T>::iter().try_for_each(|(_, prop)|
|
Proposals::<T>::iter().try_for_each(|(_, prop)|
|
||||||
if validators.iter().all(|v| !prop.validators.contains(v)) {
|
if validators.iter().all(|v| !prop.validators.contains(v)) {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -220,13 +226,13 @@ decl_module! {
|
|||||||
proposer: who,
|
proposer: who,
|
||||||
validators: validators.into(),
|
validators: validators.into(),
|
||||||
genesis_head,
|
genesis_head,
|
||||||
validation_code,
|
|
||||||
balance,
|
balance,
|
||||||
};
|
};
|
||||||
|
|
||||||
Proposals::<T>::insert(para_id, proposal);
|
Proposals::<T>::insert(para_id, proposal);
|
||||||
|
ParachainValidationCode::insert(para_id, validation_code);
|
||||||
|
|
||||||
Self::deposit_event(Event::ParachainProposed(name, para_id));
|
Self::deposit_event(RawEvent::ParachainProposed(name, para_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Approve a parachain proposal.
|
/// Approve a parachain proposal.
|
||||||
@@ -243,7 +249,7 @@ decl_module! {
|
|||||||
|
|
||||||
ApprovedProposals::append(para_id);
|
ApprovedProposals::append(para_id);
|
||||||
|
|
||||||
Self::deposit_event(Event::ParachainApproved(para_id));
|
Self::deposit_event(RawEvent::ParachainApproved(para_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cancel a parachain proposal.
|
/// Cancel a parachain proposal.
|
||||||
@@ -265,6 +271,7 @@ decl_module! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Proposals::<T>::remove(¶_id);
|
Proposals::<T>::remove(¶_id);
|
||||||
|
ParachainValidationCode::remove(¶_id);
|
||||||
|
|
||||||
pallet_balances::Module::<T>::unreserve(&proposal.proposer, T::ProposeDeposit::get());
|
pallet_balances::Module::<T>::unreserve(&proposal.proposer, T::ProposeDeposit::get());
|
||||||
}
|
}
|
||||||
@@ -289,6 +296,19 @@ decl_module! {
|
|||||||
|
|
||||||
pallet_balances::Module::<T>::unreserve(&info.proposer, T::ProposeDeposit::get());
|
pallet_balances::Module::<T>::unreserve(&info.proposer, T::ProposeDeposit::get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add new validators to the set.
|
||||||
|
#[weight = 100_000]
|
||||||
|
fn register_validators(
|
||||||
|
origin,
|
||||||
|
validators: Vec<T::ValidatorId>,
|
||||||
|
) {
|
||||||
|
T::PriviledgedOrigin::ensure_origin(origin)?;
|
||||||
|
|
||||||
|
validators.clone().into_iter().for_each(|v| ValidatorsToAdd::<T>::append(v));
|
||||||
|
|
||||||
|
Self::deposit_event(RawEvent::ValidatorsRegistered(validators));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,9 +347,10 @@ impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Module<T> {
|
|||||||
for (id, proposal) in proposals.iter().filter_map(|id| Proposals::<T>::get(&id).map(|p| (id, p))) {
|
for (id, proposal) in proposals.iter().filter_map(|id| Proposals::<T>::get(&id).map(|p| (id, p))) {
|
||||||
ScheduledProposals::append(new_index, id);
|
ScheduledProposals::append(new_index, id);
|
||||||
|
|
||||||
|
let validation_code = ParachainValidationCode::get(&id)?;
|
||||||
let genesis = ParaGenesisArgs {
|
let genesis = ParaGenesisArgs {
|
||||||
genesis_head: proposal.genesis_head,
|
genesis_head: proposal.genesis_head,
|
||||||
validation_code: proposal.validation_code,
|
validation_code,
|
||||||
parachain: true,
|
parachain: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -338,6 +359,12 @@ impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Module<T> {
|
|||||||
validators.extend(proposal.validators);
|
validators.extend(proposal.validators);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ValidatorsToAdd::<T>::take().into_iter().for_each(|v| {
|
||||||
|
if !validators.contains(&v) {
|
||||||
|
validators.push(v);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Some(validators)
|
Some(validators)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,7 +375,7 @@ impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Module<T> {
|
|||||||
|
|
||||||
// Register all parachains that are allowed to start with the new session.
|
// Register all parachains that are allowed to start with the new session.
|
||||||
for (id, proposal) in proposals.iter().filter_map(|id| Proposals::<T>::take(&id).map(|p| (id, p))) {
|
for (id, proposal) in proposals.iter().filter_map(|id| Proposals::<T>::take(&id).map(|p| (id, p))) {
|
||||||
Self::deposit_event(Event::ParachainRegistered(*id));
|
Self::deposit_event(RawEvent::ParachainRegistered(*id));
|
||||||
|
|
||||||
// Add some funds to the Parachain
|
// Add some funds to the Parachain
|
||||||
let _ = pallet_balances::Module::<T>::deposit_creating(&id.into_account(), proposal.balance);
|
let _ = pallet_balances::Module::<T>::deposit_creating(&id.into_account(), proposal.balance);
|
||||||
|
|||||||
Reference in New Issue
Block a user