Skip to content

Commit

Permalink
[refactor] #4259: Use conversions when constructing Actions
Browse files Browse the repository at this point in the history
Make Action's constructor accept `impl Into<TriggeringEventFilterBox>`. Add shorthand conversions from concrete data event filter types into the top-level boxes

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Mar 19, 2024
1 parent 7c89588 commit f7bb96f
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 97 deletions.
4 changes: 1 addition & 3 deletions client/tests/integration/domain_owner_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ fn domain_owner_trigger_permissions() -> Result<()> {
trigger_instructions,
Repeats::from(2_u32),
bob_id,
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new().for_trigger(trigger_id.clone()),
),
ExecuteTriggerEventFilter::new().for_trigger(trigger_id.clone()),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down
16 changes: 6 additions & 10 deletions client/tests/integration/events/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ fn trigger_completion_success_should_produce_event() -> Result<()> {
vec![InstructionBox::from(instruction)],
Repeats::Indefinitely,
asset_id.account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(asset_id.account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(asset_id.account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -69,11 +67,9 @@ fn trigger_completion_failure_should_produce_event() -> Result<()> {
vec![InstructionBox::from(instruction)],
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down
85 changes: 32 additions & 53 deletions client/tests/integration/triggers/by_call_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use iroha_client::{
data_model::{
prelude::*,
query::error::{FindError, QueryExecutionFail},
transaction::Executable,
transaction::{Executable, WasmSmartContract},
},
};
use iroha_data_model::{events::TriggeringEventFilterBox, transaction::WasmSmartContract};
use iroha_genesis::GenesisNetwork;
use iroha_logger::info;
use test_network::*;
Expand Down Expand Up @@ -124,11 +123,9 @@ fn trigger_failure_should_not_cancel_other_triggers_execution() -> Result<()> {
bad_trigger_instructions,
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(bad_trigger_id.clone())
.under_authority(account_id.clone()),
),
ExecuteTriggerEventFilter::new()
.for_trigger(bad_trigger_id.clone())
.under_authority(account_id.clone()),
),
));
test_client.submit(register_bad_trigger)?;
Expand All @@ -143,7 +140,7 @@ fn trigger_failure_should_not_cancel_other_triggers_execution() -> Result<()> {
Repeats::Indefinitely,
account_id,
// Time-triggers (which are Pre-commit triggers) will be executed last
TriggeringEventFilterBox::Time(TimeEventFilter::new(ExecutionTime::PreCommit)),
TimeEventFilter::new(ExecutionTime::PreCommit),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -180,11 +177,9 @@ fn trigger_should_not_be_executed_with_zero_repeats_count() -> Result<()> {
trigger_instructions,
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -245,11 +240,9 @@ fn trigger_should_be_able_to_modify_its_own_repeats_count() -> Result<()> {
trigger_instructions,
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -289,11 +282,9 @@ fn unregister_trigger() -> Result<()> {
Vec::<InstructionBox>::new(),
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
);
let register_trigger = Register::trigger(trigger.clone());
Expand Down Expand Up @@ -367,11 +358,9 @@ fn trigger_in_genesis_using_base64() -> Result<()> {
.wrap_err("Can't deserialize wasm using base64")?,
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id.clone()),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id.clone()),
),
);

Expand Down Expand Up @@ -418,11 +407,9 @@ fn trigger_should_be_able_to_modify_other_trigger() -> Result<()> {
trigger_unregister_instructions,
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id_unregister.clone())
.under_authority(account_id.clone()),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id_unregister.clone())
.under_authority(account_id.clone()),
),
));
test_client.submit_blocking(register_trigger)?;
Expand All @@ -435,11 +422,9 @@ fn trigger_should_be_able_to_modify_other_trigger() -> Result<()> {
trigger_should_be_unregistered_instructions,
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id_to_be_unregistered.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id_to_be_unregistered.clone())
.under_authority(account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -480,11 +465,9 @@ fn trigger_burn_repetitions() -> Result<()> {
trigger_instructions,
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -525,11 +508,9 @@ fn unregistering_one_of_two_triggers_with_identical_wasm_should_not_cause_origin
wasm.clone(),
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id)
.under_authority(account_id.clone()),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id)
.under_authority(account_id.clone()),
),
)
};
Expand Down Expand Up @@ -576,11 +557,9 @@ fn build_register_trigger_isi(
trigger_instructions,
Repeats::Indefinitely,
asset_id.account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id)
.under_authority(asset_id.account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id)
.under_authority(asset_id.account_id),
),
))
}
12 changes: 3 additions & 9 deletions client/tests/integration/triggers/data_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ fn must_execute_both_triggers() -> Result<()> {
[instruction.clone()],
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::Data(DataEventFilter::Account(
AccountEventFilter::new().for_events(AccountEventSet::Created),
)),
AccountEventFilter::new().for_events(AccountEventSet::Created),
),
));
test_client.submit_blocking(register_trigger)?;
Expand All @@ -36,9 +34,7 @@ fn must_execute_both_triggers() -> Result<()> {
[instruction],
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::Domain(
DomainEventFilter::new().for_events(DomainEventSet::Created),
)),
DomainEventFilter::new().for_events(DomainEventSet::Created),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down Expand Up @@ -88,9 +84,7 @@ fn domain_scoped_trigger_must_be_executed_only_on_events_in_its_domain() -> Resu
[Mint::asset_numeric(1u32, asset_id.clone())],
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::Account(
AccountEventFilter::new().for_events(AccountEventSet::Created),
)),
AccountEventFilter::new().for_events(AccountEventSet::Created),
),
));
test_client.submit_blocking(register_trigger)?;
Expand Down
4 changes: 1 addition & 3 deletions client/tests/integration/triggers/event_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ fn test_mint_asset_when_new_asset_definition_created() -> Result<()> {
vec![instruction],
Repeats::Indefinitely,
account_id,
TriggeringEventFilterBox::Data(DataEventFilter::AssetDefinition(
AssetDefinitionEventFilter::new().for_events(AssetDefinitionEventSet::Created),
)),
AssetDefinitionEventFilter::new().for_events(AssetDefinitionEventSet::Created),
),
));
test_client.submit(register_trigger)?;
Expand Down
8 changes: 4 additions & 4 deletions client/tests/integration/triggers/time_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn time_trigger_execution_count_error_should_be_less_than_15_percent() -> Result
vec![instruction],
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::Time(TimeEventFilter::new(ExecutionTime::Schedule(schedule))),
TimeEventFilter::new(ExecutionTime::Schedule(schedule)),
),
));
test_client.submit(register_trigger)?;
Expand Down Expand Up @@ -110,7 +110,7 @@ fn change_asset_metadata_after_1_sec() -> Result<()> {
vec![instruction],
Repeats::from(1_u32),
account_id.clone(),
TriggeringEventFilterBox::Time(TimeEventFilter::new(ExecutionTime::Schedule(schedule))),
TimeEventFilter::new(ExecutionTime::Schedule(schedule)),
),
));
test_client.submit(register_trigger)?;
Expand Down Expand Up @@ -154,7 +154,7 @@ fn pre_commit_trigger_should_be_executed() -> Result<()> {
vec![instruction],
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::Time(TimeEventFilter::new(ExecutionTime::PreCommit)),
TimeEventFilter::new(ExecutionTime::PreCommit),
),
));
test_client.submit(register_trigger)?;
Expand Down Expand Up @@ -229,7 +229,7 @@ fn mint_nft_for_every_user_every_1_sec() -> Result<()> {
WasmSmartContract::from_compiled(wasm),
Repeats::Indefinitely,
alice_id.clone(),
TriggeringEventFilterBox::Time(TimeEventFilter::new(ExecutionTime::Schedule(schedule))),
TimeEventFilter::new(ExecutionTime::Schedule(schedule)),
),
));
test_client.submit(register_trigger)?;
Expand Down
8 changes: 3 additions & 5 deletions client/tests/integration/triggers/trigger_rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ fn failed_trigger_revert() -> Result<()> {
instructions,
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id),
),
));
let _ = client.submit_blocking(register_trigger);
Expand Down
8 changes: 3 additions & 5 deletions core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,9 @@ mod tests {
Vec::<InstructionBox>::new(),
Repeats::Indefinitely,
account_id.clone(),
TriggeringEventFilterBox::ExecuteTrigger(
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id.clone()),
),
ExecuteTriggerEventFilter::new()
.for_trigger(trigger_id.clone())
.under_authority(account_id.clone()),
),
));

Expand Down
51 changes: 51 additions & 0 deletions data_model/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,57 @@ impl EventFilter for TriggeringEventFilterBox {
}
}

mod conversions {
use super::prelude::*;

macro_rules! last_tt {
($last:tt) => {
$last
};
($head:tt $($tail:tt)+) => {
last_tt!($($tail)*)
};
}

// chain multiple conversions into one
macro_rules! impl_from_via_path {
($($initial:ty $(=> $intermediate:ty)*),+ $(,)?) => {
$(
impl From<$initial> for last_tt!($($intermediate)*) {
fn from(filter: $initial) -> Self {
$(
let filter: $intermediate = filter.into();
)*
filter
}
}
)+
};
}

impl_from_via_path! {
PeerEventFilter => DataEventFilter => EventFilterBox,
DomainEventFilter => DataEventFilter => EventFilterBox,
AccountEventFilter => DataEventFilter => EventFilterBox,
AssetEventFilter => DataEventFilter => EventFilterBox,
AssetDefinitionEventFilter => DataEventFilter => EventFilterBox,
TriggerEventFilter => DataEventFilter => EventFilterBox,
RoleEventFilter => DataEventFilter => EventFilterBox,
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,
}
}

#[cfg(feature = "http")]
pub mod stream {
//! Structures related to event streaming over HTTP
Expand Down
4 changes: 2 additions & 2 deletions data_model/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ pub mod action {
executable: impl Into<Executable>,
repeats: impl Into<Repeats>,
authority: AccountId,
filter: TriggeringEventFilterBox,
filter: impl Into<TriggeringEventFilterBox>,
) -> Self {
Self {
executable: executable.into(),
repeats: repeats.into(),
// TODO: At this point the authority is meaningless.
authority,
filter,
filter: filter.into(),
metadata: Metadata::new(),
}
}
Expand Down
Loading

0 comments on commit f7bb96f

Please sign in to comment.