Skip to content

Commit 2508611

Browse files
Define static distributed events slice in ink crate (#2487)
* Define static distributed events slice in `ink` crate * chore: Rename events slice * fix: re-enable LTO for events test * Update changelog * fix: Make `linkme` dependency optional
1 parent 313cf9f commit 2508611

File tree

12 files changed

+45
-33
lines changed

12 files changed

+45
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ You can find binary releases of the node [here](https://github.com/use-ink/ink-n
213213
## Changed
214214
- Restrict which `cfg` attributes can be used ‒ [#2313](https://github.com/use-ink/ink/pull/2313)
215215
- More idiomatic return types for metadata getters - [#2398](https://github.com/use-ink/ink/pull/2398)
216+
- Define static distributed events slice in `ink` crate - [#2487](https://github.com/use-ink/ink/pull/2487)
216217

217218
## Added
218219
- Support for `caller_is_root` - [#2332](https://github.com/use-ink/ink/pull/2332)

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ink/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ scale-info = { workspace = true, default-features = false, features = ["derive"]
2828
derive_more = { workspace = true, features = ["from"] }
2929
const_format = { workspace = true }
3030
keccak-const = { workspace = true }
31+
linkme = { workspace = true, optional = true }
3132

3233
polkavm-derive = { workspace = true }
3334
xcm = { workspace = true }
@@ -58,7 +59,8 @@ std = [
5859
"xcm/std",
5960
"derive_more/std",
6061
"sp-runtime-interface/std",
61-
"sp-io/std"
62+
"sp-io/std",
63+
"dep:linkme"
6264
]
6365

6466
# Enable contract debug messages via `debug_print!` and `debug_println!`.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ impl Metadata<'_> {
108108
.messages([
109109
#( #messages ),*
110110
])
111-
.collect_events()
111+
.events(
112+
::ink::collect_events()
113+
)
112114
.docs([
113115
#( #docs ),*
114116
])

crates/ink/macro/src/event/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ fn event_metadata_derive_struct(s: synstructure::Structure) -> syn::Result<Token
8484
fn event_spec() -> ::ink::metadata::EventSpec {
8585
// register this event metadata function in the distributed slice for combining all
8686
// events referenced in the contract binary.
87-
#[::ink::metadata::linkme::distributed_slice(::ink::metadata::EVENTS)]
88-
#[linkme(crate = ::ink::metadata::linkme)]
87+
#[::ink::linkme::distributed_slice(::ink::CONTRACT_EVENTS)]
88+
#[linkme(crate = ::ink::linkme)]
8989
static EVENT_METADATA: fn() -> ::ink::metadata::EventSpec =
9090
<#ident as ::ink::metadata::EventMetadata>::event_spec;
9191

crates/ink/macro/src/tests/event_metadata.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn unit_struct_works() {
2727
const MODULE_PATH: &'static str = ::core::module_path!();
2828

2929
fn event_spec() -> ::ink::metadata::EventSpec {
30-
#[::ink::metadata::linkme::distributed_slice(::ink::metadata::EVENTS)]
31-
#[linkme(crate = ::ink::metadata::linkme)]
30+
#[::ink::linkme::distributed_slice(::ink::CONTRACT_EVENTS)]
31+
#[linkme(crate = ::ink::linkme)]
3232
static EVENT_METADATA: fn() -> ::ink::metadata::EventSpec =
3333
<UnitStruct as ::ink::metadata::EventMetadata>::event_spec;
3434

@@ -62,8 +62,8 @@ fn struct_with_fields_no_topics() {
6262
const MODULE_PATH: &'static str = ::core::module_path!();
6363

6464
fn event_spec() -> ::ink::metadata::EventSpec {
65-
#[::ink::metadata::linkme::distributed_slice(::ink::metadata::EVENTS)]
66-
#[linkme(crate = ::ink::metadata::linkme)]
65+
#[::ink::linkme::distributed_slice(::ink::CONTRACT_EVENTS)]
66+
#[linkme(crate = ::ink::linkme)]
6767
static EVENT_METADATA: fn() -> ::ink::metadata::EventSpec =
6868
<Event as ::ink::metadata::EventMetadata>::event_spec;
6969

@@ -130,8 +130,8 @@ fn struct_with_fields_and_some_topics() {
130130
const MODULE_PATH: &'static str = ::core::module_path!();
131131

132132
fn event_spec() -> ::ink::metadata::EventSpec {
133-
#[::ink::metadata::linkme::distributed_slice(::ink::metadata::EVENTS)]
134-
#[linkme(crate = ::ink::metadata::linkme)]
133+
#[::ink::linkme::distributed_slice(::ink::CONTRACT_EVENTS)]
134+
#[linkme(crate = ::ink::linkme)]
135135
static EVENT_METADATA: fn() -> ::ink::metadata::EventSpec =
136136
<Event as ::ink::metadata::EventMetadata>::event_spec;
137137

crates/ink/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,24 @@ pub use ink_primitives::{
105105
H256,
106106
U256,
107107
};
108+
109+
#[cfg(feature = "std")]
110+
#[doc(hidden)]
111+
pub use linkme;
112+
113+
#[cfg(feature = "std")]
114+
use ink_metadata::EventSpec;
115+
116+
/// Any event which derives `#[derive(ink::EventMetadata)]` and is used in the contract
117+
/// binary will have its implementation added to this distributed slice at linking time.
118+
#[cfg(feature = "std")]
119+
#[linkme::distributed_slice]
120+
#[linkme(crate = linkme)]
121+
pub static CONTRACT_EVENTS: [fn() -> EventSpec] = [..];
122+
123+
/// Collect the [`EventSpec`] metadata of all event definitions linked and used in the
124+
/// binary.
125+
#[cfg(feature = "std")]
126+
pub fn collect_events() -> Vec<EventSpec> {
127+
CONTRACT_EVENTS.iter().map(|event| event()).collect()
128+
}

crates/metadata/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ ink_primitives = { workspace = true }
2121
serde = { workspace = true, features = ["derive", "alloc"] }
2222
impl-serde = { workspace = true, default-features = true }
2323
derive_more = { workspace = true, features = ["from"] }
24-
linkme = { workspace = true }
2524
scale = { workspace = true }
2625
scale-info = { workspace = true, features = ["derive", "serde", "decode", "schema"] }
2726
schemars = { workspace = true }

crates/metadata/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ pub use self::specs::{
5454

5555
use impl_serde::serialize as serde_hex;
5656

57-
#[doc(hidden)]
58-
pub use linkme;
5957
pub use scale_info::TypeInfo;
6058

6159
#[cfg(feature = "derive")]
@@ -143,17 +141,6 @@ impl InkProject {
143141
}
144142
}
145143

146-
/// Any event which derives `#[derive(ink::EventMetadata)]` and is used in the contract
147-
/// binary will have its implementation added to this distributed slice at linking time.
148-
#[linkme::distributed_slice]
149-
pub static EVENTS: [fn() -> EventSpec] = [..];
150-
151-
/// Collect the [`EventSpec`] metadata of all event definitions linked and used in the
152-
/// binary.
153-
pub fn collect_events() -> Vec<EventSpec> {
154-
EVENTS.iter().map(|event| event()).collect()
155-
}
156-
157144
/// Provides metadata about an ink! event.
158145
///
159146
/// Implementations must be registered into the [`static@EVENTS`] distributed slice, in

crates/metadata/src/specs.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,6 @@ where
254254
}
255255
}
256256

257-
impl<S> ContractSpecBuilder<MetaForm, S> {
258-
/// Collect metadata for all events linked into the contract.
259-
pub fn collect_events(self) -> Self {
260-
self.events(crate::collect_events())
261-
}
262-
}
263-
264257
impl<F> ContractSpecBuilder<F, Valid>
265258
where
266259
F: Form,

integration-tests/public/events/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ std = [
2828
]
2929
ink-as-dependency = []
3030
e2e-tests = []
31+
32+
[profile.test]
33+
# Need this for linkme crate to work for the event metadata collection unit test.
34+
# See https://github.com/dtolnay/linkme/issues/61#issuecomment-1503653702
35+
# NOTE: `cargo-contract` also sets this flag when generating metadata for the same reason.
36+
# See https://github.com/use-ink/cargo-contract/pull/1200
37+
lto = "thin"

integration-tests/public/events/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub mod events {
109109

110110
#[test]
111111
fn collects_specs_for_all_linked_and_used_events() {
112-
let event_specs = ink::metadata::collect_events();
112+
let event_specs = ink::collect_events();
113113

114114
assert!(event_specs
115115
.iter()

0 commit comments

Comments
 (0)