Skip to content

Commit

Permalink
Remove NativeTokens::COUNT_MAX (iotaledger#1984)
Browse files Browse the repository at this point in the history
* Remove NativeTokens::COUNT_MAX

* Nit
  • Loading branch information
thibault-martinez authored Feb 13, 2024
1 parent 8a3933f commit 42a6fd3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
input::INPUT_COUNT_MAX,
output::{
unlock_condition::StorageDepositReturnUnlockCondition, AccountOutputBuilder, FoundryOutputBuilder,
MinimumOutputAmount, NativeTokens, NftOutputBuilder, Output, OutputId, StorageScoreParameters, TokenId,
MinimumOutputAmount, NftOutputBuilder, Output, OutputId, StorageScoreParameters, TokenId,
},
slot::SlotIndex,
},
Expand Down Expand Up @@ -159,16 +159,7 @@ impl AmountSelection {
}

if let Some(nt) = input.output.native_token() {
let mut selected_native_tokens = self.selected_native_tokens.clone();

selected_native_tokens.insert(*nt.token_id());
// Don't select input if the tx would end up with more than allowed native tokens.
if selected_native_tokens.len() > NativeTokens::COUNT_MAX.into() {
continue;
} else {
// Update selected with NTs from this output.
self.selected_native_tokens = selected_native_tokens;
}
self.selected_native_tokens.insert(*nt.token_id());
}

self.inputs_sum += input.output.amount();
Expand Down Expand Up @@ -341,19 +332,7 @@ impl InputSelection {
return Ok(r);
}

// If the available inputs have more NTs than are allowed in a single tx, we might not be able to find inputs
// without exceeding the threshold, so in this case we also try again with the outputs ordered the other way
// around.
let potentially_too_many_native_tokens = self
.available_inputs
.iter()
.filter_map(|i| i.output.native_token())
.count()
> NativeTokens::COUNT_MAX.into();

if self.selected_inputs.len() + amount_selection.newly_selected_inputs.len() > INPUT_COUNT_MAX.into()
|| potentially_too_many_native_tokens
{
if self.selected_inputs.len() + amount_selection.newly_selected_inputs.len() > INPUT_COUNT_MAX.into() {
// Clear before trying with reversed ordering.
log::debug!("Clearing amount selection");
amount_selection = AmountSelection::new(self)?;
Expand Down
5 changes: 1 addition & 4 deletions sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl From<NativeTokens> for NativeTokensBuilder {
}
}

pub(crate) type NativeTokenCount = BoundedU8<0, { NativeTokens::COUNT_MAX }>;
pub(crate) type NativeTokenCount = BoundedU8<0, 255>;

///
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deref, Packable)]
Expand Down Expand Up @@ -211,9 +211,6 @@ impl IntoIterator for NativeTokens {
}

impl NativeTokens {
/// Maximum number of different native tokens that can be referenced in one transaction.
pub const COUNT_MAX: u8 = 64;

/// Creates a new [`NativeTokens`] from a vec.
pub fn from_vec(native_tokens: Vec<NativeToken>) -> Result<Self, Error> {
let mut native_tokens =
Expand Down
12 changes: 1 addition & 11 deletions sdk/src/types/block/semantic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use self::{error::TransactionFailureReason, state_transition::StateTransitio
use crate::types::block::{
address::Address,
context_input::{BlockIssuanceCreditContextInput, CommitmentContextInput, RewardContextInput},
output::{AccountId, AnchorOutput, ChainId, FoundryId, NativeTokens, Output, OutputId, TokenId},
output::{AccountId, AnchorOutput, ChainId, FoundryId, Output, OutputId, TokenId},
payload::signed_transaction::{Transaction, TransactionCapabilityFlag, TransactionSigningHash},
protocol::ProtocolParameters,
unlock::Unlock,
Expand Down Expand Up @@ -308,9 +308,6 @@ impl<'a> SemanticValidationContext<'a> {
}
}

// Validation of input native tokens.
let mut native_token_ids = self.input_native_tokens.keys().collect::<HashSet<_>>();

// Validation of output native tokens.
for (token_id, output_amount) in self.output_native_tokens.iter() {
let input_amount = self.input_native_tokens.get(token_id).copied().unwrap_or_default();
Expand All @@ -322,13 +319,6 @@ impl<'a> SemanticValidationContext<'a> {
{
return Ok(Some(TransactionFailureReason::NativeTokenSumUnbalanced));
}

native_token_ids.insert(token_id);
}

if native_token_ids.len() > NativeTokens::COUNT_MAX as usize {
// TODO https://github.com/iotaledger/iota-sdk/issues/1954
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

// Validation of state transitions and destructions.
Expand Down

0 comments on commit 42a6fd3

Please sign in to comment.