Skip to content

Commit

Permalink
refactor: remove TriggeringEventFilterBox
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jul 22, 2024
1 parent b94fa54 commit ec25f64
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 145 deletions.
13 changes: 8 additions & 5 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ pub mod isi {
let triggers = &mut state_transaction.world.triggers;
let trigger_id = new_trigger.id().clone();
let success = match &new_trigger.action.filter {
TriggeringEventFilterBox::Data(_) => triggers.add_data_trigger(
EventFilterBox::Data(_) => triggers.add_data_trigger(
&engine,
new_trigger
.try_into()
.map_err(|e: &str| Error::Conversion(e.to_owned()))?,
),
TriggeringEventFilterBox::Pipeline(_) => triggers.add_pipeline_trigger(
EventFilterBox::Pipeline(_) => triggers.add_pipeline_trigger(
&engine,
new_trigger
.try_into()
.map_err(|e: &str| Error::Conversion(e.to_owned()))?,
),
TriggeringEventFilterBox::Time(time_filter) => {
EventFilterBox::Time(time_filter) => {
if let ExecutionTime::Schedule(schedule) = time_filter.0 {
match last_block_estimation {
// Genesis block
Expand Down Expand Up @@ -91,12 +91,15 @@ pub mod isi {
.map_err(|e: &str| Error::Conversion(e.to_owned()))?,
)
}
TriggeringEventFilterBox::ExecuteTrigger(_) => triggers.add_by_call_trigger(
EventFilterBox::ExecuteTrigger(_) => triggers.add_by_call_trigger(
&engine,
new_trigger
.try_into()
.map_err(|e: &str| Error::Conversion(e.to_owned()))?,
),
EventFilterBox::TriggerCompleted(_) => {
unreachable!("Disallowed during deserialization");
}
}
.map_err(|e| InvalidParameterError::Wasm(e.to_string()))?;

Expand Down Expand Up @@ -289,7 +292,7 @@ pub mod isi {
.world
.triggers
.inspect_by_id(id, |action| -> Result<(), Error> {
let allow_execute = if let TriggeringEventFilterBox::ExecuteTrigger(filter) =
let allow_execute = if let EventFilterBox::ExecuteTrigger(filter) =
action.clone_and_box().filter
{
filter.matches(&event) || action.authority() == authority
Expand Down
8 changes: 7 additions & 1 deletion core/src/smartcontracts/isi/triggers/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ impl<'set> SetBlock<'set> {
}
}

trait TriggeringEventFilter: EventFilter {}
impl TriggeringEventFilter for DataEventFilter {}
impl TriggeringEventFilter for PipelineEventFilterBox {}
impl TriggeringEventFilter for TimeEventFilter {}
impl TriggeringEventFilter for ExecuteTriggerEventFilter {}

impl<'block, 'set> SetTransaction<'block, 'set> {
/// Apply transaction's changes
pub fn apply(self) {
Expand Down Expand Up @@ -704,7 +710,7 @@ impl<'block, 'set> SetTransaction<'block, 'set> {
/// # Errors
///
/// Return [`Err`] if failed to preload wasm trigger
fn add_to<F: storage::Value + EventFilter>(
fn add_to<F: TriggeringEventFilter + storage::Value>(
&mut self,
engine: &wasmtime::Engine,
trigger: SpecializedTrigger<F>,
Expand Down
45 changes: 21 additions & 24 deletions core/src/smartcontracts/isi/triggers/specialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use derive_more::Constructor;
use iroha_crypto::HashOf;
use iroha_data_model::{
account::AccountId,
events::{EventFilter, TriggeringEventFilterBox},
events::{EventFilter, EventFilterBox},
metadata::Metadata,
prelude::*,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<F> SpecializedAction<F> {

impl<F> From<SpecializedAction<F>> for Action
where
F: Into<TriggeringEventFilterBox>,
F: Into<EventFilterBox>,
{
fn from(value: SpecializedAction<F>) -> Self {
Action {
Expand Down Expand Up @@ -81,7 +81,7 @@ macro_rules! impl_try_from_box {
type Error = &'static str;

fn try_from(boxed: Trigger) -> Result<Self, Self::Error> {
if let TriggeringEventFilterBox::$variant(concrete_filter) = boxed.action.filter {
if let EventFilterBox::$variant(concrete_filter) = boxed.action.filter {
let action = SpecializedAction::new(
boxed.action.executable,
boxed.action.repeats,
Expand All @@ -93,7 +93,7 @@ macro_rules! impl_try_from_box {
action,
})
} else {
Err(concat!("Expected `TriggeringEventFilterBox::", stringify!($variant),"`, but another variant found"))
Err(concat!("Expected `EventFilterBox::", stringify!($variant),"`, but another variant found"))
}
}
}
Expand Down Expand Up @@ -159,15 +159,13 @@ pub trait LoadedActionTrait {
fn mintable(&self) -> bool;

/// Convert action to a boxed representation
fn into_boxed(self) -> LoadedAction<TriggeringEventFilterBox>;
fn into_boxed(self) -> LoadedAction<EventFilterBox>;

/// Same as [`into_boxed()`](LoadedActionTrait::into_boxed) but clones `self`
fn clone_and_box(&self) -> LoadedAction<TriggeringEventFilterBox>;
fn clone_and_box(&self) -> LoadedAction<EventFilterBox>;
}

impl<F: EventFilter + Into<TriggeringEventFilterBox> + Clone> LoadedActionTrait
for LoadedAction<F>
{
impl<F: EventFilter + Into<EventFilterBox> + Clone> LoadedActionTrait for LoadedAction<F> {
fn executable(&self) -> &ExecutableRef {
&self.executable
}
Expand Down Expand Up @@ -196,7 +194,7 @@ impl<F: EventFilter + Into<TriggeringEventFilterBox> + Clone> LoadedActionTrait
self.filter.mintable()
}

fn into_boxed(self) -> LoadedAction<TriggeringEventFilterBox> {
fn into_boxed(self) -> LoadedAction<EventFilterBox> {
let Self {
executable,
repeats,
Expand All @@ -214,7 +212,7 @@ impl<F: EventFilter + Into<TriggeringEventFilterBox> + Clone> LoadedActionTrait
}
}

fn clone_and_box(&self) -> LoadedAction<TriggeringEventFilterBox> {
fn clone_and_box(&self) -> LoadedAction<EventFilterBox> {
self.clone().into_boxed()
}
}
Expand All @@ -225,30 +223,29 @@ mod tests {

#[test]
fn trigger_with_filterbox_can_be_unboxed() {
/// Should fail to compile if a new variant will be added to `TriggeringEventFilterBox`
/// Should fail to compile if a new variant will be added to `EventFilterBox`
#[allow(dead_code)]
fn compile_time_check(boxed: Trigger) {
match &boxed.action.filter {
TriggeringEventFilterBox::Data(_) => {
SpecializedTrigger::<DataEventFilter>::try_from(boxed)
.map(|_| ())
.unwrap()
}
TriggeringEventFilterBox::Pipeline(_) => {
EventFilterBox::Data(_) => SpecializedTrigger::<DataEventFilter>::try_from(boxed)
.map(|_| ())
.unwrap(),
EventFilterBox::Pipeline(_) => {
SpecializedTrigger::<PipelineEventFilterBox>::try_from(boxed)
.map(|_| ())
.unwrap()
}
TriggeringEventFilterBox::Time(_) => {
SpecializedTrigger::<TimeEventFilter>::try_from(boxed)
.map(|_| ())
.unwrap()
}
TriggeringEventFilterBox::ExecuteTrigger(_) => {
EventFilterBox::Time(_) => SpecializedTrigger::<TimeEventFilter>::try_from(boxed)
.map(|_| ())
.unwrap(),
EventFilterBox::ExecuteTrigger(_) => {
SpecializedTrigger::<ExecuteTriggerEventFilter>::try_from(boxed)
.map(|_| ())
.unwrap()
}
EventFilterBox::TriggerCompleted(_) => {
unreachable!("Disallowed during deserialization")
}
}
}
}
Expand Down
71 changes: 1 addition & 70 deletions data_model/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,6 @@ mod model {
/// Listen to trigger completion event with filter.
TriggerCompleted(trigger_completed::TriggerCompletedEventFilter),
}

/// Event filter which could be attached to trigger.
#[allow(variant_size_differences)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
FromVariant,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
// TODO: Temporarily made opaque
#[ffi_type(opaque)]
pub enum TriggeringEventFilterBox {
/// Listen to pipeline events with filter.
Pipeline(pipeline::PipelineEventFilterBox),
/// Listen to data events with filter.
Data(data::DataEventFilter),
/// Listen to time events with filter.
Time(time::TimeEventFilter),
/// Listen to trigger execution event with filter.
ExecuteTrigger(execute_trigger::ExecuteTriggerEventFilter),
}
}

impl From<TransactionEvent> for EventBox {
Expand Down Expand Up @@ -232,32 +203,6 @@ impl EventFilter for EventFilterBox {
}
}

#[cfg(feature = "transparent_api")]
impl EventFilter for TriggeringEventFilterBox {
type Event = EventBox;

/// Apply filter to event.
fn matches(&self, event: &EventBox) -> bool {
match (event, self) {
(EventBox::Pipeline(event), Self::Pipeline(filter)) => filter.matches(event),
(EventBox::Data(event), Self::Data(filter)) => filter.matches(event),
(EventBox::Time(event), Self::Time(filter)) => filter.matches(event),
(EventBox::ExecuteTrigger(event), Self::ExecuteTrigger(filter)) => {
filter.matches(event)
}
// Fail to compile in case when new variant to event or filter is added
(
EventBox::Pipeline(_)
| EventBox::Data(_)
| EventBox::Time(_)
| EventBox::ExecuteTrigger(_)
| EventBox::TriggerCompleted(_),
Self::Pipeline(_) | Self::Data(_) | Self::Time(_) | Self::ExecuteTrigger(_),
) => false,
}
}
}

mod conversions {
use super::{
pipeline::{BlockEventFilter, TransactionEventFilter},
Expand Down Expand Up @@ -300,19 +245,6 @@ mod conversions {
ConfigurationEventFilter => DataEventFilter => EventFilterBox,
ExecutorEventFilter => DataEventFilter => EventFilterBox,

PeerEventFilter => DataEventFilter => TriggeringEventFilterBox,
DomainEventFilter => DataEventFilter => TriggeringEventFilterBox,
AccountEventFilter => DataEventFilter => TriggeringEventFilterBox,
AssetEventFilter => DataEventFilter => TriggeringEventFilterBox,
AssetDefinitionEventFilter => DataEventFilter => TriggeringEventFilterBox,
TriggerEventFilter => DataEventFilter => TriggeringEventFilterBox,
RoleEventFilter => DataEventFilter => TriggeringEventFilterBox,
ConfigurationEventFilter => DataEventFilter => TriggeringEventFilterBox,
ExecutorEventFilter => DataEventFilter => TriggeringEventFilterBox,

TransactionEventFilter => PipelineEventFilterBox => TriggeringEventFilterBox,
BlockEventFilter => PipelineEventFilterBox => TriggeringEventFilterBox,

TransactionEventFilter => PipelineEventFilterBox => EventFilterBox,
BlockEventFilter => PipelineEventFilterBox => EventFilterBox,
}
Expand Down Expand Up @@ -361,7 +293,6 @@ pub mod prelude {
pub use super::EventFilter;
pub use super::{
data::prelude::*, execute_trigger::prelude::*, pipeline::prelude::*, time::prelude::*,
trigger_completed::prelude::*, EventBox, EventFilterBox, TriggeringEventFilterBox,
TriggeringEventType,
trigger_completed::prelude::*, EventBox, EventFilterBox, TriggeringEventType,
};
}
4 changes: 0 additions & 4 deletions data_model/src/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,10 +1467,6 @@ pub mod error {
pub enum InvalidParameterError {
/// Invalid WASM binary: {0}
Wasm(String),
/// Name length violation
///
/// i.e. too long [`AccountId`]
NameLength,
/// Attempt to register a time-trigger with `start` point in the past
TimeTriggerInThePast,
}
Expand Down
4 changes: 2 additions & 2 deletions data_model/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use self::{
use crate::{
account::{Account, AccountId},
block::{BlockHeader, SignedBlock},
events::TriggeringEventFilterBox,
events::EventFilterBox,
seal,
transaction::{CommittedTransaction, SignedTransaction, TransactionPayload},
IdBox, Identifiable, IdentifiableBox,
Expand Down Expand Up @@ -1031,7 +1031,7 @@ pub mod trigger {
use crate::{
account::AccountId,
domain::prelude::*,
events::TriggeringEventFilterBox,
events::EventFilterBox,
prelude::InstructionBox,
trigger::{Trigger, TriggerId},
Executable, Identifiable, Name,
Expand Down
Loading

0 comments on commit ec25f64

Please sign in to comment.