Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Jan 23, 2024
1 parent 5796fb8 commit 7e96a19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
8 changes: 2 additions & 6 deletions sdk/src/types/block/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ pub enum Error {
ConsumedAmountOverflow,
ConsumedManaOverflow,
ConsumedNativeTokensAmountOverflow,
ContextInputsNotUniqueSorted,
CreatedAmountOverflow,
CreatedManaOverflow,
CreatedNativeTokensAmountOverflow,
Crypto(CryptoError),
DuplicateBicAccountId(AccountId),
DuplicateRewardInputIndex(u16),
DuplicateSignatureUnlock(u16),
DuplicateUtxo(UtxoInput),
ExpirationUnlockConditionZero,
Expand Down Expand Up @@ -188,7 +187,6 @@ pub enum Error {
},
StorageDepositReturnOverflow,
TimelockUnlockConditionZero,
TooManyCommitmentInputs,
UnallowedFeature {
index: usize,
kind: u8,
Expand Down Expand Up @@ -222,12 +220,11 @@ impl fmt::Display for Error {
Self::ConsumedAmountOverflow => write!(f, "consumed amount overflow"),
Self::ConsumedManaOverflow => write!(f, "consumed mana overflow"),
Self::ConsumedNativeTokensAmountOverflow => write!(f, "consumed native tokens amount overflow"),
Self::ContextInputsNotUniqueSorted => write!(f, "context inputs are not unique and/or sorted"),
Self::CreatedAmountOverflow => write!(f, "created amount overflow"),
Self::CreatedManaOverflow => write!(f, "created mana overflow"),
Self::CreatedNativeTokensAmountOverflow => write!(f, "created native tokens amount overflow"),
Self::Crypto(e) => write!(f, "cryptographic error: {e}"),
Self::DuplicateBicAccountId(account_id) => write!(f, "duplicate BIC account id: {account_id}"),
Self::DuplicateRewardInputIndex(idx) => write!(f, "duplicate reward input index: {idx}"),
Self::DuplicateSignatureUnlock(index) => {
write!(f, "duplicate signature unlock at index: {index}")
}
Expand Down Expand Up @@ -434,7 +431,6 @@ impl fmt::Display for Error {
"timelock unlock condition with milestone index and timestamp set to 0",
)
}
Self::TooManyCommitmentInputs => write!(f, "too many commitment inputs"),
Self::UnallowedFeature { index, kind } => {
write!(f, "unallowed feature at index {index} with kind {kind}")
}
Expand Down
39 changes: 4 additions & 35 deletions sdk/src/types/block/payload/signed_transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,49 +367,18 @@ fn verify_context_inputs_packable<const VERIFY: bool>(
}

fn verify_context_inputs(context_inputs: &[ContextInput]) -> Result<(), Error> {
is_unique_sorted_by(context_inputs.iter(), |a, b| {
if !is_unique_sorted_by(context_inputs.iter(), |a, b| {
a.kind().cmp(&b.kind()).then_with(|| match (a, b) {
(ContextInput::Commitment(_), ContextInput::Commitment(_)) => core::cmp::Ordering::Equal,
(ContextInput::BlockIssuanceCredit(a), ContextInput::BlockIssuanceCredit(b)) => {
a.account_id().cmp(b.account_id())
}
(ContextInput::Reward(a), ContextInput::Reward(b)) => a.index().cmp(&b.index()),

// No need to evaluate all combinations as `then_with` is only called if the first cmp is Equal.
// No need to evaluate all combinations as `then_with` is only called on Equal.
_ => unreachable!(),
})
});

let mut commitment = false;
let mut bic_account_id_set = HashSet::new();
let mut reward_index_set = HashSet::new();

for input in context_inputs.iter() {
match input {
ContextInput::Commitment(_) => {
// There must be zero or one Commitment Input.
if commitment {
return Err(Error::TooManyCommitmentInputs);
}
commitment = true;
}
ContextInput::BlockIssuanceCredit(bic) => {
let account_id = bic.account_id();

// All Block Issuance Credit Inputs must reference a different Account ID.
if !bic_account_id_set.insert(account_id) {
return Err(Error::DuplicateBicAccountId(*account_id));
}
}
ContextInput::Reward(r) => {
let index = r.index();

// All Rewards Inputs must reference a different Index
if !reward_index_set.insert(index) {
return Err(Error::DuplicateRewardInputIndex(index));
}
}
}
}) {
return Err(Error::ContextInputsNotUniqueSorted);
}

Ok(())
Expand Down

0 comments on commit 7e96a19

Please sign in to comment.