difftreelog
refactor(evm-coder) toposort generated code
in: master
Previously, all items from evm-coder codegen was sorted lexically, which caused some problems, where lexically sorted names of contract were not in the same order as contract inherit chain Now they are sorted by insertion order, and macro-generated code now inserts them in order of definition, thus providing topological sort of output contract items
3 files changed
crates/evm-coder-macros/src/lib.rsdiffbeforeafterboth22use sha3::{Digest, Keccak256};22use sha3::{Digest, Keccak256};23use syn::{23use syn::{24 DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments,24 DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments,25 PathSegment, Type, parse_macro_input, spanned::Spanned, Attribute, parse::Parse,25 PathSegment, Type, parse_macro_input, spanned::Spanned,26};26};272728mod solidity_interface;28mod solidity_interface;241/// Event(#[indexed] uint32),241/// Event(#[indexed] uint32),242/// }242/// }243///243///244/// #[solidity_interface(name = "MyContract", is(SuperContract), inline_is(InlineContract))]244/// #[solidity_interface(name = MyContract, is(SuperContract), inline_is(InlineContract))]245/// impl Contract {245/// impl Contract {246/// /// Multiply two numbers246/// /// Multiply two numbers247/// #[weight(200 + a + b)]247/// #[weight(200 + a + b)]crates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth989 #solidity_functions,989 #solidity_functions,990 )*),990 )*),991 };991 };992 if is_impl {993 tc.collect("/// @dev common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());994 } else {995 tc.collect("/// @dev common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());996 }997 #(998 #solidity_generators999 )*1000 #(1001 #solidity_event_generators1002 )*10039921004 let mut out = string::new();993 let mut out = string::new();1005 // In solidity interface usage (is) should be preceeded by interface definition1006 // HACK: this comment helps to sort it in a set1007 if #solidity_name.starts_with("Inline") {994 if #solidity_name.starts_with("Inline") {1008 out.push_str("/// @dev inlined interface\n");995 out.push_str("/// @dev inlined interface\n");1009 }996 }1010 let _ = interface.format(is_impl, &mut out, tc);997 let _ = interface.format(is_impl, &mut out, tc);1011 tc.collect(out);998 tc.collect(out);999 #(1000 #solidity_event_generators1001 )*1002 #(1003 #solidity_generators1004 )*1005 if is_impl {1006 tc.collect("/// @dev common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());1007 } else {1008 tc.collect("/// @dev common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());1009 }1012 }1010 }1013 }1011 }1014 impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {1012 impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {crates/evm-coder/src/solidity.rsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Implementation detail of [`evm_coder::solidity_interface`] macro code-generation161817#[cfg(not(feature = "std"))]19#[cfg(not(feature = "std"))]18use alloc::{20use alloc::{19 string::String,21 string::String,20 vec::Vec,22 vec::Vec,21 collections::{BTreeSet, BTreeMap},23 collections::BTreeMap,22 format,24 format,23};25};24#[cfg(feature = "std")]26#[cfg(feature = "std")]25use std::collections::{BTreeSet, BTreeMap};27use std::collections::BTreeMap;26use core::{28use core::{27 fmt::{self, Write},29 fmt::{self, Write},28 marker::PhantomData,30 marker::PhantomData,29 cell::{Cell, RefCell},31 cell::{Cell, RefCell},32 cmp::Reverse,30};33};31use impl_trait_for_tuples::impl_for_tuples;34use impl_trait_for_tuples::impl_for_tuples;32use crate::types::*;35use crate::types::*;333634#[derive(Default)]37#[derive(Default)]35pub struct TypeCollector {38pub struct TypeCollector {39 /// Code => id40 /// id ordering is required to perform topo-sort on the resulting data36 structs: RefCell<BTreeSet<string>>,41 structs: RefCell<BTreeMap<string, usize>>,37 anonymous: RefCell<BTreeMap<Vec<string>, usize>>,42 anonymous: RefCell<BTreeMap<Vec<string>, usize>>,38 id: Cell<usize>,43 id: Cell<usize>,39}44}42 Self::default()47 Self::default()43 }48 }44 pub fn collect(&self, item: string) {49 pub fn collect(&self, item: string) {50 let id = self.next_id();45 self.structs.borrow_mut().insert(item);51 self.structs.borrow_mut().insert(item, id);46 }52 }47 pub fn next_id(&self) -> usize {53 pub fn next_id(&self) -> usize {48 let v = self.id.get();54 let v = self.id.get();66 self.anonymous.borrow_mut().insert(names, id);72 self.anonymous.borrow_mut().insert(names, id);67 format!("Tuple{}", id)73 format!("Tuple{}", id)68 }74 }69 pub fn finish(self) -> BTreeSet<string> {75 pub fn finish(self) -> Vec<string> {70 self.structs.into_inner()76 let mut data = self.structs.into_inner().into_iter().collect::<Vec<_>>();77 data.sort_by_key(|(_, id)| Reverse(*id));78 data.into_iter().map(|(code, _)| code).collect()71 }79 }72}80}7381