From a972ba97c356e80418b23d273715cc6913b79ef4 Mon Sep 17 00:00:00 2001 From: sydhds Date: Tue, 10 Dec 2024 15:08:20 +0100 Subject: [PATCH] Use scope --- massa-event-cache/src/controller.rs | 109 +++++++++++++++------------- 1 file changed, 58 insertions(+), 51 deletions(-) diff --git a/massa-event-cache/src/controller.rs b/massa-event-cache/src/controller.rs index 573537373b..ee7b1af955 100644 --- a/massa-event-cache/src/controller.rs +++ b/massa-event-cache/src/controller.rs @@ -62,65 +62,72 @@ impl EventCacheController for EventCacheControllerImpl { } fn get_filtered_sc_output_events(&self, filter: &EventFilter) -> Vec { - let lock_0 = self.input_data.1.lock(); - #[allow(clippy::unnecessary_filter_map)] - let it = lock_0.events.iter().filter_map(|event| { - if let Some(start) = filter.start { - if event.context.slot < start { - return None; + let mut res_0 = { + // Read from new events first + let lock_0 = self.input_data.1.lock(); + #[allow(clippy::unnecessary_filter_map)] + let it = lock_0.events.iter().filter_map(|event| { + if let Some(start) = filter.start { + if event.context.slot < start { + return None; + } } - } - if let Some(end) = filter.end { - if event.context.slot >= end { - return None; + if let Some(end) = filter.end { + if event.context.slot >= end { + return None; + } } - } - if let Some(is_final) = filter.is_final { - if event.context.is_final != is_final { - return None; + if let Some(is_final) = filter.is_final { + if event.context.is_final != is_final { + return None; + } } - } - if let Some(is_error) = filter.is_error { - if event.context.is_error != is_error { - return None; + if let Some(is_error) = filter.is_error { + if event.context.is_error != is_error { + return None; + } } - } - match ( - filter.original_caller_address, - event.context.call_stack.front(), - ) { - (Some(addr1), Some(addr2)) if addr1 != *addr2 => return None, - (Some(_), None) => return None, - _ => (), - } - match (filter.emitter_address, event.context.call_stack.back()) { - (Some(addr1), Some(addr2)) if addr1 != *addr2 => return None, - (Some(_), None) => return None, - _ => (), - } - match ( - filter.original_operation_id, - event.context.origin_operation_id, - ) { - (Some(addr1), Some(addr2)) if addr1 != addr2 => return None, - (Some(_), None) => return None, - _ => (), - } - Some(event) - }); - - let mut res_0: BTreeSet = it.cloned().collect(); - // Drop the lock on the queue as soon as possible to avoid deadlocks - drop(lock_0); + match ( + filter.original_caller_address, + event.context.call_stack.front(), + ) { + (Some(addr1), Some(addr2)) if addr1 != *addr2 => return None, + (Some(_), None) => return None, + _ => (), + } + match (filter.emitter_address, event.context.call_stack.back()) { + (Some(addr1), Some(addr2)) if addr1 != *addr2 => return None, + (Some(_), None) => return None, + _ => (), + } + match ( + filter.original_operation_id, + event.context.origin_operation_id, + ) { + (Some(addr1), Some(addr2)) if addr1 != addr2 => return None, + (Some(_), None) => return None, + _ => (), + } + Some(event) + }); - let lock = self.cache.read(); + let res_0: BTreeSet = it.cloned().collect(); + // Drop the lock on the queue as soon as possible to avoid deadlocks + drop(lock_0); + res_0 + }; - let (_, res_1) = lock.get_filtered_sc_output_events(filter); - // Drop the lock on the event cache db asap - drop(lock); + let res_1 = { + // Read from db (on disk) events + let lock = self.cache.read(); + let (_, res_1) = lock.get_filtered_sc_output_events(filter); + // Drop the lock on the event cache db asap + drop(lock); + res_1 + }; + // Merge results let res_1: BTreeSet = BTreeSet::from_iter(res_1); - res_0.extend(res_1); Vec::from_iter(res_0) }