Unverified Commit 246571e2 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Make `ParaId` constructible from a const context (#483)

parent 4a983a8b
Pipeline #54369 failed with stages
in 12 minutes and 1 second
......@@ -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<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 {
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<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.
// #360
struct TrailingZeroInput<'a>(&'a [u8]);
......
......@@ -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.
......
......@@ -610,19 +610,19 @@ mod tests {
initial_head_data: Vec<u8>
) -> 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(())
})
}
......
......@@ -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 {
......
......@@ -905,19 +905,19 @@ mod tests {
initial_head_data: Vec<u8>
) -> 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(())
})
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment