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

difftreelog

refactor Make implementations of Abi* for EthCrossAccount via AbiCoder macro

Trubnikov Sergey2022-11-17parent: #0a8a6b0.patch.diff
in: master

9 files changed

modified.maintain/scripts/generate_abi.shdiffbeforeafterboth
4dir=$PWD4dir=$PWD
55
6tmp=$(mktemp -d)6tmp=$(mktemp -d)
7echo "Tmp file: $tmp/input.sol"
7cd $tmp8cd $tmp
8cp $dir/$INPUT input.sol9cp $dir/$INPUT input.sol
9solcjs --abi -p input.sol10solcjs --abi -p input.sol
modifiedcrates/evm-coder/src/abi/impls.rsdiffbeforeafterboth
120 }120 }
121}121}
122
123impl sealed::CanBePlacedInVec for EthCrossAccount {}
124
125impl AbiType for EthCrossAccount {
126 const SIGNATURE: SignatureUnit = make_signature!(new fixed("(address,uint256)"));
127
128 fn is_dynamic() -> bool {
129 address::is_dynamic() || uint256::is_dynamic()
130 }
131
132 fn size() -> usize {
133 <address as AbiType>::size() + <uint256 as AbiType>::size()
134 }
135}
136
137impl AbiRead for EthCrossAccount {
138 fn abi_read(reader: &mut AbiReader) -> Result<EthCrossAccount> {
139 let size = if !EthCrossAccount::is_dynamic() {
140 Some(<EthCrossAccount as AbiType>::size())
141 } else {
142 None
143 };
144 let mut subresult = reader.subresult(size)?;
145 let eth = <address>::abi_read(&mut subresult)?;
146 let sub = <uint256>::abi_read(&mut subresult)?;
147
148 Ok(EthCrossAccount { eth, sub })
149 }
150}
151
152impl AbiWrite for EthCrossAccount {
153 fn abi_write(&self, writer: &mut AbiWriter) {
154 self.eth.abi_write(writer);
155 self.sub.abi_write(writer);
156 }
157}
158122
159impl sealed::CanBePlacedInVec for Property {}123impl sealed::CanBePlacedInVec for Property {}
160124
modifiedcrates/evm-coder/src/lib.rsdiffbeforeafterboth
93pub use evm_coder_procedural::solidity;93pub use evm_coder_procedural::solidity;
94/// See [`solidity_interface`]94/// See [`solidity_interface`]
95pub use evm_coder_procedural::weight;95pub use evm_coder_procedural::weight;
96pub use evm_coder_procedural::AbiCoder;
96pub use sha3_const;97pub use sha3_const;
9798
98/// Derives [`ToLog`] for enum99/// Derives [`ToLog`] for enum
119120
120 #[cfg(not(feature = "std"))]121 #[cfg(not(feature = "std"))]
121 use alloc::{vec::Vec};122 use alloc::{vec::Vec};
122 use pallet_evm::account::CrossAccountId;
123 use primitive_types::{U256, H160, H256};123 use primitive_types::{U256, H160, H256};
124124
125 pub type address = H160;125 pub type address = H160;
188 }188 }
189 }189 }
190
191 #[derive(Debug, Default)]
192 pub struct EthCrossAccount {
193 pub(crate) eth: address,
194 pub(crate) sub: uint256,
195 }
196
197 impl EthCrossAccount {
198 pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self
199 where
200 T: pallet_evm::Config,
201 T::AccountId: AsRef<[u8; 32]>,
202 {
203 if cross_account_id.is_canonical_substrate() {
204 Self {
205 eth: Default::default(),
206 sub: convert_cross_account_to_uint256::<T>(cross_account_id),
207 }
208 } else {
209 Self {
210 eth: *cross_account_id.as_eth(),
211 sub: Default::default(),
212 }
213 }
214 }
215
216 pub fn into_sub_cross_account<T>(&self) -> crate::execution::Result<T::CrossAccountId>
217 where
218 T: pallet_evm::Config,
219 T::AccountId: From<[u8; 32]>,
220 {
221 if self.eth == Default::default() && self.sub == Default::default() {
222 Err("All fields of cross account is zeroed".into())
223 } else if self.eth == Default::default() {
224 Ok(convert_uint256_to_cross_account::<T>(self.sub))
225 } else if self.sub == Default::default() {
226 Ok(T::CrossAccountId::from_eth(self.eth))
227 } else {
228 Err("All fields of cross account is non zeroed".into())
229 }
230 }
231 }
232
233 /// Convert `CrossAccountId` to `uint256`.
234 pub fn convert_cross_account_to_uint256<T: pallet_evm::Config>(
235 from: &T::CrossAccountId,
236 ) -> uint256
237 where
238 T::AccountId: AsRef<[u8; 32]>,
239 {
240 let slice = from.as_sub().as_ref();
241 uint256::from_big_endian(slice)
242 }
243
244 /// Convert `uint256` to `CrossAccountId`.
245 pub fn convert_uint256_to_cross_account<T: pallet_evm::Config>(
246 from: uint256,
247 ) -> T::CrossAccountId
248 where
249 T::AccountId: From<[u8; 32]>,
250 {
251 let mut new_admin_arr = [0_u8; 32];
252 from.to_big_endian(&mut new_admin_arr);
253 let account_id = T::AccountId::from(new_admin_arr);
254 T::CrossAccountId::from_sub(account_id)
255 }
256190
257 #[derive(Debug, Default)]191 #[derive(Debug, Default)]
258 pub struct Property {192 pub struct Property {
modifiedcrates/evm-coder/src/solidity.rsdiffbeforeafterboth
145 }145 }
146}146}
147147
148mod sealed {148pub mod sealed {
149 /// Not every type should be directly placed in vec.149 /// Not every type should be directly placed in vec.
150 /// Vec encoding is not memory efficient, as every item will be padded150 /// Vec encoding is not memory efficient, as every item will be padded
151 /// to 32 bytes.151 /// to 32 bytes.
156impl sealed::CanBePlacedInVec for uint256 {}156impl sealed::CanBePlacedInVec for uint256 {}
157impl sealed::CanBePlacedInVec for string {}157impl sealed::CanBePlacedInVec for string {}
158impl sealed::CanBePlacedInVec for address {}158impl sealed::CanBePlacedInVec for address {}
159impl sealed::CanBePlacedInVec for EthCrossAccount {}
160impl sealed::CanBePlacedInVec for Property {}159impl sealed::CanBePlacedInVec for Property {}
161160
162impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {161impl<T: SolidityTypeName + sealed::CanBePlacedInVec> SolidityTypeName for Vec<T> {
174 }173 }
175}174}
176
177impl SolidityTupleType for EthCrossAccount {
178 fn names(tc: &TypeCollector) -> Vec<string> {
179 let mut collected = Vec::with_capacity(Self::len());
180 {
181 let mut out = string::new();
182 address::solidity_name(&mut out, tc).expect("no fmt error");
183 collected.push(out);
184 }
185 {
186 let mut out = string::new();
187 uint256::solidity_name(&mut out, tc).expect("no fmt error");
188 collected.push(out);
189 }
190 collected
191 }
192
193 fn len() -> usize {
194 2
195 }
196}
197
198impl SolidityTypeName for EthCrossAccount {
199 fn solidity_name(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {
200 write!(writer, "{}", tc.collect_struct::<Self>())
201 }
202
203 fn is_simple() -> bool {
204 false
205 }
206
207 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {
208 write!(writer, "{}(", tc.collect_struct::<Self>())?;
209 address::solidity_default(writer, tc)?;
210 write!(writer, ",")?;
211 uint256::solidity_default(writer, tc)?;
212 write!(writer, ")")
213 }
214}
215
216impl StructCollect for EthCrossAccount {
217 fn name() -> String {
218 "EthCrossAccount".into()
219 }
220
221 fn declaration() -> String {
222 let mut str = String::new();
223 writeln!(str, "/// @dev Cross account struct").unwrap();
224 writeln!(str, "struct {} {{", Self::name()).unwrap();
225 writeln!(str, "\taddress eth;").unwrap();
226 writeln!(str, "\tuint256 sub;").unwrap();
227 writeln!(str, "}}").unwrap();
228 str
229 }
230}
231175
232impl StructCollect for Property {176impl StructCollect for Property {
233 fn name() -> String {177 fn name() -> String {
modifiedpallets/common/src/erc.rsdiffbeforeafterboth
1616
17//! This module contains the implementation of pallet methods for evm.17//! This module contains the implementation of pallet methods for evm.
1818
19use evm_coder::{
20 abi::AbiType,
21 solidity_interface, solidity, ToLog,
22 types::*,
23 types::Property as PropertyStruct,
24 execution::{Result, Error},
25 weight,
26};
27pub use pallet_evm::{PrecompileOutput, PrecompileResult, PrecompileHandle, account::CrossAccountId};19pub use pallet_evm::{PrecompileOutput, PrecompileResult, PrecompileHandle, account::CrossAccountId};
20use evm_coder::{
21 abi::AbiType,
22 solidity_interface, solidity, ToLog,
23 types::*,
24 types::Property as PropertyStruct,
25 execution::{Result, Error},
26 weight,
27};
28use pallet_evm_coder_substrate::dispatch_to_evm;28use pallet_evm_coder_substrate::dispatch_to_evm;
29use sp_std::vec::Vec;29use sp_std::vec::Vec;
30use up_data_structs::{30use up_data_structs::{
3535
36use crate::{36use crate::{
37 Pallet, CollectionHandle, Config, CollectionProperties, SelfWeightOf,37 Pallet, CollectionHandle, Config, CollectionProperties, SelfWeightOf,
38 eth::convert_cross_account_to_uint256, weights::WeightInfo,38 eth::{EthCrossAccount, convert_cross_account_to_uint256},
39 weights::WeightInfo,
39};40};
4041
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
1616
17//! The module contains a number of functions for converting and checking ethereum identifiers.17//! The module contains a number of functions for converting and checking ethereum identifiers.
1818
19use evm_coder::types::{uint256, address};19use evm_coder::{
20 AbiCoder,
21 types::{uint256, address},
22};
20pub use pallet_evm::{Config, account::CrossAccountId};23pub use pallet_evm::{Config, account::CrossAccountId};
21use sp_core::H160;24use sp_core::H160;
22use up_data_structs::CollectionId;25use up_data_structs::CollectionId;
110 }113 }
111}114}
115
116#[derive(Debug, Default, AbiCoder)]
117pub struct EthCrossAccount {
118 pub(crate) eth: address,
119 pub(crate) sub: uint256,
120}
121
122impl EthCrossAccount {
123 pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self
124 where
125 T: pallet_evm::account::Config,
126 T::AccountId: AsRef<[u8; 32]>,
127 {
128 if cross_account_id.is_canonical_substrate() {
129 Self {
130 eth: Default::default(),
131 sub: convert_cross_account_to_uint256::<T>(cross_account_id),
132 }
133 } else {
134 Self {
135 eth: *cross_account_id.as_eth(),
136 sub: Default::default(),
137 }
138 }
139 }
140
141 pub fn into_sub_cross_account<T>(&self) -> evm_coder::execution::Result<T::CrossAccountId>
142 where
143 T: pallet_evm::account::Config,
144 T::AccountId: From<[u8; 32]>,
145 {
146 if self.eth == Default::default() && self.sub == Default::default() {
147 Err("All fields of cross account is zeroed".into())
148 } else if self.eth == Default::default() {
149 Ok(convert_uint256_to_cross_account::<T>(self.sub))
150 } else if self.sub == Default::default() {
151 Ok(T::CrossAccountId::from_eth(self.eth))
152 } else {
153 Err("All fields of cross account is non zeroed".into())
154 }
155 }
156}
157
158impl ::evm_coder::solidity::sealed::CanBePlacedInVec for EthCrossAccount {}
159impl ::evm_coder::solidity::SolidityTupleType for EthCrossAccount {
160 fn names(tc: &::evm_coder::solidity::TypeCollector) -> Vec<String> {
161 let mut collected =
162 Vec::with_capacity(<Self as ::evm_coder::solidity::SolidityTupleType>::len());
163 {
164 let mut out = String::new();
165 <address as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
166 .expect("no fmt error");
167 collected.push(out);
168 }
169 {
170 let mut out = String::new();
171 <uint256 as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
172 .expect("no fmt error");
173 collected.push(out);
174 }
175 collected
176 }
177
178 fn len() -> usize {
179 2
180 }
181}
182impl ::evm_coder::solidity::SolidityTypeName for EthCrossAccount {
183 fn solidity_name(
184 writer: &mut impl ::core::fmt::Write,
185 tc: &::evm_coder::solidity::TypeCollector,
186 ) -> ::core::fmt::Result {
187 write!(writer, "{}", tc.collect_struct::<Self>())
188 }
189
190 fn is_simple() -> bool {
191 false
192 }
193
194 fn solidity_default(
195 writer: &mut impl ::core::fmt::Write,
196 tc: &::evm_coder::solidity::TypeCollector,
197 ) -> ::core::fmt::Result {
198 write!(writer, "{}(", tc.collect_struct::<Self>())?;
199 address::solidity_default(writer, tc)?;
200 write!(writer, ",")?;
201 uint256::solidity_default(writer, tc)?;
202 write!(writer, ")")
203 }
204}
205
206impl ::evm_coder::solidity::StructCollect for EthCrossAccount {
207 fn name() -> String {
208 "EthCrossAccount".into()
209 }
210
211 fn declaration() -> String {
212 use std::fmt::Write;
213
214 let mut str = String::new();
215 writeln!(str, "/// @dev Cross account struct").unwrap();
216 writeln!(str, "struct {} {{", Self::name()).unwrap();
217 writeln!(str, "\taddress eth;").unwrap();
218 writeln!(str, "\tuint256 sub;").unwrap();
219 writeln!(str, "}}").unwrap();
220 str
221 }
222}
112223
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
24 weight,24 weight,
25};25};
26use up_data_structs::CollectionMode;26use up_data_structs::CollectionMode;
27use pallet_common::erc::{CommonEvmHandler, PrecompileResult};27use pallet_common::{
28 CollectionHandle,
29 erc::{CommonEvmHandler, PrecompileResult, CollectionCall},
30 eth::EthCrossAccount,
31};
28use sp_std::vec::Vec;32use sp_std::vec::Vec;
29use pallet_evm::{account::CrossAccountId, PrecompileHandle};33use pallet_evm::{account::CrossAccountId, PrecompileHandle};
30use pallet_evm_coder_substrate::{call, dispatch_to_evm};34use pallet_evm_coder_substrate::{call, dispatch_to_evm};
31use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};35use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
32use pallet_common::{CollectionHandle, erc::CollectionCall};
33use sp_core::Get;36use sp_core::Get;
3437
35use crate::{38use crate::{
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
36use pallet_evm_coder_substrate::dispatch_to_evm;36use pallet_evm_coder_substrate::dispatch_to_evm;
37use sp_std::vec::Vec;37use sp_std::vec::Vec;
38use pallet_common::{38use pallet_common::{
39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,
39 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},
40 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,41 eth::EthCrossAccount,
41};42};
42use pallet_evm::{account::CrossAccountId, PrecompileHandle};43use pallet_evm::{account::CrossAccountId, PrecompileHandle};
43use pallet_evm_coder_substrate::call;44use pallet_evm_coder_substrate::call;
modifiedpallets/refungible/src/erc.rsdiffbeforeafterboth
33use pallet_common::{33use pallet_common::{
34 CollectionHandle, CollectionPropertyPermissions,34 CollectionHandle, CollectionPropertyPermissions,
35 erc::{CommonEvmHandler, CollectionCall, static_property::key},35 erc::{CommonEvmHandler, CollectionCall, static_property::key},
36 eth::EthCrossAccount,
36 CommonCollectionOperations,37 CommonCollectionOperations,
37};38};
38use pallet_evm::{account::CrossAccountId, PrecompileHandle};39use pallet_evm::{account::CrossAccountId, PrecompileHandle};