mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 05:11:02 +00:00
Replace parachain/parathread boolean by enum (#6198)
* Replace parachain/parathread boolean by enum * Address PR comments * Update dependencies * ParaType -> ParaKind * Swap enum field order to avoid migration * Rename paratype field to parakind * Manual en-/decocing of Parakind * Manual TypeInfo for ParaKind * rename field back to parachain * minor * Update runtime/parachains/src/paras/mod.rs Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com> * Manual serde Serialize and Deserialize for ParaKind * cargo fmt * Update runtime/parachains/src/paras/mod.rs Co-authored-by: Andronik <write@reusable.software> * Add test for serde_json encoding/decoding * Move serde_json dep to dev-deps Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com> Co-authored-by: Andronik <write@reusable.software>
This commit is contained in:
@@ -115,7 +115,7 @@ use primitives::v2::{
|
||||
ConsensusLog, HeadData, Id as ParaId, PvfCheckStatement, SessionIndex, UpgradeGoAhead,
|
||||
UpgradeRestriction, ValidationCode, ValidationCodeHash, ValidatorSignature,
|
||||
};
|
||||
use scale_info::TypeInfo;
|
||||
use scale_info::{Type, TypeInfo};
|
||||
use sp_core::RuntimeDebug;
|
||||
use sp_runtime::{
|
||||
traits::{AppVerify, One, Saturating},
|
||||
@@ -291,8 +291,76 @@ pub struct ParaGenesisArgs {
|
||||
pub genesis_head: HeadData,
|
||||
/// The initial validation code to use.
|
||||
pub validation_code: ValidationCode,
|
||||
/// True if parachain, false if parathread.
|
||||
pub parachain: bool,
|
||||
/// Parachain or Parathread.
|
||||
#[cfg_attr(feature = "std", serde(rename = "parachain"))]
|
||||
pub para_kind: ParaKind,
|
||||
}
|
||||
|
||||
/// Distinguishes between Parachain and Parathread
|
||||
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
|
||||
pub enum ParaKind {
|
||||
Parathread,
|
||||
Parachain,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for ParaKind {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match self {
|
||||
ParaKind::Parachain => serializer.serialize_bool(true),
|
||||
ParaKind::Parathread => serializer.serialize_bool(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for ParaKind {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
match serde::de::Deserialize::deserialize(deserializer) {
|
||||
Ok(true) => Ok(ParaKind::Parachain),
|
||||
Ok(false) => Ok(ParaKind::Parathread),
|
||||
_ => Err(serde::de::Error::custom("invalid ParaKind serde representation")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Manual encoding, decoding, and TypeInfo as the parakind field in ParaGenesisArgs used to be a bool
|
||||
impl Encode for ParaKind {
|
||||
fn size_hint(&self) -> usize {
|
||||
true.size_hint()
|
||||
}
|
||||
|
||||
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
match self {
|
||||
ParaKind::Parachain => true.using_encoded(f),
|
||||
ParaKind::Parathread => false.using_encoded(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for ParaKind {
|
||||
fn decode<I: parity_scale_codec::Input>(
|
||||
input: &mut I,
|
||||
) -> Result<Self, parity_scale_codec::Error> {
|
||||
match bool::decode(input) {
|
||||
Ok(true) => Ok(ParaKind::Parachain),
|
||||
Ok(false) => Ok(ParaKind::Parathread),
|
||||
_ => Err("Invalid ParaKind representation".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeInfo for ParaKind {
|
||||
type Identity = bool;
|
||||
fn type_info() -> Type {
|
||||
bool::type_info()
|
||||
}
|
||||
}
|
||||
|
||||
/// This enum describes a reason why a particular PVF pre-checking vote was initiated. When the
|
||||
@@ -2021,11 +2089,12 @@ impl<T: Config> Pallet<T> {
|
||||
id: ParaId,
|
||||
genesis_data: &ParaGenesisArgs,
|
||||
) {
|
||||
if genesis_data.parachain {
|
||||
parachains.add(id);
|
||||
ParaLifecycles::<T>::insert(&id, ParaLifecycle::Parachain);
|
||||
} else {
|
||||
ParaLifecycles::<T>::insert(&id, ParaLifecycle::Parathread);
|
||||
match genesis_data.para_kind {
|
||||
ParaKind::Parachain => {
|
||||
parachains.add(id);
|
||||
ParaLifecycles::<T>::insert(&id, ParaLifecycle::Parachain);
|
||||
},
|
||||
ParaKind::Parathread => ParaLifecycles::<T>::insert(&id, ParaLifecycle::Parathread),
|
||||
}
|
||||
|
||||
// HACK: see the notice in `schedule_para_initialize`.
|
||||
|
||||
Reference in New Issue
Block a user