Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sol-types): add empty bytes and string specialization #435

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/sol-types/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,24 @@ pub use decoder::{decode, decode_params, decode_sequence, Decoder};

pub mod token;
pub use token::{Token, TokenSeq};

/// The ABI encoding of an empty byte array (`bytes` or `string`).
pub const EMPTY_BYTES: &[u8; 64] = &alloy_primitives::hex!(
"0000000000000000000000000000000000000000000000000000000000000020" // offset, points to the next word
"0000000000000000000000000000000000000000000000000000000000000000" // length, 0
);

#[cfg(test)]
mod tests {
use super::*;
use crate::{SolType, SolValue};

#[test]
fn empty_bytes() {
assert_eq!(EMPTY_BYTES.len(), 64);
assert_eq!(EMPTY_BYTES[..], crate::sol_data::String::abi_encode("")[..]);
assert_eq!(EMPTY_BYTES[..], crate::sol_data::Bytes::abi_encode(b"")[..]);
assert_eq!(EMPTY_BYTES[..], "".abi_encode());
assert_eq!(EMPTY_BYTES[..], b"".abi_encode());
}
}
82 changes: 77 additions & 5 deletions crates/sol-types/src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,8 @@ impl_sol_value! {
[] Function => sol_data::Function [];
[const N: usize] FixedBytes<N> => sol_data::FixedBytes<N> [where ByteCount<N>: SupportedFixedBytes];
[const N: usize] [u8; N] => sol_data::FixedBytes<N> [where ByteCount<N>: SupportedFixedBytes];
[] String => sol_data::String [];
[] str => sol_data::String [];
[] Bytes => sol_data::Bytes [];

[] Vec<u8> => sol_data::Bytes [];
[] [u8] => sol_data::Bytes [];
// `bytes` and `string` are specialized below.

// Generic
[T: SolValue] Vec<T> => sol_data::Array<T::SolType> [];
Expand All @@ -225,6 +221,60 @@ impl SolValue for () {

all_the_tuples!(tuple_impls);

// Empty `bytes` and `string` specialization
impl SolValue for str {
type SolType = sol_data::String;

#[inline]
fn abi_encode(&self) -> Vec<u8> {
if self.is_empty() {
crate::abi::EMPTY_BYTES.to_vec()
} else {
<Self::SolType as SolType>::abi_encode(self)
}
}
}

impl SolValue for [u8] {
type SolType = sol_data::Bytes;

#[inline]
fn abi_encode(&self) -> Vec<u8> {
if self.is_empty() {
crate::abi::EMPTY_BYTES.to_vec()
} else {
<Self::SolType as SolType>::abi_encode(self)
}
}
}

impl SolValue for String {
type SolType = sol_data::String;

#[inline]
fn abi_encode(&self) -> Vec<u8> {
self[..].abi_encode()
}
}

impl SolValue for Bytes {
type SolType = sol_data::Bytes;

#[inline]
fn abi_encode(&self) -> Vec<u8> {
self[..].abi_encode()
}
}

impl SolValue for Vec<u8> {
type SolType = sol_data::Bytes;

#[inline]
fn abi_encode(&self) -> Vec<u8> {
self[..].abi_encode()
}
}

#[cfg(test)]
#[allow(clippy::type_complexity)]
mod tests {
Expand Down Expand Up @@ -396,4 +446,26 @@ mod tests {
let _: Result<(i64, Vec<(u32, String, Vec<FixedBytes<4>>)>, U256)> =
<(i64, Vec<(u32, String, Vec<FixedBytes<4>>)>, U256)>::abi_decode(b"", false);
}

#[test]
fn empty_spec() {
assert_eq!("".abi_encode(), crate::abi::EMPTY_BYTES);
assert_eq!(b"".abi_encode(), crate::abi::EMPTY_BYTES);
assert_eq!(
("", "a").abi_encode(),
<(sol_data::String, sol_data::String)>::abi_encode(&("", "a"))
);
assert_eq!(
("a", "").abi_encode(),
<(sol_data::String, sol_data::String)>::abi_encode(&("a", ""))
);
assert_eq!(
(&b""[..], &b"a"[..]).abi_encode(),
<(sol_data::Bytes, sol_data::Bytes)>::abi_encode(&(b"", b"a"))
);
assert_eq!(
(&b"a"[..], &b""[..]).abi_encode(),
<(sol_data::Bytes, sol_data::Bytes)>::abi_encode(&(b"a", b""))
);
}
}
Loading