Skip to content

Commit

Permalink
torii: validate events (starkware-libs#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l authored Oct 31, 2023
1 parent 5a2a5df commit f02b313
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ where
) -> Result<()> {
self.db.store_event(event_id, event, invoke_receipt.transaction_hash);
for processor in &self.processors.event {
if get_selector_from_name(&processor.event_key())? == event.keys[0] {
if get_selector_from_name(&processor.event_key())? == event.keys[0]
&& processor.validate(event)
{
processor
.process(&self.world, self.db, block, invoke_receipt, event_id, event)
.await?;
Expand Down
12 changes: 12 additions & 0 deletions crates/torii/core/src/processors/metadata_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ where
"MetadataUpdate".to_string()
}

fn validate(&self, event: &Event) -> bool {
if event.keys.len() > 1 {
info!(
"invalid keys for event {}: {}",
<MetadataUpdateProcessor as EventProcessor<P>>::event_key(self),
<MetadataUpdateProcessor as EventProcessor<P>>::event_keys_as_string(self, event),
);
return false;
}
true
}

async fn process(
&self,
_world: &WorldContractReader<P>,
Expand Down
6 changes: 6 additions & 0 deletions crates/torii/core/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ where
{
fn event_key(&self) -> String;

fn event_keys_as_string(&self, event: &Event) -> String {
event.keys.iter().map(|i| format!("{:#064x}", i)).collect::<Vec<_>>().join(",")
}

fn validate(&self, event: &Event) -> bool;

#[allow(clippy::too_many_arguments)]
async fn process(
&self,
Expand Down
12 changes: 12 additions & 0 deletions crates/torii/core/src/processors/register_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ where
"ModelRegistered".to_string()
}

fn validate(&self, event: &Event) -> bool {
if event.keys.len() > 1 {
info!(
"invalid keys for event {}: {}",
<RegisterModelProcessor as EventProcessor<P>>::event_key(self),
<RegisterModelProcessor as EventProcessor<P>>::event_keys_as_string(self, event),
);
return false;
}
true
}

async fn process(
&self,
world: &WorldContractReader<P>,
Expand Down
12 changes: 12 additions & 0 deletions crates/torii/core/src/processors/store_set_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ where
"StoreSetRecord".to_string()
}

fn validate(&self, event: &Event) -> bool {
if event.keys.len() > 1 {
info!(
"invalid keys for event {}: {}",
<StoreSetRecordProcessor as EventProcessor<P>>::event_key(self),
<StoreSetRecordProcessor as EventProcessor<P>>::event_keys_as_string(self, event),
);
return false;
}
true
}

async fn process(
&self,
world: &WorldContractReader<P>,
Expand Down

0 comments on commit f02b313

Please sign in to comment.