git.delta.rocks / unique-network / refs/commits / 068fc8208402

difftreelog

refactor Abi impls

Trubnikov Sergey2022-11-18parent: #e2559ac.patch.diff
in: master

5 files changed

modifiedcrates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth
121 #(121 #(
122 let #field_names = {122 let #field_names = {
123 let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;123 let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;
124 if !is_dynamic {subresult.seek(<#field_types as ::evm_coder::abi::AbiType>::size())};124 if !is_dynamic {subresult.bytes_read(<#field_types as ::evm_coder::abi::AbiType>::size())};
125 value125 value
126 };126 };
127 )*127 )*
modifiedcrates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth
406 quote! {406 quote! {
407 #name: {407 #name: {
408 let value = <#ty as ::evm_coder::abi::AbiRead>::abi_read(reader)?;408 let value = <#ty as ::evm_coder::abi::AbiRead>::abi_read(reader)?;
409 if !is_dynamic {reader.seek(<#ty as ::evm_coder::abi::AbiType>::size())};409 if !is_dynamic {reader.bytes_read(<#ty as ::evm_coder::abi::AbiType>::size())};
410 value410 value
411 }411 }
412 }412 }
modifiedcrates/evm-coder/src/abi/impls.rsdiffbeforeafterboth
10#[cfg(not(feature = "std"))]10#[cfg(not(feature = "std"))]
11use alloc::vec::Vec;11use alloc::vec::Vec;
1212
13macro_rules! impl_abi_readable {13macro_rules! impl_abi_type {
14 ($ty:ty, $method:ident, $dynamic:literal) => {14 ($ty:ty, $name:ident, $dynamic:literal) => {
15 impl sealed::CanBePlacedInVec for $ty {}15 impl sealed::CanBePlacedInVec for $ty {}
1616
17 impl AbiType for $ty {17 impl AbiType for $ty {
18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($ty)));18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));
1919
20 fn is_dynamic() -> bool {20 fn is_dynamic() -> bool {
21 $dynamic21 $dynamic
26 }26 }
27 }27 }
28
29 impl AbiRead for $ty {
30 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {
31 reader.$method()
32 }
33 }
34 };28 };
35}29}
3630
31macro_rules! impl_abi_readable {
32 ($ty:ty, $method:ident) => {
33 impl AbiRead for $ty {
34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {
35 reader.$method()
36 }
37 }
38 };
39}
40
41macro_rules! impl_abi_writeable {
42 ($ty:ty, $method:ident) => {
43 impl AbiWrite for $ty {
44 fn abi_write(&self, writer: &mut AbiWriter) {
45 writer.$method(&self)
46 }
47 }
48 };
49}
50
51macro_rules! impl_abi {
52 ($ty:ty, $method:ident, $dynamic:literal) => {
53 impl_abi_type!($ty, $method, $dynamic);
37impl_abi_readable!(uint32, uint32, false);54 impl_abi_readable!($ty, $method);
55 impl_abi_writeable!($ty, $method);
56 };
57}
58
38impl_abi_readable!(uint64, uint64, false);59impl_abi!(bool, bool, false);
39impl_abi_readable!(uint128, uint128, false);60impl_abi!(u8, uint8, false);
40impl_abi_readable!(uint256, uint256, false);61impl_abi!(u32, uint32, false);
41impl_abi_readable!(bytes4, bytes4, false);62impl_abi!(u64, uint64, false);
42impl_abi_readable!(address, address, false);63impl_abi!(u128, uint128, false);
43impl_abi_readable!(string, string, true);64impl_abi!(U256, uint256, false);
44
45impl sealed::CanBePlacedInVec for bool {}
46
47impl AbiType for bool {
48 const SIGNATURE: SignatureUnit = make_signature!(new fixed("bool"));65impl_abi!(H160, address, false);
49
50 fn is_dynamic() -> bool {
51 false
52 }
53 fn size() -> usize {
54 ABI_ALIGNMENT
55 }
56}
57impl AbiRead for bool {
58 fn abi_read(reader: &mut AbiReader) -> Result<bool> {
59 reader.bool()
60 }
61}
62
63impl AbiType for uint8 {
64 const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));66impl_abi!(string, string, true);
6567
66 fn is_dynamic() -> bool {
67 false
68 }
69 fn size() -> usize {
70 ABI_ALIGNMENT
71 }
72}
73impl AbiRead for uint8 {
74 fn abi_read(reader: &mut AbiReader) -> Result<uint8> {68impl_abi_writeable!(&str, string);
75 reader.uint8()69
76 }
77}
78
79impl AbiType for bytes {
80 const SIGNATURE: SignatureUnit = make_signature!(new fixed("bytes"));70impl_abi_type!(bytes, bytes, true);
8171
82 fn is_dynamic() -> bool {
83 true
84 }
85 fn size() -> usize {
86 ABI_ALIGNMENT
87 }
88}
89impl AbiRead for bytes {72impl AbiRead for bytes {
90 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {
91 Ok(bytes(reader.bytes()?))74 Ok(bytes(reader.bytes()?))
92 }75 }
93}76}
77
78impl AbiWrite for bytes {
79 fn abi_write(&self, writer: &mut AbiWriter) {
80 writer.bytes(self.0.as_slice())
81 }
82}
83
84impl_abi_type!(bytes4, bytes4, false);
85impl AbiRead for bytes4 {
86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {
87 reader.bytes4()
88 }
89}
9490
95impl<R: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {91impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {
96 fn abi_read(reader: &mut AbiReader) -> Result<Vec<R>> {92 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {
97 let mut sub = reader.subresult(None)?;93 let mut sub = reader.subresult(None)?;
98 let size = sub.uint32()? as usize;94 let size = sub.uint32()? as usize;
99 sub.subresult_offset = sub.offset;95 sub.subresult_offset = sub.offset;
96 let is_dynamic = <T as AbiType>::is_dynamic();
100 let mut out = Vec::with_capacity(size);97 let mut out = Vec::with_capacity(size);
101 for _ in 0..size {98 for _ in 0..size {
102 out.push(<R>::abi_read(&mut sub)?);99 out.push(<T as AbiRead>::abi_read(&mut sub)?);
103 if !<R>::is_dynamic() {100 if !is_dynamic {
104 sub.subresult_offset += <R>::size()101 sub.bytes_read(<T as AbiType>::size());
105 };102 };
106 }103 }
107 Ok(out)104 Ok(out)
108 }105 }
109}106}
110107
111impl<R: AbiType> AbiType for Vec<R> {108impl<T: AbiType> AbiType for Vec<T> {
112 const SIGNATURE: SignatureUnit = make_signature!(new nameof(R::SIGNATURE) fixed("[]"));109 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
113110
114 fn is_dynamic() -> bool {111 fn is_dynamic() -> bool {
115 true112 true
155 }152 }
156}153}
157
158macro_rules! impl_abi_writeable {
159 ($ty:ty, $method:ident) => {
160 impl AbiWrite for $ty {
161 fn abi_write(&self, writer: &mut AbiWriter) {
162 writer.$method(&self)
163 }
164 }
165 };
166}
167
168impl_abi_writeable!(u8, uint8);
169impl_abi_writeable!(u32, uint32);
170impl_abi_writeable!(u128, uint128);
171impl_abi_writeable!(U256, uint256);
172impl_abi_writeable!(H160, address);
173impl_abi_writeable!(bool, bool);
174impl_abi_writeable!(&str, string);
175
176impl AbiWrite for string {
177 fn abi_write(&self, writer: &mut AbiWriter) {
178 writer.string(self)
179 }
180}
181
182impl AbiWrite for bytes {
183 fn abi_write(&self, writer: &mut AbiWriter) {
184 writer.bytes(self.0.as_slice())
185 }
186}
187154
188impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {155impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {
189 fn abi_write(&self, writer: &mut AbiWriter) {156 fn abi_write(&self, writer: &mut AbiWriter) {
272 Ok((239 Ok((
273 $({240 $({
274 let value = <$ident>::abi_read(&mut subresult)?;241 let value = <$ident>::abi_read(&mut subresult)?;
275 if !is_dynamic {subresult.seek(<$ident as AbiType>::size())};242 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};
276 value243 value
277 },)+244 },)+
278 ))245 ))
modifiedcrates/evm-coder/src/abi/mod.rsdiffbeforeafterboth
208 }208 }
209209
210 /// Notify about readed data portion.210 /// Notify about readed data portion.
211 pub fn seek(&mut self, size: usize) {211 pub fn bytes_read(&mut self, size: usize) {
212 self.subresult_offset += size;212 self.subresult_offset += size;
213 }213 }
214214
281 self.write_padleft(&u32::to_be_bytes(*value))281 self.write_padleft(&u32::to_be_bytes(*value))
282 }282 }
283
284 /// Write [`u64`] to end of buffer
285 pub fn uint64(&mut self, value: &u64) {
286 self.write_padleft(&u64::to_be_bytes(*value))
287 }
283288
284 /// Write [`u128`] to end of buffer289 /// Write [`u128`] to end of buffer
285 pub fn uint128(&mut self, value: &u128) {290 pub fn uint128(&mut self, value: &u128) {
modifiedcrates/evm-coder/src/abi/test.rsdiffbeforeafterboth
47 test_impl_uint!(uint32);47 test_impl_uint!(uint32);
48}48}
49
50#[test]
51fn encode_decode_uint64() {
52 test_impl_uint!(uint64);
53}
4954
50#[test]55#[test]
51fn encode_decode_uint128() {56fn encode_decode_uint128() {
52 test_impl_uint!(uint128);57 test_impl_uint!(uint128);
53}58}
59
60#[test]
61fn encode_decode_bool_true() {
62 test_impl::<bool>(
63 0xdeadbeef,
64 true,
65 &hex!(
66 "
67 deadbeef
68 0000000000000000000000000000000000000000000000000000000000000001
69 "
70 ),
71 );
72}
73
74#[test]
75fn encode_decode_bool_false() {
76 test_impl::<bool>(
77 0xdeadbeef,
78 false,
79 &hex!(
80 "
81 deadbeef
82 0000000000000000000000000000000000000000000000000000000000000000
83 "
84 ),
85 );
86}
5487
55#[test]88#[test]
56fn encode_decode_uint256() {89fn encode_decode_uint256() {