From a7406aa1e15aa2023f1e520821c52f2939caa715 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:16:33 +0200 Subject: [PATCH] fix(sol-macro): overridden event signatures --- crates/sol-macro-expander/Cargo.toml | 2 +- crates/sol-macro-expander/src/expand/event.rs | 2 +- crates/sol-macro-expander/src/expand/mod.rs | 4 +- crates/sol-types/tests/macros/sol/mod.rs | 46 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/sol-macro-expander/Cargo.toml b/crates/sol-macro-expander/Cargo.toml index 5c8b144a22..ad1593fadd 100644 --- a/crates/sol-macro-expander/Cargo.toml +++ b/crates/sol-macro-expander/Cargo.toml @@ -25,7 +25,7 @@ proc-macro2.workspace = true quote.workspace = true syn = { workspace = true, features = ["extra-traits"] } -heck = "0.4" +heck = "0.5" hex.workspace = true indexmap = "2" proc-macro-error.workspace = true diff --git a/crates/sol-macro-expander/src/expand/event.rs b/crates/sol-macro-expander/src/expand/event.rs index c6e5cb86e4..2b09461879 100644 --- a/crates/sol-macro-expander/src/expand/event.rs +++ b/crates/sol-macro-expander/src/expand/event.rs @@ -30,7 +30,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, event: &ItemEvent) -> Result event.assert_valid()?; let name = cx.overloaded_name(event.into()); - let signature = cx.signature(name.as_string(), ¶ms); + let signature = cx.event_signature(event); let selector = crate::utils::event_selector(&signature); let anonymous = event.is_anonymous(); diff --git a/crates/sol-macro-expander/src/expand/mod.rs b/crates/sol-macro-expander/src/expand/mod.rs index 8d28d7aa63..e2d269e1aa 100644 --- a/crates/sol-macro-expander/src/expand/mod.rs +++ b/crates/sol-macro-expander/src/expand/mod.rs @@ -1,6 +1,5 @@ //! Functions which generate Rust code from the Solidity AST. -#![allow(rustdoc::private_intra_doc_links)] use crate::{ expand::ty::expand_rust_type, utils::{self, ExprArray}, @@ -20,6 +19,7 @@ use std::{ sync::atomic::{AtomicBool, Ordering}, }; use syn::{ext::IdentExt, parse_quote, Attribute, Error, Result}; + #[macro_use] mod macros; @@ -574,7 +574,7 @@ pub fn generate_name(i: usize) -> Ident { } /// Returns the name of a parameter, or a generated name if it is `None`. -fn anon_name + Clone>((i, name): (usize, Option<&T>)) -> Ident { +pub fn anon_name + Clone>((i, name): (usize, Option<&T>)) -> Ident { match name { Some(name) => name.clone().into(), None => generate_name(i), diff --git a/crates/sol-types/tests/macros/sol/mod.rs b/crates/sol-types/tests/macros/sol/mod.rs index 7866fbcacd..0cf750ca01 100644 --- a/crates/sol-types/tests/macros/sol/mod.rs +++ b/crates/sol-types/tests/macros/sol/mod.rs @@ -808,3 +808,49 @@ fn bytecode_attributes() { assert_eq!(Dummy::BYTECODE[..], hex::decode("1234").unwrap()); assert_eq!(Dummy::DEPLOYED_BYTECODE[..], hex::decode("5678").unwrap()); } + +#[test] +fn function_overrides() { + mod one { + alloy_sol_types::sol! { + function TestEvent(bytes32 one); + } + } + + mod two { + alloy_sol_types::sol! { + function TestEvent(bytes32 one); + function TestEvent(bytes32 one, bytes32 two); + } + } + + assert_eq!(one::TestEventCall::SIGNATURE, "TestEvent(bytes32)"); + assert_eq!(one::TestEventCall::SIGNATURE, two::TestEvent_0Call::SIGNATURE); + + assert_eq!(two::TestEvent_1Call::SIGNATURE, "TestEvent(bytes32,bytes32)"); +} + +// https://github.com/alloy-rs/core/issues/640 +#[test] +fn event_overrides() { + mod one { + alloy_sol_types::sol! { + event TestEvent(bytes32 indexed one); + } + } + + mod two { + alloy_sol_types::sol! { + event TestEvent(bytes32 indexed one); + event TestEvent(bytes32 indexed one, bytes32 indexed two); + } + } + + assert_eq!(one::TestEvent::SIGNATURE, "TestEvent(bytes32)"); + assert_eq!(one::TestEvent::SIGNATURE, two::TestEvent_0::SIGNATURE); + assert_eq!(one::TestEvent::SIGNATURE_HASH, keccak256("TestEvent(bytes32)")); + assert_eq!(one::TestEvent::SIGNATURE_HASH, two::TestEvent_0::SIGNATURE_HASH); + + assert_eq!(two::TestEvent_1::SIGNATURE, "TestEvent(bytes32,bytes32)"); + assert_eq!(two::TestEvent_1::SIGNATURE_HASH, keccak256("TestEvent(bytes32,bytes32)")); +}