Make substrate generic (#169)

* Some initial work on RPC and client

* Rephrase as params

* More work on traitifying substrate.

* Traitify in_mem.rs

* traitify client.rs

* Make new primitives (mainly traits) build again.

* Many (superficial) build fixes throughout.

* Fix remaining build issues up to bft interface.

* Make bft primitives be generic.

* Switch out MisBehaviorReport for generic version.

* Merge Hashing into Header.

* Update runtime for new generics (with Hashing).

* Update demo runtime.

* Make runtime compile.

* Build fixes for runtime

* Remove old modules.

* port substrate-bft to use generic substrate types

* port client

* port substrate-test-runtime

* mostly port test-runtime to get compiling for std

* Ensure `AccountId` has a `Default`.

* Fix type deps.

* finish porting

* initialize test_runtime from genesis correctly

* remove commented code

* maybe unsigned signatures

* runtimes compile

* port over most of network

* serialization for generic types

* fix comment

* remove some unnecessary trait bounds

* network compiles

* tests compile for sync

* fix deserialization

* temporarily remove deserialize derives

* workarounds for serde issues for deriving deserialization

* get demo-runtime compiling on std

* port extrinsic-pool

* primitives reshuffling

* get network compiling again

* remove debugging file

* runtime tests now passing

* port client-db

* start to port over substrate-rpc

* mostly port over PolkadotApi

* test_runtime follows normal conventions

* substrate runtime tests pass

* deal with inherent extrinsics correctly in polkadot-api

* port transaction-pool

* port polkadot-consensus

* port substrate-rpc

* everything compiles

* tests compile

* fix grumbles

* test-runtime uses its own transfer type

* switch to master branch of jsonrpc

* fix network tests and some warnings

* all tests pass locally

* [ci-skip] add another comment about issue

* remove some curlies
This commit is contained in:
Gav Wood
2018-06-06 17:58:45 +02:00
committed by Robert Habermeier
parent 4e844760a3
commit b94cf078af
132 changed files with 4695 additions and 4303 deletions
@@ -20,7 +20,6 @@ pub use rstd::prelude::{Vec, Clone, Eq, PartialEq};
#[cfg(feature = "std")]
pub use std::fmt;
pub use rstd::result;
pub use rstd::marker::PhantomData;
#[cfg(feature = "std")]
use serde;
pub use codec::{Slicable, Input};
@@ -47,6 +46,10 @@ pub trait AuxCallable {
type Call: AuxDispatchable + Slicable + Clone + PartialEq + Eq;
}
// dirty hack to work around serde_derive issue
// https://github.com/rust-lang/rust/issues/51331
pub type AuxCallableCallFor<A> = <A as AuxCallable>::Call;
#[cfg(feature = "std")]
pub trait Callable {
type Call: Dispatchable + Slicable + ::serde::Serialize + Clone + PartialEq + Eq;
@@ -56,6 +59,10 @@ pub trait Callable {
type Call: Dispatchable + Slicable + Clone + PartialEq + Eq;
}
// dirty hack to work around serde_derive issue.
// https://github.com/rust-lang/rust/issues/51331
pub type CallableCallFor<C> = <C as Callable>::Call;
#[cfg(feature = "std")]
pub trait Parameter: Slicable + serde::Serialize + Clone + Eq + fmt::Debug {}
@@ -68,26 +75,43 @@ pub trait Parameter: Slicable + Clone + Eq {}
#[cfg(not(feature = "std"))]
impl<T> Parameter for T where T: Slicable + Clone + Eq {}
/// Declare a struct for this module, then implement dispatch logic to create a pairing of several
/// dispatch traits and enums.
#[macro_export]
macro_rules! decl_module {
(
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$($rest:tt)*
) => {
pub struct $mod_type<$trait_instance: $trait_name>($crate::dispatch::PhantomData<$trait_instance>);
// TODO: switching based on std feature is because of an issue in
// serde-derive for when we attempt to derive `Deserialize` on these types,
// in a situation where we've imported `substrate_runtime_support` as another name.
#[cfg(feature = "std")]
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>(::std::marker::PhantomData<$trait_instance>);
#[cfg(not(feature = "std"))]
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name>(::core::marker::PhantomData<$trait_instance>);
decl_dispatch! {
impl for $mod_type<$trait_instance: $trait_name>;
$($rest)*
}
};
(
$(#[$attr:meta])*
struct $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$($rest:tt)*
) => {
struct $mod_type<$trait_instance: $trait_name>($crate::dispatch::PhantomData<$trait_instance>);
#[cfg(feature = "std")]
$(#[$attr])*
struct $mod_type<$trait_instance: $trait_name>(::std::marker::PhantomData<$trait_instance>);
#[cfg(not(feature = "std"))]
$(#[$attr])*
struct $mod_type<$trait_instance: $trait_name>(::core::marker::PhantomData<$trait_instance>);
decl_dispatch! {
impl for $mod_type<$trait_instance: $trait_name>;
$($rest)*
@@ -101,6 +125,7 @@ macro_rules! decl_dispatch {
// WITHOUT AUX
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident {
$(
fn $fn_name:ident(
@@ -115,6 +140,7 @@ macro_rules! decl_dispatch {
) => {
__decl_dispatch_module_without_aux! {
impl for $mod_type<$trait_instance: $trait_name>;
$(#[$attr])*
pub enum $call_type;
$(
fn $fn_name( $( $param_name: $param ),* ) -> $result = $id;
@@ -128,6 +154,7 @@ macro_rules! decl_dispatch {
// WITH AUX
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident where aux: $aux_type:ty {
$(
fn $fn_name:ident(aux
@@ -142,6 +169,7 @@ macro_rules! decl_dispatch {
) => {
__decl_dispatch_module_with_aux! {
impl for $mod_type<$trait_instance: $trait_name>;
$(#[$attr])*
pub enum $call_type where aux: $aux_type;
$(
fn $fn_name(aux $(, $param_name: $param )*) -> $result = $id;
@@ -172,6 +200,7 @@ macro_rules! decl_dispatch {
macro_rules! __decl_dispatch_module_without_aux {
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident;
$(
fn $fn_name:ident(
@@ -185,6 +214,7 @@ macro_rules! __decl_dispatch_module_without_aux {
) => {
__decl_dispatch_module_common! {
impl for $mod_type<$trait_instance: $trait_name>;
$(#[$attr])*
pub enum $call_type;
$( fn $fn_name( $( $param_name : $param ),* ) -> $result = $id ; )*
}
@@ -215,6 +245,7 @@ macro_rules! __decl_dispatch_module_without_aux {
macro_rules! __decl_dispatch_module_with_aux {
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident where aux: $aux_type:ty;
$(
fn $fn_name:ident(aux
@@ -228,6 +259,7 @@ macro_rules! __decl_dispatch_module_with_aux {
) => {
__decl_dispatch_module_common! {
impl for $mod_type<$trait_instance: $trait_name>;
$(#[$attr])*
pub enum $call_type;
$( fn $fn_name( $( $param_name : $param ),* ) -> $result = $id ; )*
}
@@ -254,11 +286,12 @@ macro_rules! __decl_dispatch_module_with_aux {
};
}
#[macro_export]
/// Implement a single dispatch modules to create a pairing of a dispatch trait and enum.
#[macro_export]
macro_rules! __decl_dispatch_module_common {
(
impl for $mod_type:ident<$trait_instance:ident: $trait_name:ident>;
$(#[$attr:meta])*
pub enum $call_type:ident;
$(
fn $fn_name:ident(
@@ -270,10 +303,20 @@ macro_rules! __decl_dispatch_module_common {
= $id:expr ;
)*
) => {
#[cfg_attr(feature = "std", derive(Serialize))]
#[allow(missing_docs)]
#[cfg(feature = "std")]
$(#[$attr])*
pub enum $call_type<$trait_instance: $trait_name> {
__PhantomItem($crate::dispatch::PhantomData<$trait_instance>),
__PhantomItem(::std::marker::PhantomData<$trait_instance>),
$(
#[allow(non_camel_case_types)]
$fn_name ( $( $param ),* ),
)*
}
#[cfg(not(feature = "std"))]
$(#[$attr])*
pub enum $call_type<$trait_instance: $trait_name> {
__PhantomItem(::core::marker::PhantomData<$trait_instance>),
$(
#[allow(non_camel_case_types)]
$fn_name ( $( $param ),* ),
@@ -395,6 +438,7 @@ pub trait IsAuxSubType<T: AuxCallable> {
macro_rules! impl_outer_dispatch {
() => ();
(
$(#[$attr:meta])*
pub enum $call_type:ident where aux: $aux:ty {
$(
$camelcase:ident = $id:expr,
@@ -402,12 +446,10 @@ macro_rules! impl_outer_dispatch {
}
$( $rest:tt )*
) => {
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[allow(missing_docs)]
$(#[$attr])*
pub enum $call_type {
$(
$camelcase ( <$camelcase as $crate::dispatch::AuxCallable>::Call )
$camelcase ( $crate::dispatch::AuxCallableCallFor<$camelcase> )
,)*
}
impl_outer_dispatch_common! { $call_type, $($camelcase = $id,)* }
@@ -436,6 +478,7 @@ macro_rules! impl_outer_dispatch {
impl_outer_dispatch!{ $($rest)* }
};
(
$(#[$attr:meta])*
pub enum $call_type:ident {
$(
$camelcase:ident = $id:expr,
@@ -443,12 +486,10 @@ macro_rules! impl_outer_dispatch {
}
$( $rest:tt )*
) => {
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Debug))]
#[allow(missing_docs)]
$(#[$attr])*
pub enum $call_type {
$(
$camelcase ( <$camelcase as $crate::dispatch::Callable>::Call )
$camelcase ( $crate::dispatch::CallableCallFor<$camelcase> )
,)*
}
impl_outer_dispatch_common! { $call_type, $($camelcase = $id,)* }
@@ -21,14 +21,6 @@
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
#[allow(unused_imports)] // can be removed when fixed: https://github.com/rust-lang/rust/issues/43497
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "std")]
pub use serde_derive::*;
extern crate substrate_runtime_std as rstd;
extern crate substrate_runtime_io as runtime_io;
extern crate substrate_primitives as primitives;