Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lang): reduce events generated code #2662

Merged
merged 11 commits into from
Nov 8, 2024
110 changes: 22 additions & 88 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ rpassword = "7.2.0"
rstest = "0.18.2"
rstest_reuse = "0.6.0"
salsa = "0.16.1"
scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
#scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
#scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb = { path = "/Users/glihm/swm/scarb/scarb" }
scarb-ui = { path = "/Users/glihm/swm/scarb/utils/scarb-ui" }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ohayo sensei! Please revert the hardcoded local paths.

The changes introduce hardcoded local paths that will break builds for other developers. These paths are specific to your local development environment and should not be committed.

Please revert these changes and restore the git dependencies:

-#scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
-#scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
-scarb = { path = "/Users/glihm/swm/scarb/scarb" }
-scarb-ui = { path = "/Users/glihm/swm/scarb/utils/scarb-ui" }
+scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
+scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }

If you need to use local paths for development:

  1. Use relative paths instead of absolute paths
  2. Add the changes to .gitignore
  3. Consider using a local override with [patch.crates-io] in a separate Cargo.toml.local file
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
#scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb = { path = "/Users/glihm/swm/scarb/scarb" }
scarb-ui = { path = "/Users/glihm/swm/scarb/utils/scarb-ui" }
scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }

semver = "1.0.5"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo/core/src/event/component.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dojo::event::{Event, IEvent, EventDefinition};
use dojo::event::{Event, IEvent, EventDef};
use dojo::meta::{Layout, introspect::Struct};

#[starknet::embeddable]
Expand All @@ -25,7 +25,7 @@ pub impl IStoredEventImpl<

#[starknet::embeddable]
pub impl IEventImpl<TContractState, E, +Event<E>> of IEvent<TContractState> {
fn definition(self: @TContractState) -> EventDefinition {
fn definition(self: @TContractState) -> EventDef {
Event::<E>::definition()
}
}
38 changes: 35 additions & 3 deletions crates/dojo/core/src/event/event.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
use dojo::meta::Layout;
use dojo::meta::introspect::Struct;
use dojo::meta::{Introspect, introspect::{Struct, Ty}};
use dojo::model::model::ModelParser;

#[derive(Drop, Serde, Debug, PartialEq)]
pub struct EventDefinition {
pub struct EventDef {
pub name: ByteArray,
pub layout: Layout,
pub schema: Struct
Comment on lines +6 to 9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo, sensei! The renaming needs attention in several files.

The search results show that EventDefinition is still being used in multiple places while EventDef has been introduced, creating an inconsistent state:

  • crates/dojo/core/src/lib.cairo exports both EventDefinition and EventDef
  • crates/dojo/core/src/event/event.cairo uses both names:
    • Has the new EventDef struct
    • Still uses EventDefinition trait and implementations
  • crates/dojo/lang/src/plugin_test_data/event has multiple references to EventDefinition
  • crates/dojo/lang/src/attribute_macros/patches/event.patch.cairo uses EventDefinition

The renaming appears to be incomplete and needs to be consistently applied across all these files.

🔗 Analysis chain

Ohayo, sensei! Ensure consistent renaming from EventDefinition to EventDef.

Please verify that all references to EventDefinition have been updated to EventDef throughout the codebase to prevent any compilation errors.

Run the following script to check for any remaining references:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining references to `EventDefinition`.

rg 'EventDefinition' --type cairo

Length of output: 63


Script:

#!/bin/bash
# Let's search for EventDefinition without file type restriction since cairo might not be recognized
rg "EventDefinition"

# Also search for EventDef to understand the context better
rg "EventDef"

# Let's also check all cairo files to see their presence
fd "\.cairo$"

Length of output: 9181

}
Comment on lines +2 to 10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo, sensei! The EventDef renaming is inconsistent and needs attention.

The codebase shows mixed usage of both EventDef and EventDefinition:

  • EventDef is the new struct name in event.cairo
  • EventDefinition is still used as a trait name and in test data
  • Both names are exported in lib.cairo: pub use event::{Event, EventDefinition, EventDef}

This dual naming creates confusion and violates consistent naming conventions. Either revert to EventDefinition throughout or complete the renaming to EventDef consistently.

🔗 Analysis chain

Ohayo, sensei! Verify the EventDef renaming across the codebase.

The renaming from EventDefinition to EventDef needs to be consistently applied across all files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to EventDefinition
# and verify EventDef usage

echo "Checking for remaining EventDefinition references:"
rg "EventDefinition" --type cairo

echo -e "\nVerifying EventDef usage:"
rg "EventDef" --type cairo

Length of output: 298


Script:

#!/bin/bash
# Let's try without file type filtering since cairo isn't recognized
echo "Checking for remaining EventDefinition references:"
rg "EventDefinition"

echo -e "\nVerifying EventDef usage:"
rg "EventDef"

Length of output: 4421


pub trait EventDefinition<E> {
fn name() -> ByteArray;
}

pub trait Event<T> {
fn name() -> ByteArray;
fn definition() -> EventDefinition;
fn definition() -> EventDef;
fn layout() -> Layout;
fn schema() -> Struct;
fn serialized_keys(self: @T) -> Span<felt252>;
fn serialized_values(self: @T) -> Span<felt252>;
/// Returns the selector of the event computed for the given namespace hash.
fn selector(namespace_hash: felt252) -> felt252;
}

pub impl EventImpl<E, +ModelParser<E>, +EventDefinition<E>, +Serde<E>, +Introspect<E>> of Event<E> {
fn name() -> ByteArray {
EventDefinition::<E>::name()
}
fn definition() -> EventDef {
EventDef { name: Self::name(), layout: Self::layout(), schema: Self::schema() }
}
fn layout() -> Layout {
Introspect::<E>::layout()
}
fn schema() -> Struct {
match Introspect::<E>::ty() {
Ty::Struct(s) => s,
_ => panic!("Event: invalid schema.")
}
Comment on lines +38 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ohayo, sensei! Consider graceful error handling in the schema method.

Using panic! may halt the program unexpectedly. Consider returning a Result type to handle errors more gracefully and provide better error propagation.

Apply this diff to modify the schema method:

 fn schema() -> Struct {
     match Introspect::<E>::ty() {
         Ty::Struct(s) => s,
-        _ => panic!("Event: invalid schema.")
+        _ => {
+            // Return an error for invalid schema type
+            let err_msg = "Event: invalid schema.";
+            Err(err_msg)
+        }
     }
 }

This change allows calling functions to handle the error appropriately.

Committable suggestion skipped: line range outside the PR's diff.

}
fn serialized_keys(self: @E) -> Span<felt252> {
ModelParser::<E>::serialize_keys(self)
}
fn serialized_values(self: @E) -> Span<felt252> {
ModelParser::<E>::serialize_values(self)
}
fn selector(namespace_hash: felt252) -> felt252 {
dojo::utils::selector_from_namespace_and_name(namespace_hash, @Self::name())
}
}
2 changes: 1 addition & 1 deletion crates/dojo/core/src/event/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[starknet::interface]
pub trait IEvent<T> {
fn definition(self: @T) -> super::EventDefinition;
fn definition(self: @T) -> super::EventDef;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo sensei! The EventDef transition is incomplete!

The codebase still contains multiple references to EventDefinition that need to be updated to EventDef, particularly in:

  • crates/dojo/lang/src/plugin_test_data/event (multiple occurrences)
  • crates/dojo/core/src/event/event.cairo (trait and impl definitions)
  • crates/dojo/core/src/lib.cairo (exports)

This inconsistency could lead to confusion and potential issues. The change should be applied uniformly across all files.

🔗 Analysis chain

Ohayo sensei! The return type change looks good!

The change from EventDefinition to EventDef aligns well with the PR's objective of reducing generated code by simplifying type names.

Let's verify the consistency of this change across the codebase:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that EventDefinition has been completely replaced with EventDef

# Test 1: Check for any remaining references to EventDefinition
echo "Checking for remaining EventDefinition references..."
rg "EventDefinition"

# Test 2: Verify the new EventDef usage
echo "Verifying EventDef usage..."
rg "EventDef"

Length of output: 4423

}
2 changes: 1 addition & 1 deletion crates/dojo/core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod event {
pub mod component;

pub mod event;
pub use event::{Event, EventDefinition};
pub use event::{Event, EventDefinition, EventDef};

pub mod interface;
pub use interface::{IEvent, IEventDispatcher, IEventDispatcherTrait};
Expand Down
Loading
Loading