diff --git a/tee-worker/common/core-primitives/types/src/parentchain/mod.rs b/tee-worker/common/core-primitives/types/src/parentchain/mod.rs index 64a5eddfc4..4345e31233 100644 --- a/tee-worker/common/core-primitives/types/src/parentchain/mod.rs +++ b/tee-worker/common/core-primitives/types/src/parentchain/mod.rs @@ -98,31 +98,34 @@ pub trait IdentifyParentchain { pub trait FilterEvents { type Error: From + Debug; - fn get_link_identity_events(&self) -> Result, Self::Error>; + fn get_link_identity_events(&mut self) -> Result, Self::Error>; - fn get_vc_requested_events(&self) -> Result, Self::Error>; + fn get_vc_requested_events(&mut self) -> Result, Self::Error>; fn get_deactivate_identity_events( - &self, + &mut self, ) -> Result, Self::Error>; - fn get_activate_identity_events(&self) -> Result, Self::Error>; + fn get_activate_identity_events( + &mut self, + ) -> Result, Self::Error>; - fn get_enclave_unauthorized_events(&self) -> Result, Self::Error>; + fn get_enclave_unauthorized_events(&mut self) -> Result, Self::Error>; - fn get_opaque_task_posted_events(&self) -> Result, Self::Error>; + fn get_opaque_task_posted_events(&mut self) -> Result, Self::Error>; - fn get_assertion_created_events(&self) -> Result, Self::Error>; + fn get_assertion_created_events(&mut self) -> Result, Self::Error>; fn get_parentchain_block_proccessed_events( - &self, + &mut self, ) -> Result, Self::Error>; - fn get_enclave_added_events(&self) -> Result, Self::Error>; + fn get_enclave_added_events(&mut self) -> Result, Self::Error>; - fn get_enclave_removed_events(&self) -> Result, Self::Error>; + fn get_enclave_removed_events(&mut self) -> Result, Self::Error>; - fn get_account_store_updated_events(&self) -> Result, Self::Error>; + fn get_account_store_updated_events(&mut self) + -> Result, Self::Error>; } #[derive(Debug)] @@ -143,7 +146,7 @@ where fn handle_events( &self, executor: &Executor, - events: impl FilterEvents, + events: &mut impl FilterEvents, block_number: <::Header as ParentchainHeader>::Number, ) -> Result where diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_filter.rs b/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_filter.rs index c6fcf4d94a..1776f57a8a 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_filter.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_filter.rs @@ -16,25 +16,58 @@ */ //! Various way to filter Parentchain events -use itc_parentchain_indirect_calls_executor::event_filter::ToEvents; -use itp_api_client_types::Events; - +use itp_api_client_types::{EventDetails, Events}; use itp_node_api::api_client::StaticEvent; use itp_types::{ parentchain::{events::*, FilterEvents}, H256, }; -use std::vec::Vec; +use std::{vec, vec::Vec}; #[derive(Clone)] -pub struct FilterableEvents(pub Events); +pub struct FilterableEvents { + events: Vec>, +} impl FilterableEvents { - fn filter(&self) -> Result, E> { + pub fn new(events: Events) -> Self { + let mut interesting_events = vec![]; + events.iter().flatten().for_each(|ev| { + // lets keep only events worker is interested in + if ev.pallet_name() == LinkIdentityRequested::PALLET + && ev.variant_name() == LinkIdentityRequested::EVENT + || ev.pallet_name() == VCRequested::PALLET + && ev.variant_name() == VCRequested::EVENT + || ev.pallet_name() == DeactivateIdentityRequested::PALLET + && ev.variant_name() == DeactivateIdentityRequested::EVENT + || ev.pallet_name() == ActivateIdentityRequested::PALLET + && ev.variant_name() == ActivateIdentityRequested::EVENT + || ev.pallet_name() == EnclaveUnauthorized::PALLET + && ev.variant_name() == EnclaveUnauthorized::EVENT + || ev.pallet_name() == OpaqueTaskPosted::PALLET + && ev.variant_name() == OpaqueTaskPosted::EVENT + || ev.pallet_name() == AssertionCreated::PALLET + && ev.variant_name() == AssertionCreated::EVENT + || ev.pallet_name() == ParentchainBlockProcessed::PALLET + && ev.variant_name() == ParentchainBlockProcessed::EVENT + || ev.pallet_name() == EnclaveAdded::PALLET + && ev.variant_name() == EnclaveAdded::EVENT + || ev.pallet_name() == EnclaveRemoved::PALLET + && ev.variant_name() == EnclaveRemoved::EVENT + || ev.pallet_name() == AccountStoreUpdated::PALLET + && ev.variant_name() == AccountStoreUpdated::EVENT + { + interesting_events.push(ev) + } + }); + + Self { events: interesting_events } + } + + fn filter(&mut self) -> Result, E> { Ok(self - .to_events() + .events .iter() - .flatten() .filter_map(|ev| match ev.as_event::() { Ok(maybe_event) => maybe_event, Err(e) => { @@ -46,67 +79,64 @@ impl FilterableEvents { } } -// todo: improve: https://github.com/integritee-network/worker/pull/1378#discussion_r1393933766 -impl ToEvents> for FilterableEvents { - fn to_events(&self) -> &Events { - &self.0 - } -} - impl From> for FilterableEvents { fn from(ev: Events) -> Self { - Self(ev) + Self::new(ev) } } impl FilterEvents for FilterableEvents { type Error = itc_parentchain_indirect_calls_executor::Error; - fn get_link_identity_events(&self) -> Result, Self::Error> { + fn get_link_identity_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_vc_requested_events(&self) -> Result, Self::Error> { + fn get_vc_requested_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_deactivate_identity_events( - &self, + &mut self, ) -> Result, Self::Error> { self.filter() } - fn get_activate_identity_events(&self) -> Result, Self::Error> { + fn get_activate_identity_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } - fn get_enclave_unauthorized_events(&self) -> Result, Self::Error> { + fn get_enclave_unauthorized_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_opaque_task_posted_events(&self) -> Result, Self::Error> { + fn get_opaque_task_posted_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_assertion_created_events(&self) -> Result, Self::Error> { + fn get_assertion_created_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_parentchain_block_proccessed_events( - &self, + &mut self, ) -> Result, Self::Error> { self.filter() } - fn get_enclave_added_events(&self) -> Result, Self::Error> { + fn get_enclave_added_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_enclave_removed_events(&self) -> Result, Self::Error> { + fn get_enclave_removed_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_account_store_updated_events(&self) -> Result, Self::Error> { + fn get_account_store_updated_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } } diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_handler.rs b/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_handler.rs index 39569551cd..13c0b101e5 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_handler.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/integritee/event_handler.rs @@ -255,7 +255,7 @@ where fn handle_events( &self, executor: &Executor, - events: impl FilterEvents, + events: &mut impl FilterEvents, block_number: <::Header as ParentchainHeader>::Number, ) -> Result where diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_filter.rs b/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_filter.rs index 95659a3a37..842bc49dd9 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_filter.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_filter.rs @@ -60,51 +60,55 @@ impl From> for FilterableEvents { impl FilterEvents for FilterableEvents { type Error = itc_parentchain_indirect_calls_executor::Error; - fn get_link_identity_events(&self) -> Result, Self::Error> { + fn get_link_identity_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_vc_requested_events(&self) -> Result, Self::Error> { + fn get_vc_requested_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_deactivate_identity_events( - &self, + &mut self, ) -> Result, Self::Error> { self.filter() } - fn get_activate_identity_events(&self) -> Result, Self::Error> { + fn get_activate_identity_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } - fn get_enclave_unauthorized_events(&self) -> Result, Self::Error> { + fn get_enclave_unauthorized_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_opaque_task_posted_events(&self) -> Result, Self::Error> { + fn get_opaque_task_posted_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_assertion_created_events(&self) -> Result, Self::Error> { + fn get_assertion_created_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_parentchain_block_proccessed_events( - &self, + &mut self, ) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_enclave_added_events(&self) -> Result, Self::Error> { + fn get_enclave_added_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_enclave_removed_events(&self) -> Result, Self::Error> { + fn get_enclave_removed_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_account_store_updated_events(&self) -> Result, Self::Error> { + fn get_account_store_updated_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } } diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_handler.rs b/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_handler.rs index e288b5a0bb..f0567a1814 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_handler.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/target_a/event_handler.rs @@ -36,7 +36,7 @@ where fn handle_events( &self, _executor: &Executor, - _events: impl FilterEvents, + _events: &mut impl FilterEvents, _block_number: <::Header as ParentchainHeader>::Number, ) -> Result where diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_filter.rs b/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_filter.rs index 95659a3a37..842bc49dd9 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_filter.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_filter.rs @@ -60,51 +60,55 @@ impl From> for FilterableEvents { impl FilterEvents for FilterableEvents { type Error = itc_parentchain_indirect_calls_executor::Error; - fn get_link_identity_events(&self) -> Result, Self::Error> { + fn get_link_identity_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_vc_requested_events(&self) -> Result, Self::Error> { + fn get_vc_requested_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_deactivate_identity_events( - &self, + &mut self, ) -> Result, Self::Error> { self.filter() } - fn get_activate_identity_events(&self) -> Result, Self::Error> { + fn get_activate_identity_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } - fn get_enclave_unauthorized_events(&self) -> Result, Self::Error> { + fn get_enclave_unauthorized_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_opaque_task_posted_events(&self) -> Result, Self::Error> { + fn get_opaque_task_posted_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_assertion_created_events(&self) -> Result, Self::Error> { + fn get_assertion_created_events(&mut self) -> Result, Self::Error> { self.filter() } fn get_parentchain_block_proccessed_events( - &self, + &mut self, ) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_enclave_added_events(&self) -> Result, Self::Error> { + fn get_enclave_added_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_enclave_removed_events(&self) -> Result, Self::Error> { + fn get_enclave_removed_events(&mut self) -> Result, Self::Error> { self.filter() } - fn get_account_store_updated_events(&self) -> Result, Self::Error> { + fn get_account_store_updated_events( + &mut self, + ) -> Result, Self::Error> { self.filter() } } diff --git a/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_handler.rs b/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_handler.rs index be64ba5b1b..48eef5491a 100644 --- a/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_handler.rs +++ b/tee-worker/identity/app-libs/parentchain-interface/src/target_b/event_handler.rs @@ -36,7 +36,7 @@ where fn handle_events( &self, _executor: &Executor, - _events: impl FilterEvents, + _events: &mut impl FilterEvents, _block_number: <::Header as ParentchainHeader>::Number, ) -> Result where diff --git a/tee-worker/identity/core/parentchain/indirect-calls-executor/src/executor.rs b/tee-worker/identity/core/parentchain/indirect-calls-executor/src/executor.rs index e8a24a10e4..b0f92157cf 100644 --- a/tee-worker/identity/core/parentchain/indirect-calls-executor/src/executor.rs +++ b/tee-worker/identity/core/parentchain/indirect-calls-executor/src/executor.rs @@ -164,7 +164,7 @@ impl< trace!("Scanning block {:?} for relevant events", block_number); - let events = self + let mut events = self .node_meta_data_provider .get_from_metadata(|metadata| { EventCreator::create_from_metadata(metadata.clone(), block_hash, events) @@ -173,7 +173,7 @@ impl< let (processed_events, successful_assertion_ids, failed_assertion_ids) = self .parentchain_event_handler - .handle_events::(self, events, block_number)?; + .handle_events::(self, &mut events, block_number)?; let mut calls: Vec = Vec::new(); if !successful_assertion_ids.is_empty() { calls.extend(self.create_assertion_stored_call(successful_assertion_ids)?); diff --git a/tee-worker/identity/core/parentchain/indirect-calls-executor/src/mock.rs b/tee-worker/identity/core/parentchain/indirect-calls-executor/src/mock.rs index 16a95d30ce..f0451657c2 100644 --- a/tee-worker/identity/core/parentchain/indirect-calls-executor/src/mock.rs +++ b/tee-worker/identity/core/parentchain/indirect-calls-executor/src/mock.rs @@ -28,53 +28,57 @@ pub struct MockEvents; impl FilterEvents for MockEvents { type Error = (); - fn get_opaque_task_posted_events(&self) -> Result, Self::Error> { + fn get_opaque_task_posted_events(&mut self) -> Result, Self::Error> { let opaque_task_posted_event = OpaqueTaskPosted { request: RsaRequest::new(H256::default(), Vec::from([0u8; 32])) }; Ok(Vec::from([opaque_task_posted_event])) } - fn get_link_identity_events(&self) -> Result, Self::Error> { + fn get_link_identity_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_vc_requested_events(&self) -> Result, Self::Error> { + fn get_vc_requested_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } fn get_deactivate_identity_events( - &self, + &mut self, ) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_activate_identity_events(&self) -> Result, Self::Error> { + fn get_activate_identity_events( + &mut self, + ) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_enclave_unauthorized_events(&self) -> Result, Self::Error> { + fn get_enclave_unauthorized_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_assertion_created_events(&self) -> Result, Self::Error> { + fn get_assertion_created_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } fn get_parentchain_block_proccessed_events( - &self, + &mut self, ) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_enclave_added_events(&self) -> Result, Self::Error> { + fn get_enclave_added_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_enclave_removed_events(&self) -> Result, Self::Error> { + fn get_enclave_removed_events(&mut self) -> Result, Self::Error> { Ok(Vec::new()) } - fn get_account_store_updated_events(&self) -> Result, Self::Error> { + fn get_account_store_updated_events( + &mut self, + ) -> Result, Self::Error> { Ok(Vec::new()) } } @@ -90,7 +94,7 @@ where fn handle_events( &self, _: &Executor, - _: impl FilterEvents, + _: &mut impl FilterEvents, _block_number: <::Header as ParentchainHeader>::Number, ) -> Result where