Skip to content

Commit

Permalink
[refactor] #1981, #4195: Refactor the event filters
Browse files Browse the repository at this point in the history
- rename the `Filter` trait to `EventFilter`
- "manually" implement all the event filters circumventing the macro
  - as a consequence, move all the filters to a separate module
- change the filter types naming scheme to be more logical:
  - AccountFilter -> AccountEventFilter (filters the event as a whole: by origin id and event type)
  - AccountEventFilter -> AccountEventMatcher (matches over a type of event)
- flattens the event filter hierarchy (account event is a kind of domain event, but account event filter is not)

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Jan 29, 2024
1 parent 925b556 commit d396956
Show file tree
Hide file tree
Showing 20 changed files with 564 additions and 575 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 12 additions & 21 deletions client/tests/integration/triggers/data_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ fn must_execute_both_triggers() -> Result<()> {
Repeats::Indefinitely,
account_id.clone(),
// FIXME: rewrite the filters using the builder DSL https://github.com/hyperledger/iroha/issues/3068
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByDomain(BySome(
DomainFilter::new(
AcceptAll,
BySome(DomainEventFilter::ByAccount(BySome(AccountFilter::new(
AcceptAll,
BySome(AccountEventFilter::ByCreated),
)))),
),
)))),
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByAccount(AccountEventFilter {
id_matcher: None,
event_matcher: Some(AccountEventMatcher::ByCreated),
}))),
),
));
test_client.submit_blocking(register_trigger)?;
Expand All @@ -40,9 +35,10 @@ fn must_execute_both_triggers() -> Result<()> {
[instruction],
Repeats::Indefinitely,
account_id,
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByDomain(BySome(
DomainFilter::new(AcceptAll, BySome(DomainEventFilter::ByCreated)),
)))),
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByDomain(DomainEventFilter {
id_matcher: None,
event_matcher: Some(DomainEventMatcher::ByCreated),
}))),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -94,15 +90,10 @@ fn domain_scoped_trigger_must_be_executed_only_on_events_in_its_domain() -> Resu
Repeats::Indefinitely,
account_id,
// FIXME: rewrite the filters using the builder DSL https://github.com/hyperledger/iroha/issues/3068
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByDomain(BySome(
DomainFilter::new(
AcceptAll,
BySome(DomainEventFilter::ByAccount(BySome(AccountFilter::new(
AcceptAll,
BySome(AccountEventFilter::ByCreated),
)))),
),
)))),
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByAccount(AccountEventFilter {
id_matcher: None,
event_matcher: Some(AccountEventMatcher::ByCreated),
}))),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down
17 changes: 6 additions & 11 deletions client/tests/integration/triggers/event_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ fn test_mint_asset_when_new_asset_definition_created() -> Result<()> {
Repeats::Indefinitely,
account_id,
// FIXME: rewrite the filters using the builder DSL https://github.com/hyperledger/iroha/issues/3068
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByDomain(BySome(
DomainFilter::new(
AcceptAll,
BySome(DomainEventFilter::ByAssetDefinition(BySome(
AssetDefinitionFilter::new(
AcceptAll,
BySome(AssetDefinitionEventFilter::ByCreated),
),
))),
),
)))),
TriggeringFilterBox::Data(BySome(DataEntityFilter::ByAssetDefinition(
AssetDefinitionEventFilter {
id_matcher: None,
event_matcher: Some(AssetDefinitionEventMatcher::ByCreated),
},
))),
),
));
test_client.submit(register_trigger)?;
Expand Down
Binary file modified configs/peer/executor.wasm
100644 → 100755
Binary file not shown.
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod set;
/// - TODO: authority permissions.
pub mod isi {
use iroha_data_model::{
events::Filter,
events::EventFilter,
isi::error::{InvalidParameterError, RepetitionError},
trigger::prelude::*,
};
Expand Down
10 changes: 5 additions & 5 deletions core/src/smartcontracts/isi/triggers/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{fmt, num::NonZeroU64};
use indexmap::{map::Entry, IndexMap};
use iroha_crypto::HashOf;
use iroha_data_model::{
events::Filter as EventFilter,
events::EventFilter,
isi::error::{InstructionExecutionError, MathError},
prelude::*,
query::error::FindError,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub trait LoadedActionTrait {
fn clone_and_box(&self) -> LoadedAction<TriggeringFilterBox>;
}

impl<F: Filter + Into<TriggeringFilterBox> + Clone> LoadedActionTrait for LoadedAction<F> {
impl<F: EventFilter + Into<TriggeringFilterBox> + Clone> LoadedActionTrait for LoadedAction<F> {
fn executable(&self) -> &LoadedExecutable {
&self.executable
}
Expand Down Expand Up @@ -405,7 +405,7 @@ impl Set {
/// # Errors
///
/// Return [`Err`] if failed to preload wasm trigger
fn add_to<F: Filter>(
fn add_to<F: EventFilter>(
&mut self,
engine: &wasmtime::Engine,
trigger: Trigger<F>,
Expand Down Expand Up @@ -705,7 +705,7 @@ impl Set {
/// Note that this function doesn't remove the trigger from [`Set::ids`].
///
/// Returns `true` if trigger was removed and `false` otherwise.
fn remove_from<F: Filter>(
fn remove_from<F: EventFilter>(
original_contracts: &mut WasmSmartContractMap,
triggers: &mut IndexMap<TriggerId, LoadedAction<F>>,
trigger_id: &TriggerId,
Expand Down Expand Up @@ -884,7 +884,7 @@ impl Set {
}

/// Remove actions with zero execution count from `triggers`
fn remove_zeros<F: Filter>(
fn remove_zeros<F: EventFilter>(
ids: &mut IndexMap<TriggerId, TriggeringEventType>,
original_contracts: &mut WasmSmartContractMap,
triggers: &mut IndexMap<TriggerId, LoadedAction<F>>,
Expand Down
2 changes: 2 additions & 0 deletions data_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ strum = { workspace = true, features = ["derive"] }
base64 = { workspace = true, features = ["alloc"] }
once_cell = { workspace = true, optional = true }

cfg-if = "1.0.0"

[dev-dependencies]
trybuild = { workspace = true }
criterion = { workspace = true }
Expand Down
25 changes: 9 additions & 16 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(missing_docs)]

use getset::Getters;
use iroha_data_model_derive::{model, Filter, HasOrigin};
use iroha_data_model_derive::{model, HasOrigin};

pub use self::model::*;
use super::*;
Expand All @@ -17,7 +17,6 @@ macro_rules! data_event {
Eq,
PartialOrd,
Ord,
Filter,
HasOrigin,
parity_scale_codec::Decode,
parity_scale_codec::Encode,
Expand Down Expand Up @@ -587,7 +586,7 @@ mod executor {
}

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

fn matches(&self, event: &Self::Event) -> bool {
Expand Down Expand Up @@ -641,24 +640,18 @@ impl DataEvent {

pub mod prelude {
pub use super::{
account::{
AccountEvent, AccountEventFilter, AccountFilter, AccountPermissionChanged,
AccountRoleChanged,
},
account::{AccountEvent, AccountPermissionChanged, AccountRoleChanged},
asset::{
AssetChanged, AssetDefinitionEvent, AssetDefinitionEventFilter, AssetDefinitionFilter,
AssetDefinitionOwnerChanged, AssetDefinitionTotalQuantityChanged, AssetEvent,
AssetEventFilter, AssetFilter,
AssetChanged, AssetDefinitionEvent, AssetDefinitionOwnerChanged,
AssetDefinitionTotalQuantityChanged, AssetEvent,
},
config::ConfigurationEvent,
domain::{DomainEvent, DomainEventFilter, DomainFilter, DomainOwnerChanged},
domain::{DomainEvent, DomainOwnerChanged},
executor::{ExecutorEvent, ExecutorFilter},
peer::{PeerEvent, PeerEventFilter, PeerFilter},
peer::PeerEvent,
permission::PermissionTokenSchemaUpdateEvent,
role::{PermissionRemoved, RoleEvent, RoleEventFilter, RoleFilter},
trigger::{
TriggerEvent, TriggerEventFilter, TriggerFilter, TriggerNumberOfExecutionsChanged,
},
role::{PermissionRemoved, RoleEvent},
trigger::{TriggerEvent, TriggerNumberOfExecutionsChanged},
DataEvent, HasOrigin, MetadataChanged,
};
}
Loading

0 comments on commit d396956

Please sign in to comment.