Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
163 changes: 95 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ vergen = "3"
members = [
"core/authority-discovery",
"core/application-crypto",
"core/chain-spec",
"core/chain-spec/derive",
"core/cli",
"core/client",
"core/client/db",
Expand Down
17 changes: 17 additions & 0 deletions core/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "substrate-chain-spec"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
chain-spec-derive = { package = "substrate-chain-spec-derive", path = "./derive" }
impl-trait-for-tuples = "0.1.1"
network = { package = "substrate-network", path = "../../core/network" }
primitives = { package = "substrate-primitives", path = "../primitives" }
serde = { version = "1.0.101", features = ["derive"] }
serde_json = "1.0.40"
sr-primitives = { path = "../../core/sr-primitives" }
tel = { package = "substrate-telemetry", path = "../../core/telemetry" }

[dev-dependencies]
17 changes: 17 additions & 0 deletions core/chain-spec/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "substrate-chain-spec-derive"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[lib]
proc-macro = true

[dependencies]
proc-macro-crate = "0.1.3"
proc-macro2 = "1.0.1"
quote = "1.0.2"
syn = "1.0.5"

[dev-dependencies]

191 changes: 191 additions & 0 deletions core/chain-spec/derive/src/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{DeriveInput, Ident, Error};
use proc_macro_crate::crate_name;

const CRATE_NAME: &str = "substrate-chain-spec";
const ATTRIBUTE_NAME: &str = "forks";

/// Implements `Extension's` `Group` accessor.
///
/// The struct that derives this implementation will be usable within the `ChainSpec` file.
/// The derive implements a by-type accessor method.
pub fn extension_derive(ast: &DeriveInput) -> proc_macro::TokenStream {
derive(ast, |crate_name, name, generics: &syn::Generics, field_names, field_types, fields| {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let forks = fields.named.iter().find_map(|f| {
if f.attrs.iter().any(|attr| attr.path.is_ident(ATTRIBUTE_NAME)) {
let typ = &f.ty;
Some(quote! { #typ })
} else {
None
}
}).unwrap_or_else(|| quote! { #crate_name::NoExtension });

quote! {
impl #impl_generics #crate_name::Extension for #name #ty_generics #where_clause {
type Forks = #forks;

fn get<T: 'static>(&self) -> Option<&T> {
use std::any::{Any, TypeId};

match TypeId::of::<T>() {
#( x if x == TypeId::of::<#field_types>() => Any::downcast_ref(&self.#field_names) ),*,
_ => None,
}
}
}
}
})
}


/// Implements required traits and creates `Fork` structs for `ChainSpec` custom parameter group.
pub fn group_derive(ast: &DeriveInput) -> proc_macro::TokenStream {
derive(ast, |crate_name, name, generics: &syn::Generics, field_names, field_types, _fields| {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let fork_name = Ident::new(&format!("{}Fork", name), Span::call_site());

let fork_fields = generate_fork_fields(&crate_name, &field_names, &field_types);
let to_fork = generate_base_to_fork(&fork_name, &field_names);
let combine_with = generate_combine_with(&field_names);
let to_base = generate_fork_to_base(name, &field_names);

quote! {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecExtension)]
pub struct #fork_name #ty_generics #where_clause {
#fork_fields
}

impl #impl_generics #crate_name::Group for #name #ty_generics #where_clause {
type Fork = #fork_name #ty_generics;

fn to_fork(self) -> Self::Fork {
use #crate_name::Group;
#to_fork
}
}

impl #impl_generics #crate_name::Fork for #fork_name #ty_generics #where_clause {
type Base = #name #ty_generics;

fn combine_with(&mut self, other: Self) {
use #crate_name::Fork;
#combine_with
}

fn to_base(self) -> Option<Self::Base> {
use #crate_name::Fork;
#to_base
}
}
}
})
}

pub fn derive(
ast: &DeriveInput,
derive: impl Fn(
&Ident, &Ident, &syn::Generics, Vec<&Ident>, Vec<&syn::Type>, &syn::FieldsNamed,
) -> TokenStream,
) -> proc_macro::TokenStream {
let err = || {
let err = Error::new(
Span::call_site(),
"ChainSpecGroup is only avaible for structs with named fields."
).to_compile_error();
quote!( #err ).into()
};

let data = match &ast.data {
syn::Data::Struct(ref data) => data,
_ => return err(),
};

let fields = match &data.fields {
syn::Fields::Named(ref named) => named,
_ => return err(),
};

const PROOF: &str = "CARGO_PKG_NAME always defined when compiling; qed";
let name = &ast.ident;
let crate_name = match crate_name(CRATE_NAME) {
Ok(chain_spec_name) => chain_spec_name,
Err(e) => if std::env::var("CARGO_PKG_NAME").expect(PROOF) == CRATE_NAME {
// we return the name of the crate here instead of `crate` to support doc tests.
CRATE_NAME.replace("-", "_")
} else {
let err = Error::new(Span::call_site(), &e).to_compile_error();
return quote!( #err ).into()
},
};
let crate_name = Ident::new(&crate_name, Span::call_site());
let field_names = fields.named.iter().flat_map(|x| x.ident.as_ref()).collect::<Vec<_>>();
let field_types = fields.named.iter().map(|x| &x.ty).collect::<Vec<_>>();

derive(&crate_name, name, &ast.generics, field_names, field_types, fields).into()
}

fn generate_fork_fields(
crate_name: &Ident,
names: &[&Ident],
types: &[&syn::Type],
) -> TokenStream {
let crate_name = std::iter::repeat(crate_name);
quote! {
#( pub #names: Option<<#types as #crate_name::Group>::Fork>, )*
}
}

fn generate_base_to_fork(
fork_name: &Ident,
names: &[&Ident],
) -> TokenStream {
let names2 = names.to_vec();

quote!{
#fork_name {
#( #names: Some(self.#names2.to_fork()), )*
}
}
}

fn generate_combine_with(
names: &[&Ident],
) -> TokenStream {
let names2 = names.to_vec();

quote!{
#( self.#names.combine_with(other.#names2); )*
}
}

fn generate_fork_to_base(
fork: &Ident,
names: &[&Ident],
) -> TokenStream {
let names2 = names.to_vec();

quote!{
Some(#fork {
#( #names: self.#names2?.to_base()?, )*
})
}
}

39 changes: 39 additions & 0 deletions core/chain-spec/derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Macros to derive chain spec extension traits implementation.

extern crate proc_macro;

mod impls;

use proc_macro::TokenStream;

#[proc_macro_derive(ChainSpecGroup)]
pub fn group_derive(input: TokenStream) -> TokenStream {
match syn::parse(input) {
Ok(ast) => impls::group_derive(&ast),
Err(e) => e.to_compile_error().into(),
}
}

#[proc_macro_derive(ChainSpecExtension, attributes(forks))]
pub fn extensions_derive(input: TokenStream) -> TokenStream {
match syn::parse(input) {
Ok(ast) => impls::extension_derive(&ast),
Err(e) => e.to_compile_error().into(),
}
}
31 changes: 31 additions & 0 deletions core/chain-spec/res/chain_spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "Flaming Fir",
"id": "flaming-fir",
"properties": {
"tokenDecimals": 15,
"tokenSymbol": "FIR"
},
"bootNodes": [
"/ip4/35.246.224.91/tcp/30333/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV",
"/ip4/35.246.224.91/tcp/30334/ws/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV",
"/ip4/35.246.210.11/tcp/30333/p2p/QmWv9Ww7znzgLFyCzf21SR6tUKXrmHCZH9KhebeH4gyE9f",
"/ip4/35.246.210.11/tcp/30334/ws/p2p/QmWv9Ww7znzgLFyCzf21SR6tUKXrmHCZH9KhebeH4gyE9f",
"/ip4/35.198.110.45/tcp/30333/p2p/QmTtcYKJho9vFmqtMA548QBSmLbmwAkBSiEKK3kWKfb6bJ",
"/ip4/35.198.110.45/tcp/30334/ws/p2p/QmTtcYKJho9vFmqtMA548QBSmLbmwAkBSiEKK3kWKfb6bJ",
"/ip4/35.198.114.154/tcp/30333/p2p/QmQJmDorK9c8KjMF5PdWiH2WGUXyzJtgTeJ55S5gggdju6",
"/ip4/35.198.114.154/tcp/30334/ws/p2p/QmQJmDorK9c8KjMF5PdWiH2WGUXyzJtgTeJ55S5gggdju6"
],
"telemetryEndpoints": [
["wss://telemetry.polkadot.io/submit/", 0]
],
"protocolId": "fir",
"consensusEngine": null,
"genesis": {
"raw": [
{
"0xb2029f8665aac509629f2d28cea790a3": "0x10f26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c26633919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f437800299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d655633b70b80a6c8bb16270f82cca6d56b27ed7b76c8fd5af2986a25a4788ce440482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde787932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d129becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe96993326e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f91066e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"
},
{}
]
}
}
32 changes: 32 additions & 0 deletions core/chain-spec/res/chain_spec2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "Flaming Fir",
"id": "flaming-fir",
"properties": {
"tokenDecimals": 15,
"tokenSymbol": "FIR"
},
"bootNodes": [
"/ip4/35.246.224.91/tcp/30333/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV",
"/ip4/35.246.224.91/tcp/30334/ws/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV",
"/ip4/35.246.210.11/tcp/30333/p2p/QmWv9Ww7znzgLFyCzf21SR6tUKXrmHCZH9KhebeH4gyE9f",
"/ip4/35.246.210.11/tcp/30334/ws/p2p/QmWv9Ww7znzgLFyCzf21SR6tUKXrmHCZH9KhebeH4gyE9f",
"/ip4/35.198.110.45/tcp/30333/p2p/QmTtcYKJho9vFmqtMA548QBSmLbmwAkBSiEKK3kWKfb6bJ",
"/ip4/35.198.110.45/tcp/30334/ws/p2p/QmTtcYKJho9vFmqtMA548QBSmLbmwAkBSiEKK3kWKfb6bJ",
"/ip4/35.198.114.154/tcp/30333/p2p/QmQJmDorK9c8KjMF5PdWiH2WGUXyzJtgTeJ55S5gggdju6",
"/ip4/35.198.114.154/tcp/30334/ws/p2p/QmQJmDorK9c8KjMF5PdWiH2WGUXyzJtgTeJ55S5gggdju6"
],
"telemetryEndpoints": [
["wss://telemetry.polkadot.io/submit/", 0]
],
"protocolId": "fir",
"consensusEngine": null,
"myProperty": "Test Extension",
"genesis": {
"raw": [
{
"0xb2029f8665aac509629f2d28cea790a3": "0x10f26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c26633919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f437800299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d655633b70b80a6c8bb16270f82cca6d56b27ed7b76c8fd5af2986a25a4788ce440482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde787932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d129becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe96993326e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f91066e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"
},
{}
]
}
}
Loading