Skip to content

Commit

Permalink
[feature] #4390: include trigger id in trigger entrypoint
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Mar 27, 2024
1 parent 1f0e21d commit 8dc4445
Show file tree
Hide file tree
Showing 22 changed files with 272 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeList
getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom);

#[iroha_trigger::main]
fn main(_owner: AccountId, _event: Event) {
fn main(_id: TriggerId, _owner: AccountId, _event: Event) {
iroha_trigger::log::info!("Executing trigger");

let accounts_cursor = FindAllAccounts.execute().dbg_unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom);

/// Mint 1 rose for owner
#[iroha_trigger::main]
fn main(owner: AccountId, _event: Event) {
fn main(id: TriggerId, owner: AccountId, _event: Event) {
let rose_definition_id = AssetDefinitionId::from_str("rose#wonderland")
.dbg_expect("Failed to parse `rose#wonderland` asset definition id");
let rose_id = AssetId::new(rose_definition_id, owner);

Mint::asset_numeric(1u32, rose_id)
let val = FindTriggerKeyValueByIdAndKey::new(id, "VAL".parse().unwrap()).dbg_unwrap();

Mint::asset_numeric(val, rose_id)
.execute()
.dbg_expect("Failed to mint rose");
}
8 changes: 6 additions & 2 deletions client/tests/integration/triggers/by_call_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,12 @@ fn trigger_in_genesis_using_base64() -> Result<()> {
let prev_value = get_asset_value(&mut test_client, asset_id.clone());

// Executing trigger
let call_trigger = ExecuteTrigger::new(trigger_id);
test_client.submit_blocking(call_trigger)?;
let call_trigger = ExecuteTrigger::new(trigger_id.clone());
let set_trigger_metadata = SetKeyValue::trigger(trigger_id, "VAL".parse()?, numeric!(1));
test_client.submit_all_blocking(vec![
InstructionBox::from(set_trigger_metadata),
call_trigger.into(),
])?;

// Checking result
let new_value = get_asset_value(&mut test_client, asset_id);
Expand Down
6 changes: 4 additions & 2 deletions config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ pub struct ChainWide {
pub block_time: Duration,
pub commit_time: Duration,
pub transaction_limits: TransactionLimits,
pub asset_metadata_limits: MetadataLimits,
pub domain_metadata_limits: MetadataLimits,
pub asset_definition_metadata_limits: MetadataLimits,
pub account_metadata_limits: MetadataLimits,
pub domain_metadata_limits: MetadataLimits,
pub asset_metadata_limits: MetadataLimits,
pub trigger_metadata_limits: MetadataLimits,
pub ident_length_limits: LengthLimits,
pub executor_runtime: WasmRuntime,
pub wasm_runtime: WasmRuntime,
Expand All @@ -204,6 +205,7 @@ impl Default for ChainWide {
account_metadata_limits: defaults::chain_wide::DEFAULT_METADATA_LIMITS,
asset_definition_metadata_limits: defaults::chain_wide::DEFAULT_METADATA_LIMITS,
asset_metadata_limits: defaults::chain_wide::DEFAULT_METADATA_LIMITS,
trigger_metadata_limits: defaults::chain_wide::DEFAULT_METADATA_LIMITS,
ident_length_limits: defaults::chain_wide::DEFAULT_IDENT_LENGTH_LIMITS,
executor_runtime: WasmRuntime::default(),
wasm_runtime: WasmRuntime::default(),
Expand Down
7 changes: 5 additions & 2 deletions config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,11 @@ pub struct ChainWide {
pub block_time: Duration,
pub commit_time: Duration,
pub transaction_limits: TransactionLimits,
pub asset_metadata_limits: MetadataLimits,
pub domain_metadata_limits: MetadataLimits,
pub asset_definition_metadata_limits: MetadataLimits,
pub account_metadata_limits: MetadataLimits,
pub domain_metadata_limits: MetadataLimits,
pub asset_metadata_limits: MetadataLimits,
pub trigger_metadata_limits: MetadataLimits,
pub ident_length_limits: LengthLimits,
pub executor_fuel_limit: u64,
pub executor_max_memory: HumanBytes<u32>,
Expand All @@ -585,6 +586,7 @@ impl ChainWide {
commit_time,
transaction_limits,
asset_metadata_limits,
trigger_metadata_limits,
asset_definition_metadata_limits,
account_metadata_limits,
domain_metadata_limits,
Expand All @@ -601,6 +603,7 @@ impl ChainWide {
commit_time,
transaction_limits,
asset_metadata_limits,
trigger_metadata_limits,
asset_definition_metadata_limits,
account_metadata_limits,
domain_metadata_limits,
Expand Down
16 changes: 10 additions & 6 deletions config/src/parameters/user/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,11 @@ pub struct ChainWidePartial {
pub block_time: UserField<HumanDuration>,
pub commit_time: UserField<HumanDuration>,
pub transaction_limits: UserField<TransactionLimits>,
pub asset_metadata_limits: UserField<MetadataLimits>,
pub domain_metadata_limits: UserField<MetadataLimits>,
pub asset_definition_metadata_limits: UserField<MetadataLimits>,
pub account_metadata_limits: UserField<MetadataLimits>,
pub domain_metadata_limits: UserField<MetadataLimits>,
pub asset_metadata_limits: UserField<MetadataLimits>,
pub trigger_metadata_limits: UserField<MetadataLimits>,
pub ident_length_limits: UserField<LengthLimits>,
pub executor_fuel_limit: UserField<u64>,
pub executor_max_memory: UserField<HumanBytes<u32>>,
Expand All @@ -682,17 +683,20 @@ impl UnwrapPartial for ChainWidePartial {
transaction_limits: self
.transaction_limits
.unwrap_or(DEFAULT_TRANSACTION_LIMITS),
asset_metadata_limits: self
.asset_metadata_limits
domain_metadata_limits: self
.domain_metadata_limits
.unwrap_or(DEFAULT_METADATA_LIMITS),
asset_definition_metadata_limits: self
.asset_definition_metadata_limits
.unwrap_or(DEFAULT_METADATA_LIMITS),
account_metadata_limits: self
.account_metadata_limits
.unwrap_or(DEFAULT_METADATA_LIMITS),
domain_metadata_limits: self
.domain_metadata_limits
asset_metadata_limits: self
.asset_metadata_limits
.unwrap_or(DEFAULT_METADATA_LIMITS),
trigger_metadata_limits: self
.trigger_metadata_limits
.unwrap_or(DEFAULT_METADATA_LIMITS),
ident_length_limits: self
.ident_length_limits
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ pub mod isi {
state_transaction
.world
.emit_events(Some(AccountEvent::MetadataInserted(MetadataChanged {
target_id: account_id.clone(),
key: self.key.clone(),
target_id: account_id,
key: self.key,
value: self.value,
})));

Expand Down Expand Up @@ -316,7 +316,7 @@ pub mod isi {
state_transaction
.world
.emit_events(Some(AccountEvent::MetadataRemoved(MetadataChanged {
target_id: account_id.clone(),
target_id: account_id,
key: self.key,
value,
})));
Expand Down
2 changes: 2 additions & 0 deletions core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Execute for SetKeyValueBox {
Self::Account(isi) => isi.execute(authority, state_transaction),
Self::AssetDefinition(isi) => isi.execute(authority, state_transaction),
Self::Asset(isi) => isi.execute(authority, state_transaction),
Self::Trigger(isi) => isi.execute(authority, state_transaction),
}
}
}
Expand All @@ -196,6 +197,7 @@ impl Execute for RemoveKeyValueBox {
Self::Account(isi) => isi.execute(authority, state_transaction),
Self::AssetDefinition(isi) => isi.execute(authority, state_transaction),
Self::Asset(isi) => isi.execute(authority, state_transaction),
Self::Trigger(isi) => isi.execute(authority, state_transaction),
}
}
}
Expand Down
66 changes: 66 additions & 0 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,72 @@ pub mod isi {
}
}

impl Execute for SetKeyValue<Trigger> {
#[metrics(+"set_trigger_key_value")]
fn execute(
self,
_authority: &AccountId,
state_transaction: &mut StateTransaction<'_, '_>,
) -> Result<(), Error> {
let trigger_id = self.object_id;

let trigger_metadata_limits = state_transaction.config.account_metadata_limits;
state_transaction
.world
.triggers
.inspect_by_id_mut(&trigger_id, |action| {
action.metadata_mut().insert_with_limits(
self.key.clone(),
self.value.clone(),
trigger_metadata_limits,
)
})
.ok_or(FindError::Trigger(trigger_id.clone()))??;

state_transaction
.world
.emit_events(Some(TriggerEvent::MetadataInserted(MetadataChanged {
target_id: trigger_id,
key: self.key,
value: self.value,
})));

Ok(())
}
}

impl Execute for RemoveKeyValue<Trigger> {
#[metrics(+"remove_trigger_key_value")]
fn execute(
self,
_authority: &AccountId,
state_transaction: &mut StateTransaction<'_, '_>,
) -> Result<(), Error> {
let trigger_id = self.object_id;

let value = state_transaction
.world
.triggers
.inspect_by_id_mut(&trigger_id, |action| {
action
.metadata_mut()
.remove(&self.key)
.ok_or_else(|| FindError::MetadataKey(self.key.clone()))
})
.ok_or(FindError::Trigger(trigger_id.clone()))??;

state_transaction
.world
.emit_events(Some(TriggerEvent::MetadataRemoved(MetadataChanged {
target_id: trigger_id,
key: self.key,
value,
})));

Ok(())
}
}

impl Execute for ExecuteTrigger {
#[metrics(+"execute_trigger")]
fn execute(
Expand Down
8 changes: 8 additions & 0 deletions core/src/smartcontracts/isi/triggers/specialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ pub trait LoadedActionTrait {
/// Get action metadata
fn metadata(&self) -> &Metadata;

/// Get action metadata
fn metadata_mut(&mut self) -> &mut Metadata;

/// Check if action is mintable.
fn mintable(&self) -> bool;

Expand Down Expand Up @@ -181,10 +184,15 @@ impl<F: EventFilter + Into<TriggeringEventFilterBox> + Clone> LoadedActionTrait
&self.authority
}

/// Get action metadata
fn metadata(&self) -> &Metadata {
&self.metadata
}

fn metadata_mut(&mut self) -> &mut Metadata {
&mut self.metadata
}

fn mintable(&self) -> bool {
self.filter.mintable()
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ pub mod state {
/// Trigger execution state
#[derive(Constructor)]
pub struct Trigger {
pub(in super::super) id: TriggerId,

/// Event which activated this trigger
pub(in super::super) triggering_event: Event,
}
Expand Down Expand Up @@ -985,7 +987,7 @@ impl<'wrld, 'block: 'wrld, 'state: 'block> Runtime<state::Trigger<'wrld, 'block,
self.config,
span,
state::chain_state::WithMut(state_transaction),
state::specific::Trigger::new(event),
state::specific::Trigger::new(id.clone(), event),
);

let mut store = self.create_store(state);
Expand All @@ -1006,6 +1008,7 @@ impl<'wrld, 'block: 'wrld, 'state: 'block> Runtime<state::Trigger<'wrld, 'block,
#[codec::wrap]
fn get_trigger_payload(state: &state::Trigger) -> payloads::Trigger {
payloads::Trigger {
id: state.specific_state.id.clone(),
owner: state.authority.clone(),
event: state.specific_state.triggering_event.clone(),
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl World {
}
}

/// Create struct to apply block's changes while reverting changes made in the latest block
/// Create struct to apply block's changes while reverting changes made in the latest block
pub fn block_and_revert(&self) -> WorldBlock {
WorldBlock {
parameters: self.parameters.block_and_revert(),
Expand Down Expand Up @@ -1333,10 +1333,11 @@ impl<'state> StateBlock<'state> {
}

update_params! {
WSV_ASSET_METADATA_LIMITS => self.config.asset_metadata_limits,
WSV_DOMAIN_METADATA_LIMITS => self.config.domain_metadata_limits,
WSV_ASSET_DEFINITION_METADATA_LIMITS => self.config.asset_definition_metadata_limits,
WSV_ACCOUNT_METADATA_LIMITS => self.config.account_metadata_limits,
WSV_DOMAIN_METADATA_LIMITS => self.config.domain_metadata_limits,
WSV_ASSET_METADATA_LIMITS => self.config.asset_metadata_limits,
WSV_TRIGGER_METADATA_LIMITS => self.config.trigger_metadata_limits,
WSV_IDENT_LENGTH_LIMITS => self.config.ident_length_limits,
EXECUTOR_FUEL_LIMIT => self.config.executor_runtime.fuel_limit,
EXECUTOR_MAX_MEMORY => self.config.executor_runtime.max_memory_bytes,
Expand Down
7 changes: 7 additions & 0 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ mod trigger {
pub use self::model::*;
use super::*;

// type alias required by `Filter` macro
type TriggerMetadataChanged = MetadataChanged<TriggerId>;

data_event! {
#[has_origin(origin = Trigger)]
pub enum TriggerEvent {
Expand All @@ -495,6 +498,10 @@ mod trigger {
Extended(TriggerNumberOfExecutionsChanged),
#[has_origin(number_of_executions_changed => &number_of_executions_changed.trigger_id)]
Shortened(TriggerNumberOfExecutionsChanged),
#[has_origin(metadata_changed => &metadata_changed.target_id)]
MetadataInserted(TriggerMetadataChanged),
#[has_origin(metadata_changed => &metadata_changed.target_id)]
MetadataRemoved(TriggerMetadataChanged),
}
}

Expand Down
Loading

0 comments on commit 8dc4445

Please sign in to comment.