diff --git a/Cargo.lock b/Cargo.lock index ede6cb7363dd..f4ec558ca3d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7050,6 +7050,7 @@ dependencies = [ "sc-keystore", "scale-info", "serde", + "serde_json", "sp-api", "sp-application-crypto", "sp-core", diff --git a/node/test/service/src/lib.rs b/node/test/service/src/lib.rs index 8fe31ce2b5b4..831f0c85aaaa 100644 --- a/node/test/service/src/lib.rs +++ b/node/test/service/src/lib.rs @@ -27,7 +27,7 @@ use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProt use polkadot_overseer::Handle; use polkadot_primitives::v2::{Balance, CollatorPair, HeadData, Id as ParaId, ValidationCode}; use polkadot_runtime_common::BlockHashCount; -use polkadot_runtime_parachains::paras::ParaGenesisArgs; +use polkadot_runtime_parachains::paras::{ParaGenesisArgs, ParaKind}; use polkadot_service::{ ClientHandle, Error, ExecuteWithClient, FullClient, IsCollator, NewFull, PrometheusConfig, }; @@ -305,7 +305,7 @@ impl PolkadotTestNode { genesis: ParaGenesisArgs { genesis_head: genesis_head.into(), validation_code: validation_code.into(), - parachain: true, + para_kind: ParaKind::Parachain, }, }; diff --git a/runtime/common/src/paras_registrar.rs b/runtime/common/src/paras_registrar.rs index 7245cd92d304..bfcc91e3ba71 100644 --- a/runtime/common/src/paras_registrar.rs +++ b/runtime/common/src/paras_registrar.rs @@ -35,6 +35,7 @@ use sp_std::{prelude::*, result}; use crate::traits::{OnSwap, Registrar}; pub use pallet::*; use parity_scale_codec::{Decode, Encode}; +use runtime_parachains::paras::ParaKind; use scale_info::TypeInfo; use sp_runtime::{ traits::{CheckedSub, Saturating}, @@ -570,7 +571,7 @@ impl Pallet { }; ensure!(paras::Pallet::::lifecycle(id).is_none(), Error::::AlreadyRegistered); let (genesis, deposit) = - Self::validate_onboarding_data(genesis_head, validation_code, false)?; + Self::validate_onboarding_data(genesis_head, validation_code, ParaKind::Parathread)?; let deposit = deposit_override.unwrap_or(deposit); if let Some(additional) = deposit.checked_sub(&deposited) { @@ -613,7 +614,7 @@ impl Pallet { fn validate_onboarding_data( genesis_head: HeadData, validation_code: ValidationCode, - parachain: bool, + para_kind: ParaKind, ) -> Result<(ParaGenesisArgs, BalanceOf), sp_runtime::DispatchError> { let config = configuration::Pallet::::config(); ensure!(validation_code.0.len() > 0, Error::::EmptyCode); @@ -628,7 +629,7 @@ impl Pallet { .saturating_add(per_byte_fee.saturating_mul((genesis_head.0.len() as u32).into())) .saturating_add(per_byte_fee.saturating_mul((validation_code.0.len() as u32).into())); - Ok((ParaGenesisArgs { genesis_head, validation_code, parachain }, deposit)) + Ok((ParaGenesisArgs { genesis_head, validation_code, para_kind }, deposit)) } /// Swap a parachain and parathread, which involves scheduling an appropriate lifecycle update. diff --git a/runtime/parachains/Cargo.toml b/runtime/parachains/Cargo.toml index 4361f443d9a9..cb8b989ccdda 100644 --- a/runtime/parachains/Cargo.toml +++ b/runtime/parachains/Cargo.toml @@ -57,6 +57,7 @@ test-helpers = { package = "polkadot-primitives-test-helpers", path = "../../pri sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } thousands = "0.2.0" assert_matches = "1" +serde_json = "1.0.85" [features] default = ["std"] diff --git a/runtime/parachains/src/builder.rs b/runtime/parachains/src/builder.rs index f4bc58f2d4f2..9a1c16e6aa1d 100644 --- a/runtime/parachains/src/builder.rs +++ b/runtime/parachains/src/builder.rs @@ -16,6 +16,7 @@ use crate::{ configuration, inclusion, initializer, paras, + paras::ParaKind, paras_inherent::{self}, scheduler, session_info, shared, }; @@ -345,7 +346,7 @@ impl BenchBuilder { paras::ParaGenesisArgs { genesis_head: Self::mock_head_data(), validation_code: mock_validation_code(), - parachain: true, + para_kind: ParaKind::Parachain, }, ) .unwrap(); diff --git a/runtime/parachains/src/hrmp/benchmarking.rs b/runtime/parachains/src/hrmp/benchmarking.rs index 7ea14b1dc922..562e735e6a50 100644 --- a/runtime/parachains/src/hrmp/benchmarking.rs +++ b/runtime/parachains/src/hrmp/benchmarking.rs @@ -17,7 +17,7 @@ use crate::{ configuration::Pallet as Configuration, hrmp::{Pallet as Hrmp, *}, - paras::{Pallet as Paras, ParachainsCache}, + paras::{Pallet as Paras, ParaKind, ParachainsCache}, shared::Pallet as Shared, }; use frame_support::{assert_ok, traits::Currency}; @@ -31,7 +31,7 @@ fn register_parachain_with_balance(id: ParaId, balance: BalanceOf) &mut parachains, id, &crate::paras::ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: vec![1].into(), validation_code: vec![1].into(), }, diff --git a/runtime/parachains/src/hrmp/tests.rs b/runtime/parachains/src/hrmp/tests.rs index 9e0d0646d057..85cdc459ffb9 100644 --- a/runtime/parachains/src/hrmp/tests.rs +++ b/runtime/parachains/src/hrmp/tests.rs @@ -15,9 +15,12 @@ // along with Polkadot. If not, see . use super::*; -use crate::mock::{ - new_test_ext, Configuration, Hrmp, MockGenesisConfig, Paras, ParasShared, - RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, +use crate::{ + mock::{ + new_test_ext, Configuration, Hrmp, MockGenesisConfig, Paras, ParasShared, + RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, + }, + paras::ParaKind, }; use frame_support::{assert_noop, assert_ok, traits::Currency as _}; use primitives::v2::BlockNumber; @@ -130,7 +133,7 @@ fn register_parachain_with_balance(id: ParaId, balance: Balance) { assert_ok!(Paras::schedule_para_initialize( id, crate::paras::ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: vec![1].into(), validation_code: vec![1].into(), }, diff --git a/runtime/parachains/src/inclusion/tests.rs b/runtime/parachains/src/inclusion/tests.rs index de3199c366a7..00369fb67e75 100644 --- a/runtime/parachains/src/inclusion/tests.rs +++ b/runtime/parachains/src/inclusion/tests.rs @@ -22,7 +22,7 @@ use crate::{ new_test_ext, Configuration, MockGenesisConfig, ParaInclusion, Paras, ParasShared, System, Test, }, - paras::ParaGenesisArgs, + paras::{ParaGenesisArgs, ParaKind}, paras_inherent::DisputedBitfield, scheduler::AssignmentKind, }; @@ -51,18 +51,18 @@ fn default_config() -> HostConfiguration { config } -pub(crate) fn genesis_config(paras: Vec<(ParaId, bool)>) -> MockGenesisConfig { +pub(crate) fn genesis_config(paras: Vec<(ParaId, ParaKind)>) -> MockGenesisConfig { MockGenesisConfig { paras: paras::GenesisConfig { paras: paras .into_iter() - .map(|(id, is_chain)| { + .map(|(id, para_kind)| { ( id, ParaGenesisArgs { genesis_head: Vec::new().into(), validation_code: dummy_validation_code(), - parachain: is_chain, + para_kind, }, ) }) @@ -310,7 +310,11 @@ fn collect_pending_cleans_up_pending() { let chain_b = ParaId::from(2_u32); let thread_a = ParaId::from(3_u32); - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; new_test_ext(genesis_config(paras)).execute_with(|| { let default_candidate = TestCandidateBuilder::default().build(); >::insert( @@ -368,7 +372,11 @@ fn bitfield_checks() { let chain_b = ParaId::from(2_u32); let thread_a = ParaId::from(3_u32); - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, @@ -711,7 +719,11 @@ fn supermajority_bitfields_trigger_availability() { let chain_b = ParaId::from(2_u32); let thread_a = ParaId::from(3_u32); - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, @@ -899,7 +911,11 @@ fn candidate_checks() { // The block number of the relay-parent for testing. const RELAY_PARENT_NUM: BlockNumber = 4; - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, @@ -1442,7 +1458,11 @@ fn backing_works() { // The block number of the relay-parent for testing. const RELAY_PARENT_NUM: BlockNumber = 4; - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, @@ -1722,7 +1742,7 @@ fn can_include_candidate_with_ok_code_upgrade() { // The block number of the relay-parent for testing. const RELAY_PARENT_NUM: BlockNumber = 4; - let paras = vec![(chain_a, true)]; + let paras = vec![(chain_a, ParaKind::Parachain)]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, @@ -1827,7 +1847,11 @@ fn session_change_wipes() { let chain_b = ParaId::from(2_u32); let thread_a = ParaId::from(3_u32); - let paras = vec![(chain_a, true), (chain_b, true), (thread_a, false)]; + let paras = vec![ + (chain_a, ParaKind::Parachain), + (chain_b, ParaKind::Parachain), + (thread_a, ParaKind::Parathread), + ]; let validators = vec![ Sr25519Keyring::Alice, Sr25519Keyring::Bob, diff --git a/runtime/parachains/src/initializer/tests.rs b/runtime/parachains/src/initializer/tests.rs index 7fdf8b6b4a57..d1d884200527 100644 --- a/runtime/parachains/src/initializer/tests.rs +++ b/runtime/parachains/src/initializer/tests.rs @@ -21,6 +21,7 @@ use crate::mock::{ use primitives::v2::{HeadData, Id as ParaId}; use test_helpers::dummy_validation_code; +use crate::paras::ParaKind; use frame_support::{ assert_ok, traits::{OnFinalize, OnInitialize}, @@ -90,7 +91,7 @@ fn scheduled_cleanup_performed() { let c = ParaId::from(123); let mock_genesis = crate::paras::ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: HeadData(vec![4, 5, 6]), validation_code: dummy_validation_code(), }; diff --git a/runtime/parachains/src/paras/benchmarking/pvf_check.rs b/runtime/parachains/src/paras/benchmarking/pvf_check.rs index a89315c2857b..e1400ac4297d 100644 --- a/runtime/parachains/src/paras/benchmarking/pvf_check.rs +++ b/runtime/parachains/src/paras/benchmarking/pvf_check.rs @@ -140,7 +140,7 @@ where &mut parachains, id, &ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: HeadData(vec![1, 2, 3, 4]), validation_code: old_validation_code, }, @@ -159,7 +159,7 @@ where let r = Pallet::::schedule_para_initialize( id, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: HeadData(vec![1, 2, 3, 4]), validation_code: validation_code(), }, diff --git a/runtime/parachains/src/paras/mod.rs b/runtime/parachains/src/paras/mod.rs index 273db30e2839..3c5744b96546 100644 --- a/runtime/parachains/src/paras/mod.rs +++ b/runtime/parachains/src/paras/mod.rs @@ -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(&self, serializer: S) -> Result + 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(deserializer: D) -> Result + 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>(&self, f: F) -> R { + match self { + ParaKind::Parachain => true.using_encoded(f), + ParaKind::Parathread => false.using_encoded(f), + } + } +} + +impl Decode for ParaKind { + fn decode( + input: &mut I, + ) -> Result { + 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 Pallet { id: ParaId, genesis_data: &ParaGenesisArgs, ) { - if genesis_data.parachain { - parachains.add(id); - ParaLifecycles::::insert(&id, ParaLifecycle::Parachain); - } else { - ParaLifecycles::::insert(&id, ParaLifecycle::Parathread); + match genesis_data.para_kind { + ParaKind::Parachain => { + parachains.add(id); + ParaLifecycles::::insert(&id, ParaLifecycle::Parachain); + }, + ParaKind::Parathread => ParaLifecycles::::insert(&id, ParaLifecycle::Parathread), } // HACK: see the notice in `schedule_para_initialize`. diff --git a/runtime/parachains/src/paras/tests.rs b/runtime/parachains/src/paras/tests.rs index 766fa00d01b3..f110e2a0d38a 100644 --- a/runtime/parachains/src/paras/tests.rs +++ b/runtime/parachains/src/paras/tests.rs @@ -222,7 +222,7 @@ fn schedule_para_init_rejects_empty_code() { Paras::schedule_para_initialize( 1000.into(), ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: dummy_head_data(), validation_code: ValidationCode(vec![]), } @@ -233,7 +233,7 @@ fn schedule_para_init_rejects_empty_code() { assert_ok!(Paras::schedule_para_initialize( 1000.into(), ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: dummy_head_data(), validation_code: ValidationCode(vec![1]), } @@ -248,7 +248,7 @@ fn para_past_code_pruning_in_initialize() { ( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: dummy_validation_code(), }, @@ -256,7 +256,7 @@ fn para_past_code_pruning_in_initialize() { ( 1u32.into(), ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: dummy_head_data(), validation_code: dummy_validation_code(), }, @@ -316,7 +316,7 @@ fn note_new_head_sets_head() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: dummy_validation_code(), }, @@ -349,7 +349,7 @@ fn note_past_code_sets_up_pruning_correctly() { ( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: dummy_validation_code(), }, @@ -357,7 +357,7 @@ fn note_past_code_sets_up_pruning_correctly() { ( 1u32.into(), ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: dummy_head_data(), validation_code: dummy_validation_code(), }, @@ -402,7 +402,7 @@ fn code_upgrade_applied_after_delay() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: original_code.clone(), }, @@ -505,7 +505,7 @@ fn code_upgrade_applied_after_delay_even_when_late() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: original_code.clone(), }, @@ -590,7 +590,7 @@ fn submit_code_change_when_not_allowed_is_err() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: vec![1, 2, 3].into(), }, @@ -658,7 +658,7 @@ fn upgrade_restriction_elapsed_doesnt_mean_can_upgrade() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: vec![1, 2, 3].into(), }, @@ -721,7 +721,7 @@ fn full_parachain_cleanup_storage() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: original_code.clone(), }, @@ -827,7 +827,7 @@ fn cannot_offboard_ongoing_pvf_check() { let paras = vec![( para_id, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: Default::default(), validation_code: existing_code, }, @@ -900,7 +900,7 @@ fn para_incoming_at_session() { assert_ok!(Paras::schedule_para_initialize( b, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: vec![1].into(), validation_code: code_b.clone(), }, @@ -909,7 +909,7 @@ fn para_incoming_at_session() { assert_ok!(Paras::schedule_para_initialize( a, ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: vec![2].into(), validation_code: code_a.clone(), }, @@ -918,7 +918,7 @@ fn para_incoming_at_session() { assert_ok!(Paras::schedule_para_initialize( c, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: vec![3].into(), validation_code: code_c.clone(), }, @@ -994,7 +994,7 @@ fn code_hash_at_returns_up_to_end_of_code_retention_period() { let paras = vec![( 0u32.into(), ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: dummy_head_data(), validation_code: vec![1, 2, 3].into(), }, @@ -1084,7 +1084,7 @@ fn pvf_check_coalescing_onboarding_and_upgrade() { let paras = vec![( a, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: Default::default(), validation_code: existing_code, }, @@ -1117,7 +1117,7 @@ fn pvf_check_coalescing_onboarding_and_upgrade() { assert_ok!(Paras::schedule_para_initialize( b, ParaGenesisArgs { - parachain: true, + para_kind: ParaKind::Parachain, genesis_head: vec![2].into(), validation_code: validation_code.clone(), }, @@ -1185,7 +1185,7 @@ fn pvf_check_onboarding_reject_on_expiry() { assert_ok!(Paras::schedule_para_initialize( a, ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: vec![2].into(), validation_code: validation_code.clone(), }, @@ -1224,7 +1224,7 @@ fn pvf_check_upgrade_reject() { let paras = vec![( a, ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: Default::default(), validation_code: old_code, }, @@ -1359,7 +1359,7 @@ fn pvf_check_submit_vote() { assert_ok!(Paras::schedule_para_initialize( 1000.into(), ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: vec![2].into(), validation_code: code_a.clone(), }, @@ -1446,7 +1446,7 @@ fn include_pvf_check_statement_refunds_weight() { let paras = vec![( a, ParaGenesisArgs { - parachain: false, + para_kind: ParaKind::Parathread, genesis_head: Default::default(), validation_code: old_code, }, @@ -1723,3 +1723,41 @@ fn verify_upgrade_restriction_signal_is_externally_accessible() { ); }); } + +#[test] +fn parakind_encodes_decodes_to_bool_scale() { + 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)); + + assert_eq!(bool::type_info(), ParaKind::type_info()); +} + +#[test] +fn parakind_encodes_decodes_to_bool_serde() { + let chain = ParaKind::Parachain; + let ser_chain = serde_json::to_string(&ParaKind::Parachain).unwrap(); + let de_chain: ParaKind = serde_json::from_str(&ser_chain).unwrap(); + assert_eq!(chain, de_chain); + + let ser_true = serde_json::to_string(&true).unwrap(); + assert_eq!(ser_true, ser_chain); + + let thread = ParaKind::Parathread; + let ser_thread = serde_json::to_string(&thread).unwrap(); + let de_thread: ParaKind = serde_json::from_str(&ser_thread).unwrap(); + assert_eq!(thread, de_thread); + + let ser_false = serde_json::to_string(&false).unwrap(); + assert_eq!(ser_false, ser_thread); +} diff --git a/runtime/parachains/src/scheduler/tests.rs b/runtime/parachains/src/scheduler/tests.rs index b2b5e4236dd5..40ff9db82871 100644 --- a/runtime/parachains/src/scheduler/tests.rs +++ b/runtime/parachains/src/scheduler/tests.rs @@ -26,16 +26,16 @@ use crate::{ mock::{ new_test_ext, Configuration, MockGenesisConfig, Paras, ParasShared, Scheduler, System, Test, }, - paras::ParaGenesisArgs, + paras::{ParaGenesisArgs, ParaKind}, }; -fn schedule_blank_para(id: ParaId, is_chain: bool) { +fn schedule_blank_para(id: ParaId, parakind: ParaKind) { assert_ok!(Paras::schedule_para_initialize( id, ParaGenesisArgs { genesis_head: Vec::new().into(), validation_code: vec![1, 2, 3].into(), - parachain: is_chain, + para_kind: parakind, } )); } @@ -122,7 +122,7 @@ fn add_parathread_claim_works() { let collator = CollatorId::from(Sr25519Keyring::Alice.public()); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_id, false); + schedule_blank_para(thread_id, ParaKind::Parathread); assert!(!Paras::is_parathread(thread_id)); @@ -203,7 +203,7 @@ fn cannot_add_claim_when_no_parathread_cores() { let collator = CollatorId::from(Sr25519Keyring::Alice.public()); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_id, false); + schedule_blank_para(thread_id, ParaKind::Parathread); assert!(!Paras::is_parathread(thread_id)); @@ -239,9 +239,9 @@ fn session_change_prunes_cores_beyond_retries_and_those_from_non_live_parathread // threads a, b, and c will be live in next session, but not d. { - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); - schedule_blank_para(thread_c, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); + schedule_blank_para(thread_c, ParaKind::Parathread); } // set up a queue as if `n_cores` was 4 and with some with many retries. @@ -334,8 +334,8 @@ fn session_change_shuffles_validators() { let chain_b = ParaId::from(2_u32); // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { @@ -392,9 +392,9 @@ fn session_change_takes_only_max_per_core() { let chain_c = ParaId::from(3_u32); // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); - schedule_blank_para(chain_c, false); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); + schedule_blank_para(chain_c, ParaKind::Parathread); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { @@ -447,13 +447,13 @@ fn schedule_schedules() { assert_eq!(default_config().parathread_cores, 3); // register 2 parachains - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); // and 3 parathreads - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); - schedule_blank_para(thread_c, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); + schedule_blank_para(thread_c, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -574,15 +574,15 @@ fn schedule_schedules_including_just_freed() { assert_eq!(default_config().parathread_cores, 3); // register 2 parachains - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); // and 5 parathreads - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); - schedule_blank_para(thread_c, false); - schedule_blank_para(thread_d, false); - schedule_blank_para(thread_e, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); + schedule_blank_para(thread_c, ParaKind::Parathread); + schedule_blank_para(thread_d, ParaKind::Parathread); + schedule_blank_para(thread_e, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -743,9 +743,9 @@ fn schedule_clears_availability_cores() { assert_eq!(default_config().parathread_cores, 3); // register 3 parachains - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); - schedule_blank_para(chain_c, true); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); + schedule_blank_para(chain_c, ParaKind::Parachain); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -849,8 +849,8 @@ fn schedule_rotates_groups() { new_test_ext(genesis_config).execute_with(|| { assert_eq!(default_config().parathread_cores, 3); - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -921,8 +921,8 @@ fn parathread_claims_are_pruned_after_retries() { new_test_ext(genesis_config).execute_with(|| { assert_eq!(default_config().parathread_cores, 3); - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -978,8 +978,8 @@ fn availability_predicate_works() { let thread_a = ParaId::from(2_u32); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, true); - schedule_blank_para(thread_a, false); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(thread_a, ParaKind::Parathread); // start a new session with our chain & thread registered. run_to_block(1, |number| match number { @@ -1075,8 +1075,8 @@ fn next_up_on_available_uses_next_scheduled_or_none_for_thread() { let collator = CollatorId::from(Sr25519Keyring::Alice.public()); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -1147,8 +1147,8 @@ fn next_up_on_time_out_reuses_claim_if_nothing_queued() { let collator = CollatorId::from(Sr25519Keyring::Alice.public()); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -1222,7 +1222,7 @@ fn next_up_on_available_is_parachain_always() { let chain_a = ParaId::from(1_u32); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, true); + schedule_blank_para(chain_a, ParaKind::Parachain); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -1276,7 +1276,7 @@ fn next_up_on_time_out_is_parachain_always() { let chain_a = ParaId::from(1_u32); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, true); + schedule_blank_para(chain_a, ParaKind::Parachain); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number { @@ -1330,8 +1330,8 @@ fn session_change_requires_reschedule_dropping_removed_paras() { let chain_b = ParaId::from(2_u32); // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, true); - schedule_blank_para(chain_b, true); + schedule_blank_para(chain_a, ParaKind::Parachain); + schedule_blank_para(chain_b, ParaKind::Parachain); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { @@ -1409,8 +1409,8 @@ fn parathread_claims_are_pruned_after_deregistration() { new_test_ext(genesis_config).execute_with(|| { assert_eq!(default_config().parathread_cores, 3); - schedule_blank_para(thread_a, false); - schedule_blank_para(thread_b, false); + schedule_blank_para(thread_a, ParaKind::Parathread); + schedule_blank_para(thread_b, ParaKind::Parathread); // start a new session to activate, 5 validators for 5 cores. run_to_block(1, |number| match number {