Skip to content

Commit

Permalink
[refactor] #1981, #4195, #3068: Add event filters for PermissionToken…
Browse files Browse the repository at this point in the history
…SchemaUpdate, Configuration and Executor events

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Mar 7, 2024
1 parent 09983da commit 1bc76d0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 38 deletions.
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
38 changes: 4 additions & 34 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ mod executor {
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};

use iroha_data_model_derive::EventSet;

#[derive(
Debug,
Copy,
Expand All @@ -557,6 +559,7 @@ mod executor {
serde::Deserialize,
serde::Serialize,
iroha_schema::IntoSchema,
EventSet,
)]
#[non_exhaustive]
#[ffi_type]
Expand All @@ -565,39 +568,6 @@ mod executor {
pub enum ExecutorEvent {
Upgraded,
}

/// Filter for [`ExecutorEvent`].
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
parity_scale_codec::Decode,
parity_scale_codec::Encode,
serde::Deserialize,
serde::Serialize,
iroha_schema::IntoSchema,
)]
#[non_exhaustive]
#[serde(untagged)] // Unaffected by #3330, as single unit variant
#[repr(transparent)]
pub enum ExecutorFilter {
Upgraded,
}
}

#[cfg(feature = "transparent_api")]
impl super::EventFilter for ExecutorFilter {
type Event = ExecutorEvent;

fn matches(&self, event: &Self::Event) -> bool {
match (self, event) {
(Self::Upgraded, Self::Event::Upgraded) => true,
}
}
}
}

Expand Down Expand Up @@ -652,7 +622,7 @@ pub mod prelude {
},
config::{ConfigurationEvent, ConfigurationEventSet},
domain::{DomainEvent, DomainEventSet, DomainOwnerChanged},
executor::{ExecutorEvent, ExecutorFilter},
executor::{ExecutorEvent, ExecutorEventSet},
peer::{PeerEvent, PeerEventSet},
permission::PermissionTokenSchemaUpdateEvent,
role::{PermissionRemoved, RoleEvent, RoleEventSet},
Expand Down
140 changes: 136 additions & 4 deletions data_model/src/events/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ pub mod model {
Trigger(TriggerEventFilter),
/// Matches [`RoleEvent`]s
Role(RoleEventFilter),
// We didn't have filters for these events before the refactor. Should we?
// Configuration(ConfigurationEventFilter),
// Executor(ExecutorEventFilter),
/// Matches [`PermissionTokenSchemaUpdateEvent`]s
// nothing to filter for, really
PermissionTokenSchemaUpdate,
/// Matches [`ConfigurationEvent`]s
Configuration(ConfigurationEventFilter),
/// Matches [`ExecutorEvent`]s
Executor(ExecutorEventFilter),
}

/// An event filter for [`PeerEvent`]s
Expand Down Expand Up @@ -205,6 +209,49 @@ pub mod model {
/// Matches only event from this set
pub(super) event_set: RoleEventSet,
}

/// An event filter for [`ConfigurationEvent`]s
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Getters,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct ConfigurationEventFilter {
/// If specified matches only events originating from this configuration
pub(super) id_matcher: Option<super::ParameterId>,
/// Matches only event from this set
pub(super) event_set: ConfigurationEventSet,
}

/// An event filter for [`ExecutorEvent`].
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Getters,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct ExecutorEventFilter {
// executor is a global entity, so no id here
/// Matches only event from this set
pub(super) event_set: ExecutorEventSet,
}
}

impl PeerEventFilter {
Expand Down Expand Up @@ -536,6 +583,87 @@ impl super::EventFilter for RoleEventFilter {
}
}

impl ConfigurationEventFilter {
/// Creates a new [`ConfigurationEventFilter`] accepting all [`ConfigurationEvent`]s.
pub const fn new() -> Self {
Self {
id_matcher: None,
event_set: ConfigurationEventSet::all(),
}
}

/// Modifies a [`ConfigurationEventFilter`] to accept only [`ConfigurationEvent`]s originating from ids matching `id_matcher`.
pub fn only_from(mut self, id_matcher: ParameterId) -> Self {
self.id_matcher = Some(id_matcher);
self
}

/// Modifies a [`ConfigurationEventFilter`] to accept only [`ConfigurationEvent`]s of types matching `event_set`.
pub const fn only_events(mut self, event_set: ConfigurationEventSet) -> Self {
self.event_set = event_set;
self
}
}

impl Default for ConfigurationEventFilter {
fn default() -> Self {
Self::new()
}
}

#[cfg(feature = "transparent_api")]
impl super::EventFilter for ConfigurationEventFilter {
type Event = super::ConfigurationEvent;

fn matches(&self, event: &Self::Event) -> bool {
if let Some(id_matcher) = &self.id_matcher {
if id_matcher != event.origin_id() {
return false;
}
}

if !self.event_set.matches(event) {
return false;
}

true
}
}

impl ExecutorEventFilter {
/// Creates a new [`ExecutorEventFilter`] accepting all [`ExecutorEvent`]s.
pub const fn new() -> Self {
Self {
event_set: ExecutorEventSet::all(),
}
}

/// Modifies a [`ExecutorEventFilter`] to accept only [`ExecutorEvent`]s of types matching `event_set`.
pub const fn only_events(mut self, event_set: ExecutorEventSet) -> Self {
self.event_set = event_set;
self
}
}

impl Default for ExecutorEventFilter {
fn default() -> Self {
Self::new()
}
}

#[cfg(feature = "transparent_api")]
impl super::EventFilter for ExecutorEventFilter {
type Event = super::ExecutorEvent;

fn matches(&self, event: &Self::Event) -> bool {
if !self.event_set.matches(event) {
return false;
}

true
}
}

#[cfg(feature = "transparent_api")]
impl EventFilter for DataEventFilter {
type Event = DataEvent;
Expand Down Expand Up @@ -566,9 +694,13 @@ impl EventFilter for DataEventFilter {
(DataEvent::Role(event), Role(filter)) => filter.matches(event),
(DataEvent::Role(_), _) => false,

// no corresponding filters to those yet
(DataEvent::PermissionToken(_), PermissionTokenSchemaUpdate) => true,
(DataEvent::PermissionToken(_), _) => false,

(DataEvent::Configuration(event), Configuration(filter)) => filter.matches(event),
(DataEvent::Configuration(_), _) => false,

(DataEvent::Executor(event), Executor(filter)) => filter.matches(event),
(DataEvent::Executor(_), _) => false,
}
}
Expand Down
39 changes: 39 additions & 0 deletions docs/source/references/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,19 @@
}
]
},
"ConfigurationEventFilter": {
"Struct": [
{
"name": "id_matcher",
"type": "Option<ParameterId>"
},
{
"name": "event_set",
"type": "ConfigurationEventSet"
}
]
},
"ConfigurationEventSet": "u32",
"Container": {
"Enum": [
{
Expand Down Expand Up @@ -760,6 +773,20 @@
"tag": "Role",
"discriminant": 7,
"type": "RoleEventFilter"
},
{
"tag": "PermissionTokenSchemaUpdate",
"discriminant": 8
},
{
"tag": "Configuration",
"discriminant": 9,
"type": "ConfigurationEventFilter"
},
{
"tag": "Executor",
"discriminant": 10,
"type": "ExecutorEventFilter"
}
]
},
Expand Down Expand Up @@ -1008,6 +1035,15 @@
}
]
},
"ExecutorEventFilter": {
"Struct": [
{
"name": "event_set",
"type": "ExecutorEventSet"
}
]
},
"ExecutorEventSet": "u32",
"ExecutorMode": {
"Enum": [
{
Expand Down Expand Up @@ -2322,6 +2358,9 @@
"Option<NonZero<u64>>": {
"Option": "NonZero<u64>"
},
"Option<ParameterId>": {
"Option": "ParameterId"
},
"Option<PeerId>": {
"Option": "PeerId"
},
Expand Down

0 comments on commit 1bc76d0

Please sign in to comment.