macro: Redesign event registration around module attribute#32
Conversation
Introduce the ContractEvent trait (const TOPICS: &[&str]) so event types declare their topics once, and rename schema::Event.topic to topics, a slice populated from ContractEvent::TOPICS. Contract::get_event now matches a topic against the slice.
Replace the per-method emits/no_event directives and the mut-self-must-emit lint with a single #[contract(events = [Type, ...])] module attribute. The registered list is the source of truth for the schema and decode_event dispatch. The body walker inverts from extractor to validator: it checks that every in-module abi::emit() references a registered type, anchored at the call. Both the registered list and emit-site types are resolved through the module imports before comparison, so qualified and imported-short forms unify. Emits whose data argument isn't a concrete type path (locals, field access, constructor/helper calls) are skipped as accepted false negatives. Duplicate registrations are rejected. Schema entries read topics from ContractEvent::TOPICS, and decode_event emits one linear-scan block per registered type. Update fixtures and harness.
968679a to
155d6af
Compare
There was a problem hiding this comment.
As discussed, I'll think and play around a bit more with this model over the weekend. One note though, this event model change also makes the shipped counter template stale on the CLI side of Forge.
cli/src/template/embedded.rs embeds contract-template/src/lib.rs for dusk-forge new. That template still uses plain #[dusk_forge::contract] and emits count_changed tuple events. On main, those abi::emit(...) calls are picked up by the body walker and included in the event schema. With the new model, plain #[contract] means an empty registered event list, so newly scaffolded counter contracts would still emit count_changed but their schema/data-driver would no longer advertise or decode it.
Can we migrate the template/docs as part of this PR please? A follow-up would also be fine.
|
good catch! Will update and make template/docs checks part of the CI so this doesn't happen in the future! |
Resolves #31