Skip to content

Commit

Permalink
[fix] #0000: Fix trigger atomicity
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Nov 30, 2023
1 parent 395a578 commit 33d8fbf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/tests/integration/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod by_call_trigger;
mod data_trigger;
mod event_trigger;
mod time_trigger;
mod trigger_rollback;
43 changes: 43 additions & 0 deletions client/tests/integration/triggers/trigger_rollback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::str::FromStr as _;

use eyre::Result;
use iroha_client::client::QueryResult;
use iroha_data_model::{prelude::*, query::asset::FindAllAssetsDefinitions, trigger::TriggerId};
use test_network::*;

#[test]
fn failed_trigger_revert() -> Result<()> {
let (_rt, _peer, client) = <PeerBuilder>::new().with_port(11_110).start_with_runtime();
wait_for_genesis_committed(&[client.clone()], 0);

//When
let trigger_id = TriggerId::from_str("trigger")?;
let account_id = AccountId::from_str("alice@wonderland")?;
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland")?;
let create_asset = RegisterExpr::new(AssetDefinition::quantity(asset_definition_id.clone()));
let instructions: [InstructionExpr; 2] = [create_asset.into(), Fail::new("Always fail").into()];
let register_trigger = RegisterExpr::new(Trigger::new(
trigger_id.clone(),
Action::new(
instructions,
Repeats::Indefinitely,
account_id.clone(),
TriggeringFilterBox::ExecuteTrigger(ExecuteTriggerEventFilter::new(
trigger_id.clone(),
account_id,
)),
),
));
let _ = client.submit_blocking(register_trigger);

let call_trigger = ExecuteTriggerExpr::new(trigger_id);
client.submit_blocking(call_trigger)?;

//Then
let request = FindAllAssetsDefinitions;
let query_result = client.request(request)?.collect::<QueryResult<Vec<_>>>()?;
assert!(query_result
.iter()
.all(|asset_definition| asset_definition.id() != &asset_definition_id));
Ok(())
}
Binary file modified configs/peer/executor.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions core/src/wsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,15 @@ impl WorldStateView {
continue;
}
}
let wsv = self.clone();
let event = match self.process_trigger(&id, &action, event) {
Ok(_) => {
succeed.push(id.clone());
TriggerCompletedEvent::new(id, TriggerCompletedOutcome::Success)
}
Err(error) => {
// Revert to previous state on failure inside trigger
*self = wsv;
let event = TriggerCompletedEvent::new(
id,
TriggerCompletedOutcome::Failure(error.to_string()),
Expand Down

0 comments on commit 33d8fbf

Please sign in to comment.