From 2842be7b8193f95c8d29c0f8dabb8918b156f39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 19 Oct 2019 13:09:21 +0200 Subject: [PATCH] Make `ParaId` constructible from a const context (#483) --- polkadot/parachain/src/lib.rs | 33 ++++++++++++------------------ polkadot/parachain/src/wasm_api.rs | 2 +- polkadot/runtime/src/crowdfund.rs | 8 ++++---- polkadot/runtime/src/registrar.rs | 2 +- polkadot/runtime/src/slots.rs | 8 ++++---- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/polkadot/parachain/src/lib.rs b/polkadot/parachain/src/lib.rs index 07c5b4166c..b337b5f248 100644 --- a/polkadot/parachain/src/lib.rs +++ b/polkadot/parachain/src/lib.rs @@ -53,7 +53,7 @@ pub mod wasm_api; use rstd::vec::Vec; -use codec::{Encode, Decode}; +use codec::{Encode, Decode, CompactAs}; use substrate_primitives::TypeId; /// Validation parameters for evaluating the parachain validity function. @@ -79,7 +79,7 @@ pub struct ValidationResult { } /// Unique identifier of a parachain. -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Default, Clone, Copy, Encode, Decode)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Default, Clone, Copy, Encode, Decode, CompactAs)] #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, Debug))] pub struct Id(u32); @@ -93,21 +93,6 @@ pub trait ActiveThreads { fn active_threads() -> Vec; } -impl codec::CompactAs for Id { - type As = u32; - fn encode_as(&self) -> &u32 { - &self.0 - } - fn decode_from(x: u32) -> Self { - Self(x) - } -} -impl From> for Id { - fn from(x: codec::Compact) -> Id { - x.0 - } -} - impl From for u32 { fn from(x: Id) -> Self { x.0 } } @@ -122,15 +107,23 @@ const USER_INDEX_START: u32 = 1000; pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START); impl Id { - /// Convert this Id into its inner representation. - pub fn into_inner(self) -> u32 { - self.0 + /// Create an `Id`. + pub const fn new(id: u32) -> Self { + Self(id) } /// Returns `true` if this parachain runs with system-level privileges. pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START } } +impl rstd::ops::Add for Id { + type Output = Self; + + fn add(self, other: u32) -> Self { + Self(self.0 + other) + } +} + // TODO: Remove all of this, move sr-primitives::AccountIdConversion to own crate and and use that. // #360 struct TrailingZeroInput<'a>(&'a [u8]); diff --git a/polkadot/parachain/src/wasm_api.rs b/polkadot/parachain/src/wasm_api.rs index dec6b60b3d..90f804b451 100644 --- a/polkadot/parachain/src/wasm_api.rs +++ b/polkadot/parachain/src/wasm_api.rs @@ -60,7 +60,7 @@ pub fn post_message(message: MessageRef) { let data_ptr = message.data.as_ptr(); let data_len = message.data.len(); - unsafe { ll::ext_post_message(message.target.into_inner(), data_ptr, data_len as u32) } + unsafe { ll::ext_post_message(message.target.into(), data_ptr, data_len as u32) } } /// Post a message to this parachain's relay chain. diff --git a/polkadot/runtime/src/crowdfund.rs b/polkadot/runtime/src/crowdfund.rs index 505f0ef013..ad7b9184c7 100644 --- a/polkadot/runtime/src/crowdfund.rs +++ b/polkadot/runtime/src/crowdfund.rs @@ -610,19 +610,19 @@ mod tests { initial_head_data: Vec ) -> Result<(), &'static str> { PARACHAINS.with(|p| { - if p.borrow().contains_key(&id.into_inner()) { + if p.borrow().contains_key(&id.into()) { panic!("ID already exists") } - p.borrow_mut().insert(id.into_inner(), (code, initial_head_data)); + p.borrow_mut().insert(id.into(), (code, initial_head_data)); Ok(()) }) } fn deregister_para(id: ParaId) -> Result<(), &'static str> { PARACHAINS.with(|p| { - if !p.borrow().contains_key(&id.into_inner()) { + if !p.borrow().contains_key(&id.into()) { panic!("ID doesn't exist") } - p.borrow_mut().remove(&id.into_inner()); + p.borrow_mut().remove(&id.into()); Ok(()) }) } diff --git a/polkadot/runtime/src/registrar.rs b/polkadot/runtime/src/registrar.rs index dace68bf02..e2a1642a37 100644 --- a/polkadot/runtime/src/registrar.rs +++ b/polkadot/runtime/src/registrar.rs @@ -829,7 +829,7 @@ mod tests { } fn user_id(i: u32) -> ParaId { - (LOWEST_USER_ID.into_inner() + i).into() + LOWEST_USER_ID + i } fn attest(id: ParaId, collator: &CollatorPair, head_data: &[u8], block_data: &[u8]) -> AttestedCandidate { diff --git a/polkadot/runtime/src/slots.rs b/polkadot/runtime/src/slots.rs index 5dd4ca6e20..5edd0546ee 100644 --- a/polkadot/runtime/src/slots.rs +++ b/polkadot/runtime/src/slots.rs @@ -905,19 +905,19 @@ mod tests { initial_head_data: Vec ) -> Result<(), &'static str> { PARACHAINS.with(|p| { - if p.borrow().contains_key(&id.into_inner()) { + if p.borrow().contains_key(&id.into()) { panic!("ID already exists") } - p.borrow_mut().insert(id.into_inner(), (code, initial_head_data)); + p.borrow_mut().insert(id.into(), (code, initial_head_data)); Ok(()) }) } fn deregister_para(id: ParaId) -> Result<(), &'static str> { PARACHAINS.with(|p| { - if !p.borrow().contains_key(&id.into_inner()) { + if !p.borrow().contains_key(&id.into()) { panic!("ID doesn't exist") } - p.borrow_mut().remove(&id.into_inner()); + p.borrow_mut().remove(&id.into()); Ok(()) }) }