Skip to content

Commit

Permalink
Add non unique token id for foundry check (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Apr 6, 2022
1 parent 51c1035 commit 147e9fa
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions bee-message/src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum ConflictReason {
AddressNotUnlocked = 14,
/// Too many native tokens.
TooManyNativeTokens = 15,
/// Non unique token id for founfry.
NonUniqueTokenIdForFoundry = 16,
/// The semantic validation failed for a reason not covered by the previous variants.
SemanticValidationFailed = 255,
}
Expand Down Expand Up @@ -103,6 +105,7 @@ impl TryFrom<u8> for ConflictReason {
13 => Self::UnlockAddressMismatch,
14 => Self::AddressNotUnlocked,
15 => Self::TooManyNativeTokens,
16 => Self::NonUniqueTokenIdForFoundry,
255 => Self::SemanticValidationFailed,
x => return Err(Self::Error::InvalidConflict(x)),
})
Expand Down Expand Up @@ -330,7 +333,7 @@ pub fn semantic_validation(
return Ok(ConflictReason::CreatedConsumedAmountMismatch);
}

let mut native_token_ids = HashSet::new();
let mut native_token_ids = HashMap::new();

// Validation of input native tokens.
for (token_id, input_amount) in context.input_native_tokens.iter() {
Expand All @@ -344,7 +347,11 @@ pub fn semantic_validation(
return Ok(ConflictReason::CreatedConsumedNativeTokensAmountMismatch);
}

native_token_ids.insert(token_id);
if let Some(token_tag) = native_token_ids.insert(token_id.foundry_id(), token_id.token_tag()) {
if token_tag != token_id.token_tag() {
return Ok(ConflictReason::NonUniqueTokenIdForFoundry);
}
}
}

// Validation of output native tokens.
Expand All @@ -359,7 +366,11 @@ pub fn semantic_validation(
return Ok(ConflictReason::CreatedConsumedNativeTokensAmountMismatch);
}

native_token_ids.insert(token_id);
if let Some(token_tag) = native_token_ids.insert(token_id.foundry_id(), token_id.token_tag()) {
if token_tag != token_id.token_tag() {
return Ok(ConflictReason::NonUniqueTokenIdForFoundry);
}
}
}

if native_token_ids.len() > NativeTokens::COUNT_MAX as usize {
Expand Down

0 comments on commit 147e9fa

Please sign in to comment.