From 4fb52c82adfdaf3af98edfe36b280133bcd4f9d3 Mon Sep 17 00:00:00 2001 From: 4meta5 Date: Fri, 2 Aug 2019 13:55:02 +0200 Subject: [PATCH] fix some small errors --- runtime/src/parachains.rs | 44 +++++++++++++++++++++++++++------------ runtime/src/registrar.rs | 2 +- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/runtime/src/parachains.rs b/runtime/src/parachains.rs index 678d6d48650e..185b357b1c58 100644 --- a/runtime/src/parachains.rs +++ b/runtime/src/parachains.rs @@ -22,13 +22,13 @@ use parity_codec::Decode; use srml_support::{decl_storage, decl_module, ensure}; use bitvec::{bitvec, BigEndian}; -use sr_primitives::traits::{Hash as HashT, BlakeTwo256, Saturating, One}; +use sr_primitives::traits::{Hash as HashT, EnsureOrigin, BlakeTwo256, Saturating, One}; use sr_primitives::weights::SimpleDispatchInfo; use primitives::{Hash, Balance, parachain::{ self, Id as ParaId, Chain, DutyRoster, AttestedCandidate, Statement, AccountIdConversion, ParachainDispatchOrigin, UpwardMessage, BlockIngressRoots, ActiveParas, CollatorId }}; -use {system, session}; +use session; use srml_support::{ StorageValue, StorageMap, storage::AppendableStorageMap, Parameter, Dispatchable, dispatch::Result, traits::{Currency, WithdrawReason, ExistenceRequirement} @@ -36,7 +36,7 @@ use srml_support::{ use inherents::{ProvideInherent, InherentData, RuntimeString, MakeFatalError, InherentIdentifier}; -use system::ensure_none; +use system::{ensure_none, self}; use crate::registrar::Registrar; // ranges for iteration of general block number don't work, so this @@ -98,7 +98,7 @@ impl> ParachainCurrency for T where pub trait Trait: session::Trait { /// The outer origin type. - type Origin: From + From>; + type Origin: From + Into>; /// The outer call dispatch type. type Call: Parameter + Dispatchable::Origin>; @@ -116,11 +116,14 @@ pub trait Trait: session::Trait { /// Origin for the parachains module. #[derive(PartialEq, Eq, Clone)] #[cfg_attr(feature = "std", derive(Debug))] -pub enum Origin { +pub enum RawOrigin { /// It comes from a parachain. Parachain(ParaId), } +/// Origin for the parachain module. +pub type Origin = RawOrigin; + // result of as trie_db::NodeCodec>::hashed_null_node() const EMPTY_TRIE_ROOT: [u8; 32] = [ 3, 23, 10, 46, 117, 151, 183, 183, 227, 216, 76, 5, 57, 29, 19, 154, @@ -169,7 +172,7 @@ decl_storage! { decl_module! { /// Parachains module. - pub struct Module for enum Call where origin: ::Origin { + pub struct Module for enum Call where origin: ::Origin { /// Provide candidate receipts for parachains, in ascending order by id. #[weight = SimpleDispatchInfo::FixedNormal(1_000_000)] fn set_heads(origin, heads: Vec) -> Result { @@ -202,7 +205,7 @@ decl_module! { .ok_or("candidate for unregistered parachain {}")?; if let Some(required_collator) = maybe_required_collator { - ensure!(required_collator == head.candidate.collator, "invalid collator"); + ensure!(required_collator == &head.candidate.collator, "invalid collator"); } Self::check_upward_messages( @@ -211,7 +214,8 @@ decl_module! { MAX_QUEUE_COUNT, WATERMARK_QUEUE_SIZE, )?; - Self::check_egress_queue_roots(&head, &active_parachains)?; + let id_slice = active_parachains.into_iter().map(|(a, b)| a).collect(); + Self::check_egress_queue_roots(&head, &id_slice[..])?; last_id = Some(head.parachain_index()); } @@ -228,7 +232,6 @@ decl_module! { Self::dispatch_upward_messages( current_number, - &active_parachains, MAX_QUEUE_COUNT, WATERMARK_QUEUE_SIZE, Self::dispatch_message, @@ -306,7 +309,7 @@ impl Module { ParachainDispatchOrigin::Signed => system::RawOrigin::Signed(id.into_account()).into(), ParachainDispatchOrigin::Parachain => - Origin::Parachain(id).into(), + RawOrigin::Parachain(id).into(), ParachainDispatchOrigin::Root => system::RawOrigin::Root.into(), }; @@ -565,7 +568,7 @@ impl Module { } /// Get the currently active set of parachains. - fn active_parachains() -> Vec<(ParaId, Option)> { + pub fn active_parachains() -> Vec<(ParaId, Option)> { T::ActiveParas::active_paras() } @@ -781,13 +784,28 @@ impl ProvideInherent for Module { /// Ensure that the origin `o` represents a parachain. /// Returns `Ok` with the parachain ID that effected the extrinsic or an `Err` otherwise. pub fn ensure_parachain(o: OuterOrigin) -> result::Result - where OuterOrigin: Into> + where OuterOrigin: Into> { match o.into() { - Ok(Origin::Parachain(id)) => Ok(id), + Ok(RawOrigin::Parachain(id)) => Ok(id), _ => Err("bad origin: expected to be a parachain origin"), } } +pub struct EnsureParachain(ParaId); +impl< + O: Into> + From, + ParaId, +> EnsureOrigin for EnsureParachain { + type Success = ParaId; + fn try_origin(o: O) -> result::Result { + o.into().and_then(|o| match o { + RawOrigin::Parachain(id) => Ok(id), + r => Err(O::from(r)), + }) + } +} + + #[cfg(test)] mod tests { diff --git a/runtime/src/registrar.rs b/runtime/src/registrar.rs index f5efae7a7880..b43f1f09a890 100644 --- a/runtime/src/registrar.rs +++ b/runtime/src/registrar.rs @@ -200,7 +200,7 @@ decl_module! { /// /// Must be sent from a Signed origin that is able to have ParathreadDeposit reserved. /// `code` and `initial_head_data` are used to initialize the parathread's state. - fn register_parathead(origin, + fn register_parathread(origin, code: Vec, initial_head_data: Vec ) {