Skip to content

Commit

Permalink
[refactor]: Add AssetDefinition builder.
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <[email protected]>
  • Loading branch information
appetrosyan committed May 12, 2022
1 parent c67ccf9 commit 7863f97
Show file tree
Hide file tree
Showing 32 changed files with 278 additions and 198 deletions.
11 changes: 7 additions & 4 deletions cli/src/torii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,13 @@ fn register_account(name: &str) -> Instruction {
}

fn register_asset_definition(name: &str) -> Instruction {
RegisterBox::new(AssetDefinition::new_quantity(AssetDefinitionId::new(
name.parse().expect("Valid"),
DOMAIN.parse().expect("Valid"),
)))
RegisterBox::new(
AssetDefinition::quantity(AssetDefinitionId::new(
name.parse().expect("Valid"),
DOMAIN.parse().expect("Valid"),
))
.build(),
)
.into()
}

Expand Down
3 changes: 2 additions & 1 deletion client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ fn query_requests(criterion: &mut Criterion) {
.public_key],
));
let asset_definition_id = AssetDefinitionId::new("xor".parse().expect("Valid"), domain_id);
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let quantity: u32 = 200;
let mint_asset = MintBox::new(
Value::U32(quantity),
Expand Down
9 changes: 6 additions & 3 deletions client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ fn main() {
PublicKey::default(),
)
.with_asset(
format!("xor-{}", i).parse().expect("Valid"),
AssetValueType::Quantity,
false,
AssetDefinition::quantity(
format!("xor-{}", i)
.parse::<<AssetDefinition as Identifiable>::Id>()
.expect("Valid"),
)
.build(),
)
.finish_domain();
}
Expand Down
20 changes: 9 additions & 11 deletions client/tests/integration/add_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() ->
// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
//When
let quantity: u32 = 200;
Expand Down Expand Up @@ -48,9 +49,8 @@ fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount(
// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_big_quantity(
asset_definition_id.clone(),
));
let create_asset =
RegisterBox::new(AssetDefinition::big_quantity(asset_definition_id.clone()).build());
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
//When
let quantity: u128 = 2_u128.pow(65);
Expand Down Expand Up @@ -80,7 +80,7 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let identifiable_box = AssetDefinition::new_fixed_precision(asset_definition_id.clone());
let identifiable_box = AssetDefinition::fixed(asset_definition_id.clone()).build();
let create_asset = RegisterBox::new(identifiable_box);
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();

Expand Down Expand Up @@ -132,18 +132,16 @@ fn client_add_asset_with_name_length_more_than_limit_should_not_commit_transacti

// Given
let normal_asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(
normal_asset_definition_id.clone(),
));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(normal_asset_definition_id.clone()).build());
test_client.submit(create_asset)?;
iroha_logger::info!("Creating asset");

let too_long_asset_name = "0".repeat(2_usize.pow(14));
let incorrect_asset_definition_id =
AssetDefinitionId::from_str(&(too_long_asset_name + "#wonderland")).expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(
incorrect_asset_definition_id.clone(),
));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(incorrect_asset_definition_id.clone()).build());

test_client.submit(create_asset)?;
iroha_logger::info!("Creating another asset");
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
[KeyPair::generate()?.public_key],
));
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
iroha_client.submit_all(vec![
create_domain.into(),
create_account.into(),
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/multiple_blocks_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn long_multiple_blocks_created() {
.public_key],
));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());

iroha_client
.submit_all(vec![
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/multisignature_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fn transaction_signed_by_new_signatory_of_account_should_pass() -> Result<()> {
// Given
let account_id: AccountId = "alice@wonderland".parse().expect("Valid");
let asset_definition_id: AssetDefinitionId = "xor#wonderland".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let key_pair = KeyPair::generate()?;
let add_signatory = MintBox::new(
key_pair.public_key.clone(),
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/multisignature_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn multisignature_transactions_should_wait_for_all_signatures() {
[key_pair_1.public_key.clone()],
));
let asset_definition_id = AssetDefinitionId::from_str("xor#domain").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let set_signature_condition = MintBox::new(
SignatureCheckCondition(
ContainsAll::new(
Expand Down
12 changes: 7 additions & 5 deletions client/tests/integration/non_mintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {
// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(IdentifiableBox::from(
AssetDefinition::new_quantity_token(asset_definition_id.clone()),
));
let create_asset = RegisterBox::new(
AssetDefinition::quantity(asset_definition_id.clone())
.mintable_once()
.build(),
);

let metadata = UnlimitedMetadata::default();

Expand All @@ -29,7 +31,7 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {
)),
);

let instructions: Vec<Instruction> = vec![create_asset.into(), mint.clone().into()];
let instructions: [Instruction; 2] = [create_asset.into(), mint.clone().into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;

// We can register and mint the non-mintable token
Expand All @@ -42,7 +44,7 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {
})?;

// We can submit the request to mint again.
test_client.submit_all(vec![mint.into()])?;
test_client.submit_all([mint.into()])?;

// However, this will fail
assert!(test_client
Expand Down
8 changes: 5 additions & 3 deletions client/tests/integration/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() {
let register: Vec<Instruction> = ('a'..'z')
.map(|c| c.to_string())
.map(|name| (name + "#wonderland").parse().expect("Valid"))
.map(AssetDefinition::new_quantity)
.map(RegisterBox::new)
.map(Instruction::Register)
.map(|asset_definition_id| {
Instruction::Register(RegisterBox::new(
AssetDefinition::quantity(asset_definition_id).build(),
))
})
.collect();
iroha_client
.submit_all(register)
Expand Down
6 changes: 4 additions & 2 deletions client/tests/integration/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn permissions_disallow_asset_transfer() {
let alice_id = AccountId::from_str("alice@wonderland").expect("Valid");
let bob_id = AccountId::from_str("bob@wonderland").expect("Valid");
let asset_definition_id: AssetDefinitionId = "xor#wonderland".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let register_bob = RegisterBox::new(Account::new(bob_id.clone(), []));

let alice_start_assets = get_assets(&mut iroha_client, &alice_id);
Expand Down Expand Up @@ -100,7 +101,8 @@ fn permissions_disallow_asset_burn() {
let alice_id = "alice@wonderland".parse().expect("Valid");
let bob_id: AccountId = "bob@wonderland".parse().expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let register_bob = RegisterBox::new(Account::new(bob_id.clone(), []));

let alice_start_assets = get_assets(&mut iroha_client, &alice_id);
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/restart_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn restarted_peer_should_have_the_same_asset_amount() -> Result<()> {

let account_id = AccountId::from_str("alice@wonderland").unwrap();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").unwrap();
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
iroha_client.submit(create_asset)?;
thread::sleep(pipeline_time * 2);
//When
Expand Down
14 changes: 4 additions & 10 deletions client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::restriction)]

use std::{str::FromStr as _, time::Duration};
use std::{collections::BTreeMap, str::FromStr as _, time::Duration};

use eyre::{eyre, Result};
use iroha_client::client::{self, Client};
Expand All @@ -9,8 +9,6 @@ use iroha_data_model::{permissions::Permissions, prelude::*};
use iroha_permissions_validators::public_blockchain::transfer;
use test_network::{Peer as TestPeer, *};
use tokio::runtime::Runtime;
use std::collections::BTreeMap;


#[test]
fn add_role_to_limit_transfer_count() -> Result<()> {
Expand Down Expand Up @@ -97,24 +95,20 @@ fn get_asset_value(client: &mut Client, asset_id: AssetId) -> Result<u32> {
Ok(*TryAsRef::<u32>::try_as_ref(asset.value())?)
}


#[test]
fn register_empty_role() -> Result<()> {
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

let role_id = iroha_data_model::role::Id::new("root".parse::<Name>().expect("Valid"));
let register_role = RegisterBox::new(IdentifiableBox::from(Role::new(
role_id,
Permissions::new(),
)));
let register_role = RegisterBox::new(Role::new(role_id, Permissions::new()));

test_client.submit(register_role)?;
Ok(())
}

#[test]
fn register_role_with_empty_token() -> Result<()> {
fn register_role_with_empty_token_params() -> Result<()> {
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

Expand All @@ -124,7 +118,7 @@ fn register_role_with_empty_token() -> Result<()> {
name: "token".parse().expect("Valid"),
params: BTreeMap::new(),
});
let register_role = RegisterBox::new(IdentifiableBox::from(Role::new(role_id, permissions)));
let register_role = RegisterBox::new(Role::new(role_id, permissions));

test_client.submit(register_role)?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/transfer_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fn client_can_transfer_asset_to_another_account() {
));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let quantity: u32 = 200;
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
let mint_asset = MintBox::new(
Value::U32(quantity),
IdBox::AssetId(AssetId::new(
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/triggers/event_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_mint_asset_when_new_asset_definition_created() -> Result<()> {

let tea_definition_id = "tea#wonderland".parse()?;
let register_tea_definition =
RegisterBox::new(AssetDefinition::new_quantity(tea_definition_id));
RegisterBox::new(AssetDefinition::quantity(tea_definition_id).build());
test_client.submit_blocking(register_tea_definition)?;

let new_value = get_asset_value(&mut test_client, asset_id)?;
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn client_has_rejected_and_acepted_txs_should_return_tx_history() {
// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
iroha_client
.submit(create_asset)
.expect("Failed to prepare state.");
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/tx_rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn client_sends_transaction_with_invalid_instruction_should_not_see_any_changes(
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let wrong_asset_definition_id = AssetDefinitionId::from_str("ksor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id));
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id).build());
let quantity: u32 = 200;
let mint_asset = MintBox::new(
Value::U32(quantity),
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/unregister_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ fn init() -> Result<(
[KeyPair::generate()?.public_key],
));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::new_quantity(asset_definition_id.clone()));
let create_asset =
RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()).build());
client.submit_all(vec![
create_domain.into(),
create_account.into(),
Expand Down
3 changes: 2 additions & 1 deletion client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ mod account {

mod asset {
use iroha_client::client::{self, asset, Client};
use iroha_data_model::asset::definition_builder::NewAssetDefinition;

use super::*;

Expand Down Expand Up @@ -466,7 +467,7 @@ mod asset {
metadata: Metadata(metadata),
} = self;
submit(
RegisterBox::new(AssetDefinition::new(id, value_type, !unmintable)),
RegisterBox::new(NewAssetDefinition::new(id, value_type, !unmintable).build()),
cfg,
metadata,
)
Expand Down
6 changes: 1 addition & 5 deletions core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ fn build_test_transaction(keys: KeyPair) -> Transaction {
"xor".parse().expect("Valid"),
domain_name.parse().expect("Valid"),
);
let create_asset = RegisterBox::new(AssetDefinition::new(
asset_definition_id,
AssetValueType::Quantity,
true,
));
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id).build());
let instructions: Vec<Instruction> = vec![
create_domain.into(),
create_account.into(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ impl From<&ValidBlock> for Vec<Event> {
block
.transactions
.iter()
.map(|transaction| {
.map(|transaction| -> Event {
PipelineEvent::new(
PipelineEntityKind::Transaction,
PipelineStatus::Validating,
Expand Down
Loading

0 comments on commit 7863f97

Please sign in to comment.