Skip to content

Commit

Permalink
refactor: move multisig args into custom executor data model
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Jul 3, 2024
1 parent b2b0601 commit 3c6f11b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 53 deletions.
28 changes: 3 additions & 25 deletions client/tests/integration/multisig.rs
Original file line number Diff line number Diff line change
@@ -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<AccountId>,
}

/// Mirror trigger args for `multisig`
#[derive(Serialize, Deserialize)]
enum MultisigArgs {
/// Accept instruction proposal and initialize votes with the proposer's one
Instruction(Vec<InstructionBox>),
/// Accept vote for certain instruction
Vote(HashOf<Vec<InstructionBox>>),
}

#[test]
fn mutlisig() -> Result<()> {
let (_rt, _peer, test_client) = <PeerBuilder>::new().with_port(11_400).start_with_runtime();
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern crate alloc;

pub mod complex_isi;
pub mod multisig;
pub mod parameters;
pub mod permissions;
pub mod simple_isi;
Original file line number Diff line number Diff line change
@@ -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<AccountId>,
}

/// 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<InstructionBox>),
/// Accept vote for certain instructions
Vote(HashOf<Vec<InstructionBox>>),
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions client/tests/integration/smartcontracts/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FreeListAllocator> = 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<InstructionBox>),
/// Accept vote for certain instruction
Vote(HashOf<Vec<InstructionBox>>),
}

#[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()
Expand All @@ -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");
Expand All @@ -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<AccountId> =
FindTriggerKeyValueByIdAndKey::new(id.clone(), votes_metadata_key.clone())
.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FreeListAllocator> = LockedAllocator::new(FreeListAllocator::new());
Expand All @@ -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<AccountId>,
}

#[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")
Expand Down

0 comments on commit 3c6f11b

Please sign in to comment.