From 6dd0442b3e3f8c484b795394acd4ee77cccaa211 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Fri, 1 Sep 2023 14:13:17 +0200 Subject: [PATCH] Bring back output amount 0 check --- Cargo.lock | 12 +++++------ sdk/src/types/block/output/account.rs | 14 ++++++++----- sdk/src/types/block/output/basic.rs | 13 +++++++----- sdk/src/types/block/output/delegation.rs | 11 +++++----- sdk/src/types/block/output/foundry.rs | 14 ++++++++----- sdk/src/types/block/output/mod.rs | 21 +++++++++++++++---- sdk/src/types/block/output/nft.rs | 14 ++++++++----- .../storage_deposit_return.rs | 8 +++---- 8 files changed, 68 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 370849b480..4e5fe94802 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -430,9 +430,9 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.28" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95ed24df0632f708f5f6d8082675bef2596f7084dee3dd55f632290bf35bfe0f" +checksum = "f56b4c72906975ca04becb8a30e102dfecddd0c06181e3e95ddc444be28881f8" dependencies = [ "num-traits", ] @@ -1958,9 +1958,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.2" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5486aed0026218e61b8a01d5fbd5a0a134649abb71a0e53b7bc088529dced86e" +checksum = "f478948fd84d9f8e86967bf432640e46adfb5a4bd4f14ef7e864ab38220534ae" [[package]] name = "memoffset" @@ -3659,9 +3659,9 @@ dependencies = [ [[package]] name = "webpki" -version = "0.22.1" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0e74f82d49d545ad128049b7e88f6576df2da6b02e9ce565c6f533be576957e" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" dependencies = [ "ring", "untrusted", diff --git a/sdk/src/types/block/output/account.rs b/sdk/src/types/block/output/account.rs index acc1b5deee..fb0abff849 100644 --- a/sdk/src/types/block/output/account.rs +++ b/sdk/src/types/block/output/account.rs @@ -13,7 +13,6 @@ use packable::{ Packable, }; -use super::verify_output_amount_packable; use crate::types::{ block::{ address::{AccountAddress, Address}, @@ -22,8 +21,9 @@ use crate::types::{ unlock_condition::{ verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions, }, - verify_output_amount, AccountId, ChainId, NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, - Rent, RentStructure, StateTransitionError, StateTransitionVerifier, + verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, AccountId, ChainId, + NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, + StateTransitionError, StateTransitionVerifier, }, protocol::ProtocolParameters, semantic::{TransactionFailureReason, ValidationContext}, @@ -299,6 +299,8 @@ impl AccountOutputBuilder { } }; + verify_output_amount_min(output.amount)?; + Ok(output) } @@ -310,7 +312,7 @@ impl AccountOutputBuilder { let output = self.finish()?; if let Some(token_supply) = params.into().token_supply() { - verify_output_amount(&output.amount, &token_supply)?; + verify_output_amount_supply(output.amount, token_supply)?; } Ok(output) @@ -918,8 +920,9 @@ mod tests { let sender_2 = rand_sender_feature(); let issuer_1 = rand_issuer_feature(); let issuer_2 = rand_issuer_feature(); + let amount = 500_000; - let mut builder = AccountOutput::build_with_amount(0, account_id) + let mut builder = AccountOutput::build_with_amount(amount, account_id) .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(gov_address_1) .add_unlock_condition(state_address_1) @@ -929,6 +932,7 @@ mod tests { .add_immutable_feature(issuer_2); let output = builder.clone().finish().unwrap(); + assert_eq!(output.amount(), amount); assert_eq!(output.unlock_conditions().governor_address(), Some(&gov_address_1)); assert_eq!( output.unlock_conditions().state_controller_address(), diff --git a/sdk/src/types/block/output/basic.rs b/sdk/src/types/block/output/basic.rs index b25f3e2c4f..6fc5577f52 100644 --- a/sdk/src/types/block/output/basic.rs +++ b/sdk/src/types/block/output/basic.rs @@ -5,7 +5,6 @@ use alloc::collections::BTreeSet; use packable::Packable; -use super::verify_output_amount_packable; use crate::types::{ block::{ address::Address, @@ -14,8 +13,8 @@ use crate::types::{ unlock_condition::{ verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions, }, - verify_output_amount, NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, - RentStructure, + verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, NativeToken, + NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, }, protocol::ProtocolParameters, semantic::{TransactionFailureReason, ValidationContext}, @@ -177,6 +176,8 @@ impl BasicOutputBuilder { } }; + verify_output_amount_min(output.amount)?; + Ok(output) } @@ -185,7 +186,7 @@ impl BasicOutputBuilder { let output = self.finish()?; if let Some(token_supply) = params.into().token_supply() { - verify_output_amount(&output.amount, &token_supply)?; + verify_output_amount_supply(output.amount, token_supply)?; } Ok(output) @@ -481,14 +482,16 @@ mod tests { let address_2 = rand_address_unlock_condition(); let sender_1 = rand_sender_feature(); let sender_2 = rand_sender_feature(); + let amount = 500_000; - let mut builder = BasicOutput::build_with_amount(0) + let mut builder = BasicOutput::build_with_amount(amount) .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address_1) .add_feature(sender_1) .replace_feature(sender_2); let output = builder.clone().finish().unwrap(); + assert_eq!(output.amount(), amount); assert_eq!(output.unlock_conditions().address(), Some(&address_1)); assert_eq!(output.features().sender(), Some(&sender_2)); diff --git a/sdk/src/types/block/output/delegation.rs b/sdk/src/types/block/output/delegation.rs index 769f5998a6..a5c0fe5bc1 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -20,7 +20,8 @@ use crate::types::{ unlock_condition::{ verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions, }, - verify_output_amount, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, + verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, Output, + OutputBuilderAmount, OutputId, Rent, RentStructure, }, protocol::ProtocolParameters, semantic::{TransactionFailureReason, ValidationContext}, @@ -196,6 +197,8 @@ impl DelegationOutputBuilder { } }; + verify_output_amount_min(output.amount)?; + Ok(output) } @@ -207,7 +210,7 @@ impl DelegationOutputBuilder { let output = self.finish()?; if let Some(token_supply) = params.into().token_supply() { - verify_output_amount(&output.amount, &token_supply)?; + verify_output_amount_supply(output.amount, token_supply)?; } Ok(output) @@ -376,9 +379,7 @@ impl Packable for DelegationOutput { ) -> Result> { let amount = u64::unpack::<_, VERIFY>(unpacker, &()).coerce()?; - if VERIFY { - verify_output_amount(&amount, &visitor.token_supply()).map_err(UnpackError::Packable)?; - } + verify_output_amount_packable::(&amount, visitor).map_err(UnpackError::Packable)?; let delegated_amount = u64::unpack::<_, VERIFY>(unpacker, &()).coerce()?; let delegation_id = DelegationId::unpack::<_, VERIFY>(unpacker, &()).coerce()?; diff --git a/sdk/src/types/block/output/foundry.rs b/sdk/src/types/block/output/foundry.rs index 17a28668f1..7a3249a711 100644 --- a/sdk/src/types/block/output/foundry.rs +++ b/sdk/src/types/block/output/foundry.rs @@ -12,7 +12,6 @@ use packable::{ }; use primitive_types::U256; -use super::verify_output_amount_packable; use crate::types::{ block::{ address::{AccountAddress, Address}, @@ -21,8 +20,9 @@ use crate::types::{ unlock_condition::{ verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions, }, - verify_output_amount, ChainId, FoundryId, NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, - Rent, RentStructure, StateTransitionError, StateTransitionVerifier, TokenId, TokenScheme, + verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, FoundryId, + NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, + StateTransitionError, StateTransitionVerifier, TokenId, TokenScheme, }, protocol::ProtocolParameters, semantic::{TransactionFailureReason, ValidationContext}, @@ -238,6 +238,8 @@ impl FoundryOutputBuilder { } }; + verify_output_amount_min(output.amount)?; + Ok(output) } @@ -249,7 +251,7 @@ impl FoundryOutputBuilder { let output = self.finish()?; if let Some(token_supply) = params.into().token_supply() { - verify_output_amount(&output.amount, &token_supply)?; + verify_output_amount_supply(output.amount, token_supply)?; } Ok(output) @@ -766,8 +768,9 @@ mod tests { let account_2 = ImmutableAccountAddressUnlockCondition::new(rand_account_address()); let metadata_1 = rand_metadata_feature(); let metadata_2 = rand_metadata_feature(); + let amount = 500_000; - let mut builder = FoundryOutput::build_with_amount(0, 234, rand_token_scheme()) + let mut builder = FoundryOutput::build_with_amount(amount, 234, rand_token_scheme()) .with_serial_number(85) .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .with_unlock_conditions([account_1]) @@ -777,6 +780,7 @@ mod tests { .replace_immutable_feature(metadata_1.clone()); let output = builder.clone().finish().unwrap(); + assert_eq!(output.amount(), amount); assert_eq!(output.serial_number(), 85); assert_eq!(output.unlock_conditions().immutable_account_address(), Some(&account_1)); assert_eq!(output.features().metadata(), Some(&metadata_2)); diff --git a/sdk/src/types/block/output/mod.rs b/sdk/src/types/block/output/mod.rs index 0eade08ab7..eaa7f3dbfd 100644 --- a/sdk/src/types/block/output/mod.rs +++ b/sdk/src/types/block/output/mod.rs @@ -472,20 +472,33 @@ impl Rent for Output { } } -pub(crate) fn verify_output_amount(amount: &u64, token_supply: &u64) -> Result<(), Error> { - if *amount < Output::AMOUNT_MIN || amount > token_supply { - Err(Error::InvalidOutputAmount(*amount)) +pub(crate) fn verify_output_amount_min(amount: u64) -> Result<(), Error> { + if amount < Output::AMOUNT_MIN { + Err(Error::InvalidOutputAmount(amount)) } else { Ok(()) } } +pub(crate) fn verify_output_amount_supply(amount: u64, token_supply: u64) -> Result<(), Error> { + if amount > token_supply { + Err(Error::InvalidOutputAmount(amount)) + } else { + Ok(()) + } +} + +pub(crate) fn verify_output_amount(amount: u64, token_supply: u64) -> Result<(), Error> { + verify_output_amount_min(amount)?; + verify_output_amount_supply(amount, token_supply) +} + pub(crate) fn verify_output_amount_packable( amount: &u64, protocol_parameters: &ProtocolParameters, ) -> Result<(), Error> { if VERIFY { - verify_output_amount(amount, &protocol_parameters.token_supply())?; + verify_output_amount(*amount, protocol_parameters.token_supply())?; } Ok(()) } diff --git a/sdk/src/types/block/output/nft.rs b/sdk/src/types/block/output/nft.rs index 541a782fca..b8d5e6609a 100644 --- a/sdk/src/types/block/output/nft.rs +++ b/sdk/src/types/block/output/nft.rs @@ -10,7 +10,6 @@ use packable::{ Packable, }; -use super::verify_output_amount_packable; use crate::types::{ block::{ address::{Address, NftAddress}, @@ -19,8 +18,9 @@ use crate::types::{ unlock_condition::{ verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions, }, - verify_output_amount, ChainId, NativeToken, NativeTokens, NftId, Output, OutputBuilderAmount, OutputId, - Rent, RentStructure, StateTransitionError, StateTransitionVerifier, + verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken, + NativeTokens, NftId, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError, + StateTransitionVerifier, }, protocol::ProtocolParameters, semantic::{TransactionFailureReason, ValidationContext}, @@ -224,6 +224,8 @@ impl NftOutputBuilder { } }; + verify_output_amount_min(output.amount)?; + Ok(output) } @@ -232,7 +234,7 @@ impl NftOutputBuilder { let output = self.finish()?; if let Some(token_supply) = params.into().token_supply() { - verify_output_amount(&output.amount, &token_supply)?; + verify_output_amount_supply(output.amount, token_supply)?; } Ok(output) @@ -651,8 +653,9 @@ mod tests { let sender_2 = rand_sender_feature(); let issuer_1 = rand_issuer_feature(); let issuer_2 = rand_issuer_feature(); + let amount = 500_000; - let mut builder = NftOutput::build_with_amount(0, NftId::null()) + let mut builder = NftOutput::build_with_amount(amount, NftId::null()) .add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) .add_unlock_condition(address_1) .add_feature(sender_1) @@ -661,6 +664,7 @@ mod tests { .add_immutable_feature(issuer_2); let output = builder.clone().finish().unwrap(); + assert_eq!(output.amount(), amount); assert_eq!(output.unlock_conditions().address(), Some(&address_1)); assert_eq!(output.features().sender(), Some(&sender_2)); assert_eq!(output.immutable_features().issuer(), Some(&issuer_1)); diff --git a/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs b/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs index 3bc031635f..bec25f3eca 100644 --- a/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs +++ b/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs @@ -22,7 +22,7 @@ impl StorageDepositReturnUnlockCondition { /// Creates a new [`StorageDepositReturnUnlockCondition`]. #[inline(always)] pub fn new(return_address: impl Into
, amount: u64, token_supply: u64) -> Result { - verify_amount::(&amount, &token_supply)?; + verify_amount::(amount, token_supply)?; Ok(Self { return_address: return_address.into(), @@ -43,9 +43,9 @@ impl StorageDepositReturnUnlockCondition { } } -fn verify_amount(amount: &u64, token_supply: &u64) -> Result<(), Error> { +fn verify_amount(amount: u64, token_supply: u64) -> Result<(), Error> { if VERIFY { - verify_output_amount(amount, token_supply).map_err(|_| Error::InvalidStorageDepositAmount(*amount))?; + verify_output_amount(amount, token_supply).map_err(|_| Error::InvalidStorageDepositAmount(amount))?; } Ok(()) @@ -55,7 +55,7 @@ fn verify_amount_packable( amount: &u64, protocol_parameters: &ProtocolParameters, ) -> Result<(), Error> { - verify_amount::(amount, &protocol_parameters.token_supply()) + verify_amount::(*amount, protocol_parameters.token_supply()) } #[cfg(feature = "serde")]