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 28, 2024
1 parent 2ffcd00 commit f0df0ad
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use lol_alloc::{FreeListAllocator, LockedAllocator};
static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new());

#[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 = FindAllAccounts.execute().dbg_unwrap();
let limits = MetadataLimits::new(256, 256);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeList

/// 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);

MintExpr::new(1_u32, rose_id)
let val: Value = FindTriggerKeyValueByIdAndKey::new(id, Name::from_str("VAL").unwrap())
.execute()
.dbg_unwrap()
.into();

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

// Executing trigger
test_client
.submit_blocking(SetKeyValueExpr::new(
trigger_id.clone(),
Name::from_str("VAL")?,
Value::from(1_u32),
))
.unwrap();
let call_trigger = ExecuteTriggerExpr::new(trigger_id);
test_client.submit_blocking(call_trigger)?;

Expand Down
4 changes: 4 additions & 0 deletions config/iroha_test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"TRIGGER_METADATA_LIMITS": {
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"IDENT_LENGTH_LIMITS": {
"min": 1,
"max": 128
Expand Down
6 changes: 5 additions & 1 deletion config/src/wsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct Configuration {
pub account_metadata_limits: MetadataLimits,
/// [`MetadataLimits`] of any domain metadata.
pub domain_metadata_limits: MetadataLimits,
/// [`MetadataLimits`] of any trigger metadata.
pub trigger_metadata_limits: MetadataLimits,
/// [`LengthLimits`] for the number of chars in identifiers that can be stored in the WSV.
pub ident_length_limits: LengthLimits,
/// Limits that all transactions need to obey, in terms of size
Expand All @@ -55,6 +57,7 @@ impl Default for ConfigurationProxy {
asset_definition_metadata_limits: Some(DEFAULT_METADATA_LIMITS),
account_metadata_limits: Some(DEFAULT_METADATA_LIMITS),
domain_metadata_limits: Some(DEFAULT_METADATA_LIMITS),
trigger_metadata_limits: Some(DEFAULT_METADATA_LIMITS),
ident_length_limits: Some(DEFAULT_IDENT_LENGTH_LIMITS),
transaction_limits: Some(DEFAULT_TRANSACTION_LIMITS),
wasm_runtime_config: Some(wasm::ConfigurationProxy::default()),
Expand All @@ -75,12 +78,13 @@ pub mod tests {
asset_definition_metadata_limits in prop::option::of(Just(DEFAULT_METADATA_LIMITS)),
account_metadata_limits in prop::option::of(Just(DEFAULT_METADATA_LIMITS)),
domain_metadata_limits in prop::option::of(Just(DEFAULT_METADATA_LIMITS)),
trigger_metadata_limits in prop::option::of(Just(DEFAULT_METADATA_LIMITS)),
ident_length_limits in prop::option::of(Just(DEFAULT_IDENT_LENGTH_LIMITS)),
transaction_limits in prop::option::of(Just(DEFAULT_TRANSACTION_LIMITS)),
wasm_runtime_config in prop::option::of(Just(wasm::ConfigurationProxy::default())),
)
-> ConfigurationProxy {
ConfigurationProxy { asset_metadata_limits, asset_definition_metadata_limits, account_metadata_limits, domain_metadata_limits, ident_length_limits, transaction_limits, wasm_runtime_config }
ConfigurationProxy { asset_metadata_limits, asset_definition_metadata_limits, account_metadata_limits, domain_metadata_limits, trigger_metadata_limits, ident_length_limits, transaction_limits, wasm_runtime_config }
}
}
}
4 changes: 4 additions & 0 deletions configs/peer/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"TRIGGER_METADATA_LIMITS": {
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"IDENT_LENGTH_LIMITS": {
"min": 1,
"max": 128
Expand Down
Binary file modified configs/peer/executor.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions configs/peer/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
"Parameter": "?WSVDomainMetadataLimits=1048576,4096_ML"
}
},
{
"NewParameter": {
"Parameter": "?WSVTriggerMetadataLimits=1048576,4096_ML"
}
},
{
"NewParameter": {
"Parameter": "?WSVIdentLengthLimits=1,128_LL"
Expand Down
4 changes: 4 additions & 0 deletions configs/peer/stable/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"TRIGGER_METADATA_LIMITS": {
"max_len": 1048576,
"max_entry_byte_size": 4096
},
"IDENT_LENGTH_LIMITS": {
"min": 1,
"max": 128
Expand Down
5 changes: 5 additions & 0 deletions configs/peer/stable/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
"Parameter": "?WSVDomainMetadataLimits=1048576,4096_ML"
}
},
{
"NewParameter": {
"Parameter": "?WSVTriggerMetadataLimits=1048576,4096_ML"
}
},
{
"NewParameter": {
"Parameter": "?WSVIdentLengthLimits=1,128_LL"
Expand Down
10 changes: 10 additions & 0 deletions core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ impl Execute for SetKeyValueExpr {
value,
}
.execute(authority, wsv),
IdBox::TriggerId(object_id) => SetKeyValue::<Trigger<TriggeringFilterBox>> {
object_id,
key,
value,
}
.execute(authority, wsv),
_ => Err(Error::Evaluate(InstructionType::SetKeyValue.into())),
}
}
Expand All @@ -332,6 +338,10 @@ impl Execute for RemoveKeyValueExpr {
IdBox::DomainId(object_id) => {
RemoveKeyValue::<Domain> { object_id, key }.execute(authority, wsv)
}
IdBox::TriggerId(object_id) => {
RemoveKeyValue::<Trigger<TriggeringFilterBox>> { object_id, key }
.execute(authority, wsv)
}
_ => Err(Error::Evaluate(InstructionType::RemoveKeyValue.into())),
}
}
Expand Down
53 changes: 53 additions & 0 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,59 @@ pub mod isi {
}
}

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

let trigger_metadata_limits = wsv.config.account_metadata_limits;
wsv.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()))??;

wsv.emit_events(Some(TriggerEvent::MetadataInserted(MetadataChanged {
target_id: trigger_id,
key: self.key,
value: Box::new(self.value),
})));

Ok(())
}
}

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

let value = wsv
.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()))??;

wsv.emit_events(Some(TriggerEvent::MetadataRemoved(MetadataChanged {
target_id: trigger_id,
key: self.key,
value: Box::new(value),
})));

Ok(())
}
}

impl Execute for ExecuteTriggerExpr {
#[metrics(+"execute_trigger")]
fn execute(self, authority: &AccountId, wsv: &mut WorldStateView) -> Result<(), Error> {
Expand Down
7 changes: 7 additions & 0 deletions core/src/smartcontracts/isi/triggers/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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 @@ -115,6 +118,10 @@ impl<F: Filter + Into<TriggeringFilterBox> + Clone> LoadedActionTrait for Loaded
&self.metadata
}

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

fn mintable(&self) -> bool {
self.filter.mintable()
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ pub mod state {
/// Trigger execution state
pub struct Trigger<'wrld> {
pub(super) common: Common<'wrld>,
/// Id of this trigger
pub(in super::super) id: TriggerId,
/// Event which activated this trigger
pub(super) triggering_event: Event,
}
Expand Down Expand Up @@ -923,6 +925,7 @@ impl<'wrld> Runtime<state::Trigger<'wrld>> {
let span = wasm_log_span!("Trigger execution", %id, %authority);
let state = state::Trigger {
common: state::Common::new(wsv, authority, self.config, span),
id: id.clone(),
triggering_event: event,
};

Expand All @@ -941,6 +944,7 @@ impl<'wrld> Runtime<state::Trigger<'wrld>> {
#[codec::wrap]
fn get_trigger_payload(state: &state::Trigger) -> payloads::Trigger {
payloads::Trigger {
id: state.id.clone(),
owner: state.authority().clone(),
event: state.triggering_event.clone(),
}
Expand Down
9 changes: 8 additions & 1 deletion data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,22 @@ mod trigger {
pub use self::model::*;
use super::*;

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

data_event! {
#[has_origin(origin = Trigger<FilterBox>)]
#[has_origin(origin = Trigger<TriggeringFilterBox>)]
pub enum TriggerEvent {
Created(TriggerId),
Deleted(TriggerId),
#[has_origin(number_of_executions_changed => &number_of_executions_changed.trigger_id)]
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
1 change: 1 addition & 0 deletions data_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ pub mod parameter {
pub const WSV_ASSET_DEFINITION_METADATA_LIMITS: &str = "WSVAssetDefinitionMetadataLimits";
pub const WSV_ACCOUNT_METADATA_LIMITS: &str = "WSVAccountMetadataLimits";
pub const WSV_DOMAIN_METADATA_LIMITS: &str = "WSVDomainMetadataLimits";
pub const WSV_TRIGGER_METADATA_LIMITS: &str = "WSVTriggerMetadataLimits";
pub const WSV_IDENT_LENGTH_LIMITS: &str = "WSVIdentLengthLimits";
pub const WASM_FUEL_LIMIT: &str = "WASMFuelLimit";
pub const WASM_MAX_MEMORY: &str = "WASMMaxMemory";
Expand Down
2 changes: 2 additions & 0 deletions data_model/src/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod payloads {
/// Payload for trigger entrypoint
#[derive(Debug, Clone, Encode, Decode)]
pub struct Trigger {
/// Id of this trigger
pub id: TriggerId,
/// Trigger owner who registered the trigger
pub owner: AccountId,
/// Event which triggered the execution
Expand Down
15 changes: 15 additions & 0 deletions data_model/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@ pub trait Visit: ExpressionEvaluator {
visit_set_account_key_value(SetKeyValue<Account>),
visit_set_asset_definition_key_value(SetKeyValue<AssetDefinition>),
visit_set_asset_key_value(SetKeyValue<Asset>),
visit_set_trigger_key_value(SetKeyValue<Trigger<TriggeringFilterBox>>),

// Visit RemoveKeyValueExpr
visit_remove_domain_key_value(RemoveKeyValue<Domain>),
visit_remove_account_key_value(RemoveKeyValue<Account>),
visit_remove_asset_definition_key_value(RemoveKeyValue<AssetDefinition>),
visit_remove_asset_key_value(RemoveKeyValue<Asset>),
visit_remove_trigger_key_value(RemoveKeyValue<Trigger<TriggeringFilterBox>>),

// Visit GrantExpr
visit_grant_account_permission(Grant<PermissionToken>),
Expand Down Expand Up @@ -575,6 +577,14 @@ pub fn visit_set_key_value<V: Visit + ?Sized>(
value,
},
),
IdBox::TriggerId(object_id) => visitor.visit_set_trigger_key_value(
authority,
SetKeyValue {
object_id,
key,
value,
},
),
_ => visitor.visit_unsupported(authority, isi),
}
}
Expand All @@ -599,6 +609,9 @@ pub fn visit_remove_key_value<V: Visit + ?Sized>(
IdBox::DomainId(object_id) => {
visitor.visit_remove_domain_key_value(authority, RemoveKeyValue { object_id, key });
}
IdBox::TriggerId(object_id) => {
visitor.visit_remove_trigger_key_value(authority, RemoveKeyValue { object_id, key });
}
_ => visitor.visit_unsupported(authority, isi),
}
}
Expand Down Expand Up @@ -710,6 +723,8 @@ leaf_visitors! {
visit_transfer_asset(Transfer<Asset, NumericValue, Account>),
visit_set_asset_key_value(SetKeyValue<Asset>),
visit_remove_asset_key_value(RemoveKeyValue<Asset>),
visit_set_trigger_key_value(SetKeyValue<Trigger<TriggeringFilterBox>>),
visit_remove_trigger_key_value(RemoveKeyValue<Trigger<TriggeringFilterBox>>),
visit_register_asset_definition(Register<AssetDefinition>),
visit_unregister_asset_definition(Unregister<AssetDefinition>),
visit_transfer_asset_definition(Transfer<Account, AssetDefinitionId, Account>),
Expand Down
Loading

0 comments on commit f0df0ad

Please sign in to comment.