Skip to content

Commit

Permalink
[feature] hyperledger-iroha#2227: disallow minting asset without regi…
Browse files Browse the repository at this point in the history
…stering it first

Signed-off-by: Artemii Gerasimovich <gerasimovich@soramitsu.co.jp>
  • Loading branch information
Artemii Gerasimovich committed Jun 8, 2022
1 parent 7c0bfa4 commit cdbc8ff
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 71 deletions.
10 changes: 7 additions & 3 deletions cli/src/torii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ fn register_asset_definition(name: &str) -> Instruction {
RegisterBox::new(AssetDefinition::quantity(asset_definition_id(name))).into()
}

fn mint_asset(quantity: u32, asset: &str, account: &str) -> Instruction {
MintBox::new(Value::U32(quantity), asset_id(asset, account)).into()
fn register_asset(quantity: u32, asset: &str, account: &str) -> Instruction {
RegisterBox::new(Asset::new(
asset_id(asset, account),
AssetValue::Quantity(quantity),
))
.into()
}

fn asset_definition_id(name: &str) -> AssetDefinitionId {
Expand All @@ -304,7 +308,7 @@ async fn find_asset() {
.given(register_domain())
.given(register_account("alice"))
.given(register_asset_definition("rose"))
.given(mint_asset(99, "rose", "alice"))
.given(register_asset(99, "rose", "alice"))
.query(QueryBox::FindAssetById(FindAssetById::new(asset_id(
"rose", "alice",
))))
Expand Down
5 changes: 5 additions & 0 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ fn query_requests(criterion: &mut Criterion) {
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let asset_definition_id = AssetDefinitionId::new("xor".parse().expect("Valid"), domain_id);
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
let quantity: u32 = 200;
let mint_asset = MintBox::new(
Value::U32(quantity),
Expand All @@ -82,6 +86,7 @@ fn query_requests(criterion: &mut Criterion) {
create_domain.into(),
create_account.into(),
create_asset.into(),
register_asset.into(),
mint_asset.into(),
])
.expect("Failed to prepare state");
Expand Down
5 changes: 3 additions & 2 deletions client/benches/tps/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ impl MeasurerUnit {
.into();

let register_me = RegisterBox::new(Account::new(account_id(self.name), [public_key]));
let mint_a_rose = MintBox::new(1_u32, asset_id(self.name));
let register_a_rose =
RegisterBox::new(Asset::new(asset_id(self.name), AssetValue::Quantity(13)));

let _ = self.client.submit_blocking(register_me)?;
let _ = self.client.submit_blocking(mint_a_rose)?;
let _ = self.client.submit_blocking(register_a_rose)?;

Ok(self)
}
Expand Down
21 changes: 18 additions & 3 deletions client/tests/integration/add_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() ->
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::quantity(asset_definition_id.clone()));
let regiser_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
//When
let quantity: u32 = 200;
Expand All @@ -28,7 +32,8 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() ->
account_id.clone(),
)),
);
let instructions: Vec<Instruction> = vec![create_asset.into(), mint.into()];
let instructions: Vec<Instruction> =
vec![create_asset.into(), regiser_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id), |result| {
Expand All @@ -49,6 +54,10 @@ fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount(
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::big_quantity(asset_definition_id.clone()));
let regiser_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::BigQuantity(0),
));
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
//When
let quantity: u128 = 2_u128.pow(65);
Expand All @@ -59,7 +68,8 @@ fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount(
account_id.clone(),
)),
);
let instructions: Vec<Instruction> = vec![create_asset.into(), mint.into()];
let instructions: Vec<Instruction> =
vec![create_asset.into(), regiser_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id), |result| {
Expand All @@ -80,6 +90,10 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let identifiable_box = AssetDefinition::fixed(asset_definition_id.clone());
let create_asset = RegisterBox::new(identifiable_box);
let regiser_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Fixed(Fixed::ZERO),
));
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();

//When
Expand All @@ -91,7 +105,8 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
account_id.clone(),
)),
);
let instructions: Vec<Instruction> = vec![create_asset.into(), mint.into()];
let instructions: Vec<Instruction> =
vec![create_asset.into(), regiser_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id.clone()), |result| {
Expand Down
5 changes: 5 additions & 0 deletions client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
iroha_client.submit_all(vec![
create_domain.into(),
create_account.into(),
create_asset.into(),
register_asset.into(),
])?;
thread::sleep(pipeline_time * 3);
//When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ fn smartcontract_entry_point(_account_id: AccountId) {
let account_nft_id = <Asset as Identifiable>::Id::new(nft_id, account.id().clone());

Instruction::Register(RegisterBox::new(nft_definition)).execute();
Instruction::Register(RegisterBox::new(Asset::new(
account_nft_id.clone(),
AssetValue::Store(Metadata::new()),
)))
.execute();
Instruction::SetKeyValue(SetKeyValueBox::new(
account_nft_id,
Name::from_str("has_this_nft").dbg_unwrap(),
Expand Down
5 changes: 5 additions & 0 deletions client/tests/integration/multiple_blocks_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ fn long_multiple_blocks_created() {
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));

iroha_client
.submit_all(vec![
create_domain.into(),
create_account.into(),
create_asset.into(),
register_asset.into(),
])
.expect("Failed to prepare state.");

Expand Down
10 changes: 9 additions & 1 deletion client/tests/integration/multisignature_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ fn transaction_signed_by_new_signatory_of_account_should_pass() -> Result<()> {
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::quantity(asset_definition_id.clone()));
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
let key_pair = KeyPair::generate()?;
let add_signatory = MintBox::new(
key_pair.public_key().clone(),
IdBox::AccountId(account_id.clone()),
);

iroha_client.submit_all(vec![create_asset.into(), add_signatory.into()])?;
iroha_client.submit_all(vec![
create_asset.into(),
register_asset.into(),
add_signatory.into(),
])?;
thread::sleep(pipeline_time * 2);
//When
let quantity: u32 = 200;
Expand Down
9 changes: 7 additions & 2 deletions client/tests/integration/multisignature_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ fn multisignature_transactions_should_wait_for_all_signatures() {

//When
let quantity: u32 = 200;
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
let mint_asset = MintBox::new(
Value::U32(quantity),
IdBox::AssetId(AssetId::new(asset_definition_id, account_id.clone())),
Expand All @@ -70,7 +74,8 @@ fn multisignature_transactions_should_wait_for_all_signatures() {
client_configuration.public_key = public_key1;
client_configuration.private_key = private_key1;
let iroha_client = Client::new(&client_configuration).expect("Invalid client configuration");
let instructions: Vec<Instruction> = vec![mint_asset.clone().into()];
let instructions: Vec<Instruction> =
vec![register_asset.clone().into(), mint_asset.clone().into()];
let transaction = iroha_client
.build_transaction(instructions.into(), UnlimitedMetadata::new())
.expect("Failed to create transaction.");
Expand All @@ -97,7 +102,7 @@ fn multisignature_transactions_should_wait_for_all_signatures() {
client_configuration.public_key = public_key2;
client_configuration.private_key = private_key2;
let iroha_client_2 = Client::new(&client_configuration).expect("Invalid client configuration");
let instructions: Vec<Instruction> = vec![mint_asset.into()];
let instructions: Vec<Instruction> = vec![register_asset.into(), mint_asset.into()];
let transaction = iroha_client_2
.build_transaction(instructions.into(), UnlimitedMetadata::new())
.expect("Failed to create transaction.");
Expand Down
17 changes: 15 additions & 2 deletions client/tests/integration/non_mintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {

let metadata = UnlimitedMetadata::default();

let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));

let mint = MintBox::new(
Value::U32(200_u32),
IdBox::AssetId(AssetId::new(
Expand All @@ -28,10 +33,10 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {
)),
);

let instructions: [Instruction; 2] = [create_asset.into(), mint.clone().into()];
let instructions: [Instruction; 3] = [create_asset.into(), register_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;

// We can register and mint the non-mintable token
// We can register the non-mintable token
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id.clone()), |result| {
result.iter().any(|asset| {
Expand All @@ -40,6 +45,14 @@ fn non_mintable_asset_can_be_minted_once_but_not_twice() -> Result<()> {
})
})?;

let mint = MintBox::new(
Value::U32(200_u32),
IdBox::AssetId(AssetId::new(
asset_definition_id.clone(),
account_id.clone(),
)),
);

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

Expand Down
6 changes: 3 additions & 3 deletions client/tests/integration/queries/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ fn find_accounts_with_asset() -> Result<()> {
.collect::<Vec<_>>();
test_client.submit_all_blocking(register_accounts)?;

let mint_asset = accounts
let register_asset = accounts
.iter()
.cloned()
.map(|account_id| <Asset as Identifiable>::Id::new(definition_id.clone(), account_id))
.map(|asset_id| MintBox::new(1_u32, asset_id).into())
.map(|asset_id| RegisterBox::new(Asset::new(asset_id, AssetValue::Quantity(0))).into())
.collect::<Vec<_>>();
test_client.submit_all_blocking(mint_asset)?;
test_client.submit_all_blocking(register_asset)?;

let accounts = HashSet::from(accounts);

Expand Down
6 changes: 5 additions & 1 deletion client/tests/integration/restart_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ 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::quantity(asset_definition_id.clone()));
iroha_client.submit(create_asset)?;
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
iroha_client.submit_all(vec![create_asset.into(), register_asset.into()])?;
thread::sleep(pipeline_time * 2);
// When
let quantity: u32 = 200;
Expand Down
4 changes: 3 additions & 1 deletion client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ fn add_role_to_limit_transfer_count() -> Result<()> {

// Registering Bob
let register_bob = RegisterBox::new(Account::new(bob_id, []));
test_client.submit_blocking(register_bob)?;
let register_roses_bob =
RegisterBox::new(Asset::new(bob_rose_id.clone(), AssetValue::Quantity(0)));
test_client.submit_all_blocking(vec![register_bob.into(), register_roses_bob.into()])?;

// Registering new role which sets `Transfer` execution count limit to
// `COUNT` for every `PERIOD_MS` milliseconds
Expand Down
28 changes: 26 additions & 2 deletions client/tests/integration/transfer_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ use super::Configuration;

#[test]
fn simulate_transfer_quantity() {
simulate_transfer(200_u32, 20_u32, AssetDefinition::quantity)
simulate_transfer(
200_u32,
20_u32,
AssetDefinition::quantity,
AssetValue::Quantity(0),
)
}

#[test]
fn simulate_transfer_big_quantity() {
simulate_transfer(200_u128, 20_u128, AssetDefinition::big_quantity)
simulate_transfer(
200_u128,
20_u128,
AssetDefinition::big_quantity,
AssetValue::BigQuantity(0),
)
}

#[test]
Expand All @@ -25,6 +35,7 @@ fn simulate_transfer_fixed() {
Fixed::try_from(200_f64).expect("Valid"),
Fixed::try_from(20_f64).expect("Valid"),
AssetDefinition::fixed,
AssetValue::Fixed(Fixed::try_from(0_f64).expect("Valid")),
)
}

Expand All @@ -36,6 +47,7 @@ fn simulate_insufficient_funds() {
Fixed::try_from(20_f64).expect("Valid"),
Fixed::try_from(200_f64).expect("Valid"),
AssetDefinition::fixed,
AssetValue::Fixed(Fixed::try_from(0_f64).expect("Valid")),
)
}

Expand All @@ -48,6 +60,7 @@ fn simulate_transfer<
starting_amount: T,
amount_to_transfer: T,
value_type: D,
default_value: AssetValue,
) where
Value: From<T>,
{
Expand All @@ -68,6 +81,15 @@ fn simulate_transfer<
let create_account2 = RegisterBox::new(Account::new(account2_id.clone(), [public_key2]));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let create_asset = RegisterBox::new(value_type(asset_definition_id.clone()));
let register_asset1 = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account1_id.clone()),
default_value.clone(),
));

let register_asset2 = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account2_id.clone()),
default_value,
));
let mint_asset = MintBox::new(
Value::from(starting_amount),
IdBox::AssetId(AssetId::new(
Expand All @@ -82,6 +104,8 @@ fn simulate_transfer<
create_account1.into(),
create_account2.into(),
create_asset.into(),
register_asset1.into(),
register_asset2.into(),
mint_asset.into(),
])
.expect("Failed to prepare state.");
Expand Down
5 changes: 5 additions & 0 deletions client/tests/integration/unregister_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ fn init() -> Result<(
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse().expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let register_asset = RegisterBox::new(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
AssetValue::Quantity(0),
));
client.submit_all(vec![
create_domain.into(),
create_account.into(),
create_asset.into(),
register_asset.into(),
])?;
thread::sleep(pipeline_time * 2);
iroha_logger::info!("Init");
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/unstable_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn unstable_network_7_peers_1_fault() {
}

#[test]
#[ignore = "This test does not guarantee to have positive outcome given a fixed time."]
// #[ignore = "This test does not guarantee to have positive outcome given a fixed time."]
fn unstable_network_7_peers_2_faults() {
unstable_network(7, 2, 5, 100, Configuration::pipeline_time());
}
Expand Down
Loading

0 comments on commit cdbc8ff

Please sign in to comment.