Skip to content

Commit

Permalink
refactor: add client entity to smart contracts (#5073)
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic authored Oct 9, 2024
1 parent 90940a1 commit 7473d04
Show file tree
Hide file tree
Showing 51 changed files with 1,602 additions and 1,441 deletions.
12 changes: 6 additions & 6 deletions crates/iroha/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ fn query_requests(criterion: &mut Criterion) {
format!("http://{}", peer.api_address).parse().unwrap(),
);

let iroha = Client::new(client_config);
let client = Client::new(client_config);
thread::sleep(std::time::Duration::from_millis(5000));

let _ = iroha
let _ = client
.submit_all::<InstructionBox>([
create_domain.into(),
create_account.into(),
Expand All @@ -82,7 +82,7 @@ fn query_requests(criterion: &mut Criterion) {
])
.expect("Failed to prepare state");

let query = iroha
let query = client
.query(asset::all())
.filter_with(|asset| asset.id.account.eq(account_id));
thread::sleep(std::time::Duration::from_millis(1500));
Expand Down Expand Up @@ -151,9 +151,9 @@ fn instruction_submits(criterion: &mut Criterion) {
get_key_pair(iroha_test_network::Signatory::Alice),
format!("http://{}", peer.api_address).parse().unwrap(),
);
let iroha = Client::new(client_config);
let client = Client::new(client_config);
thread::sleep(std::time::Duration::from_millis(5000));
let _ = iroha
let _ = client
.submit_all::<InstructionBox>([create_domain.into(), create_account.into()])
.expect("Failed to create role.");
thread::sleep(std::time::Duration::from_millis(500));
Expand All @@ -165,7 +165,7 @@ fn instruction_submits(criterion: &mut Criterion) {
200u32,
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
match iroha.submit(mint_asset) {
match client.submit(mint_asset) {
Ok(_) => success_count += 1,
Err(e) => {
eprintln!("Failed to execute instruction: {e}");
Expand Down
7 changes: 5 additions & 2 deletions crates/iroha/examples/register_1000_triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use std::num::NonZeroU64;
use iroha::{
client::Client,
crypto::KeyPair,
data_model::{prelude::*, trigger::TriggerId},
data_model::{
parameter::{Parameter, SmartContractParameter},
prelude::*,
trigger::TriggerId,
},
};
use iroha_data_model::parameter::{Parameter, SmartContractParameter};
use iroha_genesis::{GenesisBlock, GenesisBuilder};
use iroha_primitives::unique_vec;
use iroha_test_network::{
Expand Down
30 changes: 15 additions & 15 deletions crates/iroha/examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ fn domain_registration_test(config: Config) -> Result<(), Error> {

// #region rust_client_create
// Create an Iroha client
let iroha = Client::new(config);
let client = Client::new(config);
// #endregion rust_client_create

// #region domain_register_example_prepare_tx
// Prepare a transaction
let metadata = Metadata::default();
let instructions: Vec<InstructionBox> = vec![create_looking_glass.into()];
let tx = iroha.build_transaction(instructions, metadata);
let tx = client.build_transaction(instructions, metadata);
// #endregion domain_register_example_prepare_tx

// #region domain_register_example_submit_tx
// Submit a prepared domain registration transaction
iroha
client
.submit_transaction(&tx)
.wrap_err("Failed to submit transaction")?;
// #endregion domain_register_example_submit_tx
Expand Down Expand Up @@ -108,7 +108,7 @@ fn account_registration_test(config: Config) -> Result<(), Error> {
// #endregion register_account_crates

// Create an Iroha client
let iroha = Client::new(config);
let client = Client::new(config);

// #region register_account_create
// Generate a new public key for a new account
Expand All @@ -129,12 +129,12 @@ fn account_registration_test(config: Config) -> Result<(), Error> {
// Account's RegisterBox
let metadata = Metadata::default();
let instructions: Vec<InstructionBox> = vec![create_account.into()];
let tx = iroha.build_transaction(instructions, metadata);
let tx = client.build_transaction(instructions, metadata);
// #endregion register_account_prepare_tx

// #region register_account_submit_tx
// Submit a prepared account registration transaction
iroha.submit_transaction(&tx)?;
client.submit_transaction(&tx)?;
// #endregion register_account_submit_tx

// Finish the test successfully
Expand All @@ -153,7 +153,7 @@ fn asset_registration_test(config: Config) -> Result<(), Error> {
// #endregion register_asset_crates

// Create an Iroha client
let iroha = Client::new(config);
let client = Client::new(config);

// #region register_asset_create_asset
// Create an asset
Expand All @@ -167,7 +167,7 @@ fn asset_registration_test(config: Config) -> Result<(), Error> {
Register::asset_definition(AssetDefinition::numeric(asset_def_id.clone()).mintable_once());

// Submit a registration time
iroha.submit(register_time)?;
client.submit(register_time)?;
// #endregion register_asset_init_submit

// Generate a new public key for a new account
Expand All @@ -182,7 +182,7 @@ fn asset_registration_test(config: Config) -> Result<(), Error> {
let mint = Mint::asset_numeric(numeric!(12.34), AssetId::new(asset_def_id, account_id));

// Submit a minting transaction
iroha.submit_all([mint])?;
client.submit_all([mint])?;
// #endregion register_asset_mint_submit

// Finish the test successfully
Expand All @@ -198,7 +198,7 @@ fn asset_minting_test(config: Config) -> Result<(), Error> {
// #endregion mint_asset_crates

// Create an Iroha client
let iroha = Client::new(config);
let client = Client::new(config);

// Define the instances of an Asset and Account
// #region mint_asset_define_asset_account
Expand All @@ -214,7 +214,7 @@ fn asset_minting_test(config: Config) -> Result<(), Error> {
// #endregion mint_asset_mint

// #region mint_asset_submit_tx
iroha
client
.submit(mint_roses)
.wrap_err("Failed to submit transaction")?;
// #endregion mint_asset_submit_tx
Expand All @@ -232,7 +232,7 @@ fn asset_minting_test(config: Config) -> Result<(), Error> {
// #endregion mint_asset_mint_alt

// #region mint_asset_submit_tx_alt
iroha
client
.submit(mint_roses_alt)
.wrap_err("Failed to submit transaction")?;
// #endregion mint_asset_submit_tx_alt
Expand All @@ -250,7 +250,7 @@ fn asset_burning_test(config: Config) -> Result<(), Error> {
// #endregion burn_asset_crates

// Create an Iroha client
let iroha = Client::new(config);
let client = Client::new(config);

// #region burn_asset_define_asset_account
// Define the instances of an Asset and Account
Expand All @@ -266,7 +266,7 @@ fn asset_burning_test(config: Config) -> Result<(), Error> {
// #endregion burn_asset_burn

// #region burn_asset_submit_tx
iroha
client
.submit(burn_roses)
.wrap_err("Failed to submit transaction")?;
// #endregion burn_asset_submit_tx
Expand All @@ -284,7 +284,7 @@ fn asset_burning_test(config: Config) -> Result<(), Error> {
// #endregion burn_asset_burn_alt

// #region burn_asset_submit_tx_alt
iroha
client
.submit(burn_roses_alt)
.wrap_err("Failed to submit transaction")?;
// #endregion burn_asset_submit_tx_alt
Expand Down
3 changes: 1 addition & 2 deletions crates/iroha/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit<I: Instruction>(&self, instruction: I) -> Result<HashOf<SignedTransaction>> {
let isi = instruction.into();
pub fn submit<I: Instruction>(&self, isi: I) -> Result<HashOf<SignedTransaction>> {
self.submit_all([isi])
}

Expand Down
14 changes: 7 additions & 7 deletions crates/iroha/tests/integration/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use iroha::{
client,
crypto::KeyPair,
data_model::{
asset::{AssetDefinition, AssetDefinitionId},
parameter::SmartContractParameter,
prelude::*,
query::{builder::SingleQueryError, trigger::FindTriggers},
transaction::TransactionBuilder,
},
};
use iroha_data_model::asset::{AssetDefinition, AssetDefinitionId};
use iroha_executor_data_model::permission::asset_definition::CanRegisterAssetDefinition;
use iroha_test_network::*;
use iroha_test_samples::{gen_account_in, load_sample_wasm, ALICE_ID};
Expand Down Expand Up @@ -64,7 +64,7 @@ fn mutlisig() -> Result<()> {
.take(5)
.collect::<BTreeMap<AccountId, KeyPair>>();

let args = MultisigRegisterArgs {
let args = &MultisigRegisterArgs {
account: Account::new(multisig_account_id.clone()),
signatories: signatories.keys().cloned().collect(),
};
Expand All @@ -77,7 +77,7 @@ fn mutlisig() -> Result<()> {
.map(Register::account),
)?;

let call_trigger = ExecuteTrigger::new(multisig_register_trigger_id).with_args(&args);
let call_trigger = ExecuteTrigger::new(multisig_register_trigger_id).with_args(args);
test_client.submit_blocking(call_trigger)?;

// Check that multisig account exist
Expand Down Expand Up @@ -112,8 +112,8 @@ fn mutlisig() -> Result<()> {
let mut signatories_iter = signatories.into_iter();

if let Some((signatory, key_pair)) = signatories_iter.next() {
let args = MultisigArgs::Instructions(isi);
let call_trigger = ExecuteTrigger::new(multisig_trigger_id.clone()).with_args(&args);
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)
.with_instructions([call_trigger])
Expand All @@ -130,8 +130,8 @@ fn mutlisig() -> Result<()> {
assert!(matches!(err, SingleQueryError::ExpectedOneGotNone));

for (signatory, key_pair) in signatories_iter {
let args = MultisigArgs::Vote(isi_hash);
let call_trigger = ExecuteTrigger::new(multisig_trigger_id.clone()).with_args(&args);
let args = &MultisigArgs::Vote(isi_hash);
let call_trigger = ExecuteTrigger::new(multisig_trigger_id.clone()).with_args(args);
test_client.submit_transaction_blocking(
&TransactionBuilder::new(test_client.chain.clone(), signatory)
.with_instructions([call_trigger])
Expand Down
11 changes: 7 additions & 4 deletions crates/iroha/tests/integration/triggers/by_call_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use eyre::{eyre, Result, WrapErr};
use iroha::{
client::{self, Client},
crypto::KeyPair,
data_model::{prelude::*, query::error::FindError, transaction::Executable},
data_model::{
prelude::*,
query::{builder::SingleQueryError, error::FindError, trigger::FindTriggers},
transaction::Executable,
},
};
use iroha_data_model::query::{builder::SingleQueryError, trigger::FindTriggers};
use iroha_executor_data_model::permission::trigger::CanRegisterTrigger;
use iroha_genesis::GenesisBlock;
use iroha_logger::info;
Expand Down Expand Up @@ -653,8 +656,8 @@ fn call_execute_trigger_with_args() -> Result<()> {

test_client.submit_blocking(Register::trigger(trigger))?;

let args: MintRoseArgs = MintRoseArgs { val: 42 };
let call_trigger = ExecuteTrigger::new(trigger_id).with_args(&args);
let args = &MintRoseArgs { val: 42 };
let call_trigger = ExecuteTrigger::new(trigger_id).with_args(args);
test_client.submit_blocking(call_trigger)?;

let new_value = get_asset_value(&mut test_client, asset_id);
Expand Down
8 changes: 5 additions & 3 deletions crates/iroha/tests/integration/triggers/orphans.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use iroha::{client::Client, data_model::prelude::*};
use iroha_data_model::query::trigger::FindTriggers;
use iroha::{
client::Client,
data_model::{prelude::*, query::trigger::FindTriggers},
};
use iroha_test_network::{wait_for_genesis_committed, Peer, PeerBuilder};
use iroha_test_samples::gen_account_in;
use tokio::runtime::Runtime;

fn find_trigger(iroha: &Client, trigger_id: TriggerId) -> Option<TriggerId> {
iroha
.query(FindTriggers::new())
.filter_with(|trigger| trigger.id.eq(trigger_id.clone()))
.filter_with(|trigger| trigger.id.eq(trigger_id))
.execute_single()
.ok()
.map(|trigger| trigger.id)
Expand Down
18 changes: 9 additions & 9 deletions crates/iroha_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ fn submit(
metadata: Metadata,
context: &mut dyn RunContext,
) -> Result<()> {
let iroha = context.client_from_config();
let client = context.client_from_config();
let instructions = instructions.into();
let tx = iroha.build_transaction(instructions, metadata);
let tx = client.build_transaction(instructions, metadata);

#[cfg(not(debug_assertions))]
let err_msg = "Failed to submit transaction.";
#[cfg(debug_assertions)]
let err_msg = format!("Failed to submit transaction {tx:?}");
let hash = iroha.submit_transaction_blocking(&tx).wrap_err(err_msg)?;
let hash = client.submit_transaction_blocking(&tx).wrap_err(err_msg)?;
context.print_data(&hash)?;

Ok(())
Expand Down Expand Up @@ -333,9 +333,9 @@ mod events {

fn listen(filter: impl Into<EventFilterBox>, context: &mut dyn RunContext) -> Result<()> {
let filter = filter.into();
let iroha = context.client_from_config();
let client = context.client_from_config();
eprintln!("Listening to events with filter: {filter:?}");
iroha
client
.listen_for_events([filter])
.wrap_err("Failed to listen for events.")?
.try_for_each(|event| context.print_data(&event?))?;
Expand Down Expand Up @@ -363,9 +363,9 @@ mod blocks {
}

fn listen(height: NonZeroU64, context: &mut dyn RunContext) -> Result<()> {
let iroha = context.client_from_config();
let client = context.client_from_config();
eprintln!("Listening to blocks from height: {height}");
iroha
client
.listen_for_blocks(height)
.wrap_err("Failed to listen for blocks.")?
.try_for_each(|event| context.print_data(&event?))?;
Expand Down Expand Up @@ -909,8 +909,8 @@ mod asset {
impl RunArgs for Get {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
let Self { id: asset_id } = self;
let iroha = context.client_from_config();
let asset = iroha
let client = context.client_from_config();
let asset = client
.query(asset::all())
.filter_with(|asset| asset.id.eq(asset_id))
.execute_single()
Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ mod event {
let tx_events = self.as_ref().transactions().map(move |tx| {
let status = tx.error.as_ref().map_or_else(
|| TransactionStatus::Approved,
|error| TransactionStatus::Rejected(error.clone().into()),
|error| TransactionStatus::Rejected(error.clone()),
);

TransactionEvent {
Expand Down
Loading

0 comments on commit 7473d04

Please sign in to comment.