Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Replace parachain/parathread boolean by enum #6198

Merged
merged 17 commits into from
Nov 1, 2022
Prev Previous commit
Next Next commit
Manual en-/decocing of Parakind
  • Loading branch information
alexgparity committed Oct 28, 2022
commit 739e6e3e2ea0a4d575ff824ce13ff25daa93efd8
28 changes: 27 additions & 1 deletion runtime/parachains/src/paras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,39 @@ pub struct ParaGenesisArgs {
}

/// Distinguishes between Parachain and Parathread
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum ParaKind {
Parathread,
Parachain,
}

// manual encoding and decoding 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()),
}
}
}

/// This enum describes a reason why a particular PVF pre-checking vote was initiated. When the
/// PVF vote in question is concluded, this enum indicates what changes should be performed.
#[derive(Encode, Decode, TypeInfo)]
Expand Down
17 changes: 17 additions & 0 deletions runtime/parachains/src/paras/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,3 +1723,20 @@ fn verify_upgrade_restriction_signal_is_externally_accessible() {
);
});
}

#[test]
fn parakind_encodes_decodes_to_bool() {
let chain_kind = ParaKind::Parachain.encode();
let chain_bool = true.encode();
assert_eq!(chain_kind, chain_bool);

let chain_dec = ParaKind::decode(&mut chain_kind.as_slice());
assert_eq!(chain_dec, Ok(ParaKind::Parachain));

let thread_kind = ParaKind::Parathread.encode();
let thread_bool = false.encode();
assert_eq!(thread_kind, thread_bool);

let thread_dec = ParaKind::decode(&mut thread_kind.as_slice());
assert_eq!(thread_dec, Ok(ParaKind::Parathread));
}
2 changes: 1 addition & 1 deletion runtime/parachains/src/scheduler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
paras::{ParaGenesisArgs, ParaKind},
};

fn schedule_blank_para(id: ParaId, paratype: ParaKind) {
fn schedule_blank_para(id: ParaId, parakind: ParaKind) {
assert_ok!(Paras::schedule_para_initialize(
id,
ParaGenesisArgs {
Expand Down