Skip to content

Commit

Permalink
refactor: add client entity to smart contracts
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Sep 20, 2024
1 parent cdf7dd7 commit 3833fb0
Show file tree
Hide file tree
Showing 46 changed files with 1,477 additions and 1,413 deletions.
12 changes: 6 additions & 6 deletions crates/iroha/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,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 @@ -85,7 +85,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 @@ -155,9 +155,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 @@ -169,7 +169,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
6 changes: 3 additions & 3 deletions crates/iroha/tests/integration/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 @@ -110,7 +110,7 @@ fn mutlisig() -> Result<()> {

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 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 @@ -128,7 +128,7 @@ fn mutlisig() -> Result<()> {

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 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
8 changes: 3 additions & 5 deletions crates/iroha/tests/integration/triggers/by_call_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use iroha::{
crypto::KeyPair,
data_model::{
prelude::*,
query::error::FindError,
query::{builder::SingleQueryError, error::FindError, trigger::FindTriggers},
transaction::{Executable, WasmSmartContract},
},
};
use iroha_data_model::query::{builder::SingleQueryError, trigger::FindTriggers};
use iroha_executor_data_model::permission::trigger::CanRegisterUserTrigger;
use iroha_genesis::GenesisBlock;
use iroha_logger::info;
Expand Down Expand Up @@ -217,8 +216,7 @@ fn trigger_should_not_be_executed_with_zero_repeats_count() -> Result<()> {
downcasted_error,
Some(FindError::Trigger(id)) if *id == trigger_id
),
"Unexpected error received: {:?}",
error
"Unexpected error received: {error:?}",
);

// Checking results
Expand Down Expand Up @@ -697,7 +695,7 @@ 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 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
Loading

0 comments on commit 3833fb0

Please sign in to comment.