Skip to content

Commit

Permalink
Remove unit visitor from verify_with (#1629)
Browse files Browse the repository at this point in the history
* Remove unit visitor from verify_with

* Fix compilation

* Use released version
  • Loading branch information
thibault-martinez authored Nov 17, 2023
1 parent eeaea97 commit e2703a1
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 42 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindings/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ iota-crypto = { version = "0.23.0", default-features = false, features = [
"bip44",
] }
log = { version = "0.4.20", default-features = false }
packable = { version = "0.9.0", default-features = false }
packable = { version = "0.10.0", default-features = false }
prefix-hex = { version = "0.7.1", default-features = false }
primitive-types = { version = "0.12.2", default-features = false }
serde = { version = "1.0.188", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ iota-crypto = { version = "0.23.0", default-features = false, features = [
"secp256k1",
] }
iterator-sorted = { version = "0.1.0", default-features = false }
packable = { version = "0.9.0", default-features = false, features = [
packable = { version = "0.10.0", default-features = false, features = [
"primitive-types",
] }
paste = { version = "1.0.14", default-features = false }
Expand Down
20 changes: 10 additions & 10 deletions sdk/src/types/block/address/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub struct WeightedAddress {
impl WeightedAddress {
/// Creates a new [`WeightedAddress`].
pub fn new(address: Address, weight: u8) -> Result<Self, Error> {
verify_address::<true>(&address, &())?;
verify_weight::<true>(&weight, &())?;
verify_address::<true>(&address)?;
verify_weight::<true>(&weight)?;

Ok(Self { address, weight })
}
Expand All @@ -47,7 +47,7 @@ impl WeightedAddress {
}
}

fn verify_address<const VERIFY: bool>(address: &Address, _visitor: &()) -> Result<(), Error> {
fn verify_address<const VERIFY: bool>(address: &Address) -> Result<(), Error> {
if VERIFY {
if !matches!(
address,
Expand All @@ -59,7 +59,7 @@ fn verify_address<const VERIFY: bool>(address: &Address, _visitor: &()) -> Resul
Ok(())
}

fn verify_weight<const VERIFY: bool>(weight: &u8, _visitor: &()) -> Result<(), Error> {
fn verify_weight<const VERIFY: bool>(weight: &u8) -> Result<(), Error> {
if VERIFY && *weight == 0 {
return Err(Error::InvalidAddressWeight(*weight));
} else {
Expand Down Expand Up @@ -95,15 +95,15 @@ impl MultiAddress {
pub fn new(addresses: impl IntoIterator<Item = WeightedAddress>, threshold: u16) -> Result<Self, Error> {
let addresses = addresses.into_iter().collect::<Box<[_]>>();

verify_addresses::<true>(&addresses, &())?;
verify_threshold::<true>(&threshold, &())?;
verify_addresses::<true>(&addresses)?;
verify_threshold::<true>(&threshold)?;

let addresses = BoxedSlicePrefix::<WeightedAddress, WeightedAddressCount>::try_from(addresses)
.map_err(Error::InvalidWeightedAddressCount)?;

let multi_address = Self { addresses, threshold };

verify_multi_address::<true>(&multi_address, &())?;
verify_multi_address::<true>(&multi_address)?;

Ok(multi_address)
}
Expand All @@ -121,23 +121,23 @@ impl MultiAddress {
}
}

fn verify_addresses<const VERIFY: bool>(addresses: &[WeightedAddress], _visitor: &()) -> Result<(), Error> {
fn verify_addresses<const VERIFY: bool>(addresses: &[WeightedAddress]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(addresses.iter().map(WeightedAddress::address)) {
return Err(Error::WeightedAddressesNotUniqueSorted);
} else {
Ok(())
}
}

fn verify_threshold<const VERIFY: bool>(threshold: &u16, _visitor: &()) -> Result<(), Error> {
fn verify_threshold<const VERIFY: bool>(threshold: &u16) -> Result<(), Error> {
if VERIFY && *threshold == 0 {
return Err(Error::InvalidMultiAddressThreshold(*threshold));
} else {
Ok(())
}
}

fn verify_multi_address<const VERIFY: bool>(address: &MultiAddress, _visitor: &()) -> Result<(), Error> {
fn verify_multi_address<const VERIFY: bool>(address: &MultiAddress) -> Result<(), Error> {
if VERIFY {
let cumulative_weight = address.iter().map(|address| address.weight as u16).sum::<u16>();

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<const MIN: u8, const MAX: u8> Parents<MIN, MAX> {
}
}

fn verify_parents<const VERIFY: bool>(parents: &[BlockId], _: &()) -> Result<(), Error> {
fn verify_parents<const VERIFY: bool>(parents: &[BlockId]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(parents.iter().map(AsRef::as_ref)) {
Err(Error::ParentsNotUniqueSorted)
} else {
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types/block/mana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) type ManaAllotmentCount =

/// A list of [`ManaAllotment`]s with unique [`AccountId`]s.
#[derive(Clone, Debug, Eq, PartialEq, Deref, Packable)]
#[packable(unpack_visitor = ProtocolParameters)]
#[packable(unpack_error = Error, with = |e| e.unwrap_item_err_or_else(|p| Error::InvalidManaAllotmentCount(p.into())))]
pub struct ManaAllotments(
#[packable(verify_with = verify_mana_allotments)] BoxedSlicePrefix<ManaAllotment, ManaAllotmentCount>,
Expand Down
7 changes: 2 additions & 5 deletions sdk/src/types/block/output/feature/block_issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ pub struct BlockIssuerKeys(
#[packable(verify_with = verify_block_issuer_keys)] BoxedSlicePrefix<BlockIssuerKey, BlockIssuerKeyCount>,
);

fn verify_block_issuer_keys<const VERIFY: bool>(
block_issuer_keys: &[BlockIssuerKey],
_visitor: &(),
) -> Result<(), Error> {
fn verify_block_issuer_keys<const VERIFY: bool>(block_issuer_keys: &[BlockIssuerKey]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(block_issuer_keys.iter()) {
return Err(Error::BlockIssuerKeysNotUniqueSorted);
}
Expand Down Expand Up @@ -177,7 +174,7 @@ impl BlockIssuerKeys {
block_issuer_keys.sort();

// Still need to verify the duplicate block issuer keys.
verify_block_issuer_keys::<true>(&block_issuer_keys, &())?;
verify_block_issuer_keys::<true>(&block_issuer_keys)?;

Ok(Self(block_issuer_keys))
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/feature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Features {

features.sort_by_key(Feature::kind);
// Sort is obviously fine now but uniqueness still needs to be checked.
verify_unique_sorted::<true>(&features, &())?;
verify_unique_sorted::<true>(&features)?;

Ok(Self(features))
}
Expand Down Expand Up @@ -248,7 +248,7 @@ impl StorageScore for Features {
}

#[inline]
fn verify_unique_sorted<const VERIFY: bool>(features: &[Feature], _: &()) -> Result<(), Error> {
fn verify_unique_sorted<const VERIFY: bool>(features: &[Feature]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(features.iter().map(Feature::kind)) {
Err(Error::FeaturesNotUniqueSorted)
} else {
Expand Down
8 changes: 4 additions & 4 deletions sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl NativeToken {
pub fn new(token_id: TokenId, amount: impl Into<U256>) -> Result<Self, Error> {
let amount = amount.into();

verify_amount::<true>(&amount, &())?;
verify_amount::<true>(&amount)?;

Ok(Self { token_id, amount })
}
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Ord for NativeToken {
impl StorageScore for NativeToken {}

#[inline]
fn verify_amount<const VERIFY: bool>(amount: &U256, _: &()) -> Result<(), Error> {
fn verify_amount<const VERIFY: bool>(amount: &U256) -> Result<(), Error> {
if VERIFY && amount.is_zero() {
Err(Error::NativeTokensNullAmount)
} else {
Expand Down Expand Up @@ -218,7 +218,7 @@ impl NativeTokens {

native_tokens.sort_by(|a, b| a.token_id().cmp(b.token_id()));
// Sort is obviously fine now but uniqueness still needs to be checked.
verify_unique_sorted::<true>(&native_tokens, &())?;
verify_unique_sorted::<true>(&native_tokens)?;

Ok(Self(native_tokens))
}
Expand Down Expand Up @@ -258,7 +258,7 @@ impl NativeTokens {
}

#[inline]
fn verify_unique_sorted<const VERIFY: bool>(native_tokens: &[NativeToken], _: &()) -> Result<(), Error> {
fn verify_unique_sorted<const VERIFY: bool>(native_tokens: &[NativeToken]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(native_tokens.iter().map(NativeToken::token_id)) {
Err(Error::NativeTokensNotUniqueSorted)
} else {
Expand Down
7 changes: 2 additions & 5 deletions sdk/src/types/block/output/token_scheme/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl SimpleTokenScheme {
maximum_supply,
};

verify_simple_token_scheme::<true>(&token_scheme, &())?;
verify_simple_token_scheme::<true>(&token_scheme)?;

Ok(token_scheme)
}
Expand Down Expand Up @@ -71,10 +71,7 @@ impl SimpleTokenScheme {
}

#[inline]
fn verify_simple_token_scheme<const VERIFY: bool>(
token_scheme: &SimpleTokenScheme,
_visitor: &(),
) -> Result<(), Error> {
fn verify_simple_token_scheme<const VERIFY: bool>(token_scheme: &SimpleTokenScheme) -> Result<(), Error> {
if VERIFY
&& (token_scheme.maximum_supply.is_zero()
|| token_scheme.melted_tokens > token_scheme.minted_tokens
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/unlock_condition/expiration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ExpirationUnlockCondition {
pub fn new(return_address: impl Into<Address>, slot_index: impl Into<SlotIndex>) -> Result<Self, Error> {
let slot_index = slot_index.into();

verify_slot_index::<true>(&slot_index, &())?;
verify_slot_index::<true>(&slot_index)?;

Ok(Self {
return_address: return_address.into(),
Expand Down Expand Up @@ -67,7 +67,7 @@ impl StorageScore for ExpirationUnlockCondition {
}

#[inline]
fn verify_slot_index<const VERIFY: bool>(slot_index: &SlotIndex, _: &()) -> Result<(), Error> {
fn verify_slot_index<const VERIFY: bool>(slot_index: &SlotIndex) -> Result<(), Error> {
if VERIFY && *slot_index == 0 {
Err(Error::ExpirationUnlockConditionZero)
} else {
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/unlock_condition/timelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl TimelockUnlockCondition {
pub fn new(slot_index: impl Into<SlotIndex>) -> Result<Self, Error> {
let slot_index = slot_index.into();

verify_slot_index::<true>(&slot_index, &())?;
verify_slot_index::<true>(&slot_index)?;

Ok(Self(slot_index))
}
Expand All @@ -34,7 +34,7 @@ impl TimelockUnlockCondition {
impl StorageScore for TimelockUnlockCondition {}

#[inline]
fn verify_slot_index<const VERIFY: bool>(slot_index: &SlotIndex, _: &()) -> Result<(), Error> {
fn verify_slot_index<const VERIFY: bool>(slot_index: &SlotIndex) -> Result<(), Error> {
if VERIFY && *slot_index == 0 {
Err(Error::TimelockUnlockConditionZero)
} else {
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/unlock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Unlocks {
let unlocks: BoxedSlicePrefix<Unlock, UnlockCount> =
unlocks.into().try_into().map_err(Error::InvalidUnlockCount)?;

verify_unlocks::<true>(&unlocks, &())?;
verify_unlocks::<true>(&unlocks)?;

Ok(Self(unlocks))
}
Expand Down Expand Up @@ -177,7 +177,7 @@ fn verify_non_multi_unlock<'a>(
Ok(())
}

fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock], _: &()) -> Result<(), Error> {
fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock]) -> Result<(), Error> {
if VERIFY {
let mut seen_signatures = HashSet::new();

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/unlock/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MultiUnlock {
pub fn new(unlocks: impl IntoIterator<Item = Unlock>) -> Result<Self, Error> {
let unlocks = unlocks.into_iter().collect::<Box<[_]>>();

verify_unlocks::<true>(&unlocks, &())?;
verify_unlocks::<true>(&unlocks)?;

Ok(Self(
BoxedSlicePrefix::<Unlock, UnlocksCount>::try_from(unlocks).map_err(Error::InvalidMultiUnlockCount)?,
Expand All @@ -38,7 +38,7 @@ impl MultiUnlock {
}
}

fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock], _visitor: &()) -> Result<(), Error> {
fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock]) -> Result<(), Error> {
if VERIFY && unlocks.iter().any(Unlock::is_multi) {
return Err(Error::MultiUnlockRecursion);
} else {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cargo-fuzz = true
iota-types = { path = "..", default-features = false }

libfuzzer-sys = { version = "0.4.7", default-features = false }
packable = { version = "0.9.0", default-features = false }
packable = { version = "0.10.0", default-features = false }

# Prevent this from interfering with workspaces
[workspace]
Expand Down

0 comments on commit e2703a1

Please sign in to comment.