From f7bb96f9295cbcd1ebcfe1793d0cfc125ebfbd6f Mon Sep 17 00:00:00 2001 From: Nikita Strygin Date: Tue, 19 Mar 2024 12:57:18 +0300 Subject: [PATCH] [refactor] #4259: Use conversions when constructing `Action`s Make Action's constructor accept `impl Into`. Add shorthand conversions from concrete data event filter types into the top-level boxes Signed-off-by: Nikita Strygin --- .../integration/domain_owner_permissions.rs | 4 +- .../tests/integration/events/notification.rs | 16 ++-- .../integration/triggers/by_call_trigger.rs | 85 +++++++------------ .../integration/triggers/data_trigger.rs | 12 +-- .../integration/triggers/event_trigger.rs | 4 +- .../integration/triggers/time_trigger.rs | 8 +- .../integration/triggers/trigger_rollback.rs | 8 +- core/src/smartcontracts/isi/mod.rs | 8 +- data_model/src/events/mod.rs | 51 +++++++++++ data_model/src/trigger.rs | 4 +- tools/parity_scale_decoder/src/main.rs | 4 +- 11 files changed, 107 insertions(+), 97 deletions(-) diff --git a/client/tests/integration/domain_owner_permissions.rs b/client/tests/integration/domain_owner_permissions.rs index b26eb647728..e0945b85f70 100644 --- a/client/tests/integration/domain_owner_permissions.rs +++ b/client/tests/integration/domain_owner_permissions.rs @@ -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)?; diff --git a/client/tests/integration/events/notification.rs b/client/tests/integration/events/notification.rs index e7738e04aaa..bf26feb351b 100644 --- a/client/tests/integration/events/notification.rs +++ b/client/tests/integration/events/notification.rs @@ -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)?; @@ -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)?; diff --git a/client/tests/integration/triggers/by_call_trigger.rs b/client/tests/integration/triggers/by_call_trigger.rs index b5ae1402fe1..a2c2ac2b41d 100644 --- a/client/tests/integration/triggers/by_call_trigger.rs +++ b/client/tests/integration/triggers/by_call_trigger.rs @@ -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::*; @@ -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)?; @@ -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)?; @@ -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)?; @@ -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)?; @@ -289,11 +282,9 @@ fn unregister_trigger() -> Result<()> { Vec::::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()); @@ -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()), ), ); @@ -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)?; @@ -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)?; @@ -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)?; @@ -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()), ), ) }; @@ -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), ), )) } diff --git a/client/tests/integration/triggers/data_trigger.rs b/client/tests/integration/triggers/data_trigger.rs index decbb865cd5..46f505a9f9f 100644 --- a/client/tests/integration/triggers/data_trigger.rs +++ b/client/tests/integration/triggers/data_trigger.rs @@ -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)?; @@ -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)?; @@ -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)?; diff --git a/client/tests/integration/triggers/event_trigger.rs b/client/tests/integration/triggers/event_trigger.rs index a153b262464..12a5dca633c 100644 --- a/client/tests/integration/triggers/event_trigger.rs +++ b/client/tests/integration/triggers/event_trigger.rs @@ -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)?; diff --git a/client/tests/integration/triggers/time_trigger.rs b/client/tests/integration/triggers/time_trigger.rs index 06042776200..1f29a0d8ba9 100644 --- a/client/tests/integration/triggers/time_trigger.rs +++ b/client/tests/integration/triggers/time_trigger.rs @@ -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)?; @@ -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)?; @@ -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)?; @@ -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)?; diff --git a/client/tests/integration/triggers/trigger_rollback.rs b/client/tests/integration/triggers/trigger_rollback.rs index 2bbcd387699..6f61cae9835 100644 --- a/client/tests/integration/triggers/trigger_rollback.rs +++ b/client/tests/integration/triggers/trigger_rollback.rs @@ -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); diff --git a/core/src/smartcontracts/isi/mod.rs b/core/src/smartcontracts/isi/mod.rs index 8218f0bb994..16dd365a157 100644 --- a/core/src/smartcontracts/isi/mod.rs +++ b/core/src/smartcontracts/isi/mod.rs @@ -378,11 +378,9 @@ mod tests { Vec::::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()), ), )); diff --git a/data_model/src/events/mod.rs b/data_model/src/events/mod.rs index daa9e544e26..94c003526bc 100644 --- a/data_model/src/events/mod.rs +++ b/data_model/src/events/mod.rs @@ -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 diff --git a/data_model/src/trigger.rs b/data_model/src/trigger.rs index fb0ed684981..e65cf5f4a96 100644 --- a/data_model/src/trigger.rs +++ b/data_model/src/trigger.rs @@ -215,14 +215,14 @@ pub mod action { executable: impl Into, repeats: impl Into, authority: AccountId, - filter: TriggeringEventFilterBox, + filter: impl Into, ) -> Self { Self { executable: executable.into(), repeats: repeats.into(), // TODO: At this point the authority is meaningless. authority, - filter, + filter: filter.into(), metadata: Metadata::new(), } } diff --git a/tools/parity_scale_decoder/src/main.rs b/tools/parity_scale_decoder/src/main.rs index 511bdc37cf5..9bf3a824693 100644 --- a/tools/parity_scale_decoder/src/main.rs +++ b/tools/parity_scale_decoder/src/main.rs @@ -275,9 +275,7 @@ mod tests { vec![Mint::asset_numeric(1u32, rose_id)], Repeats::Indefinitely, account_id, - TriggeringEventFilterBox::Data(DataEventFilter::Domain( - DomainEventFilter::new().for_events(DomainEventSet::AnyAccount), - )), + DomainEventFilter::new().for_events(DomainEventSet::AnyAccount), ); let trigger = Trigger::new(trigger_id, action);