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
--- a/crates/evm-coder/procedural/src/abi_derive.rs
+++ b/crates/evm-coder/procedural/src/abi_derive.rs
@@ -121,7 +121,7 @@
 				#(
 					let #field_names = {
 						let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;
-						if !is_dynamic {subresult.seek(<#field_types as ::evm_coder::abi::AbiType>::size())};
+						if !is_dynamic {subresult.bytes_read(<#field_types as ::evm_coder::abi::AbiType>::size())};
 						value
 					};
 				)*
modifiedcrates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder/procedural/src/solidity_interface.rs
+++ b/crates/evm-coder/procedural/src/solidity_interface.rs
@@ -406,7 +406,7 @@
 		quote! {
 			#name: {
 				let value = <#ty as ::evm_coder::abi::AbiRead>::abi_read(reader)?;
-				if !is_dynamic {reader.seek(<#ty as ::evm_coder::abi::AbiType>::size())};
+				if !is_dynamic {reader.bytes_read(<#ty as ::evm_coder::abi::AbiType>::size())};
 				value
 			}
 		}
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
--- a/crates/evm-coder/src/abi/mod.rs
+++ b/crates/evm-coder/src/abi/mod.rs
@@ -208,7 +208,7 @@
 	}
 
 	/// Notify about readed data portion.
-	pub fn seek(&mut self, size: usize) {
+	pub fn bytes_read(&mut self, size: usize) {
 		self.subresult_offset += size;
 	}
 
@@ -281,6 +281,11 @@
 		self.write_padleft(&u32::to_be_bytes(*value))
 	}
 
+	/// Write [`u64`] to end of buffer
+	pub fn uint64(&mut self, value: &u64) {
+		self.write_padleft(&u64::to_be_bytes(*value))
+	}
+
 	/// Write [`u128`] to end of buffer
 	pub fn uint128(&mut self, value: &u128) {
 		self.write_padleft(&u128::to_be_bytes(*value))
modifiedcrates/evm-coder/src/abi/test.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi/test.rs
+++ b/crates/evm-coder/src/abi/test.rs
@@ -48,11 +48,44 @@
 }
 
 #[test]
+fn encode_decode_uint64() {
+	test_impl_uint!(uint64);
+}
+
+#[test]
 fn encode_decode_uint128() {
 	test_impl_uint!(uint128);
 }
 
 #[test]
+fn encode_decode_bool_true() {
+	test_impl::<bool>(
+		0xdeadbeef,
+		true,
+		&hex!(
+			"
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000001
+            "
+		),
+	);
+}
+
+#[test]
+fn encode_decode_bool_false() {
+	test_impl::<bool>(
+		0xdeadbeef,
+		false,
+		&hex!(
+			"
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000000
+            "
+		),
+	);
+}
+
+#[test]
 fn encode_decode_uint256() {
 	test_impl::<uint256>(
 		0xdeadbeef,