Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(client): allow turbofish with multiple Instructions #4805

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ fn query_requests(criterion: &mut Criterion) {
let iroha = Client::new(client_config);
thread::sleep(std::time::Duration::from_millis(5000));

let instructions: [InstructionBox; 4] = [
create_domain.into(),
create_account.into(),
create_asset.into(),
mint_asset.into(),
];
let _ = iroha
.submit_all(instructions)
.submit_all::<InstructionBox>([
create_domain.into(),
create_account.into(),
create_asset.into(),
mint_asset.into(),
])
.expect("Failed to prepare state");

let request = asset::by_account_id(account_id);
Expand Down Expand Up @@ -138,9 +137,9 @@ fn instruction_submits(criterion: &mut Criterion) {
rt.block_on(builder.start_with_peer(&mut peer));
let mut group = criterion.benchmark_group("instruction-requests");
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id)).into();
let create_domain = Register::domain(Domain::new(domain_id));
let (account_id, _account_keypair) = gen_account_in("domain");
let create_account = Register::account(Account::new(account_id.clone())).into();
let create_account = Register::account(Account::new(account_id.clone()));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let client_config = iroha::samples::get_client_config(
get_chain_id(),
Expand All @@ -150,7 +149,7 @@ fn instruction_submits(criterion: &mut Criterion) {
let iroha = Client::new(client_config);
thread::sleep(std::time::Duration::from_millis(5000));
let _ = iroha
.submit_all([create_domain, create_account])
.submit_all::<InstructionBox>([create_domain.into(), create_account.into()])
.expect("Failed to create role.");
thread::sleep(std::time::Duration::from_millis(500));
let mut success_count = 0;
Expand Down
6 changes: 3 additions & 3 deletions client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ fn create_million_accounts_directly() {
for i in 0_u32..1_000_000_u32 {
let domain_id: DomainId = format!("wonderland-{i}").parse().expect("Valid");
let normal_account_id = AccountId::new(domain_id.clone(), KeyPair::random().into_parts().0);
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id)).into();
let create_account = Register::account(Account::new(normal_account_id.clone())).into();
let create_domain = Register::domain(Domain::new(domain_id));
let create_account = Register::account(Account::new(normal_account_id.clone()));
if test_client
.submit_all([create_domain, create_account])
.submit_all::<InstructionBox>([create_domain.into(), create_account.into()])
.is_err()
{
thread::sleep(Duration::from_millis(100));
Expand Down
34 changes: 17 additions & 17 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ impl Client {
///
/// # Errors
/// Fails if signing transaction fails
pub fn build_transaction(
pub fn build_transaction<Exec: Into<Executable>>(
&self,
instructions: impl Into<Executable>,
instructions: Exec,
metadata: Metadata,
) -> SignedTransaction {
let tx_builder = TransactionBuilder::new(self.chain.clone(), self.account.clone());
Expand Down Expand Up @@ -497,7 +497,7 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit(&self, instruction: impl Instruction) -> Result<HashOf<SignedTransaction>> {
pub fn submit<I: Instruction>(&self, instruction: I) -> Result<HashOf<SignedTransaction>> {
let isi = instruction.into();
self.submit_all([isi])
}
Expand All @@ -507,9 +507,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_all(
pub fn submit_all<I: Instruction>(
&self,
instructions: impl IntoIterator<Item = impl Instruction>,
instructions: impl IntoIterator<Item = I>,
) -> Result<HashOf<SignedTransaction>> {
self.submit_all_with_metadata(instructions, Metadata::default())
}
Expand All @@ -520,9 +520,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_with_metadata(
pub fn submit_with_metadata<I: Instruction>(
&self,
instruction: impl Instruction,
instruction: I,
metadata: Metadata,
) -> Result<HashOf<SignedTransaction>> {
self.submit_all_with_metadata([instruction], metadata)
Expand All @@ -534,9 +534,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_all_with_metadata(
pub fn submit_all_with_metadata<I: Instruction>(
&self,
instructions: impl IntoIterator<Item = impl Instruction>,
instructions: impl IntoIterator<Item = I>,
metadata: Metadata,
) -> Result<HashOf<SignedTransaction>> {
self.submit_transaction(&self.build_transaction(instructions, metadata))
Expand Down Expand Up @@ -704,9 +704,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_blocking(
pub fn submit_blocking<I: Instruction>(
&self,
instruction: impl Instruction,
instruction: I,
) -> Result<HashOf<SignedTransaction>> {
self.submit_all_blocking(vec![instruction.into()])
}
Expand All @@ -716,9 +716,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_all_blocking(
pub fn submit_all_blocking<I: Instruction>(
&self,
instructions: impl IntoIterator<Item = impl Instruction>,
instructions: impl IntoIterator<Item = I>,
) -> Result<HashOf<SignedTransaction>> {
self.submit_all_blocking_with_metadata(instructions, Metadata::default())
}
Expand All @@ -729,9 +729,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_blocking_with_metadata(
pub fn submit_blocking_with_metadata<I: Instruction>(
&self,
instruction: impl Instruction,
instruction: I,
metadata: Metadata,
) -> Result<HashOf<SignedTransaction>> {
self.submit_all_blocking_with_metadata(vec![instruction.into()], metadata)
Expand All @@ -743,9 +743,9 @@ impl Client {
///
/// # Errors
/// Fails if sending transaction to peer fails or if it response with error
pub fn submit_all_blocking_with_metadata(
pub fn submit_all_blocking_with_metadata<I: Instruction>(
&self,
instructions: impl IntoIterator<Item = impl Instruction>,
instructions: impl IntoIterator<Item = I>,
metadata: Metadata,
) -> Result<HashOf<SignedTransaction>> {
let transaction = self.build_transaction(instructions, metadata);
Expand Down
45 changes: 22 additions & 23 deletions client/tests/integration/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ fn client_register_asset_should_add_asset_once_but_not_twice() -> Result<()> {
let account_id = ALICE_ID.clone();

let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").expect("Valid");
let create_asset: InstructionBox =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone())).into();
let register_asset: InstructionBox = Register::asset(Asset::new(
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
let register_asset = Register::asset(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
0_u32,
))
.into();
));

test_client.submit_all([create_asset, register_asset.clone()])?;
test_client
.submit_all::<InstructionBox>([create_asset.into(), register_asset.clone().into()])?;

// Registering an asset to an account which doesn't have one
// should result in asset being created
Expand Down Expand Up @@ -275,22 +275,21 @@ fn find_rate_and_make_exchange_isi_should_succeed() {
let buyer_eth: AssetId = format!("eth#crypto#{}", &buyer_id)
.parse()
.expect("should be valid");
let instructions: [InstructionBox; 12] = [
register::domain("exchange").into(),
register::domain("company").into(),
register::domain("crypto").into(),
register::account(dex_id.clone()).into(),
register::account(seller_id.clone()).into(),
register::account(buyer_id.clone()).into(),
register::asset_definition_numeric("btc/eth#exchange").into(),
register::asset_definition_numeric("btc#crypto").into(),
register::asset_definition_numeric("eth#crypto").into(),
Mint::asset_numeric(20_u32, rate.clone()).into(),
Mint::asset_numeric(10_u32, seller_btc.clone()).into(),
Mint::asset_numeric(200_u32, buyer_eth.clone()).into(),
];
test_client
.submit_all_blocking(instructions)
.submit_all_blocking::<InstructionBox>([
register::domain("exchange").into(),
register::domain("company").into(),
register::domain("crypto").into(),
register::account(dex_id.clone()).into(),
register::account(seller_id.clone()).into(),
register::account(buyer_id.clone()).into(),
register::asset_definition_numeric("btc/eth#exchange").into(),
register::asset_definition_numeric("btc#crypto").into(),
register::asset_definition_numeric("eth#crypto").into(),
Mint::asset_numeric(20_u32, rate.clone()).into(),
Mint::asset_numeric(10_u32, seller_btc.clone()).into(),
Mint::asset_numeric(200_u32, buyer_eth.clone()).into(),
])
.expect("transaction should be committed");

let alice_id = ALICE_ID.clone();
Expand Down Expand Up @@ -408,7 +407,7 @@ fn fail_if_dont_satisfy_spec() {

let isi = |value: Numeric| {
[
InstructionBox::from(Register::asset(Asset::new(asset_id.clone(), value))),
Register::asset(Asset::new(asset_id.clone(), value)).into(),
Mint::asset_numeric(value, asset_id.clone()).into(),
Burn::asset_numeric(value, asset_id.clone()).into(),
Transfer::asset_numeric(asset_id.clone(), value, bob_id.clone()).into(),
Expand All @@ -420,7 +419,7 @@ fn fail_if_dont_satisfy_spec() {

for isi in isi(fractional_value) {
let err = test_client
.submit_blocking(isi)
.submit_blocking::<InstructionBox>(isi)
.expect_err("Should be rejected due to non integer value");

let rejection_reason = err
Expand Down
13 changes: 8 additions & 5 deletions client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
BlockParameter::MaxTransactions(nonzero!(1_u64)),
)))?;

let create_domain: InstructionBox =
Register::domain(Domain::new(DomainId::from_str("domain")?)).into();
let create_domain = Register::domain(Domain::new(DomainId::from_str("domain")?));
let (account_id, _account_keypair) = gen_account_in("domain");
let create_account = Register::account(Account::new(account_id.clone())).into();
let create_account = Register::account(Account::new(account_id.clone()));
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone())).into();
client.submit_all([create_domain, create_account, create_asset])?;
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
client.submit_all::<InstructionBox>([
create_domain.into(),
create_account.into(),
create_asset.into(),
])?;
thread::sleep(pipeline_time * 3);
//When
let quantity = numeric!(200);
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration/events/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn trigger_completion_success_should_produce_event() -> Result<()> {
let register_trigger = Register::trigger(Trigger::new(
trigger_id.clone(),
Action::new(
vec![InstructionBox::from(instruction)],
vec![instruction],
Repeats::Indefinitely,
asset_id.account().clone(),
ExecuteTriggerEventFilter::new()
Expand Down Expand Up @@ -63,7 +63,7 @@ fn trigger_completion_failure_should_produce_event() -> Result<()> {
let register_trigger = Register::trigger(Trigger::new(
trigger_id.clone(),
Action::new(
vec![InstructionBox::from(fail_isi)],
vec![fail_isi],
Repeats::Indefinitely,
account_id.clone(),
ExecuteTriggerEventFilter::new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ fn long_multiple_blocks_created() -> Result<()> {
BlockParameter::MaxTransactions(nonzero!(1_u64)),
)))?;

let create_domain: InstructionBox = Register::domain(Domain::new("domain".parse()?)).into();
let create_domain = Register::domain(Domain::new("domain".parse()?));
let (account_id, _account_keypair) = gen_account_in("domain");
let create_account = Register::account(Account::new(account_id.clone())).into();
let create_account = Register::account(Account::new(account_id.clone()));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone())).into();
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));

client.submit_all([create_domain, create_account, create_asset])?;
client.submit_all::<InstructionBox>([
create_domain.into(),
create_account.into(),
create_asset.into(),
])?;

thread::sleep(pipeline_time);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ fn init() -> Result<(
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
let instructions: [InstructionBox; 4] = [
client.submit_all_blocking::<InstructionBox>([
set_max_txns_in_block.into(),
create_domain.into(),
create_account.into(),
create_asset.into(),
];
client.submit_all_blocking(instructions)?;
])?;
iroha_logger::info!("Init");
Ok((
rt,
Expand Down
4 changes: 1 addition & 3 deletions client/tests/integration/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ fn mutlisig() -> Result<()> {
assert_eq!(trigger.id(), &multisig_trigger_id);

let domain_id: DomainId = "domain_controlled_by_multisig".parse().unwrap();
let isi = vec![InstructionBox::from(Register::domain(Domain::new(
domain_id.clone(),
)))];
let isi = vec![Register::domain(Domain::new(domain_id.clone())).into()];
let isi_hash = HashOf::new(&isi);

let mut signatories_iter = signatories.into_iter();
Expand Down
19 changes: 10 additions & 9 deletions client/tests/integration/non_mintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,16 @@ fn non_mintable_asset_cannot_be_minted_if_registered_with_non_zero_value() -> Re
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset: InstructionBox = Register::asset_definition(
let create_asset = Register::asset_definition(
AssetDefinition::numeric(asset_definition_id.clone()).mintable_once(),
)
.into();
);

let asset_id = AssetId::new(asset_definition_id.clone(), account_id.clone());
let register_asset: InstructionBox =
Register::asset(Asset::new(asset_id.clone(), 1_u32)).into();
let register_asset = Register::asset(Asset::new(asset_id.clone(), 1_u32));

// We can register the non-mintable token
test_client.submit_all([create_asset, register_asset.clone()])?;
test_client
.submit_all::<InstructionBox>([create_asset.into(), register_asset.clone().into()])?;
test_client.poll_request(client::asset::by_account_id(account_id), |result| {
let assets = result.collect::<QueryResult<Vec<_>>>().expect("Valid");
assets.iter().any(|asset| {
Expand Down Expand Up @@ -110,9 +109,11 @@ fn non_mintable_asset_can_be_minted_if_registered_with_zero_value() -> Result<()
let mint = Mint::asset_numeric(1u32, asset_id);

// We can register the non-mintable token wih zero value and then mint it
let instructions: [InstructionBox; 3] =
[create_asset.into(), register_asset.into(), mint.into()];
test_client.submit_all(instructions)?;
test_client.submit_all::<InstructionBox>([
create_asset.into(),
register_asset.into(),
mint.into(),
])?;
test_client.poll_request(client::asset::by_account_id(account_id), |result| {
let assets = result.collect::<QueryResult<Vec<_>>>().expect("Valid");
assets.iter().any(|asset| {
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ fn fetch_size_should_work() -> Result<()> {

fn register_assets(client: &Client) -> Result<()> {
// FIXME transaction is rejected for more than a certain number of instructions
let register: Vec<InstructionBox> = ('a'..='j')
let register: Vec<_> = ('a'..='j')
.map(|c| c.to_string())
.map(|name| (name + "#wonderland").parse().expect("Valid"))
.map(|asset_definition_id| {
Register::asset_definition(AssetDefinition::numeric(asset_definition_id)).into()
Register::asset_definition(AssetDefinition::numeric(asset_definition_id))
})
.collect();
let _ = client.submit_all_blocking(register)?;
Expand Down
Loading
Loading