Skip to content

Commit

Permalink
Use event_count_in_current_exec for event limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon committed Feb 3, 2025
1 parent d7c750c commit c6a6910
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
11 changes: 11 additions & 0 deletions massa-execution-worker/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub struct ExecutionContextSnapshot {
/// This is used to avoid stack overflow issues in the VM (that would crash the node instead of failing the call),
/// by limiting the depth of recursion contracts can have with the max_recursive_calls_depth value.
pub recursion_counter: u16,

/// Counts the number of event in the current execution_context
/// Should be reset to 0 when executing a new op / readonly request / asc / deferred call
pub event_count_in_current_exec: u16,
}

/// An execution context that needs to be initialized before executing bytecode,
Expand Down Expand Up @@ -202,6 +206,10 @@ pub struct ExecutionContext {

/// recursion counter, incremented for each new nested call
pub recursion_counter: u16,

/// Counts the number of event in the current execution_context
/// Should be reset to 0 when executing a new op / readonly request / asc / deferred call
pub event_count_in_current_exec: u16,
}

impl ExecutionContext {
Expand Down Expand Up @@ -286,6 +294,7 @@ impl ExecutionContext {
execution_component_version: mip_store
.get_latest_component_version_at(&MipComponent::Execution, ts),
recursion_counter: 0,
event_count_in_current_exec: 0,
}
}

Expand All @@ -309,6 +318,7 @@ impl ExecutionContext {
unsafe_rng: self.unsafe_rng.clone(),
gas_remaining_before_subexecution: self.gas_remaining_before_subexecution,
recursion_counter: self.recursion_counter,
event_count_in_current_exec: self.event_count_in_current_exec,
}
}

Expand Down Expand Up @@ -355,6 +365,7 @@ impl ExecutionContext {
self.unsafe_rng = snapshot.unsafe_rng;
self.gas_remaining_before_subexecution = snapshot.gas_remaining_before_subexecution;
self.recursion_counter = snapshot.recursion_counter;
self.event_count_in_current_exec = snapshot.event_count_in_current_exec;

// For events, set snapshot delta to error events.
for event in self.events.0.range_mut(snapshot.event_count..) {
Expand Down
3 changes: 3 additions & 0 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ impl ExecutionState {
if execution_component_version == 0 {
context.origin_operation_id = Some(operation_id);
}
context.event_count_in_current_exec = 0;

Ok(context_snapshot)
}
Expand Down Expand Up @@ -1199,6 +1200,7 @@ impl ExecutionState {
operation_datastore: None,
},
];
context.event_count_in_current_exec = 0;

// check the target address
if let Err(err) = context.check_target_sc_address(message.destination) {
Expand Down Expand Up @@ -1357,6 +1359,7 @@ impl ExecutionState {
operation_datastore: None,
},
];
context.event_count_in_current_exec = 0;

// Ensure that the target address is an SC address
// Ensure that the target address exists
Expand Down
40 changes: 11 additions & 29 deletions massa-execution-worker/src/interface_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use tracing::warn;
test
))]
use massa_models::datastore::Datastore;
use massa_models::execution::EventFilter;

/// helper for locking the context mutex
macro_rules! context_guard {
Expand Down Expand Up @@ -1227,21 +1226,13 @@ impl Interface for InterfaceImpl {
let mut context = context_guard!(self);

let event = context.event_create(data, false);
let event_filter = EventFilter {
start: None,
end: None,
emitter_address: None,
original_caller_address: None,
original_operation_id: event.context.origin_operation_id,
is_final: None,
is_error: None,
};

context.event_count_in_current_exec = context.event_count_in_current_exec.saturating_add(1);

if execution_component_version > 0 {
let event_per_op = context
.events
.get_filtered_sc_output_events_iter(&event_filter)
.count();
if event_per_op >= self.config.max_event_per_operation {
let event_per_op = context.event_count_in_current_exec as usize;

if event_per_op > self.config.max_event_per_operation {
bail!("Too many event for this operation");
}
}
Expand All @@ -1268,21 +1259,12 @@ impl Interface for InterfaceImpl {
let mut context = context_guard!(self);
let event = context.event_create(data_str, false);

let event_filter = EventFilter {
start: None,
end: None,
emitter_address: None,
original_caller_address: None,
original_operation_id: event.context.origin_operation_id,
is_final: None,
is_error: None,
};
context.event_count_in_current_exec = context.event_count_in_current_exec.saturating_add(1);

if execution_component_version > 0 {
let event_per_op = context
.events
.get_filtered_sc_output_events_iter(&event_filter)
.count();
if event_per_op >= self.config.max_event_per_operation {
let event_per_op = context.event_count_in_current_exec as usize;

if event_per_op > self.config.max_event_per_operation {
bail!("Too many event for this operation");
}
}
Expand Down

0 comments on commit c6a6910

Please sign in to comment.