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): rm validate: bool #863

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions crates/dyn-abi/benches/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ fn sol_types_decode(c: &mut Criterion) {

g.bench_function("word", |b| {
let input = decode_word_input();
b.iter(|| sol_data::Uint::<256>::abi_decode(black_box(&input), false).unwrap());
b.iter(|| sol_data::Uint::<256>::abi_decode(black_box(&input)).unwrap());
});

g.bench_function("dynamic", |b| {
let input = decode_dynamic_input();
b.iter(|| sol_data::String::abi_decode(black_box(&input), false).unwrap());
b.iter(|| sol_data::String::abi_decode(black_box(&input)).unwrap());
});

g.finish();
Expand Down
16 changes: 6 additions & 10 deletions crates/dyn-abi/src/dynamic/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl DynSolCall {
}

/// ABI decode the given data as function returns.
pub fn abi_decode_input(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.parameters, validate)
pub fn abi_decode_input(&self, data: &[u8], _validate: bool) -> Result<Vec<DynSolValue>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about here?

abi_decode(data, &self.parameters)
}

/// ABI encode the given values as function return values.
Expand Down Expand Up @@ -109,8 +109,8 @@ impl DynSolReturns {
}

/// ABI decode the given data as function return values.
pub fn abi_decode_output(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, self.types(), validate)
pub fn abi_decode_output(&self, data: &[u8], _validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, self.types())
}
}

Expand Down Expand Up @@ -146,13 +146,9 @@ pub(crate) fn abi_encode(values: &[DynSolValue]) -> Vec<u8> {
DynSolValue::encode_seq(values)
}

pub(crate) fn abi_decode(
data: &[u8],
tys: &[DynSolType],
validate: bool,
) -> Result<Vec<DynSolValue>> {
pub(crate) fn abi_decode(data: &[u8], tys: &[DynSolType]) -> Result<Vec<DynSolValue>> {
let mut values = Vec::with_capacity(tys.len());
let mut decoder = Decoder::new(data, validate);
let mut decoder = Decoder::new(data);
for ty in tys {
let value = ty.abi_decode_inner(&mut decoder, crate::DynToken::decode_single_populate)?;
values.push(value);
Expand Down
24 changes: 20 additions & 4 deletions crates/dyn-abi/src/dynamic/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl DynSolType {
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn abi_decode(&self, data: &[u8]) -> Result<DynSolValue> {
self.abi_decode_inner(&mut Decoder::new(data, false), DynToken::decode_single_populate)
self.abi_decode_inner(&mut Decoder::new(data), DynToken::decode_single_populate)
}

/// Decode a [`DynSolValue`] from a byte slice. Fails if the value does not
Expand Down Expand Up @@ -555,7 +555,7 @@ impl DynSolType {
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
pub fn abi_decode_sequence(&self, data: &[u8]) -> Result<DynSolValue> {
self.abi_decode_inner(&mut Decoder::new(data, false), DynToken::decode_sequence_populate)
self.abi_decode_inner(&mut Decoder::new(data), DynToken::decode_sequence_populate)
}

/// Calculate the minimum number of ABI words necessary to encode this
Expand Down Expand Up @@ -1063,11 +1063,27 @@ re-enc: {re_enc}
.repeat(64);
let my_type: DynSolType = "uint256[][][][][][][][][][]".parse().unwrap();
let decoded = my_type.abi_decode(&hex::decode(payload).unwrap());
assert_eq!(decoded, Err(alloy_sol_types::Error::RecursionLimitExceeded(16).into()));
assert_eq!(
decoded,
Err(alloy_sol_types::Error::TypeCheckFail {
expected_type: "offset (usize)".into(),
data: "0000000000000000000000000000000000000000000a00000000000000000000"
.to_string()
}
.into())
);

let my_type: DynSolType = "bytes[][][][][][][][][][]".parse().unwrap();
let decoded = my_type.abi_decode(&hex::decode(payload).unwrap());
assert_eq!(decoded, Err(alloy_sol_types::Error::RecursionLimitExceeded(16).into()));
assert_eq!(
decoded,
Err(alloy_sol_types::Error::TypeCheckFail {
expected_type: "offset (usize)".into(),
data: "0000000000000000000000000000000000000000000a00000000000000000000"
.to_string()
}
.into())
);
}

// https://github.com/alloy-rs/core/issues/490
Expand Down
29 changes: 14 additions & 15 deletions crates/dyn-abi/src/ext/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait JsonAbiExt: Sealed {
///
/// This function will return an error if the decoded data does not match
/// the expected input types.
fn abi_decode_input(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>>;
fn abi_decode_input(&self, data: &[u8]) -> Result<Vec<DynSolValue>>;
}

/// Provide ABI encoding and decoding for the [`Function`] type.
Expand All @@ -79,7 +79,7 @@ pub trait FunctionExt: JsonAbiExt + Sealed {
/// ABI-decodes the given data according to this functions's output types.
///
/// This method does not check for any prefixes or selectors.
fn abi_decode_output(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>>;
fn abi_decode_output(&self, data: &[u8]) -> Result<Vec<DynSolValue>>;
}

impl JsonAbiExt for Constructor {
Expand All @@ -94,8 +94,8 @@ impl JsonAbiExt for Constructor {
}

#[inline]
fn abi_decode_input(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs, validate)
fn abi_decode_input(&self, data: &[u8]) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs)
}
}

Expand All @@ -111,8 +111,8 @@ impl JsonAbiExt for Error {
}

#[inline]
fn abi_decode_input(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs, validate)
fn abi_decode_input(&self, data: &[u8]) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs)
}
}

Expand All @@ -128,8 +128,8 @@ impl JsonAbiExt for Function {
}

#[inline]
fn abi_decode_input(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs, validate)
fn abi_decode_input(&self, data: &[u8]) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.inputs)
}
}

Expand All @@ -140,8 +140,8 @@ impl FunctionExt for Function {
}

#[inline]
fn abi_decode_output(&self, data: &[u8], validate: bool) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.outputs, validate)
fn abi_decode_output(&self, data: &[u8]) -> Result<Vec<DynSolValue>> {
abi_decode(data, &self.outputs)
}
}

Expand Down Expand Up @@ -180,9 +180,9 @@ fn abi_encode(values: &[DynSolValue]) -> Vec<u8> {
DynSolValue::encode_seq(values)
}

fn abi_decode(data: &[u8], params: &[Param], validate: bool) -> Result<Vec<DynSolValue>> {
fn abi_decode(data: &[u8], params: &[Param]) -> Result<Vec<DynSolValue>> {
let mut values = Vec::with_capacity(params.len());
let mut decoder = Decoder::new(data, validate);
let mut decoder = Decoder::new(data);
for param in params {
let ty = param.resolve()?;
let value = ty.abi_decode_inner(&mut decoder, crate::DynToken::decode_single_populate)?;
Expand Down Expand Up @@ -250,13 +250,12 @@ mod tests {

// decode
let response = U256::from(1u8).to_be_bytes_vec();
let decoded = func.abi_decode_output(&response, true).unwrap();
let decoded = func.abi_decode_output(&response).unwrap();
assert_eq!(decoded, [DynSolValue::Uint(U256::from(1u8), 256)]);

// Fail on wrong response type
let bad_response = Address::repeat_byte(3u8).to_vec();
assert!(func.abi_decode_output(&bad_response, true).is_err());
assert!(func.abi_decode_output(&bad_response, false).is_err());
assert!(func.abi_decode_output(&bad_response).is_err());
}

// https://github.com/foundry-rs/foundry/issues/7280
Expand Down
15 changes: 7 additions & 8 deletions crates/sol-macro-expander/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,11 @@ impl CallLikeExpander<'_> {
fn abi_decode_raw(
selector: [u8; 4],
data: &[u8],
validate: bool
)-> alloy_sol_types::Result<Self> {
static DECODE_SHIMS: &[fn(&[u8], bool) -> alloy_sol_types::Result<#name>] = &[
static DECODE_SHIMS: &[fn(&[u8]) -> alloy_sol_types::Result<#name>] = &[
#({
fn #sorted_variants(data: &[u8], validate: bool) -> alloy_sol_types::Result<#name> {
<#sorted_types as alloy_sol_types::#trait_>::abi_decode_raw(data, validate)
fn #sorted_variants(data: &[u8]) -> alloy_sol_types::Result<#name> {
<#sorted_types as alloy_sol_types::#trait_>::abi_decode_raw(data)
.map(#name::#sorted_variants)
}
#sorted_variants
Expand All @@ -738,7 +737,7 @@ impl CallLikeExpander<'_> {
));
};
// `SELECTORS` and `DECODE_SHIMS` have the same length and are sorted in the same order.
DECODE_SHIMS[idx](data, validate)
DECODE_SHIMS[idx](data)
}

#[inline]
Expand Down Expand Up @@ -794,7 +793,7 @@ impl CallLikeExpander<'_> {
match topics.first().copied() {
#(
Some(<#variants as alloy_sol_types::#trait_>::SIGNATURE_HASH) =>
#ret <#variants as alloy_sol_types::#trait_>::decode_raw_log(topics, data, validate)
#ret <#variants as alloy_sol_types::#trait_>::decode_raw_log(topics, data)
.map(Self::#variants),
)*
_ => { #ret_err }
Expand All @@ -805,7 +804,7 @@ impl CallLikeExpander<'_> {
let variants = events.iter().filter(|e| e.is_anonymous()).map(e_name);
quote! {
#(
if let Ok(res) = <#variants as alloy_sol_types::#trait_>::decode_raw_log(topics, data, validate) {
if let Ok(res) = <#variants as alloy_sol_types::#trait_>::decode_raw_log(topics, data) {
return Ok(Self::#variants(res));
}
)*
Expand Down Expand Up @@ -844,7 +843,7 @@ impl CallLikeExpander<'_> {
const NAME: &'static str = #name_s;
const COUNT: usize = #count;

fn decode_raw_log(topics: &[alloy_sol_types::Word], data: &[u8], validate: bool) -> alloy_sol_types::Result<Self> {
fn decode_raw_log(topics: &[alloy_sol_types::Word], data: &[u8]) -> alloy_sol_types::Result<Self> {
#non_anon_impl
#anon_impl
}
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro-expander/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
}

#[inline]
fn abi_decode_returns(data: &[u8], validate: bool) -> alloy_sol_types::Result<Self::Return> {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate).map(Into::into)
fn abi_decode_returns(data: &[u8]) -> alloy_sol_types::Result<Self::Return> {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data).map(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/doctests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn transfer() {
};

assert_eq!(data[..4], ERC20::transferCall::SELECTOR);
let decoded = ERC20::ERC20Calls::abi_decode(&data, true).unwrap();
let decoded = ERC20::ERC20Calls::abi_decode(&data).unwrap();
assert_eq!(decoded, ERC20::ERC20Calls::transfer(expected));
assert_eq!(decoded.abi_encode(), data);
}
2 changes: 1 addition & 1 deletion crates/sol-macro/doctests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn event_rlp_roundtrip() {
let rlp_decoded = Log::decode(&mut rlp_encoded.as_slice()).unwrap();
assert_eq!(rlp_decoded, rlpable_log.reserialize());

let decoded_log = MyEvent::decode_log(&rlp_decoded, true).unwrap();
let decoded_log = MyEvent::decode_log(&rlp_decoded).unwrap();

assert_eq!(decoded_log, rlpable_log)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/doctests/function_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn error() {
"0000000000000000000000000000000000000000000000000000000000000002"
);
assert_eq!(
MyError::abi_decode_raw(&call_data, true),
MyError::abi_decode_raw(&call_data),
Ok(MyError { a: U256::from(1), b: U256::from(2) })
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ assert_eq!(&MySolType::sol_type_name(), "bool[2]");

// SolTypes are used to transform Rust into ABI blobs, and back.
let encoded: Vec<u8> = MySolType::abi_encode(&data);
let decoded: [bool; 2] = MySolType::abi_decode(&encoded, validate)?;
let decoded: [bool; 2] = MySolType::abi_decode(&encoded)?;
assert_eq!(data, decoded);

// This is more easily done with the `SolValue` trait:
let encoded: Vec<u8> = data.abi_encode();
let decoded: [bool; 2] = <[bool; 2]>::abi_decode(&encoded, validate)?;
let decoded: [bool; 2] = <[bool; 2]>::abi_decode(&encoded)?;
assert_eq!(data, decoded);
# Ok::<_, alloy_sol_types::Error>(())
```
Expand Down
Loading