Skip to content

Commit

Permalink
[feature] #4390: include trigger id in trigger entrypoint (#4391)
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic authored Mar 28, 2024
1 parent e7e24db commit b2b8d40
Show file tree
Hide file tree
Showing 34 changed files with 413 additions and 90 deletions.
4 changes: 2 additions & 2 deletions client/tests/integration/events/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ fn produce_multiple_events() -> Result<()> {
let alice_id = AccountId::from_str("alice@wonderland")?;
let role_id = RoleId::from_str("TEST_ROLE")?;
let token_1 = PermissionToken::new(
"CanRemoveKeyValueInUserAccount".parse()?,
"CanRemoveKeyValueInAccount".parse()?,
&json!({ "account_id": alice_id }),
);
let token_2 = PermissionToken::new(
"CanSetKeyValueInUserAccount".parse()?,
"CanSetKeyValueInAccount".parse()?,
&json!({ "account_id": alice_id }),
);
let role = iroha_client::data_model::role::Role::new(role_id.clone())
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/queries/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn find_roles_by_account_id() -> Result<()> {
.cloned()
.map(|role_id| {
Register::role(Role::new(role_id).add_permission(PermissionToken::new(
"CanSetKeyValueInUserAccount".parse().unwrap(),
"CanSetKeyValueInAccount".parse().unwrap(),
&json!({ "account_id": alice_id }),
)))
})
Expand Down
10 changes: 5 additions & 5 deletions client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ fn register_and_grant_role_for_metadata_access() -> Result<()> {
let role_id = RoleId::from_str("ACCESS_TO_MOUSE_METADATA")?;
let role = Role::new(role_id.clone())
.add_permission(PermissionToken::new(
"CanSetKeyValueInUserAccount".parse()?,
"CanSetKeyValueInAccount".parse()?,
&json!({ "account_id": mouse_id }),
))
.add_permission(PermissionToken::new(
"CanRemoveKeyValueInUserAccount".parse()?,
"CanRemoveKeyValueInAccount".parse()?,
&json!({ "account_id": mouse_id }),
));
let register_role = Register::role(role);
Expand Down Expand Up @@ -119,7 +119,7 @@ fn unregistered_role_removed_from_account() -> Result<()> {
// Register root role
let register_role = Register::role(Role::new(role_id.clone()).add_permission(
PermissionToken::new(
"CanSetKeyValueInUserAccount".parse()?,
"CanSetKeyValueInAccount".parse()?,
&json!({ "account_id": alice_id }),
),
));
Expand Down Expand Up @@ -156,7 +156,7 @@ fn role_with_invalid_permissions_is_not_accepted() -> Result<()> {
let role_id = RoleId::from_str("ACCESS_TO_ACCOUNT_METADATA")?;
let rose_asset_id = AssetId::from_str("rose##alice@wonderland")?;
let role = Role::new(role_id).add_permission(PermissionToken::new(
"CanSetKeyValueInUserAccount".parse()?,
"CanSetKeyValueInAccount".parse()?,
&json!({ "account_id": rose_asset_id }),
));

Expand Down Expand Up @@ -257,7 +257,7 @@ fn grant_revoke_role_permissions() -> Result<()> {
"value".to_owned(),
);
let permission = PermissionToken::new(
"CanSetKeyValueInUserAccount".parse()?,
"CanSetKeyValueInAccount".parse()?,
&json!({ "account_id": mouse_id }),
);
let grant_role_permission = Grant::role_permission(permission.clone(), role_id.clone());
Expand Down
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: EventBox) {
fn main(_id: TriggerId, _owner: AccountId, _event: EventBox) {
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,19 @@ getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom);

/// Mint 1 rose for owner
#[iroha_trigger::main]
fn main(owner: AccountId, _event: EventBox) {
fn main(id: TriggerId, owner: AccountId, _event: EventBox) {
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: u32 = FindTriggerKeyValueByIdAndKey::new(id, "VAL".parse().unwrap())
.execute()
.dbg_unwrap()
.into_inner()
.try_into()
.dbg_unwrap();

Mint::asset_numeric(val, rose_id)
.execute()
.dbg_expect("Failed to mint rose");
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ iroha_smart_contract.workspace = true
panic-halt.workspace = true
lol_alloc.workspace = true
getrandom.workspace = true
parity-scale-codec.workspace = true
serde_json = { version = "1.0.108", default-features = false }
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,45 @@ extern crate alloc;
use alloc::string::ToString as _;
use core::num::NonZeroU32;

use iroha_smart_contract::{data_model::metadata::MetadataValueBox, parse, prelude::*};
use iroha_smart_contract::{
data_model::{metadata::MetadataValueBox, query::cursor::ForwardCursor},
parse,
prelude::*,
};
use lol_alloc::{FreeListAllocator, LockedAllocator};
use parity_scale_codec::{Decode, DecodeAll, Encode};

#[global_allocator]
static ALLOC: LockedAllocator<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new());

getrandom::register_custom_getrandom!(iroha_smart_contract::stub_getrandom);

#[derive(Debug, Decode)]
struct QueryOutputCursor {
_batch: alloc::vec::Vec<Asset>,
cursor: ForwardCursor,
}

/// Execute [`FindAllAssets`] and save cursor to the owner's metadata.
/// NOTE: DON'T TAKE THIS AS AN EXAMPLE, THIS IS ONLY FOR TESTING INTERNALS OF IROHA
#[iroha_smart_contract::main]
fn main(owner: AccountId) {
let asset_cursor = FindAllAssets
.fetch_size(FetchSize::new(Some(NonZeroU32::try_from(1).dbg_unwrap())))
.execute()
.dbg_unwrap();

let (_batch, cursor) = asset_cursor.into_parts();
// NOTE: QueryOutputCursor fields are private therefore
// we guess the layout by encoding and then decoding
let asset_cursor = QueryOutputCursor::decode_all(
&mut &FindAllAssets
.fetch_size(FetchSize::new(Some(NonZeroU32::try_from(1).dbg_unwrap())))
.execute()
.dbg_unwrap()
.encode()[..],
)
.dbg_unwrap();

SetKeyValue::account(
owner,
parse!("cursor" as Name),
MetadataValueBox::String(
serde_json::to_value(cursor)
serde_json::to_value(&asset_cursor.cursor)
.dbg_expect("Failed to convert cursor to JSON")
.to_string(),
),
Expand Down
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 @@ -376,6 +376,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(SetKeyValue::trigger(
trigger_id.clone(),
"VAL".parse()?,
numeric!(1),
))
.unwrap();
let call_trigger = ExecuteTrigger::new(trigger_id);
test_client.submit_blocking(call_trigger)?;

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
13 changes: 9 additions & 4 deletions config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn minimal_config_snapshot() -> Result<()> {
max_instruction_number: 4096,
max_wasm_size_bytes: 4194304,
},
asset_metadata_limits: Limits {
domain_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
Expand All @@ -150,7 +150,11 @@ fn minimal_config_snapshot() -> Result<()> {
capacity: 1048576,
max_entry_len: 4096,
},
domain_metadata_limits: Limits {
asset_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
trigger_metadata_limits: Limits {
capacity: 1048576,
max_entry_len: 4096,
},
Expand Down Expand Up @@ -410,10 +414,11 @@ fn full_envs_set_is_consumed() -> Result<()> {
block_time: None,
commit_time: None,
transaction_limits: None,
asset_metadata_limits: None,
domain_metadata_limits: None,
asset_definition_metadata_limits: None,
account_metadata_limits: None,
domain_metadata_limits: None,
asset_metadata_limits: None,
trigger_metadata_limits: None,
ident_length_limits: None,
executor_fuel_limit: None,
executor_max_memory: None,
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
11 changes: 7 additions & 4 deletions configs/swarm/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"NewParameter": "?TransactionLimits=4096,4194304_TL"
},
{
"NewParameter": "?WSVAssetMetadataLimits=1048576,4096_ML"
"NewParameter": "?WSVDomainMetadataLimits=1048576,4096_ML"
},
{
"NewParameter": "?WSVAssetDefinitionMetadataLimits=1048576,4096_ML"
Expand All @@ -144,7 +144,10 @@
"NewParameter": "?WSVAccountMetadataLimits=1048576,4096_ML"
},
{
"NewParameter": "?WSVDomainMetadataLimits=1048576,4096_ML"
"NewParameter": "?WSVAssetMetadataLimits=1048576,4096_ML"
},
{
"NewParameter": "?WSVTriggerMetadataLimits=1048576,4096_ML"
},
{
"NewParameter": "?WSVIdentLengthLimits=1,128_LL"
Expand All @@ -167,13 +170,13 @@
"id": "ALICE_METADATA_ACCESS",
"permissions": [
{
"definition_id": "CanRemoveKeyValueInUserAccount",
"definition_id": "CanRemoveKeyValueInAccount",
"payload": {
"account_id": "alice@wonderland"
}
},
{
"definition_id": "CanSetKeyValueInUserAccount",
"definition_id": "CanSetKeyValueInAccount",
"payload": {
"account_id": "alice@wonderland"
}
Expand Down
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
Loading

0 comments on commit b2b8d40

Please sign in to comment.