difftreelog
feat calculate signature in compile time
in: master
11 files changed
Cargo.lockdiffbeforeafterboth1095source = "registry+https://github.com/rust-lang/crates.io-index"1095source = "registry+https://github.com/rust-lang/crates.io-index"1096checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3"1096checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3"10971098[[package]]1099name = "const_format"1100version = "0.2.26"1101source = "registry+https://github.com/rust-lang/crates.io-index"1102checksum = "939dc9e2eb9077e0679d2ce32de1ded8531779360b003b4a972a7a39ec263495"1103dependencies = [1104 "const_format_proc_macros",1105]11061107[[package]]1108name = "const_format_proc_macros"1109version = "0.2.22"1110source = "registry+https://github.com/rust-lang/crates.io-index"1111checksum = "ef196d5d972878a48da7decb7686eded338b4858fbabeed513d63a7c98b2b82d"1112dependencies = [1113 "proc-macro2",1114 "quote",1115 "unicode-xid",1116]109711171098[[package]]1118[[package]]1099name = "constant_time_eq"1119name = "constant_time_eq"2353version = "0.1.3"2373version = "0.1.3"2354dependencies = [2374dependencies = [2355 "concat-idents",2375 "concat-idents",2376 "const_format",2356 "ethereum",2377 "ethereum",2357 "evm-coder-procedural",2378 "evm-coder-procedural",2358 "evm-core 0.35.0 (git+https://github.com/uniquenetwork/evm?branch=unique-polkadot-v0.9.30)",2379 "evm-core 0.35.0 (git+https://github.com/uniquenetwork/evm?branch=unique-polkadot-v0.9.30)",2362 "impl-trait-for-tuples",2383 "impl-trait-for-tuples",2363 "pallet-evm 6.0.0-dev (git+https://github.com/uniquenetwork/frontier?branch=unique-polkadot-v0.9.27-fee-limit)",2384 "pallet-evm 6.0.0-dev (git+https://github.com/uniquenetwork/frontier?branch=unique-polkadot-v0.9.27-fee-limit)",2364 "primitive-types",2385 "primitive-types",2386 "sha3-const",2365 "similar-asserts",2387 "similar-asserts",2366 "sp-std 4.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.30)",2388 "sp-std 4.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.30)",2367]2389]11084 "keccak",11106 "keccak",11085]11107]1110811109[[package]]11110name = "sha3-const"11111version = "0.1.0"11112source = "registry+https://github.com/rust-lang/crates.io-index"11113checksum = "af9625d558d174dbdc711248b479271c0fdca39e9d3d67f6e890b3d4251fbf8e"110861111411087[[package]]11115[[package]]11088name = "sharded-slab"11116name = "sharded-slab"crates/evm-coder/Cargo.tomldiffbeforeafterboth5edition = "2021"5edition = "2021"667[dependencies]7[dependencies]8const_format = { version = "0.2.26", default-features = false }9sha3-const = { version = "0.1.0", default-features = false }10# Ethereum uses keccak (=sha3) for selectors11# sha3 = "0.10.1"8# evm-coder reexports those proc-macro12# evm-coder reexports those proc-macro9evm-coder-procedural = { path = "./procedural" }13evm-coder-procedural = { path = "./procedural" }10# Evm uses primitive-types for H160, H256 and others14# Evm uses primitive-types for H160, H256 and otherscrates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth20// about Procedural Macros in Rust book:20// about Procedural Macros in Rust book:21// https://doc.rust-lang.org/reference/procedural-macros.html21// https://doc.rust-lang.org/reference/procedural-macros.html222223use proc_macro2::{TokenStream, Group};23use quote::{quote, ToTokens};24use quote::{quote, ToTokens};24use inflector::cases;25use inflector::cases;25use std::fmt::Write;26use std::fmt::Write;328 }329 }329}330}330331332#[derive(Debug)]331enum AbiType {333enum AbiType {332 // type334 // type333 Plain(Ident),335 Plain(Ident),473 }475 }474}476}475477478#[derive(Debug)]476struct MethodArg {479struct MethodArg {477 name: Ident,480 name: Ident,478 camel_name: String,481 camel_name: String,722 }725 }723 }726 }727728 fn expand_selector(&self) -> proc_macro2::TokenStream {729 let custom_signature = self.expand_custom_signature();730 quote! {731 {732 let a = ::evm_coder::sha3_const::Keccak256::new()733 .update(#custom_signature.as_bytes())734 .finalize();735 [a[0], a[1], a[2], a[3]]736 }737 }738 }724739725 fn expand_const(&self) -> proc_macro2::TokenStream {740 fn expand_const(&self) -> proc_macro2::TokenStream {726 let screaming_name = &self.screaming_name;741 let screaming_name = &self.screaming_name;727 let selector = u32::to_be_bytes(self.selector);742 let selector_str = &self.selector_str;728 let selector_str = &self.selector_str;743 let selector = &self.expand_selector();729 quote! {744 quote! {730 #[doc = #selector_str]745 #[doc = #selector_str]731 const #screaming_name: ::evm_coder::types::bytes4 = [#(#selector,)*];746 const #screaming_name: ::evm_coder::types::bytes4 = #selector;732 }747 }733 }748 }734749831 }846 }832 }847 }848849 fn expand_custom_signature(&self) -> proc_macro2::TokenStream {850 let mut first_comma = true;851 let mut custom_signature = TokenStream::new();852 let mut template = self.camel_name.clone() + "(";853 self.args854 .iter()855 .filter_map(|a| {856 if a.is_special() {857 return None;858 };859860 match a.ty {861 AbiType::Plain(ref ident) => Some(ident),862 _ => None,863 }864 })865 .for_each(|ident| {866 if !first_comma {867 custom_signature.extend(quote!(,));868 template.push(',');869 } else {870 first_comma = false;871 };872 template.push_str("{}");873 let ident_str = ident.to_string();874 match ident_str.as_str() {875 "address" | "uint8" | "uint16" | "uint32" | "uint64" | "uint128"876 | "uint256" | "bytes4" | "topic" | "string" | "bytes" | "void" | "caller"877 | "bool" | "" => {878 custom_signature.extend(quote!(#ident_str));879 }880 _ => {881 custom_signature.extend(quote! {882 #ident::SIGNATURE_STRING883 });884 }885 }886 });887888 template.push(')');889 let mut template = quote!(#template);890 template.extend(quote!(,));891 template.extend(custom_signature);892 let custom_signature_group = Group::new(proc_macro2::Delimiter::Parenthesis, template);893 let mut custom_signature = quote! {894 ::evm_coder::const_format::formatcp!895 };896 custom_signature.extend(custom_signature_group.to_token_stream());897898 // println!("!!!!! {}", custom_signature);899 custom_signature900 }833901834 fn expand_solidity_function(&self) -> proc_macro2::TokenStream {902 fn expand_solidity_function(&self) -> proc_macro2::TokenStream {835 let camel_name = &self.camel_name;903 let camel_name = &self.camel_name;847 .map(MethodArg::expand_solidity_argument);915 .map(MethodArg::expand_solidity_argument);848 let docs = &self.docs;916 let docs = &self.docs;849 let selector_str = &self.selector_str;917 let selector_str = &self.selector_str;850 let selector = self.selector;918 let selector = &self.expand_selector();851 let hide = self.hide;919 let hide = self.hide;920 let custom_signature = self.expand_custom_signature();852 let is_payable = self.has_value_args;921 let is_payable = self.has_value_args;853 quote! {922 quote! {854 SolidityFunction {923 SolidityFunction {855 docs: &[#(#docs),*],924 docs: &[#(#docs),*],856 selector_str: #selector_str,925 selector_str: #selector_str,926 hide: #hide,857 selector: #selector,927 selector: u32::from_be_bytes(#selector),858 hide: #hide,928 custom_signature: #custom_signature,859 name: #camel_name,929 name: #camel_name,860 mutability: #mutability,930 mutability: #mutability,861 is_payable: #is_payable,931 is_payable: #is_payable,crates/evm-coder/src/lib.rsdiffbeforeafterboth90pub use evm_coder_procedural::solidity;90pub use evm_coder_procedural::solidity;91/// See [`solidity_interface`]91/// See [`solidity_interface`]92pub use evm_coder_procedural::weight;92pub use evm_coder_procedural::weight;93pub use const_format;94pub use sha3_const;939594/// Derives [`ToLog`] for enum96/// Derives [`ToLog`] for enum95///97///227 }229 }228 }230 }231232 impl SignatureString for EthCrossAccount {233 const SIGNATURE_STRING: &'static str = "(address,uint256)";234 }235236 pub trait SignatureString {237 const SIGNATURE_STRING: &'static str;238 }229239230 /// Convert `CrossAccountId` to `uint256`.240 /// Convert `CrossAccountId` to `uint256`.231 pub fn convert_cross_account_to_uint256<T: pallet_evm::account::Config>(241 pub fn convert_cross_account_to_uint256<T: pallet_evm::account::Config>(348 assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);358 assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);349 }359 }360361 // #[test]362 // fn function_selector_generation_1() {363 // assert_eq!(364 // fn_selector!(transferFromCrossAccountToCrossAccount(365 // EthCrossAccount,366 // EthCrossAccount,367 // uint256368 // )),369 // 2543295963370 // );371 // }350372351 #[test]373 #[test]352 fn event_topic_generation() {374 fn event_topic_generation() {crates/evm-coder/src/solidity.rsdiffbeforeafterboth34use impl_trait_for_tuples::impl_for_tuples;34use impl_trait_for_tuples::impl_for_tuples;35use crate::types::*;35use crate::types::*;3637#[derive(Default)]38pub struct FunctionSelectorMaker {39 pub name: string,40 pub args: Vec<fn() -> string>,41}364237#[derive(Default)]43#[derive(Default)]38pub struct TypeCollector {44pub struct TypeCollector {483 Mutable,489 Mutable,484}490}491492// fn fn_selector_str(input: &str) -> u32 {493// use sha3::Digest;494// let mut hasher = sha3::Keccak256::new();495// hasher.update(input.as_bytes());496// let result = hasher.finalize();497498// let mut selector_bytes = [0; 4];499// selector_bytes.copy_from_slice(&result[0..4]);500501// u32::from_be_bytes(selector_bytes)502// }503485pub struct SolidityFunction<A, R> {504pub struct SolidityFunction<A, R> {486 pub docs: &'static [&'static str],505 pub docs: &'static [&'static str],487 pub selector_str: &'static str,506 pub selector_str: &'static str,488 pub selector: u32,507 pub selector: u32,489 pub hide: bool,508 pub hide: bool,509 pub custom_signature: &'static str,490 pub name: &'static str,510 pub name: &'static str,491 pub args: A,511 pub args: A,492 pub result: R,512 pub result: R,512 writeln!(532 writeln!(513 writer,533 writer,514 "\t{hide_comment}/// or in textual repr: {}",534 "\t{hide_comment}/// or in textual repr: {}",515 self.selector_str535 self.custom_signature516 )?;536 )?;517 write!(writer, "\t{hide_comment}function {}(", self.name)?;537 write!(writer, "\t{hide_comment}function {}(", self.name)?;518 self.args.solidity_name(writer, tc)?;538 self.args.solidity_name(writer, tc)?;pallets/evm-contract-helpers/Cargo.tomldiffbeforeafterbothno syntactic changes
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth161617//! Implementation of magic contract17//! Implementation of magic contract181819extern crate alloc;20use alloc::{format, string::ToString};19use core::marker::PhantomData;21use core::marker::PhantomData;20use evm_coder::{22use evm_coder::{21 abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*, ToLog,23 abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*, ToLog,pallets/fungible/src/erc.rsdiffbeforeafterboth161617//! ERC-20 standart support implementation.17//! ERC-20 standart support implementation.181819extern crate alloc;20use alloc::{format, string::ToString};19use core::char::{REPLACEMENT_CHARACTER, decode_utf16};21use core::char::{REPLACEMENT_CHARACTER, decode_utf16};20use core::convert::TryInto;22use core::convert::TryInto;21use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};23use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};pallets/nonfungible/src/erc.rsdiffbeforeafterboth20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.212122extern crate alloc;22extern crate alloc;23use alloc::{format, string::ToString};23use core::{24use core::{24 char::{REPLACEMENT_CHARACTER, decode_utf16},25 char::{REPLACEMENT_CHARACTER, decode_utf16},25 convert::TryInto,26 convert::TryInto,pallets/refungible/Cargo.tomldiffbeforeafterbothno syntactic changes
pallets/refungible/src/erc.rsdiffbeforeafterboth212122extern crate alloc;22extern crate alloc;232324use alloc::{format, string::ToString};24use core::{25use core::{25 char::{REPLACEMENT_CHARACTER, decode_utf16},26 char::{REPLACEMENT_CHARACTER, decode_utf16},26 convert::TryInto,27 convert::TryInto,