-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add generate_function_selector macro * replace Debug to RuntimeDebug
- Loading branch information
Showing
17 changed files
with
209 additions
and
126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ members = [ | |
"modules/*", | ||
"inspect", | ||
"primitives", | ||
"primitives/proc-macro", | ||
"rpc", | ||
|
||
"runtime/common", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "primitives-proc-macro" | ||
version = "1.1.1" | ||
authors = ["Acala Developers"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
sha3 = { version = "0.9.1" } | ||
quote = "1.0.3" | ||
syn = { version = "1.0.58", features = ["full", "fold", "extra-traits", "visit"] } | ||
proc-macro2 = "1.0.6" | ||
proc-macro-crate = "1.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This file is part of Acala. | ||
|
||
// Copyright (C) 2020-2021 Acala Foundation. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program 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. | ||
|
||
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro2::Literal; | ||
use quote::quote; | ||
use sha3::{Digest, Keccak256}; | ||
use std::convert::TryInto; | ||
use syn::{parse_macro_input, Expr, ExprLit, Ident, ItemEnum, Lit}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn generate_function_selector(_: TokenStream, input: TokenStream) -> TokenStream { | ||
let item = parse_macro_input!(input as ItemEnum); | ||
|
||
let ItemEnum { | ||
attrs, | ||
vis, | ||
enum_token, | ||
ident, | ||
variants, | ||
.. | ||
} = item; | ||
|
||
let mut ident_expressions: Vec<Ident> = vec![]; | ||
let mut variant_expressions: Vec<Expr> = vec![]; | ||
for variant in variants { | ||
if let Some((_, Expr::Lit(ExprLit { lit, .. }))) = variant.discriminant { | ||
if let Lit::Str(token) = lit { | ||
let selector = get_function_selector(&token.value()); | ||
// println!("method: {:?}, selector: {:?}", token.value(), selector); | ||
ident_expressions.push(variant.ident); | ||
variant_expressions.push(Expr::Lit(ExprLit { | ||
lit: Lit::Verbatim(Literal::u32_suffixed(selector)), | ||
attrs: Default::default(), | ||
})); | ||
} else { | ||
panic!("Not method string: `{:?}`", lit); | ||
} | ||
} else { | ||
panic!("Not enum: `{:?}`", variant); | ||
} | ||
} | ||
|
||
(quote! { | ||
#(#attrs)* | ||
#vis #enum_token #ident { | ||
#( | ||
#ident_expressions = #variant_expressions, | ||
)* | ||
} | ||
}) | ||
.into() | ||
} | ||
|
||
fn get_function_selector(s: &str) -> u32 { | ||
// create a SHA3-256 object | ||
let mut hasher = Keccak256::new(); | ||
// write input message | ||
hasher.update(s); | ||
// read hash digest | ||
let result = hasher.finalize(); | ||
u32::from_be_bytes(result[..4].try_into().unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.