Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Factor out common components of demo-runtime and merge into polkadot #101

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ members = [
"substrate/runtime-io",
"substrate/runtime-std",
"substrate/runtime-support",
"substrate/runtime/consensus",
"substrate/serializer",
"substrate/state-machine",
"substrate/test-runtime",
"demo/runtime",
"demo/primitives",
"demo/executor",
"demo/cli",
"safe-mix",
]
exclude = [
"polkadot/runtime/wasm",
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

cd demo/runtime/wasm && ./build.sh && cd ../../..
cd substrate/executor/wasm && ./build.sh && cd ../../..
cd substrate/test-runtime/wasm && ./build.sh && cd ../../..
cd polkadot/runtime/wasm && ./build.sh && cd ../../..
cd demo/runtime/wasm && ./build.sh && cd ../../..
8 changes: 7 additions & 1 deletion demo/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ hex-literal = "0.1.0"
log = { version = "0.3", optional = true }
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
safe-mix = { path = "../../safe-mix", default_features = false}
substrate-codec = { path = "../../substrate/codec" }
substrate-runtime-std = { path = "../../substrate/runtime-std" }
substrate-runtime-io = { path = "../../substrate/runtime-io" }
substrate-runtime-support = { path = "../../substrate/runtime-support" }
substrate-primitives = { path = "../../substrate/primitives" }
substrate-keyring = { path = "../../substrate/keyring" }
substrate-runtime-consensus = { path = "../../substrate/runtime/consensus" }
substrate-runtime-timestamp = { path = "../../substrate/runtime/timestamp" }
demo-primitives = { path = "../primitives" }
integer-sqrt = "0.1.0"

Expand All @@ -26,8 +29,11 @@ std = [
"substrate-runtime-io/std",
"substrate-runtime-support/std",
"substrate-primitives/std",
"substrate-runtime-consensus/std",
"substrate-runtime-timestamp/std",
"demo-primitives/std",
"serde_derive",
"serde/std",
"log"
"log",
"safe-mix/std"
]
3 changes: 2 additions & 1 deletion demo/runtime/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>.

use runtime::{system, consensus, session};
use runtime::{system, session};
use consensus;

impl_stubs!(
execute_block => |block| system::internal::execute_block(block),
Expand Down
228 changes: 14 additions & 214 deletions demo/runtime/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,230 +16,30 @@

//! Dispatch system. Just dispatches calls.

use runtime::{staking, democracy};
pub use rstd::prelude::Vec;
pub use codec::{Slicable, Input, NonTrivialSlicable};

/// Implement a dispatch module to create a pairing of a dispatch trait and enum.
#[macro_export]
macro_rules! impl_dispatch {
(
pub mod $mod_name:ident;
$(
fn $fn_name:ident(
$(
$param_name:ident : $param:ty
),*
)
= $id:expr ;
)*
) => {
pub mod $mod_name {
use super::*;

#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[repr(u32)]
#[allow(non_camel_case_types)]
enum Id {
$(
#[allow(non_camel_case_types)]
$fn_name = $id,
)*
}

impl Id {
/// Derive `Some` value from a `u8`, or `None` if it's invalid.
fn from_u8(value: u8) -> Option<Id> {
match value {
$(
$id => Some(Id::$fn_name),
)*
_ => None,
}
}
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[allow(missing_docs)]
pub enum Call {
$(
#[allow(non_camel_case_types)]
$fn_name ( $( $param ),* )
,)*
}

pub trait Dispatch: Sized {
$(
fn $fn_name (self, $( $param_name: $param ),* );
)*
}

impl Call {
pub fn dispatch<D: Dispatch>(self, d: D) {
match self {
$(
Call::$fn_name( $( $param_name ),* ) =>
d.$fn_name( $( $param_name ),* ),
)*
}
}
}

impl $crate::dispatch::Slicable for Call {
fn decode<I: $crate::dispatch::Input>(input: &mut I) -> Option<Self> {
let id = u8::decode(input).and_then(Id::from_u8)?;
Some(match id {
$(
Id::$fn_name => {
$(
let $param_name = $crate::dispatch::Slicable::decode(input)?;
)*
Call :: $fn_name( $( $param_name ),* )
}
)*
})
}

fn encode(&self) -> $crate::dispatch::Vec<u8> {
let mut v = $crate::dispatch::Vec::new();
match *self {
$(
Call::$fn_name(
$(
ref $param_name
),*
) => {
(Id::$fn_name as u8).using_encoded(|s| v.extend(s));
$(
$param_name.using_encoded(|s| v.extend(s));
)*
}
)*
}
v
}

fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(self.encode().as_slice())
}
}
impl $crate::dispatch::NonTrivialSlicable for Call {}
}
}
}

macro_rules! impl_meta_dispatch {
(
pub mod $super_name:ident;
path $path:ident;
trait $trait:ty;
$(
$camelcase:ident(mod $sub_name:ident) = $id:expr ;
)*
) => {
pub mod $super_name {
use super::*;

#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[repr(u32)]
#[allow(non_camel_case_types)]
enum Id {
$(
#[allow(non_camel_case_types)]
$camelcase = $id,
)*
}

impl Id {
/// Derive `Some` value from a `u8`, or `None` if it's invalid.
fn from_u8(value: u8) -> Option<Id> {
match value {
$(
$id => Some(Id::$camelcase),
)*
_ => None,
}
}
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[allow(missing_docs)]
pub enum Call {
$(
#[allow(non_camel_case_types)]
$camelcase ( $crate::runtime::$sub_name::$path::Call )
,)*
}

impl Call {
pub fn dispatch(self, d: $trait) {
match self {
$(
Call::$camelcase(x) => x.dispatch(d),
)*
}
}
}

impl $crate::dispatch::Slicable for Call {
fn decode<I: $crate::dispatch::Input>(input: &mut I) -> Option<Self> {
let id = u8::decode(input).and_then(Id::from_u8)?;
Some(match id {
$(
Id::$camelcase =>
Call::$camelcase( $crate::dispatch::Slicable::decode(input)? ),
)*
})
}

fn encode(&self) -> Vec<u8> {
let mut v = $crate::dispatch::Vec::new();
match *self {
$(
Call::$camelcase( ref sub ) => {
(Id::$camelcase as u8).using_encoded(|s| v.extend(s));
sub.using_encoded(|s| v.extend(s));
}
)*
}
v
}

fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(self.encode().as_slice())
}
}
impl $crate::dispatch::NonTrivialSlicable for Call {}
}
}
}
use {runtime, runtime_support, consensus, timestamp};

impl_meta_dispatch! {
pub mod public;
path public;
trait staking::PublicPass;
Session(mod session) = 1;
Staking(mod staking) = 2;
trait runtime_support::PublicPass;
Session(mod runtime::session) = 1;
Staking(mod runtime::staking) = 2;
Timestamp(mod timestamp) = 3;
Democracy(mod democracy) = 5;
Council(mod council) = 6;
CouncilVote(mod council) = 7;
Democracy(mod runtime::democracy) = 5;
Council(mod runtime::council) = 6;
CouncilVote(mod runtime::council_vote) = 7;
}

impl_meta_dispatch! {
pub mod privileged;
path privileged;
trait democracy::PrivPass;
System(mod system) = 0;
Session(mod session) = 1;
Staking(mod staking) = 2;
Democracy(mod democracy) = 5;
Council(mod council) = 6;
CouncilVote(mod council) = 7;
trait runtime_support::PrivPass;
Consensus(mod consensus) = 0;
Session(mod runtime::session) = 1;
Staking(mod runtime::staking) = 2;
Democracy(mod runtime::democracy) = 5;
Council(mod runtime::council) = 6;
CouncilVote(mod runtime::council_vote) = 7;
}

pub use self::privileged::Call as PrivCall;
Expand Down
Loading