Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[Unreleased]

### Fixed
- `name` override fixes for message id computation and trait definitions - [#2649](https://github.com/use-ink/ink/pull/2649)

## Version 6.0.0-alpha.4

### Added
Expand Down
14 changes: 3 additions & 11 deletions crates/ink/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ impl Metadata<'_> {
let is_payable = constructor.is_payable();
let is_default = constructor.is_default();
let constructor = constructor.callable();
let name = constructor
.name()
.map(ToString::to_string)
.unwrap_or_else(|| constructor.ident().to_string());
let name = constructor.normalized_name();
let args = constructor.inputs().map(Self::generate_dispatch_argument);
let storage_ident = self.contract.module().storage().ident();
let ret_ty = Self::generate_constructor_return_type(storage_ident, selector_id);
Expand Down Expand Up @@ -218,10 +215,7 @@ impl Metadata<'_> {
let is_default = message.is_default();
let message = message.callable();
let mutates = message.receiver().is_ref_mut();
let name = message
.name()
.map(ToString::to_string)
.unwrap_or_else(|| message.ident().to_string());
let name = message.normalized_name();
let args = message.inputs().map(Self::generate_dispatch_argument);
let cfg_attrs = message.get_cfg_attrs(span);
let ret_ty =
Expand Down Expand Up @@ -268,9 +262,7 @@ impl Metadata<'_> {
.map(|((trait_ident, trait_path), message)| {
let message_span = message.span();
let message_name = message
.name()
.map(ToString::to_string)
.unwrap_or_else(|| message.ident().to_string());
.normalized_name();
let message_docs = message
.attrs()
.iter()
Expand Down
10 changes: 2 additions & 8 deletions crates/ink/codegen/src/generator/sol/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ impl SolidityMetadata<'_> {
.impls()
.flat_map(|item_impl| item_impl.iter_constructors())
.map(|ctor| {
let name = ctor
.name()
.map(ToString::to_string)
.unwrap_or_else(|| ctor.ident().to_string());
let name = ctor.normalized_name();
let inputs = params_info(ctor.inputs());
let is_payable = ctor.is_payable();
let is_default = ctor.is_default();
Expand All @@ -101,10 +98,7 @@ impl SolidityMetadata<'_> {
.impls()
.flat_map(|item_impl| item_impl.iter_messages())
.map(|msg| {
let name = msg
.name()
.map(ToString::to_string)
.unwrap_or_else(|| msg.ident().to_string());
let name = msg.normalized_name();
let inputs = params_info(msg.inputs());
let output = msg
.output()
Expand Down
5 changes: 1 addition & 4 deletions crates/ink/codegen/src/generator/sol/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ pub fn sol_return_type(ty: &Type) -> TokenStream2 {

/// Returns Solidity ABI compatible selector of an ink! message.
pub fn selector(message: &Message) -> TokenStream2 {
let name = message
.name()
.map(ToString::to_string)
.unwrap_or_else(|| message.ident().to_string());
let name = message.normalized_name();
let signature = call_signature(name, message.inputs());
quote! {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,9 @@ impl MessageBuilder<'_> {
(quote!([ #( #selector_bytes ),* ]), quote!(::ink::abi::Ink))
}
Abi::Sol => {
let message_ident = message.ident();
let signature = sol::utils::call_signature(
message_ident.to_string(),
message.inputs(),
);
let name = message.normalized_name();
let signature =
sol::utils::call_signature(name, message.inputs());
(
quote!(::ink::codegen::sol::selector_bytes(#signature)),
quote!(::ink::abi::Sol),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ impl TraitRegistry<'_> {
)
}
Abi::Sol => {
let ident_str = message.ident().to_string();
let signature = sol::utils::call_signature(ident_str, message.inputs());
let name = message.normalized_name();
let signature = sol::utils::call_signature(name, message.inputs());
let selector_bytes = quote! {
::ink::codegen::sol::selector_bytes(#signature)
};
Expand Down
18 changes: 14 additions & 4 deletions crates/ink/ir/src/ir/item_impl/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ where
fn name(&self) -> Option<&str> {
<C as Callable>::name(self.callable)
}

fn normalized_name(&self) -> String {
self.name()
.map(ToString::to_string)
.unwrap_or_else(|| self.ident().to_string())
}
}

impl<C> ::core::ops::Deref for CallableWithSelector<'_, C> {
Expand Down Expand Up @@ -201,6 +207,13 @@ pub trait Callable {

/// Returns the function name override (if any).
fn name(&self) -> Option<&str>;

/// Returns the "normalized" function name
///
/// # Note
/// This returns the name override (if provided), otherwise the identifier is
/// returned.
fn normalized_name(&self) -> String;
}

/// Returns the composed selector of the ink! callable.
Expand Down Expand Up @@ -339,10 +352,7 @@ fn compose_selector_preimage<C>(item_impl: &ir::ItemImpl, callable: &C) -> Vec<u
where
C: Callable,
{
let callable_name = callable
.name()
.map(|name| name.as_bytes().to_vec())
.unwrap_or_else(|| callable.ident().to_string().into_bytes());
let callable_name = callable.normalized_name().into_bytes();
let namespace_bytes = item_impl
.namespace()
.map(|namespace| namespace.as_bytes().to_vec())
Expand Down
15 changes: 15 additions & 0 deletions crates/ink/ir/src/ir/item_impl/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ impl Callable for Constructor {
fn name(&self) -> Option<&str> {
self.name.as_deref()
}

fn normalized_name(&self) -> String {
self.normalized_name()
}
}

impl Constructor {
Expand Down Expand Up @@ -281,6 +285,17 @@ impl Constructor {
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}

/// Returns the "normalized" function name
///
/// # Note
/// This returns the name override (if provided), otherwise the identifier is
/// returned.
pub fn normalized_name(&self) -> String {
self.name()
.map(ToString::to_string)
.unwrap_or_else(|| self.ident().to_string())
}
}

#[cfg(test)]
Expand Down
17 changes: 16 additions & 1 deletion crates/ink/ir/src/ir/item_impl/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ impl Callable for Message {
fn name(&self) -> Option<&str> {
self.name.as_deref()
}

fn normalized_name(&self) -> String {
self.normalized_name()
}
}

impl Message {
Expand Down Expand Up @@ -370,7 +374,7 @@ impl Message {
/// Although the above scenario is very unlikely since the local ID is computed
/// solely by the identifier of the ink! message.
pub fn local_id(&self) -> u32 {
utils::local_message_id(self.ident())
utils::local_message_id(&self.normalized_name())
}

/// Returns the identifier of the message with an additional `try_` prefix attached.
Expand All @@ -382,6 +386,17 @@ impl Message {
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}

/// Returns the "normalized" function name
///
/// # Note
/// This returns the name override (if provided), otherwise the identifier is
/// returned.
pub fn normalized_name(&self) -> String {
self.name()
.map(ToString::to_string)
.unwrap_or_else(|| self.ident().to_string())
}
}

#[cfg(test)]
Expand Down
4 changes: 1 addition & 3 deletions crates/ink/ir/src/ir/trait_def/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,7 @@ impl InkItemTrait {
manual_selector
}
_ => {
let name = ink_attrs
.name()
.unwrap_or_else(|| callable.ident().to_string());
let name = callable.normalized_name();
Selector::compose(trait_prefix, name)
}
};
Expand Down
34 changes: 33 additions & 1 deletion crates/ink/ir/src/ir/trait_def/item/trait_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ impl<'a> InkTraitItem<'a> {
Self::Message(ink_trait_message) => Some(ink_trait_message),
}
}

/// Returns the function name override (if any).
pub fn name(&self) -> Option<String> {
match self {
Self::Message(message) => message.name(),
}
}

/// Returns the "normalized" function name
///
/// # Note
/// This returns the name override (if provided), otherwise the identifier is
/// returned.
pub fn normalized_name(&self) -> String {
match self {
Self::Message(message) => message.normalized_name(),
}
}
}

/// A checked ink! message of an ink! trait definition.
Expand Down Expand Up @@ -173,7 +191,7 @@ impl<'a> InkTraitMessage<'a> {
/// Although the above scenario is very unlikely since the local ID is computed
/// solely by the identifier of the ink! message.
pub fn local_id(&self) -> u32 {
utils::local_message_id(self.ident())
utils::local_message_id(&self.normalized_name())
}

/// Returns the span of the ink! message.
Expand All @@ -188,6 +206,20 @@ impl<'a> InkTraitMessage<'a> {
.map(|receiver| receiver.mutability.is_some())
.expect("encountered missing receiver for ink! message")
}

/// Returns the function name override (if any).
pub fn name(&self) -> Option<String> {
self.ink_attrs().name()
}

/// Returns the "normalized" function name
///
/// # Note
/// This returns the name override (if provided), otherwise the identifier is
/// returned.
pub fn normalized_name(&self) -> String {
self.name().unwrap_or_else(|| self.ident().to_string())
}
}

impl<'a> From<&'a InkTraitMessage<'a>> for InputsIter<'a> {
Expand Down
6 changes: 3 additions & 3 deletions crates/ink/ir/src/ir/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub fn ensure_pub_visibility(
///
/// - The returned value is equal to the selector of the message identifier.
/// - Used from within ink! trait definitions as well as ink! trait implementation blocks.
pub fn local_message_id(ident: &syn::Ident) -> u32 {
let input = ident.to_string().into_bytes();
let selector = Selector::compute(&input);
pub fn local_message_id(ident: &str) -> u32 {
let input = ident.as_bytes();
let selector = Selector::compute(input);
selector.into_be_u32()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use ink::{
reflect::{
TraitDefinitionRegistry,
TraitMessageInfo,
},
selector_bytes,
selector_id,
};
use ink_env::DefaultEnvironment;

#[ink::trait_definition]
pub trait TraitDefinition {
#[ink(message, name = "myMessage")]
fn message(&self);
}

fn main() {
// Message selector and selector id both use the name override.
macro_rules! assert_selector_eq {
( $message_id:expr, $expected_selector:expr $(,)? ) => {
assert_eq!(
<<TraitDefinitionRegistry<DefaultEnvironment> as TraitDefinition>::__ink_TraitInfo
as TraitMessageInfo<{$message_id}>>::SELECTOR,
$expected_selector
);
}
}

// ink! selector
assert_selector_eq!(selector_id!("myMessage"), selector_bytes!("TraitDefinition::myMessage"));

// `keccak256("myMessage()")` == `0x1b008a9f`
assert_selector_eq!(0x1b008a9f_u32, [0x1b, 0x00, 0x8a, 0x9f]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ink::{
reflect::{
TraitDefinitionRegistry,
TraitMessageInfo,
},
};
use ink_env::DefaultEnvironment;

#[ink::trait_definition]
pub trait TraitDefinition {
#[ink(message, name = "myMessage")]
fn message(&self);
}

fn main() {
// Message selector and selector id both use the name override.
macro_rules! assert_selector_eq {
( $message_id:literal, $expected_selector:expr $(,)? ) => {
assert_eq!(
<<TraitDefinitionRegistry<DefaultEnvironment> as TraitDefinition>::__ink_TraitInfo
as TraitMessageInfo<$message_id>>::SELECTOR,
$expected_selector
);
}
}

// `keccak256("myMessage()")` == `0x1b008a9f`
assert_selector_eq!(0x1b008a9f_u32, [0x1b, 0x00, 0x8a, 0x9f]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait TraitDefinition {
}

fn main() {
// Message selector and selector id both use the name override.
macro_rules! assert_selector_eq {
( $message_id:literal, $expected_selector:expr $(,)? ) => {
assert_eq!(
Expand All @@ -26,7 +27,7 @@ fn main() {
}

assert_selector_eq!(
"message",
"myMessage",
selector_bytes!("foo::TraitDefinition::myMessage"),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait TraitDefinition {
}

fn main() {
// Message selector and selector id both use the name override.
macro_rules! assert_selector_eq {
( $message_id:literal, $expected_selector:expr $(,)? ) => {
assert_eq!(
Expand All @@ -25,5 +26,5 @@ fn main() {
}
}

assert_selector_eq!("message", selector_bytes!("TraitDefinition::myMessage"),);
assert_selector_eq!("myMessage", selector_bytes!("TraitDefinition::myMessage"),);
}
Loading
Loading