Make ParaId constructible from a const context (#483)

This commit is contained in:
Bastian Köcher
2019-10-19 13:09:21 +02:00
committed by GitHub
parent b890df6cde
commit 2842be7b81
5 changed files with 23 additions and 30 deletions
+13 -20
View File
@@ -53,7 +53,7 @@ pub mod wasm_api;
use rstd::vec::Vec; use rstd::vec::Vec;
use codec::{Encode, Decode}; use codec::{Encode, Decode, CompactAs};
use substrate_primitives::TypeId; use substrate_primitives::TypeId;
/// Validation parameters for evaluating the parachain validity function. /// Validation parameters for evaluating the parachain validity function.
@@ -79,7 +79,7 @@ pub struct ValidationResult {
} }
/// Unique identifier of a parachain. /// 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))] #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, Debug))]
pub struct Id(u32); pub struct Id(u32);
@@ -93,21 +93,6 @@ pub trait ActiveThreads {
fn active_threads() -> Vec<Id>; fn active_threads() -> Vec<Id>;
} }
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<codec::Compact<Id>> for Id {
fn from(x: codec::Compact<Id>) -> Id {
x.0
}
}
impl From<Id> for u32 { impl From<Id> for u32 {
fn from(x: Id) -> Self { x.0 } 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); pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START);
impl Id { impl Id {
/// Convert this Id into its inner representation. /// Create an `Id`.
pub fn into_inner(self) -> u32 { pub const fn new(id: u32) -> Self {
self.0 Self(id)
} }
/// Returns `true` if this parachain runs with system-level privileges. /// Returns `true` if this parachain runs with system-level privileges.
pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START } pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START }
} }
impl rstd::ops::Add<u32> 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. // TODO: Remove all of this, move sr-primitives::AccountIdConversion to own crate and and use that.
// #360 // #360
struct TrailingZeroInput<'a>(&'a [u8]); struct TrailingZeroInput<'a>(&'a [u8]);
+1 -1
View File
@@ -60,7 +60,7 @@ pub fn post_message(message: MessageRef) {
let data_ptr = message.data.as_ptr(); let data_ptr = message.data.as_ptr();
let data_len = message.data.len(); 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. /// Post a message to this parachain's relay chain.
+4 -4
View File
@@ -610,19 +610,19 @@ mod tests {
initial_head_data: Vec<u8> initial_head_data: Vec<u8>
) -> Result<(), &'static str> { ) -> Result<(), &'static str> {
PARACHAINS.with(|p| { PARACHAINS.with(|p| {
if p.borrow().contains_key(&id.into_inner()) { if p.borrow().contains_key(&id.into()) {
panic!("ID already exists") 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(()) Ok(())
}) })
} }
fn deregister_para(id: ParaId) -> Result<(), &'static str> { fn deregister_para(id: ParaId) -> Result<(), &'static str> {
PARACHAINS.with(|p| { PARACHAINS.with(|p| {
if !p.borrow().contains_key(&id.into_inner()) { if !p.borrow().contains_key(&id.into()) {
panic!("ID doesn't exist") panic!("ID doesn't exist")
} }
p.borrow_mut().remove(&id.into_inner()); p.borrow_mut().remove(&id.into());
Ok(()) Ok(())
}) })
} }
+1 -1
View File
@@ -829,7 +829,7 @@ mod tests {
} }
fn user_id(i: u32) -> ParaId { 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 { fn attest(id: ParaId, collator: &CollatorPair, head_data: &[u8], block_data: &[u8]) -> AttestedCandidate {
+4 -4
View File
@@ -905,19 +905,19 @@ mod tests {
initial_head_data: Vec<u8> initial_head_data: Vec<u8>
) -> Result<(), &'static str> { ) -> Result<(), &'static str> {
PARACHAINS.with(|p| { PARACHAINS.with(|p| {
if p.borrow().contains_key(&id.into_inner()) { if p.borrow().contains_key(&id.into()) {
panic!("ID already exists") 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(()) Ok(())
}) })
} }
fn deregister_para(id: ParaId) -> Result<(), &'static str> { fn deregister_para(id: ParaId) -> Result<(), &'static str> {
PARACHAINS.with(|p| { PARACHAINS.with(|p| {
if !p.borrow().contains_key(&id.into_inner()) { if !p.borrow().contains_key(&id.into()) {
panic!("ID doesn't exist") panic!("ID doesn't exist")
} }
p.borrow_mut().remove(&id.into_inner()); p.borrow_mut().remove(&id.into());
Ok(()) Ok(())
}) })
} }