From 745a0bc2725131f1468566868c972a2873940057 Mon Sep 17 00:00:00 2001 From: Artemii Gerasimovich Date: Fri, 6 Oct 2023 20:26:55 +0300 Subject: [PATCH] [feature] #3516: make FindTrigger queries return original WASM Signed-off-by: Artemii Gerasimovich --- client/src/client.rs | 2 +- .../integration/triggers/by_call_trigger.rs | 4 +- core/src/smartcontracts/isi/query.rs | 2 +- core/src/smartcontracts/isi/triggers/mod.rs | 38 +++++++++++++++---- data_model/src/query/mod.rs | 6 +-- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index ce4cbad91b9..706beba129e 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -329,7 +329,7 @@ impl_query_result! { iroha_data_model::query::MetadataValue, iroha_data_model::query::TransactionQueryOutput, iroha_data_model::permission::PermissionTokenSchema, - iroha_data_model::trigger::Trigger, + iroha_data_model::trigger::Trigger, } /// Iroha client diff --git a/client/tests/integration/triggers/by_call_trigger.rs b/client/tests/integration/triggers/by_call_trigger.rs index e5541bec506..90c62dc2e22 100644 --- a/client/tests/integration/triggers/by_call_trigger.rs +++ b/client/tests/integration/triggers/by_call_trigger.rs @@ -5,7 +5,7 @@ use iroha_client::client::{self, Client}; use iroha_data_model::{ prelude::*, query::error::{FindError, QueryExecutionFail}, - trigger::OptimizedExecutable, + transaction::Executable, }; use iroha_genesis::GenesisNetwork; use iroha_logger::info; @@ -286,7 +286,7 @@ fn unregister_trigger() -> Result<()> { }; let found_trigger = test_client.request(find_trigger.clone())?; let found_action = found_trigger.action; - let OptimizedExecutable::Instructions(found_instructions) = found_action.executable else { + let Executable::Instructions(found_instructions) = found_action.executable else { panic!("Expected instructions"); }; let found_trigger = Trigger::new( diff --git a/core/src/smartcontracts/isi/query.rs b/core/src/smartcontracts/isi/query.rs index 0efbb5d2d57..39271242116 100644 --- a/core/src/smartcontracts/isi/query.rs +++ b/core/src/smartcontracts/isi/query.rs @@ -47,7 +47,7 @@ impl_lazy! { iroha_data_model::query::MetadataValue, iroha_data_model::query::TransactionQueryOutput, iroha_data_model::permission::PermissionTokenSchema, - iroha_data_model::trigger::Trigger, + iroha_data_model::trigger::Trigger, } /// Query Request statefully validated on the Iroha node side. diff --git a/core/src/smartcontracts/isi/triggers/mod.rs b/core/src/smartcontracts/isi/triggers/mod.rs index f8f071d4d44..f554bec10fe 100644 --- a/core/src/smartcontracts/isi/triggers/mod.rs +++ b/core/src/smartcontracts/isi/triggers/mod.rs @@ -198,11 +198,11 @@ pub mod query { use iroha_data_model::{ events::TriggeringFilterBox, query::{error::QueryExecutionFail as Error, MetadataValue}, - trigger::{OptimizedExecutable, Trigger, TriggerId}, + trigger::{Trigger, TriggerId}, }; use super::*; - use crate::prelude::*; + use crate::{prelude::*, smartcontracts::triggers::set::LoadedExecutable}; impl ValidQuery for FindAllActiveTriggerIds { #[metrics(+"find_all_active_triggers")] @@ -219,7 +219,7 @@ pub mod query { fn execute( &self, wsv: &WorldStateView, - ) -> Result, Error> { + ) -> Result, Error> { let id = wsv .evaluate(&self.id) .map_err(|e| Error::Evaluate(format!("Failed to evaluate trigger id. {e}")))?; @@ -237,8 +237,20 @@ pub mod query { .inspect_by_id(&id, |action| action.clone_and_box()) .ok_or_else(|| Error::Find(FindError::Trigger(id.clone())))?; - let action = - Action::new(loaded_executable, repeats, authority, filter).with_metadata(metadata); + let original_executable = match loaded_executable { + LoadedExecutable::Wasm(_) => { + let original_wasm = wsv + .triggers() + .get_original_contract(&id) + .cloned() + .expect("No original smartcontract saved for trigger. This is a bug."); + Executable::Wasm(original_wasm) + } + LoadedExecutable::Instructions(isi) => Executable::Instructions(isi), + }; + + let action = Action::new(original_executable, repeats, authority, filter) + .with_metadata(metadata); // TODO: Should we redact the metadata if the account is not the authority/owner? Ok(Trigger::new(id, action)) @@ -274,7 +286,7 @@ pub mod query { &self, wsv: &'wsv WorldStateView, ) -> eyre::Result< - Box> + 'wsv>, + Box> + 'wsv>, Error, > { let domain_id = wsv @@ -292,9 +304,21 @@ pub mod query { metadata, } = action.clone_and_box(); + let original_executable = + match loaded_executable { + LoadedExecutable::Wasm(_) => { + let original_wasm = + wsv.triggers().get_original_contract(&trigger_id).cloned().expect( + "No original smartcontract saved for trigger. This is a bug.", + ); + Executable::Wasm(original_wasm) + } + LoadedExecutable::Instructions(isi) => Executable::Instructions(isi), + }; + Trigger::new( trigger_id.clone(), - Action::new(loaded_executable, repeats, authority, filter) + Action::new(original_executable, repeats, authority, filter) .with_metadata(metadata), ) }, diff --git a/data_model/src/query/mod.rs b/data_model/src/query/mod.rs index e14933992bf..3edbc4ed64c 100644 --- a/data_model/src/query/mod.rs +++ b/data_model/src/query/mod.rs @@ -971,7 +971,7 @@ pub mod trigger { events::TriggeringFilterBox, expression::EvaluatesTo, prelude::InstructionExpr, - trigger::{OptimizedExecutable, Trigger, TriggerId}, + trigger::{Trigger, TriggerId}, Executable, Identifiable, Name, Value, }; @@ -1025,7 +1025,7 @@ pub mod trigger { } impl Query for FindTriggerById { - type Output = Trigger; + type Output = Trigger; } impl Query for FindTriggerKeyValueByIdAndKey { @@ -1033,7 +1033,7 @@ pub mod trigger { } impl Query for FindTriggersByDomainId { - type Output = Vec>; + type Output = Vec>; } impl FindTriggerById {