From 6c0931114690010c9a7c19837d62a11435b777af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cem=20=C3=96zer?= Date: Tue, 24 Oct 2023 21:33:49 -0400 Subject: [PATCH] Add get_key_string method to RuntimeEvent --- .../provers/risc0/guest-mock/Cargo.lock | 21 +++++---- examples/demo-rollup/stf/src/runtime.rs | 4 +- .../examples/sov-value-setter/src/call.rs | 16 +++++++ .../examples/sov-value-setter/src/lib.rs | 2 +- module-system/sov-modules-macros/src/event.rs | 45 ++++++++++++++++++- .../tests/dispatch/derive_event.rs | 33 +++++++++++--- 6 files changed, 100 insertions(+), 21 deletions(-) diff --git a/examples/demo-rollup/provers/risc0/guest-mock/Cargo.lock b/examples/demo-rollup/provers/risc0/guest-mock/Cargo.lock index e4dc43feb..3f57b722f 100644 --- a/examples/demo-rollup/provers/risc0/guest-mock/Cargo.lock +++ b/examples/demo-rollup/provers/risc0/guest-mock/Cargo.lock @@ -1134,18 +1134,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.48" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.48" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", @@ -1163,11 +1163,10 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ - "cfg-if", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -1175,9 +1174,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.26" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", @@ -1186,9 +1185,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.31" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", ] diff --git a/examples/demo-rollup/stf/src/runtime.rs b/examples/demo-rollup/stf/src/runtime.rs index 39b28b143..d66c3eb0a 100644 --- a/examples/demo-rollup/stf/src/runtime.rs +++ b/examples/demo-rollup/stf/src/runtime.rs @@ -50,7 +50,7 @@ use sov_modules_api::macros::DefaultRuntime; use sov_modules_api::macros::{expose_rpc, CliWallet}; #[cfg(feature = "native")] use sov_modules_api::Spec; -use sov_modules_api::{Context, DispatchCall, Genesis, MessageCodec}; +use sov_modules_api::{Context, DispatchCall, Event, Genesis, MessageCodec}; #[cfg(feature = "native")] use sov_nft_module::{NonFungibleTokenRpcImpl, NonFungibleTokenRpcServer}; use sov_rollup_interface::da::DaSpec; @@ -61,7 +61,7 @@ use sov_value_setter::{ValueSetterRpcImpl, ValueSetterRpcServer}; /// The `demo-stf runtime`. #[cfg_attr(feature = "native", derive(CliWallet), expose_rpc)] -#[derive(Genesis, DispatchCall, MessageCodec, DefaultRuntime)] +#[derive(Genesis, DispatchCall, Event, MessageCodec, DefaultRuntime)] #[serialization(borsh::BorshDeserialize, borsh::BorshSerialize)] #[cfg_attr( feature = "native", diff --git a/module-system/module-implementations/examples/sov-value-setter/src/call.rs b/module-system/module-implementations/examples/sov-value-setter/src/call.rs index fb9040f68..2c479e822 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/call.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/call.rs @@ -25,6 +25,21 @@ pub enum CallMessage { ), } +/// This enumeration represents the available events that result from interacting with the `sov-value-setter` module. +#[cfg_attr( + feature = "native", + derive(serde::Serialize), + derive(serde::Deserialize) +)] +#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Clone)] +pub enum Event { + /// Value set + ValueSet( + /// new value + u32, + ), +} + /// Example of a custom error. #[derive(Debug, Error)] enum SetValueError { @@ -50,6 +65,7 @@ impl ValueSetter { // This is how we set a new value: self.value.set(&new_value, working_set); + // TODO: replace add event functionality to be similar to self.event.add() working_set.add_event("set", &format!("value_set: {new_value:?}")); Ok(CallResponse::default()) diff --git a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs index 44b730037..12ec7900f 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs @@ -42,7 +42,7 @@ impl sov_modules_api::Module for ValueSetter { type CallMessage = call::CallMessage; - type Event = (); + type Event = call::Event; fn genesis(&self, config: &Self::Config, working_set: &mut WorkingSet) -> Result<(), Error> { // The initialization logic diff --git a/module-system/sov-modules-macros/src/event.rs b/module-system/sov-modules-macros/src/event.rs index d47613804..4510af904 100644 --- a/module-system/sov-modules-macros/src/event.rs +++ b/module-system/sov-modules-macros/src/event.rs @@ -26,6 +26,44 @@ impl<'a> StructDef<'a> { }) .collect() } + + fn create_get_key_string_impl(&self) -> proc_macro2::TokenStream { + let enum_ident = self.enum_ident(EVENT); + + let match_legs: Vec = self + .fields + .iter() + .map(|field| { + let module_name = &field.ident; + let module_name_str = &field.ident.to_string(); + quote::quote!( + #enum_ident::#module_name(inner)=>{ + let enum_name: String = format!("{:?}", inner) + .split('(') + .collect::>()[0].to_string(); + format!("{}-{}", #module_name_str, enum_name) + }, + ) + }) + .collect(); + + let impl_generics = &self.impl_generics; + let enum_ident = self.enum_ident(EVENT); + let where_clause = &self.where_clause; + let ty_generics = &self.type_generics; + + quote::quote! { + impl #impl_generics #enum_ident #ty_generics #where_clause { + + /// Returns a string that identifies both the module and the event type + pub fn get_key_string(&self) -> String { + match self { + #(#match_legs)* + } + } + } + } + } } impl EventMacro { @@ -62,12 +100,15 @@ impl EventMacro { where_clause, ); - let event_enum_legs = struct_def.create_event_enum_legs(); - let event_enum = struct_def.create_enum(&event_enum_legs, EVENT, &serialization_methods); + let enum_legs = struct_def.create_event_enum_legs(); + let event_enum = struct_def.create_enum(&enum_legs, EVENT, &serialization_methods); + let get_key_string_impl = struct_def.create_get_key_string_impl(); Ok(quote::quote! { #[doc="This enum is generated from the underlying Runtime, the variants correspond to events from the relevant modules"] #event_enum + + #get_key_string_impl } .into()) } diff --git a/module-system/sov-modules-macros/tests/dispatch/derive_event.rs b/module-system/sov-modules-macros/tests/dispatch/derive_event.rs index 24334e3e3..79bf75e30 100644 --- a/module-system/sov-modules-macros/tests/dispatch/derive_event.rs +++ b/module-system/sov-modules-macros/tests/dispatch/derive_event.rs @@ -1,5 +1,5 @@ mod modules; -use modules::{first_test_module, second_test_module}; +use modules::{first_test_module, second_test_module, third_test_module}; use sov_modules_api::default_context::DefaultContext; use sov_modules_api::macros::DefaultRuntime; use sov_modules_api::{ @@ -12,12 +12,35 @@ struct Runtime { pub first: first_test_module::FirstTestStruct, pub second: second_test_module::SecondTestStruct, + pub third: third_test_module::ThirdTestStruct, } fn main() { // Check to see if the runtime events are getting initialized correctly - let _event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum1(10)); - let _event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum2); - let _event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum3(vec![1;3])); - let _event = RuntimeEvent::::second(second_test_module::Event::SecondModuleEnum); + { + let event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum1(10)); + assert_eq!(event.get_key_string(), "first-FirstModuleEnum1"); + } + + { + let event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum2); + assert_eq!(event.get_key_string(), "first-FirstModuleEnum2"); + } + + { + let event = RuntimeEvent::::first(first_test_module::Event::FirstModuleEnum3(vec![1; 3])); + assert_eq!(event.get_key_string(), "first-FirstModuleEnum3"); + } + + { + let event = RuntimeEvent::::second(second_test_module::Event::SecondModuleEnum); + assert_eq!(event.get_key_string(), "second-SecondModuleEnum"); + } + + { + // Not sure if this is how we'd want to keep this. But wanted to highlight this edge case. + let event = RuntimeEvent::::third(()); + assert_eq!(event.get_key_string(), "third-"); + } + }