git.delta.rocks / unique-network / refs/commits / b44fe9bf17b6

difftreelog

feat calculate signature in compile time

Trubnikov Sergey2022-10-11parent: #4d4a023.patch.diff
in: master

11 files changed

modifiedCargo.lockdiffbeforeafterboth
1095source = "registry+https://github.com/rust-lang/crates.io-index"1095source = "registry+https://github.com/rust-lang/crates.io-index"
1096checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3"1096checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3"
1097
1098[[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]
1106
1107[[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]
10971117
1098[[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]
11108
11109[[package]]
11110name = "sha3-const"
11111version = "0.1.0"
11112source = "registry+https://github.com/rust-lang/crates.io-index"
11113checksum = "af9625d558d174dbdc711248b479271c0fdca39e9d3d67f6e890b3d4251fbf8e"
1108611114
11087[[package]]11115[[package]]
11088name = "sharded-slab"11116name = "sharded-slab"
modifiedcrates/evm-coder/Cargo.tomldiffbeforeafterboth
5edition = "2021"5edition = "2021"
66
7[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 selectors
11# sha3 = "0.10.1"
8# evm-coder reexports those proc-macro12# evm-coder reexports those proc-macro
9evm-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 others
modifiedcrates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth
20// 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.html
2222
23use 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}
330331
332#[derive(Debug)]
331enum AbiType {333enum AbiType {
332 // type334 // type
333 Plain(Ident),335 Plain(Ident),
473 }475 }
474}476}
475477
478#[derive(Debug)]
476struct MethodArg {479struct MethodArg {
477 name: Ident,480 name: Ident,
478 camel_name: String,481 camel_name: String,
722 }725 }
723 }726 }
727
728 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 }
724739
725 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 }
734749
831 }846 }
832 }847 }
848
849 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.args
854 .iter()
855 .filter_map(|a| {
856 if a.is_special() {
857 return None;
858 };
859
860 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_STRING
883 });
884 }
885 }
886 });
887
888 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());
897
898 // println!("!!!!! {}", custom_signature);
899 custom_signature
900 }
833901
834 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,
modifiedcrates/evm-coder/src/lib.rsdiffbeforeafterboth
90pub 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;
9395
94/// Derives [`ToLog`] for enum96/// Derives [`ToLog`] for enum
95///97///
227 }229 }
228 }230 }
231
232 impl SignatureString for EthCrossAccount {
233 const SIGNATURE_STRING: &'static str = "(address,uint256)";
234 }
235
236 pub trait SignatureString {
237 const SIGNATURE_STRING: &'static str;
238 }
229239
230 /// 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 }
360
361 // #[test]
362 // fn function_selector_generation_1() {
363 // assert_eq!(
364 // fn_selector!(transferFromCrossAccountToCrossAccount(
365 // EthCrossAccount,
366 // EthCrossAccount,
367 // uint256
368 // )),
369 // 2543295963
370 // );
371 // }
350372
351 #[test]373 #[test]
352 fn event_topic_generation() {374 fn event_topic_generation() {
modifiedcrates/evm-coder/src/solidity.rsdiffbeforeafterboth
34use impl_trait_for_tuples::impl_for_tuples;34use impl_trait_for_tuples::impl_for_tuples;
35use crate::types::*;35use crate::types::*;
36
37#[derive(Default)]
38pub struct FunctionSelectorMaker {
39 pub name: string,
40 pub args: Vec<fn() -> string>,
41}
3642
37#[derive(Default)]43#[derive(Default)]
38pub struct TypeCollector {44pub struct TypeCollector {
483 Mutable,489 Mutable,
484}490}
491
492// 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();
497
498// let mut selector_bytes = [0; 4];
499// selector_bytes.copy_from_slice(&result[0..4]);
500
501// u32::from_be_bytes(selector_bytes)
502// }
503
485pub 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_signature
516 )?;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)?;
modifiedpallets/evm-contract-helpers/Cargo.tomldiffbeforeafterboth

no syntactic changes

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
1616
17//! Implementation of magic contract17//! Implementation of magic contract
1818
19extern 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,
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
1616
17//! ERC-20 standart support implementation.17//! ERC-20 standart support implementation.
1818
19extern 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};
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
20//! 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.
2121
22extern 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,
modifiedpallets/refungible/Cargo.tomldiffbeforeafterboth

no syntactic changes

modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
2121
22extern crate alloc;22extern crate alloc;
2323
24use 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,