Skip to content

Commit 3c788c3

Browse files
committed
Refactor. Remove signature topic macro in favor of local custom ink attr in the derive
1 parent 91b3f91 commit 3c788c3

File tree

19 files changed

+249
-298
lines changed

19 files changed

+249
-298
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ In a module annotated with `#[ink::contract]` these attributes are available:
230230
| `#[ink(constructor)]` | Applicable to method. | Flags a method for the ink! storage struct as constructor making it available to the API for instantiating the contract. |
231231
| `#[ink(event)]` | On `struct` definitions. | Defines an ink! event. A contract can define multiple such ink! events. |
232232
| `#[ink(anonymous)]` | Applicable to ink! events. | Tells the ink! codegen to treat the ink! event as anonymous which omits the event signature as topic upon emitting. Very similar to anonymous events in Solidity. |
233+
| `#[ink(signature_topic = _)]` | Applicable to ink! events. | Specifies custom signature topic of the event that allows to use manually specify shared event definition. |
233234
| `#[ink(topic)]` | Applicable on ink! event field. | Tells the ink! codegen to provide a topic hash for the given field. Every ink! event can only have a limited number of such topic fields. Similar semantics as to indexed event arguments in Solidity. |
234235
| `#[ink(payable)]` | Applicable to ink! messages. | Allows receiving value as part of the call of the ink! message. ink! constructors are implicitly payable. |
235236
| `#[ink(selector = S:u32)]` | Applicable to ink! messages and ink! constructors. | Specifies a concrete dispatch selector for the flagged entity. This allows a contract author to precisely control the selectors of their APIs making it possible to rename their API without breakage. |

crates/env/src/event.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,19 @@ impl EventTopicsAmount for state::NoRemainingTopics {
195195
/// builder.
196196
///
197197
/// Normally this trait should be implemented automatically via `#[derive(ink::Event)`.
198-
pub trait Event: scale::Encode + GetSignatureTopic {
198+
pub trait Event: scale::Encode {
199199
/// Type state indicating how many event topics are to be expected by the topics
200200
/// builder.
201201
type RemainingTopics: EventTopicsAmount;
202202

203+
/// The unique signature topic of the event. `None` for anonymous events.
204+
///
205+
/// It can be automatically calculated or manually specified.
206+
///
207+
/// Usually this is calculated using the `#[derive(ink::Event)]` derive, which by
208+
/// default calculates this as `blake2b("Event(field1_type,field2_type)"
209+
const SIGNATURE_TOPIC: core::option::Option<[u8; 32]>;
210+
203211
/// Guides event topic serialization using the given topics builder.
204212
fn topics<E, B>(
205213
&self,
@@ -209,16 +217,3 @@ pub trait Event: scale::Encode + GetSignatureTopic {
209217
E: Environment,
210218
B: TopicsBuilderBackend<E>;
211219
}
212-
213-
/// Getter that returns the signature topic for the specific event.
214-
///
215-
/// It can be automatically calculated or manually specified.
216-
///
217-
/// The unique signature topic of the event. `None` for anonymous events.
218-
///
219-
/// Usually this is calculated using the `#[derive(ink::Event)]` derive, which by
220-
/// default calculates this as `blake2b("Event(field1_type,field2_type)")`
221-
pub trait GetSignatureTopic {
222-
/// Retrieve the signature topic
223-
const SIGNATURE_TOPIC: core::option::Option<[u8; 32]>;
224-
}

crates/env/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ pub use self::{
120120
Error,
121121
Result,
122122
},
123-
event::{
124-
Event,
125-
GetSignatureTopic,
126-
},
123+
event::Event,
127124
types::{
128125
AccountIdGuard,
129126
DefaultEnvironment,

crates/ink/codegen/src/generator/event.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use crate::GenerateCode;
1616
use derive_more::From;
1717
use proc_macro2::TokenStream as TokenStream2;
18-
use quote::quote;
1918
use syn::spanned::Spanned;
2019

2120
/// Generates code for the event item.
@@ -33,39 +32,20 @@ impl GenerateCode for Event<'_> {
3332
.item
3433
.anonymous()
3534
.then(|| quote::quote! { #[ink(anonymous)] });
36-
37-
let signature_topic = self.generate_signature_topic();
35+
let signature_topic = self
36+
.item
37+
.signature_topic_hash()
38+
.map(|hash| quote::quote! { #[ink(signature_topic = #hash)] });
3839
let cfg_attrs = self.item.get_cfg_attrs(item.span());
3940

4041
quote::quote! (
4142
#( #cfg_attrs )*
4243
#[cfg_attr(feature = "std", derive(::ink::EventMetadata))]
4344
#[derive(::ink::Event)]
4445
#[::ink::scale_derive(Encode, Decode)]
45-
#signature_topic
4646
#anonymous
47+
#signature_topic
4748
#item
4849
)
4950
}
5051
}
51-
52-
impl Event<'_> {
53-
/// Generates the `#[ink::signature_topic]` attribute for the given event.
54-
///
55-
/// # Note
56-
/// If `anonymous` is present, no attribute is generated
57-
/// and the blank implementation is generated by `#derive(Event)`.
58-
fn generate_signature_topic(&self) -> TokenStream2 {
59-
let signature_topic = if let Some(hash) = self.item.signature_topic_hash() {
60-
quote! {
61-
#[::ink::signature_topic(hash = #hash)]
62-
}
63-
} else if self.item.anonymous() {
64-
quote! {}
65-
} else {
66-
quote! { #[::ink::signature_topic] }
67-
};
68-
69-
quote! { #signature_topic }
70-
}
71-
}

crates/ink/codegen/src/generator/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ mod ink_test;
3838
mod item_impls;
3939
mod metadata;
4040
mod selector;
41-
mod signature_topic;
4241
mod storage;
4342
mod storage_item;
4443
mod trait_def;
@@ -68,7 +67,6 @@ pub use self::{
6867
SelectorBytes,
6968
SelectorId,
7069
},
71-
signature_topic::SignatureTopic,
7270
storage::Storage,
7371
storage_item::StorageItem,
7472
trait_def::TraitDefinition,

crates/ink/codegen/src/generator/signature_topic.rs

Lines changed: 0 additions & 79 deletions
This file was deleted.

crates/ink/codegen/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ impl<'a> CodeGenerator for &'a ir::Event {
6262
type Generator = generator::Event<'a>;
6363
}
6464

65-
impl<'a> CodeGenerator for &'a ir::SignatureTopic {
66-
type Generator = generator::SignatureTopic<'a>;
67-
}
68-
6965
impl<'a> CodeGenerator for &'a ir::StorageItem {
7066
type Generator = generator::StorageItem<'a>;
7167
}

crates/ink/ir/src/ir/event/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ use crate::{
2929
utils::extract_cfg_attributes,
3030
};
3131

32-
pub use signature_topic::SignatureTopic;
32+
pub use signature_topic::{
33+
SignatureTopic,
34+
SignatureTopicArg,
35+
};
3336

3437
/// A checked ink! event with its configuration.
3538
#[derive(Debug, PartialEq, Eq)]

crates/ink/ir/src/ir/event/signature_topic.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,43 @@ impl From<&[u8; 32]> for SignatureTopicArg {
4646
}
4747
}
4848

49-
impl TryFrom<&ast::MetaValue> for SignatureTopicArg {
49+
impl TryFrom<&syn::Lit> for SignatureTopicArg {
5050
type Error = syn::Error;
5151

52-
fn try_from(value: &ast::MetaValue) -> Result<Self, Self::Error> {
53-
if let ast::MetaValue::Lit(lit) = value {
54-
if let syn::Lit::Str(s) = lit {
55-
let bytes: [u8; 32] = serde_hex::from_hex(&s.value())
52+
fn try_from(lit: &syn::Lit) -> Result<Self, Self::Error> {
53+
if let syn::Lit::Str(s) = lit {
54+
let bytes: [u8; 32] = serde_hex::from_hex(&s.value())
5655
.map_err(|_| {
5756
format_err_spanned!(
58-
value,
57+
lit,
5958
"`signature_topic` has invalid hex string",
6059
)
6160
})?
6261
.try_into()
6362
.map_err(|e: Vec<u8>| {
6463
format_err_spanned!(
65-
value,
64+
lit,
6665
"`signature_topic` is expected to be 32-byte hash. Found {} bytes",
6766
e.len()
6867
)
6968
})?;
7069

71-
Ok(Self { topic: bytes })
72-
} else {
73-
Err(format_err_spanned!(
74-
value,
75-
"Expected string literal argument for the `signature_topic`"
76-
))
77-
}
70+
Ok(Self { topic: bytes })
71+
} else {
72+
Err(format_err_spanned!(
73+
lit,
74+
"Expected string literal argument for the `signature_topic`"
75+
))
76+
}
77+
}
78+
}
79+
80+
impl TryFrom<&ast::MetaValue> for SignatureTopicArg {
81+
type Error = syn::Error;
82+
83+
fn try_from(value: &ast::MetaValue) -> Result<Self, Self::Error> {
84+
if let ast::MetaValue::Lit(lit) = value {
85+
Self::try_from(lit)
7886
} else {
7987
Err(format_err_spanned!(
8088
value,
@@ -109,6 +117,22 @@ impl TryFrom<ast::AttributeArgs> for Option<SignatureTopicArg> {
109117
}
110118
}
111119

120+
impl TryFrom<&syn::MetaNameValue> for SignatureTopicArg {
121+
type Error = syn::Error;
122+
123+
fn try_from(nv: &syn::MetaNameValue) -> Result<Self, Self::Error> {
124+
if nv.path.is_ident("signature_topic") {
125+
if let syn::Expr::Lit(lit_expr) = &nv.value {
126+
Self::try_from(&lit_expr.lit)
127+
} else {
128+
Err(format_err_spanned!(&nv.value, "Expected literal argument"))
129+
}
130+
} else {
131+
Err(format_err_spanned!(nv, "Expected `signature_topic` ident"))
132+
}
133+
}
134+
}
135+
112136
/// The signature topic argument of an event variant.
113137
///
114138
/// Used as part of `ink::signature_topic` macro.

crates/ink/ir/src/ir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub use self::{
7373
event::{
7474
Event,
7575
SignatureTopic,
76+
SignatureTopicArg,
7677
},
7778
ink_test::InkTest,
7879
item::{

0 commit comments

Comments
 (0)