From 3c6f11b73b991c9e653644e6ebc55f41b618bd4c Mon Sep 17 00:00:00 2001 From: Shanin Roman Date: Wed, 3 Jul 2024 11:04:05 +0300 Subject: [PATCH] refactor: move multisig args into custom executor data model Signed-off-by: Shanin Roman --- client/tests/integration/multisig.rs | 28 ++----------------- .../executor_custom_data_model/src/lib.rs | 1 + .../src/multisig.rs | 24 ++++++++++++++++ .../smartcontracts/multisig/Cargo.toml | 1 + .../smartcontracts/multisig/src/lib.rs | 20 ++++--------- .../multisig_register/Cargo.toml | 1 + .../multisig_register/src/lib.rs | 18 +++--------- 7 files changed, 40 insertions(+), 53 deletions(-) create mode 100644 client/tests/integration/smartcontracts/executor_custom_data_model/src/multisig.rs diff --git a/client/tests/integration/multisig.rs b/client/tests/integration/multisig.rs index f8f1a5f0356..5d92c330b8c 100644 --- a/client/tests/integration/multisig.rs +++ b/client/tests/integration/multisig.rs @@ -1,42 +1,20 @@ -use std::{ - collections::{BTreeMap, BTreeSet}, - str::FromStr, -}; +use std::{collections::BTreeMap, str::FromStr}; +use executor_custom_data_model::multisig::{MultisigArgs, MultisigRegisterArgs}; use eyre::Result; use iroha::{ client::{self, ClientQueryError}, crypto::KeyPair, data_model::{ - account::NewAccount, prelude::*, transaction::{TransactionBuilder, WasmSmartContract}, }, }; use iroha_data_model::parameter::SmartContractParameter; use nonzero_ext::nonzero; -use serde::{Deserialize, Serialize}; use test_network::*; use test_samples::{gen_account_in, ALICE_ID}; -/// Mirror trigger args for `multisig_register` -#[derive(Serialize, Deserialize)] -struct MultisigRegisterArgs { - // Account id of multisig account should be manually checked to not have corresponding private key (or having master key is ok) - account: NewAccount, - // List of accounts responsible for handling multisig account - signatories: BTreeSet, -} - -/// Mirror trigger args for `multisig` -#[derive(Serialize, Deserialize)] -enum MultisigArgs { - /// Accept instruction proposal and initialize votes with the proposer's one - Instruction(Vec), - /// Accept vote for certain instruction - Vote(HashOf>), -} - #[test] fn mutlisig() -> Result<()> { let (_rt, _peer, test_client) = ::new().with_port(11_400).start_with_runtime(); @@ -129,7 +107,7 @@ fn mutlisig() -> Result<()> { let mut signatories_iter = signatories.into_iter(); if let Some((signatory, key_pair)) = signatories_iter.next() { - let args = MultisigArgs::Instruction(isi); + let args = MultisigArgs::Instructions(isi); let call_trigger = ExecuteTrigger::new(multisig_trigger_id.clone()).with_args(&args); test_client.submit_transaction_blocking( &TransactionBuilder::new(test_client.chain.clone(), signatory) diff --git a/client/tests/integration/smartcontracts/executor_custom_data_model/src/lib.rs b/client/tests/integration/smartcontracts/executor_custom_data_model/src/lib.rs index 4db9f68bde5..9c4eae98337 100644 --- a/client/tests/integration/smartcontracts/executor_custom_data_model/src/lib.rs +++ b/client/tests/integration/smartcontracts/executor_custom_data_model/src/lib.rs @@ -5,6 +5,7 @@ extern crate alloc; pub mod complex_isi; +pub mod multisig; pub mod parameters; pub mod permissions; pub mod simple_isi; diff --git a/client/tests/integration/smartcontracts/executor_custom_data_model/src/multisig.rs b/client/tests/integration/smartcontracts/executor_custom_data_model/src/multisig.rs new file mode 100644 index 00000000000..916fa06eaf6 --- /dev/null +++ b/client/tests/integration/smartcontracts/executor_custom_data_model/src/multisig.rs @@ -0,0 +1,24 @@ +//! Arguments to register and manage multisig account + +use alloc::{collections::btree_set::BTreeSet, vec::Vec}; + +use iroha_data_model::{account::NewAccount, prelude::*}; +use serde::{Deserialize, Serialize}; + +/// Arguments to multisig account register trigger +#[derive(Serialize, Deserialize)] +pub struct MultisigRegisterArgs { + // Account id of multisig account should be manually checked to not have corresponding private key (or having master key is ok) + pub account: NewAccount, + // List of accounts responsible for handling multisig account + pub signatories: BTreeSet, +} + +/// Arguments to multisig account manager trigger +#[derive(Serialize, Deserialize)] +pub enum MultisigArgs { + /// Accept instructions proposal and initialize votes with the proposer's one + Instructions(Vec), + /// Accept vote for certain instructions + Vote(HashOf>), +} diff --git a/client/tests/integration/smartcontracts/multisig/Cargo.toml b/client/tests/integration/smartcontracts/multisig/Cargo.toml index f35eb103395..956becaf9a5 100644 --- a/client/tests/integration/smartcontracts/multisig/Cargo.toml +++ b/client/tests/integration/smartcontracts/multisig/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ['cdylib'] [dependencies] iroha_trigger.workspace = true +executor_custom_data_model.workspace = true panic-halt.workspace = true lol_alloc.workspace = true diff --git a/client/tests/integration/smartcontracts/multisig/src/lib.rs b/client/tests/integration/smartcontracts/multisig/src/lib.rs index 2a0a196ba12..f26a9af2c5a 100644 --- a/client/tests/integration/smartcontracts/multisig/src/lib.rs +++ b/client/tests/integration/smartcontracts/multisig/src/lib.rs @@ -8,26 +8,18 @@ extern crate panic_halt; use alloc::{collections::btree_set::BTreeSet, format, vec::Vec}; +use executor_custom_data_model::multisig::MultisigArgs; use iroha_trigger::{debug::dbg_panic, prelude::*}; use lol_alloc::{FreeListAllocator, LockedAllocator}; -use serde::{Deserialize, Serialize}; #[global_allocator] static ALLOC: LockedAllocator = LockedAllocator::new(FreeListAllocator::new()); getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); -#[derive(Serialize, Deserialize)] -enum Args { - /// Accept instruction proposal and initialize votes with the proposer's one - Instruction(Vec), - /// Accept vote for certain instruction - Vote(HashOf>), -} - #[iroha_trigger::main] fn main(id: TriggerId, _owner: AccountId, event: EventBox) { - let (args, signatory): (Args, AccountId) = match event { + let (args, signatory): (MultisigArgs, AccountId) = match event { EventBox::ExecuteTrigger(event) => ( event .args() @@ -40,15 +32,15 @@ fn main(id: TriggerId, _owner: AccountId, event: EventBox) { }; let instructions_hash = match &args { - Args::Instruction(instructions) => HashOf::new(instructions), - Args::Vote(instructions_hash) => *instructions_hash, + MultisigArgs::Instructions(instructions) => HashOf::new(instructions), + MultisigArgs::Vote(instructions_hash) => *instructions_hash, }; let votes_metadata_key: Name = format!("{instructions_hash}/votes").parse().unwrap(); let instructions_metadata_key: Name = format!("{instructions_hash}/instructions").parse().unwrap(); let (votes, instructions) = match args { - Args::Instruction(instructions) => { + MultisigArgs::Instructions(instructions) => { FindTriggerKeyValueByIdAndKey::new(id.clone(), votes_metadata_key.clone()) .execute() .expect_err("instructions are already submitted"); @@ -73,7 +65,7 @@ fn main(id: TriggerId, _owner: AccountId, event: EventBox) { (votes, instructions) } - Args::Vote(_instructions_hash) => { + MultisigArgs::Vote(_instructions_hash) => { let mut votes: BTreeSet = FindTriggerKeyValueByIdAndKey::new(id.clone(), votes_metadata_key.clone()) .execute() diff --git a/client/tests/integration/smartcontracts/multisig_register/Cargo.toml b/client/tests/integration/smartcontracts/multisig_register/Cargo.toml index 08442628486..c4d39bd128f 100644 --- a/client/tests/integration/smartcontracts/multisig_register/Cargo.toml +++ b/client/tests/integration/smartcontracts/multisig_register/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ['cdylib'] [dependencies] iroha_trigger.workspace = true iroha_executor_data_model.workspace = true +executor_custom_data_model.workspace = true panic-halt.workspace = true lol_alloc.workspace = true diff --git a/client/tests/integration/smartcontracts/multisig_register/src/lib.rs b/client/tests/integration/smartcontracts/multisig_register/src/lib.rs index ae587cc59c9..f3f2dfb6530 100644 --- a/client/tests/integration/smartcontracts/multisig_register/src/lib.rs +++ b/client/tests/integration/smartcontracts/multisig_register/src/lib.rs @@ -6,14 +6,12 @@ extern crate alloc; #[cfg(not(test))] extern crate panic_halt; -use alloc::{collections::btree_set::BTreeSet, format}; +use alloc::format; +use executor_custom_data_model::multisig::MultisigRegisterArgs; use iroha_executor_data_model::permission::trigger::CanExecuteUserTrigger; -use iroha_trigger::{ - debug::dbg_panic, prelude::*, smart_contract::data_model::account::NewAccount, -}; +use iroha_trigger::{debug::dbg_panic, prelude::*}; use lol_alloc::{FreeListAllocator, LockedAllocator}; -use serde::{Deserialize, Serialize}; #[global_allocator] static ALLOC: LockedAllocator = LockedAllocator::new(FreeListAllocator::new()); @@ -23,17 +21,9 @@ getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); // Trigger wasm code for handling multisig logic const WASM: &[u8] = core::include_bytes!(concat!(core::env!("OUT_DIR"), "/multisig.wasm")); -#[derive(Serialize, Deserialize)] -struct Args { - // Account id of multisig account should be manually checked to not have corresponding private key (or having master key is ok) - account: NewAccount, - // List of accounts responsible for handling multisig account - signatories: BTreeSet, -} - #[iroha_trigger::main] fn main(_id: TriggerId, _owner: AccountId, event: EventBox) { - let args: Args = match event { + let args: MultisigRegisterArgs = match event { EventBox::ExecuteTrigger(event) => event .args() .dbg_expect("trigger expect args")