Skip to content

Commit

Permalink
[feature] #3516: make FindTrigger queries return original WASM
Browse files Browse the repository at this point in the history
Signed-off-by: Artemii Gerasimovich <[email protected]>
  • Loading branch information
Artemii Gerasimovich authored and 6r1d committed Oct 17, 2023
1 parent 185416e commit 745a0bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::events::TriggeringFilterBox, iroha_data_model::trigger::OptimizedExecutable>,
iroha_data_model::trigger::Trigger<iroha_data_model::events::TriggeringFilterBox, iroha_data_model::transaction::Executable>,
}

/// Iroha client
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration/triggers/by_call_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::events::TriggeringFilterBox, iroha_data_model::trigger::OptimizedExecutable>,
iroha_data_model::trigger::Trigger<iroha_data_model::events::TriggeringFilterBox, iroha_data_model::transaction::Executable>,
}

/// Query Request statefully validated on the Iroha node side.
Expand Down
38 changes: 31 additions & 7 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -219,7 +219,7 @@ pub mod query {
fn execute(
&self,
wsv: &WorldStateView,
) -> Result<Trigger<TriggeringFilterBox, OptimizedExecutable>, Error> {
) -> Result<Trigger<TriggeringFilterBox, Executable>, Error> {
let id = wsv
.evaluate(&self.id)
.map_err(|e| Error::Evaluate(format!("Failed to evaluate trigger id. {e}")))?;
Expand All @@ -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))
Expand Down Expand Up @@ -274,7 +286,7 @@ pub mod query {
&self,
wsv: &'wsv WorldStateView,
) -> eyre::Result<
Box<dyn Iterator<Item = Trigger<TriggeringFilterBox, OptimizedExecutable>> + 'wsv>,
Box<dyn Iterator<Item = Trigger<TriggeringFilterBox, Executable>> + 'wsv>,
Error,
> {
let domain_id = wsv
Expand All @@ -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),
)
},
Expand Down
6 changes: 3 additions & 3 deletions data_model/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ pub mod trigger {
events::TriggeringFilterBox,
expression::EvaluatesTo,
prelude::InstructionExpr,
trigger::{OptimizedExecutable, Trigger, TriggerId},
trigger::{Trigger, TriggerId},
Executable, Identifiable, Name, Value,
};

Expand Down Expand Up @@ -1025,15 +1025,15 @@ pub mod trigger {
}

impl Query for FindTriggerById {
type Output = Trigger<TriggeringFilterBox, OptimizedExecutable>;
type Output = Trigger<TriggeringFilterBox, Executable>;
}

impl Query for FindTriggerKeyValueByIdAndKey {
type Output = MetadataValue;
}

impl Query for FindTriggersByDomainId {
type Output = Vec<Trigger<TriggeringFilterBox, OptimizedExecutable>>;
type Output = Vec<Trigger<TriggeringFilterBox, Executable>>;
}

impl FindTriggerById {
Expand Down

0 comments on commit 745a0bc

Please sign in to comment.