diff --git a/client/benches/tps/utils.rs b/client/benches/tps/utils.rs index 59aeaff31ca..98994235951 100644 --- a/client/benches/tps/utils.rs +++ b/client/benches/tps/utils.rs @@ -263,9 +263,9 @@ impl MeasurerUnit { fn mint_or_burn(&self) -> InstructionBox { let is_running_out = Less::new( - EvaluatesTo::new_unchecked( - Expression::Query(FindAssetQuantityById::new(asset_id(self.name)).into()).into(), - ), + EvaluatesTo::new_unchecked(Expression::Query( + FindAssetQuantityById::new(asset_id(self.name)).into(), + )), 100_u32, ); let supply_roses = MintBox::new(100_u32.to_value(), asset_id(self.name)); @@ -279,9 +279,9 @@ impl MeasurerUnit { // because if asset value hits 0 it's automatically deleted from account // and query `FindAssetQuantityById` return error let enough_to_transfer = Greater::new( - EvaluatesTo::new_unchecked( - Expression::Query(FindAssetQuantityById::new(asset_id(self.name)).into()).into(), - ), + EvaluatesTo::new_unchecked(Expression::Query( + FindAssetQuantityById::new(asset_id(self.name)).into(), + )), 1_u32, ); let transfer_rose = TransferBox::new( diff --git a/client/tests/integration/asset.rs b/client/tests/integration/asset.rs index 6c8c8f858d1..37afaf28fff 100644 --- a/client/tests/integration/asset.rs +++ b/client/tests/integration/asset.rs @@ -278,6 +278,13 @@ fn find_rate_and_make_exchange_isi_should_succeed() { )); }; + let buyer_account_id = account_id_new("buyer", "company"); + let seller_account_id = account_id_new("seller", "company"); + let asset_id = asset_id_new( + "btc2eth_rate", + "exchange", + account_id_new("dex", "exchange"), + ); test_client .submit_all_blocking(vec![ register::domain("exchange").into(), @@ -291,19 +298,15 @@ fn find_rate_and_make_exchange_isi_should_succeed() { register::asset_definition("btc2eth_rate", "exchange").into(), MintBox::new( 200_u32.to_value(), - IdBox::AssetId(asset_id_new("eth", "crypto", "buyer", "company")), - ) - .into(), - MintBox::new( - 20_u32.to_value(), - IdBox::AssetId(asset_id_new("btc", "crypto", "seller", "company")), + IdBox::AssetId(asset_id_new("eth", "crypto", buyer_account_id.clone())), ) .into(), MintBox::new( 20_u32.to_value(), - IdBox::AssetId(asset_id_new("btc2eth_rate", "exchange", "dex", "exchange")), + IdBox::AssetId(asset_id_new("btc", "crypto", seller_account_id.clone())), ) .into(), + MintBox::new(20_u32.to_value(), IdBox::AssetId(asset_id.clone())).into(), ]) .expect("Failed to prepare accounts."); @@ -313,36 +316,18 @@ fn find_rate_and_make_exchange_isi_should_succeed() { test_client .submit_all_blocking(vec![Pair::new( TransferBox::new( - IdBox::AssetId(asset_id_new("btc", "crypto", "seller", "company")), - EvaluatesTo::new_evaluates_to_value( - Expression::Query( - FindAssetQuantityById::new(asset_id_new( - "btc2eth_rate", - "exchange", - "dex", - "exchange", - )) - .into(), - ) - .into(), - ), - IdBox::AssetId(asset_id_new("btc", "crypto", "buyer", "company")), + IdBox::AssetId(asset_id_new("btc", "crypto", seller_account_id.clone())), + EvaluatesTo::new_evaluates_to_value(Expression::Query( + FindAssetQuantityById::new(asset_id.clone()).into(), + )), + IdBox::AccountId(buyer_account_id.clone()), ), TransferBox::new( - IdBox::AssetId(asset_id_new("eth", "crypto", "buyer", "company")), - EvaluatesTo::new_evaluates_to_value( - Expression::Query( - FindAssetQuantityById::new(asset_id_new( - "btc2eth_rate", - "exchange", - "dex", - "exchange", - )) - .into(), - ) - .into(), - ), - IdBox::AssetId(asset_id_new("eth", "crypto", "seller", "company")), + IdBox::AssetId(asset_id_new("eth", "crypto", buyer_account_id)), + EvaluatesTo::new_evaluates_to_value(Expression::Query( + FindAssetQuantityById::new(asset_id).into(), + )), + IdBox::AccountId(seller_account_id), ), ) .into()]) @@ -354,7 +339,9 @@ fn find_rate_and_make_exchange_isi_should_succeed() { let eth_quantity = test_client .request(FindAssetQuantityById::new(asset_id_new( - "eth", "crypto", "seller", "company", + "eth", + "crypto", + account_id_new("seller", "company"), ))) .expect("Failed to execute Iroha Query"); assert_eq!(expected_seller_eth, eth_quantity); @@ -362,40 +349,45 @@ fn find_rate_and_make_exchange_isi_should_succeed() { // For the btc amount we expect an error, as zero assets are purged from accounts test_client .request(FindAssetQuantityById::new(asset_id_new( - "btc", "crypto", "seller", "company", + "btc", + "crypto", + account_id_new("seller", "company"), ))) .expect_err("Query must fail"); let buyer_eth_quantity = test_client .request(FindAssetQuantityById::new(asset_id_new( - "eth", "crypto", "buyer", "company", + "eth", + "crypto", + account_id_new("buyer", "company"), ))) .expect("Failed to execute Iroha Query"); assert_eq!(expected_buyer_eth, buyer_eth_quantity); let buyer_btc_quantity = test_client .request(FindAssetQuantityById::new(asset_id_new( - "btc", "crypto", "buyer", "company", + "btc", + "crypto", + account_id_new("buyer", "company"), ))) .expect("Failed to execute Iroha Query"); assert_eq!(expected_buyer_btc, buyer_btc_quantity); } -fn asset_id_new( - definition_name: &str, - definition_domain: &str, - account_name: &str, - account_domain: &str, -) -> AssetId { +fn account_id_new(account_name: &str, account_domain: &str) -> AccountId { + AccountId::new( + account_name.parse().expect("Valid"), + account_domain.parse().expect("Valid"), + ) +} + +fn asset_id_new(definition_name: &str, definition_domain: &str, account_id: AccountId) -> AssetId { AssetId::new( AssetDefinitionId::new( definition_name.parse().expect("Valid"), definition_domain.parse().expect("Valid"), ), - AccountId::new( - account_name.parse().expect("Valid"), - account_domain.parse().expect("Valid"), - ), + account_id, ) } diff --git a/client/tests/integration/multisignature_transaction.rs b/client/tests/integration/multisignature_transaction.rs index 317a3bd2e82..f9c5eb45491 100644 --- a/client/tests/integration/multisignature_transaction.rs +++ b/client/tests/integration/multisignature_transaction.rs @@ -24,21 +24,15 @@ fn multisignature_transactions_should_wait_for_all_signatures() { let asset_definition_id = AssetDefinitionId::from_str("camomile#wonderland").expect("Valid"); let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone())); let set_signature_condition = MintBox::new( - SignatureCheckCondition::new(EvaluatesTo::new_unchecked( - ContainsAll::new( - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(TRANSACTION_SIGNATORIES_VALUE).expect("Can't fail."), - ) - .into(), - ), - val_vec![ - alice_key_pair.public_key().clone(), - key_pair_2.public_key().clone(), - ], - ) - .into(), - )), + SignatureCheckCondition::new(EvaluatesTo::new_unchecked(ContainsAll::new( + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(TRANSACTION_SIGNATORIES_VALUE).expect("Can't fail."), + )), + val_vec![ + alice_key_pair.public_key().clone(), + key_pair_2.public_key().clone(), + ], + ))), IdBox::AccountId(alice_id.clone()), ); diff --git a/client/tests/integration/permissions.rs b/client/tests/integration/permissions.rs index b9c504c71c0..ef7481c0140 100644 --- a/client/tests/integration/permissions.rs +++ b/client/tests/integration/permissions.rs @@ -80,9 +80,9 @@ fn permissions_disallow_asset_transfer() { //When let transfer_asset = TransferBox::new( - IdBox::AssetId(AssetId::new(asset_definition_id.clone(), bob_id)), + IdBox::AssetId(AssetId::new(asset_definition_id, bob_id)), quantity.to_value(), - IdBox::AssetId(AssetId::new(asset_definition_id, alice_id.clone())), + IdBox::AccountId(alice_id.clone()), ); let err = iroha_client .submit_blocking(transfer_asset) diff --git a/client/tests/integration/transfer_asset.rs b/client/tests/integration/transfer_asset.rs index c1a49945469..7ff62f0b070 100644 --- a/client/tests/integration/transfer_asset.rs +++ b/client/tests/integration/transfer_asset.rs @@ -82,7 +82,7 @@ fn simulate_transfer< let transfer_asset = TransferBox::new( IdBox::AssetId(AssetId::new(asset_definition_id.clone(), alice_id)), amount_to_transfer.clone().to_value(), - IdBox::AssetId(AssetId::new(asset_definition_id.clone(), mouse_id.clone())), + IdBox::AccountId(mouse_id.clone()), ); iroha_client .submit_till( diff --git a/client/tests/integration/upgrade.rs b/client/tests/integration/upgrade.rs index ec124b77a0b..7a44ff4c5ce 100644 --- a/client/tests/integration/upgrade.rs +++ b/client/tests/integration/upgrade.rs @@ -12,7 +12,6 @@ fn validator_upgrade_should_work() -> Result<()> { wait_for_genesis_committed(&vec![client.clone()], 0); // Register `admin` domain and account - let admin_domain = Domain::new("admin".parse()?); let register_admin_domain = RegisterBox::new(admin_domain); client.submit_blocking(register_admin_domain)?; @@ -25,7 +24,7 @@ fn validator_upgrade_should_work() -> Result<()> { // Check that admin isn't allowed to transfer alice's rose by default let alice_rose: ::Id = "rose##alice@wonderland".parse()?; - let admin_rose: ::Id = "rose#wonderland#admin@admin".parse()?; + let admin_rose: ::Id = "admin@admin".parse()?; let transfer_alice_rose = TransferBox::new(alice_rose, NumericValue::U32(1), admin_rose); let transfer_rose_tx = TransactionBuilder::new( admin_id.clone(), @@ -38,7 +37,6 @@ fn validator_upgrade_should_work() -> Result<()> { .expect_err("Should fail"); // Upgrade Validator - info!("Building validator"); let temp_out_dir = tempfile::tempdir().wrap_err("Failed to create temporary output directory")?; @@ -60,7 +58,6 @@ fn validator_upgrade_should_work() -> Result<()> { client.submit_blocking(upgrade_validator)?; // Check that admin can transfer alice's rose now - // Creating new transaction instead of cloning, because we need to update it's creation time let transfer_rose_tx = TransactionBuilder::new(admin_id, vec![transfer_alice_rose.into()], 100_000) diff --git a/client_cli/src/main.rs b/client_cli/src/main.rs index db315ebea33..586fca0c0d1 100644 --- a/client_cli/src/main.rs +++ b/client_cli/src/main.rs @@ -462,9 +462,8 @@ mod account { condition: Signature(condition), metadata: Metadata(metadata), } = self; - let mint_box = - MintBox::new(account, EvaluatesTo::new_unchecked(condition.into())).into(); - submit([mint_box], cfg, metadata).wrap_err("Failed to set signature condition") + let mint_box = MintBox::new(account, EvaluatesTo::new_unchecked(condition)); + submit([mint_box.into()], cfg, metadata).wrap_err("Failed to set signature condition") } } @@ -685,9 +684,9 @@ mod asset { metadata: Metadata(metadata), } = self; let transfer_asset = TransferBox::new( - IdBox::AssetId(AssetId::new(asset_id.clone(), from)), + IdBox::AssetId(AssetId::new(asset_id, from)), quantity.to_value(), - IdBox::AssetId(AssetId::new(asset_id, to)), + IdBox::AccountId(to), ) .into(); submit([transfer_asset], cfg, metadata).wrap_err("Failed to transfer asset") diff --git a/configs/peer/validator.wasm b/configs/peer/validator.wasm index d806ab45480..f6ff2b137f8 100644 Binary files a/configs/peer/validator.wasm and b/configs/peer/validator.wasm differ diff --git a/core/benches/kura.rs b/core/benches/kura.rs index 97402e59592..eb431f627c7 100644 --- a/core/benches/kura.rs +++ b/core/benches/kura.rs @@ -14,13 +14,12 @@ async fn measure_block_size_for_n_validators(n_validators: u32) { let alice_id = AccountId::from_str("alice@test").expect("tested"); let bob_id = AccountId::from_str("bob@test").expect("tested"); let xor_id = AssetDefinitionId::from_str("xor#test").expect("tested"); - let alice_xor_id = ::Id::new(xor_id.clone(), alice_id); - let bob_xor_id = ::Id::new(xor_id, bob_id); - let transfer = TransferBox { - source_id: IdBox::AssetId(alice_xor_id).into(), - object: 10_u32.to_value().into(), - destination_id: IdBox::AssetId(bob_xor_id).into(), - } + let alice_xor_id = ::Id::new(xor_id, alice_id); + let transfer = TransferBox::new( + IdBox::AssetId(alice_xor_id), + 10_u32.to_value(), + IdBox::AccountId(bob_id), + ) .into(); let keypair = KeyPair::generate().expect("Failed to generate KeyPair."); let tx = TransactionBuilder::new( diff --git a/core/src/queue.rs b/core/src/queue.rs index 9fb045fb3a0..1ac8cb7028a 100644 --- a/core/src/queue.rs +++ b/core/src/queue.rs @@ -498,20 +498,14 @@ mod tests { .build(alice_id); alice.signature_check_condition = SignatureCheckCondition( ContainsAll::new( - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(TRANSACTION_SIGNATORIES_VALUE) - .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(ACCOUNT_SIGNATORIES_VALUE) - .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(TRANSACTION_SIGNATORIES_VALUE) + .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), + )), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(ACCOUNT_SIGNATORIES_VALUE) + .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), + )), ) .into(), ); @@ -584,20 +578,14 @@ mod tests { .build(alice_id); alice.signature_check_condition = SignatureCheckCondition( ContainsAll::new( - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(TRANSACTION_SIGNATORIES_VALUE) - .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(ACCOUNT_SIGNATORIES_VALUE) - .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(TRANSACTION_SIGNATORIES_VALUE) + .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), + )), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(ACCOUNT_SIGNATORIES_VALUE) + .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), + )), ) .into(), ); @@ -664,7 +652,7 @@ mod tests { Account::new(account_id.clone(), [key_pair.public_key().clone()]).build(account_id); // Cause `check_siganture_condition` failure by trying to convert `u32` to `bool` account.signature_check_condition = - SignatureCheckCondition(EvaluatesTo::new_unchecked(0u32.into())); + SignatureCheckCondition(EvaluatesTo::new_unchecked(0u32)); assert!(domain.add_account(account).is_none()); let kura = Kura::blank_kura_for_testing(); @@ -707,20 +695,14 @@ mod tests { .build(account_id); account.signature_check_condition = SignatureCheckCondition( ContainsAll::new( - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(TRANSACTION_SIGNATORIES_VALUE) - .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(ACCOUNT_SIGNATORIES_VALUE) - .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(TRANSACTION_SIGNATORIES_VALUE) + .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), + )), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(ACCOUNT_SIGNATORIES_VALUE) + .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), + )), ) .into(), ); diff --git a/core/src/smartcontracts/isi/asset.rs b/core/src/smartcontracts/isi/asset.rs index 0d752c76355..14fe281258f 100644 --- a/core/src/smartcontracts/isi/asset.rs +++ b/core/src/smartcontracts/isi/asset.rs @@ -168,7 +168,7 @@ pub mod isi { ($ty:ty, $metrics:literal) => { impl InnerTransfer for $ty {} - impl Execute for Transfer { + impl Execute for Transfer { type Error = Error; #[metrics(+$metrics)] @@ -304,7 +304,7 @@ pub mod isi { /// Trait for blanket transfer implementation. trait InnerTransfer { fn execute( - transfer: Transfer, + transfer: Transfer, _authority: ::Id, wsv: &WorldStateView, ) -> Result<(), Err> @@ -315,18 +315,17 @@ pub mod isi { Value: From, Err: From, { - assert_matching_definitions( - &transfer.source_id, - &transfer.destination_id, - wsv, - ::EXPECTED_VALUE_TYPE, - )?; + let source_id = &transfer.source_id; + let destination_id = AssetId::new( + source_id.definition_id.clone(), + transfer.destination_id.clone(), + ); wsv.asset_or_insert( - &transfer.destination_id, + &destination_id, ::DEFAULT_ASSET_VALUE, )?; - wsv.modify_asset(&transfer.source_id, |asset| { + wsv.modify_asset(source_id, |asset| { let quantity: &mut Self = asset .try_as_mut() .map_err(eyre::Error::from) @@ -336,11 +335,12 @@ pub mod isi { .ok_or(MathError::NotEnoughQuantity)?; Ok(AssetEvent::Removed(AssetChanged { - asset_id: transfer.source_id.clone(), + asset_id: source_id.clone(), amount: transfer.object.into(), })) })?; - wsv.modify_asset(&transfer.destination_id, |asset| { + let destination_id_clone = destination_id.clone(); + wsv.modify_asset(&destination_id, |asset| { let quantity: &mut Self = asset .try_as_mut() .map_err(eyre::Error::from) @@ -355,7 +355,7 @@ pub mod isi { .set(wsv.metric_tx_amounts_counter.get() + 1); Ok(AssetEvent::Added(AssetChanged { - asset_id: transfer.destination_id.clone(), + asset_id: destination_id_clone, amount: transfer.object.into(), })) })?; @@ -420,26 +420,6 @@ pub mod isi { }), } } - - /// Assert that the two assets have the same asset `definition_id`. - fn assert_matching_definitions( - source: &::Id, - destination: &::Id, - wsv: &WorldStateView, - value_type: AssetValueType, - ) -> Result<(), Error> { - if destination.definition_id != source.definition_id { - let expected = wsv - .asset_definition(&destination.definition_id)? - .id() - .clone(); - let actual = wsv.asset_definition(&source.definition_id)?.id().clone(); - return Err(TypeError::from(Mismatch { expected, actual }).into()); - } - assert_asset_type(&source.definition_id, wsv, value_type)?; - assert_asset_type(&destination.definition_id, wsv, value_type)?; - Ok(()) - } } /// Asset-related query implementations. diff --git a/core/src/smartcontracts/isi/mod.rs b/core/src/smartcontracts/isi/mod.rs index c9b2e343c15..c6d2ea44a16 100644 --- a/core/src/smartcontracts/isi/mod.rs +++ b/core/src/smartcontracts/isi/mod.rs @@ -226,7 +226,7 @@ impl Execute for TransferBox { wsv: &WorldStateView, ) -> Result<(), Self::Error> { let context = Context::new(wsv); - let (IdBox::AssetId(source_asset_id), IdBox::AssetId(destination_asset_id)) = ( + let (IdBox::AssetId(source_asset_id), IdBox::AccountId(destination_account_id)) = ( self.source_id.evaluate(&context)?, self.destination_id.evaluate(&context)?, ) else { @@ -235,20 +235,20 @@ impl Execute for TransferBox { let value = self.object.evaluate(&context)?; Span::current().record("from", source_asset_id.to_string()); - Span::current().record("to", destination_asset_id.to_string()); + Span::current().record("to", destination_account_id.to_string()); iroha_logger::trace!(%value, %authority); match value { Value::Numeric(NumericValue::U32(quantity)) => { - Transfer::new(source_asset_id, quantity, destination_asset_id) + Transfer::new(source_asset_id, quantity, destination_account_id) .execute(authority, wsv) } Value::Numeric(NumericValue::U128(quantity)) => { - Transfer::new(source_asset_id, quantity, destination_asset_id) + Transfer::new(source_asset_id, quantity, destination_account_id) .execute(authority, wsv) } Value::Numeric(NumericValue::Fixed(quantity)) => { - Transfer::new(source_asset_id, quantity, destination_asset_id) + Transfer::new(source_asset_id, quantity, destination_account_id) .execute(authority, wsv) } _ => Err(Error::Evaluate(InstructionType::Transfer.into())), diff --git a/core/src/tx.rs b/core/src/tx.rs index 96e9d86b375..93886fc729c 100644 --- a/core/src/tx.rs +++ b/core/src/tx.rs @@ -278,7 +278,8 @@ fn check_signature_condition( .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), signatories.into_iter().collect::>(), ); - EvaluatesTo::new_unchecked(where_expr.into()) + + EvaluatesTo::new_unchecked(where_expr) } impl IsInBlockchain for VersionedSignedTransaction { diff --git a/data_model/src/account.rs b/data_model/src/account.rs index 789f0845351..86081060738 100644 --- a/data_model/src/account.rs +++ b/data_model/src/account.rs @@ -308,20 +308,14 @@ impl Default for SignatureCheckCondition { fn default() -> Self { Self( ContainsAny::new( - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(TRANSACTION_SIGNATORIES_VALUE) - .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), - EvaluatesTo::new_unchecked( - ContextValue::new( - Name::from_str(ACCOUNT_SIGNATORIES_VALUE) - .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), - ) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(TRANSACTION_SIGNATORIES_VALUE) + .expect("TRANSACTION_SIGNATORIES_VALUE should be valid."), + )), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str(ACCOUNT_SIGNATORIES_VALUE) + .expect("ACCOUNT_SIGNATORIES_VALUE should be valid."), + )), ) .into(), ) diff --git a/data_model/src/evaluate.rs b/data_model/src/evaluate.rs index cb8ba099d3a..c3ac350d694 100644 --- a/data_model/src/evaluate.rs +++ b/data_model/src/evaluate.rs @@ -540,87 +540,74 @@ mod tests { let condition = If::new( And::new( Greater::new( - EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("usd_quantity").expect("Can't fail.")) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("usd_quantity").expect("Can't fail."), + )), 500_u32, ), Less::new( - EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("usd_quantity").expect("Can't fail.")) - .into(), - ), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("usd_quantity").expect("Can't fail."), + )), 1000_u32, ), ), - EvaluatesTo::new_evaluates_to_value( - Or::new( - ContainsAll::new( - EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("signatories").expect("Can't fail.")) - .into(), - ), - teller_signatory_set.clone(), - ), - Contains::new( - EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("signatories").expect("Can't fail.")) - .into(), - ), - manager_signatory, - ), - ) - .into(), - ), + EvaluatesTo::new_evaluates_to_value(Or::new( + ContainsAll::new( + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("signatories").expect("Can't fail."), + )), + teller_signatory_set.clone(), + ), + Contains::new( + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("signatories").expect("Can't fail."), + )), + manager_signatory, + ), + )), true, ); // Signed by all tellers - let expression = Where::new(EvaluatesTo::new_evaluates_to_value( - condition.clone().into(), - )) - .with_value( - //TODO: use query to get the actual quantity of an asset from WSV - // in that case this test should be moved to iroha_core - Name::from_str("usd_quantity").expect("Can't fail."), - asset_quantity_high.clone(), - ) - .with_value( - Name::from_str("signatories").expect("Can't fail."), - teller_signatory_set, - ); + let expression = Where::new(EvaluatesTo::new_evaluates_to_value(condition.clone())) + .with_value( + //TODO: use query to get the actual quantity of an asset from WSV + // in that case this test should be moved to iroha_core + Name::from_str("usd_quantity").expect("Can't fail."), + asset_quantity_high.clone(), + ) + .with_value( + Name::from_str("signatories").expect("Can't fail."), + teller_signatory_set, + ); assert_eq!(expression.evaluate(&TestContext::new())?, Value::Bool(true)); // Signed by manager - let expression = Where::new(EvaluatesTo::new_evaluates_to_value( - condition.clone().into(), - )) - .with_value( - Name::from_str("usd_quantity").expect("Can't fail."), - asset_quantity_high.clone(), - ) - .with_value( - Name::from_str("signatories").expect("Can't fail."), - manager_signatory_set, - ); + let expression = Where::new(EvaluatesTo::new_evaluates_to_value(condition.clone())) + .with_value( + Name::from_str("usd_quantity").expect("Can't fail."), + asset_quantity_high.clone(), + ) + .with_value( + Name::from_str("signatories").expect("Can't fail."), + manager_signatory_set, + ); assert_eq!(expression.evaluate(&TestContext::new())?, Value::Bool(true)); // Signed by one teller - let expression = Where::new(EvaluatesTo::new_evaluates_to_value( - condition.clone().into(), - )) - .with_value( - Name::from_str("usd_quantity").expect("Can't fail."), - asset_quantity_high, - ) - .with_value( - Name::from_str("signatories").expect("Can't fail."), - one_teller_set.clone(), - ); + let expression = Where::new(EvaluatesTo::new_evaluates_to_value(condition.clone())) + .with_value( + Name::from_str("usd_quantity").expect("Can't fail."), + asset_quantity_high, + ) + .with_value( + Name::from_str("signatories").expect("Can't fail."), + one_teller_set.clone(), + ); assert_eq!( expression.evaluate(&TestContext::new())?, Value::Bool(false) ); // Signed by one teller with less value - let expression = Where::new(EvaluatesTo::new_evaluates_to_value(condition.into())) + let expression = Where::new(EvaluatesTo::new_evaluates_to_value(condition)) .with_value( Name::from_str("usd_quantity").expect("Can't fail."), asset_quantity_low, @@ -636,12 +623,12 @@ mod tests { #[test] fn where_expression() -> Result<(), Error> { assert_eq!( - Where::new(EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("test_value").expect("Can't fail.")).into() - )) + Where::new(EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("test_value").expect("Can't fail.") + ))) .with_value( Name::from_str("test_value").expect("Can't fail."), - EvaluatesTo::new_evaluates_to_value(Add::new(2_u32, 3_u32).into()) + EvaluatesTo::new_evaluates_to_value(Add::new(2_u32, 3_u32)) ) .evaluate(&TestContext::new())?, 5_u32.to_value() @@ -651,21 +638,17 @@ mod tests { #[test] fn nested_where_expression() -> Result<(), Error> { - let expression = Where::new(EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("a").expect("Can't fail.")).into(), - )) + let expression = Where::new(EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("a").expect("Can't fail."), + ))) .with_value(Name::from_str("a").expect("Can't fail."), 2_u32); - let outer_expression: ExpressionBox = Where::new(EvaluatesTo::new_evaluates_to_value( - Add::new( - EvaluatesTo::new_unchecked(expression.into()), - EvaluatesTo::new_unchecked( - ContextValue::new(Name::from_str("b").expect("Can't fail.")).into(), - ), - ) - .into(), - )) - .with_value(Name::from_str("b").expect("Can't fail."), 4_u32) - .into(); + let outer_expression = Where::new(EvaluatesTo::new_evaluates_to_value(Add::new( + EvaluatesTo::new_unchecked(expression), + EvaluatesTo::new_unchecked(ContextValue::new( + Name::from_str("b").expect("Can't fail."), + )), + ))) + .with_value(Name::from_str("b").expect("Can't fail."), 4_u32); assert_eq!( outer_expression.evaluate(&TestContext::new())?, 6_u32.to_value() @@ -700,34 +683,34 @@ mod tests { assert_eval( &And::new( - EvaluatesTo::new_unchecked(1_u32.into()), - EvaluatesTo::new_unchecked(Vec::::new().into()), + EvaluatesTo::new_unchecked(1_u32), + EvaluatesTo::new_unchecked(Vec::::new()), ), "Should not be possible to apply logical and to int and vec.", ); assert_eval( &Or::new( - EvaluatesTo::new_unchecked(1_u32.into()), - EvaluatesTo::new_unchecked(Vec::::new().into()), + EvaluatesTo::new_unchecked(1_u32), + EvaluatesTo::new_unchecked(Vec::::new()), ), "Should not be possible to apply logical or to int and vec.", ); assert_eval( &Greater::new( - EvaluatesTo::new_unchecked(1_u32.into()), - EvaluatesTo::new_unchecked(Vec::::new().into()), + EvaluatesTo::new_unchecked(1_u32), + EvaluatesTo::new_unchecked(Vec::::new()), ), "Should not be possible to apply greater sign to int and vec.", ); assert_eval( &Less::new( - EvaluatesTo::new_unchecked(1_u32.into()), - EvaluatesTo::new_unchecked(Vec::::new().into()), + EvaluatesTo::new_unchecked(1_u32), + EvaluatesTo::new_unchecked(Vec::::new()), ), "Should not be possible to apply greater sign to int and vec.", ); assert_eval( - &If::new(EvaluatesTo::new_unchecked(1_u32.into()), 2_u32, 3_u32), + &If::new(EvaluatesTo::new_unchecked(1_u32), 2_u32, 3_u32), "If condition should be bool", ); assert_eval( diff --git a/data_model/src/expression.rs b/data_model/src/expression.rs index 9bd41133c72..eeb8e19a3c5 100644 --- a/data_model/src/expression.rs +++ b/data_model/src/expression.rs @@ -101,7 +101,7 @@ use crate::NumericValue; /// /// impl From for EvaluatesTo { /// fn from(expression: Add) -> Self { -/// EvaluatesTo::new_unchecked(expression.into()) +/// EvaluatesTo::new_unchecked(expression) /// } /// } /// ``` @@ -162,7 +162,7 @@ macro_rules! gen_expr_and_impls { (impl_extra_convert $i:ident $result_type:ty) => { impl From<$i> for EvaluatesTo<$result_type> { fn from(expression: $i) -> Self { - EvaluatesTo::new_unchecked(expression.into()) + EvaluatesTo::new_unchecked(expression) } } }; @@ -499,7 +499,7 @@ pub mod model { impl, E: Into + Into> From for EvaluatesTo { fn from(expression: E) -> Self { - Self::new_unchecked(expression.into()) + Self::new_unchecked(expression) } } @@ -523,9 +523,9 @@ impl> EvaluatesTo { /// Prefer using [`Into`] conversions rather than this method, /// because it does not check the value type at compile-time. #[inline] - pub fn new_unchecked(expression: ExpressionBox) -> Self { + pub fn new_unchecked(expression: impl Into) -> Self { Self { - expression, + expression: expression.into(), _value_type: PhantomData::default(), } } @@ -567,7 +567,7 @@ impl EvaluatesTo { /// Construct `EvaluatesTo` from any `expression` /// because all of them evaluate to [`Value`]. #[inline] - pub fn new_evaluates_to_value(expression: ExpressionBox) -> Self { + pub fn new_evaluates_to_value(expression: impl Into) -> Self { Self::new_unchecked(expression) } } diff --git a/data_model/tests/data_model.rs b/data_model/tests/data_model.rs index 7ddac040169..6c4c3bb6ef6 100644 --- a/data_model/tests/data_model.rs +++ b/data_model/tests/data_model.rs @@ -4,77 +4,22 @@ use std::str::FromStr as _; use iroha_data_model::{prelude::*, ParseError}; -fn asset_id_new( - definition_name: &str, - definition_domain: &str, - account_name: &str, - account_domain: &str, -) -> AssetId { - AssetId::new( - AssetDefinitionId::new( - definition_name.parse().expect("Valid"), - definition_domain.parse().expect("Valid"), - ), - AccountId::new( - account_name.parse().expect("Valid"), - account_domain.parse().expect("Valid"), - ), - ) -} - #[test] -fn find_rate_and_make_exchange_isi_should_be_valid() { - let _instruction = Pair::new( - TransferBox::new( - IdBox::AssetId(asset_id_new("btc", "crypto", "seller", "company")), - EvaluatesTo::new_evaluates_to_value( - Expression::Query( - FindAssetQuantityById::new(asset_id_new( - "btc2eth_rate", - "exchange", - "dex", - "exchange", - )) - .into(), - ) - .into(), - ), - IdBox::AssetId(asset_id_new("btc", "crypto", "buyer", "company")), - ), - TransferBox::new( - IdBox::AssetId(asset_id_new("btc", "crypto", "buyer", "company")), - EvaluatesTo::new_evaluates_to_value( - Expression::Query( - FindAssetQuantityById::new(asset_id_new( - "btc2eth_rate", - "exchange", - "dex", - "exchange", - )) - .into(), - ) - .into(), - ), - IdBox::AssetId(asset_id_new("btc", "crypto", "seller", "company")), - ), +fn transfer_isi_should_be_valid() { + let _instruction = TransferBox::new( + IdBox::AssetId("btc##seller@crypto".parse().expect("Valid")), + 12_u32, + IdBox::AccountId("buyer@crypto".parse().expect("Valid")), ); } #[test] -fn find_rate_and_check_it_greater_than_value_isi_should_be_valid() { +fn find_quantity_and_check_it_greater_than_value_isi_should_be_valid() { + let asset_id: AssetId = "rose##alice@wonderland".parse().expect("Valid"); + let find_asset = QueryBox::from(FindAssetQuantityById::new(asset_id)); + let _instruction = Conditional::new( - Not::new(Greater::new( - EvaluatesTo::new_unchecked( - QueryBox::from(FindAssetQuantityById::new(asset_id_new( - "btc2eth_rate", - "exchange", - "dex", - "exchange", - ))) - .into(), - ), - 10_u32, - )), + Not::new(Greater::new(EvaluatesTo::new_unchecked(find_asset), 10_u32)), FailBox::new("rate is less or equal to value"), ); } @@ -97,15 +42,14 @@ impl FindRateAndCheckItGreaterThanValue { pub fn into_isi(self) -> Conditional { Conditional::new( Not::new(Greater::new( - EvaluatesTo::new_unchecked( - QueryBox::from(FindAssetQuantityById::new(AssetId::new( + EvaluatesTo::new_unchecked(QueryBox::from(FindAssetQuantityById::new( + AssetId::new( format!("{}2{}_rate#exchange", self.from_currency, self.to_currency) .parse() .expect("Valid"), AccountId::from_str("dex@exchange").expect("Valid"), - ))) - .into(), - ), + ), + ))), self.value, )), FailBox::new("rate is less or equal to value"), diff --git a/default_validator/src/isi/asset.rs b/default_validator/src/isi/asset.rs index b36c7ce9439..87ed47d5684 100644 --- a/default_validator/src/isi/asset.rs +++ b/default_validator/src/isi/asset.rs @@ -220,7 +220,7 @@ impl DefaultValidate for Mint { } } -impl DefaultValidate for Transfer { +impl DefaultValidate for Transfer { fn default_validate( &self, authority: &::Id, diff --git a/default_validator/src/isi/mod.rs b/default_validator/src/isi/mod.rs index 4dddfb31fd4..9c44dcffe9d 100644 --- a/default_validator/src/isi/mod.rs +++ b/default_validator/src/isi/mod.rs @@ -257,7 +257,7 @@ impl DefaultValidate for TransferBox { { let object = evaluate_field!(authority, validate_query, ::object); - let (IdBox::AssetId(source_asset_id), IdBox::AssetId(destination_asset_id)) = ( + let (IdBox::AssetId(source_asset_id), IdBox::AccountId(destination_account_id)) = ( evaluate_field!(authority, validate_query, ::source_id), evaluate_field!(authority, validate_query, ::destination_id), ) else { @@ -268,7 +268,7 @@ impl DefaultValidate for TransferBox { (Value::Numeric(NumericValue::U32(quantity))) | (Value::Numeric(NumericValue::U128(quantity))) | (Value::Numeric(NumericValue::Fixed(quantity))) => { - Transfer::new(source_asset_id, quantity, destination_asset_id).default_validate(authority, validate_query) + Transfer::new(source_asset_id, quantity, destination_account_id).default_validate(authority, validate_query) } _ => deny_unsupported_instruction!(Transfer) })