Skip to content

Commit

Permalink
ensure strings have upper bound in benchmark and add verification
Browse files Browse the repository at this point in the history
  • Loading branch information
JCSanPedro committed Jan 12, 2025
1 parent d09e6be commit 7ed93e4
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 125 deletions.
5 changes: 4 additions & 1 deletion pallet/fee-proxy/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ where
// if the call is a sylo pallet call, then we always force a fee swap with the
// sylo token
if is_sylo_call {
// let payment_asset =
// pallet_sylo::Pallet::<T>::payment_asset().ok_or(InvalidTransaction::Payment)?;

let payment_asset =
pallet_sylo::Pallet::<T>::payment_asset().ok_or(InvalidTransaction::Payment)?;
pallet_sylo::SyloAssetId::<T>::get().ok_or(InvalidTransaction::Payment)?;

do_fee_swap(who, &payment_asset, Balance::from(fee), u128::MAX)?;
}
Expand Down
137 changes: 107 additions & 30 deletions pallet/sylo/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ pub fn bounded_string<T: Config>(name: &str) -> BoundedVec<u8, <T as Config>::St
BoundedVec::truncate_from(name.as_bytes().to_vec())
}

pub fn max_bounded_string<T: Config>(bound: u32) -> BoundedVec<u8, <T as Config>::StringLimit> {
let mut max_string = BoundedVec::new();
for _ in 1..bound {
max_string.force_push(b'a');
}
max_string
}

pub fn setup_resolver<T: Config>(
caller: T::AccountId,
identifier: BoundedVec<u8, <T as Config>::StringLimit>,
Expand Down Expand Up @@ -97,18 +105,21 @@ benchmarks! {

let alice = account::<T>("Alice");

let mut identifier = BoundedVec::new();
for _ in 1..p {
identifier.force_push(b'a');
}
let identifier = max_bounded_string::<T>(p);

let mut service_endpoints = BoundedVec::new();
for _ in 1..q {
service_endpoints.force_push(bounded_string::<T>("https://service-endpoint.one.two.three"));
service_endpoints.force_push(max_bounded_string::<T>(p));
}
}: _(origin::<T>(&alice), identifier, service_endpoints)
}: _(origin::<T>(&alice), identifier.clone(), service_endpoints.clone())
verify {
assert_eq!(Resolvers::<T>::get(identifier), Some(Resolver {
controller: alice, service_endpoints
}));
}

update_resolver {
let p in 1 .. STRING_LIMIT;
let q in 1 .. MAX_SERVICE_ENDPOINTS;

let alice = account::<T>("Alice");
Expand All @@ -117,87 +128,153 @@ benchmarks! {

let mut service_endpoints = BoundedVec::new();
for _ in 1..q {
service_endpoints.force_push(bounded_string::<T>("https://service-endpoint.one.two.three"));
service_endpoints.force_push(max_bounded_string::<T>(p));
}
}: _(origin::<T>(&alice), identifier, service_endpoints)
}: _(origin::<T>(&alice), identifier.clone(), service_endpoints.clone())
verify {
assert_eq!(Resolvers::<T>::get(identifier), Some(Resolver {
controller: alice, service_endpoints
}));
}

unregister_resolver {
let alice = account::<T>("Alice");

let identifier = setup_resolver::<T>(alice.clone(), bounded_string::<T>("sylo-data-resolver"));
}: _(origin::<T>(&alice), identifier)
}: _(origin::<T>(&alice), identifier.clone())
verify {
assert_eq!(Resolvers::<T>::get(identifier), None);
}

create_validation_record {
let p in 1 .. MAX_RESOLVERS;
let q in 1 .. MAX_TAGS;
let p in 1 .. STRING_LIMIT;
let q in 1 .. MAX_RESOLVERS;
let r in 1 .. MAX_TAGS;

let alice = account::<T>("Alice");

let data_id = bounded_string::<T>("data-id");

let mut resolvers = BoundedVec::new();
for i in 1 .. p {
for i in 1 .. q {
let mut resolver_id = String::from("sylo-resolver");
resolver_id.push_str(i.to_string().as_str());
let mut resolver_id = bounded_string::<T>(resolver_id.as_str());
let max_affix = max_bounded_string::<T>(
p - <usize as TryInto<u32>>::try_into(resolver_id.len()
).unwrap());
resolver_id.try_append(&mut max_affix.to_vec()).unwrap();

let resolver_id = setup_resolver::<T>(alice.clone(), bounded_string::<T>(resolver_id.as_str()));
let resolver_id = setup_resolver::<T>(alice.clone(), resolver_id);
resolvers.force_push(ResolverId {
method: bounded_string::<T>("sylo_resolver"),
method: max_bounded_string::<T>(p),
identifier: resolver_id,
});
}

let data_type = bounded_string::<T>("data-type");
let data_type = max_bounded_string::<T>(p);

let mut tags = BoundedVec::new();
for _ in 1 .. q {
tags.force_push(bounded_string::<T>("tag"));
for _ in 1 .. r {
tags.force_push(max_bounded_string::<T>(p));
}

let checksum = H256::from_low_u64_be(123);
}: _(origin::<T>(&alice), data_id, resolvers, data_type, tags, checksum)

let block: BlockNumberFor<T> = 1_u32.into();
}: _(origin::<T>(&alice), data_id.clone(), resolvers.clone(), data_type.clone(), tags.clone(), checksum.clone())
verify {
assert_eq!(ValidationRecords::<T>::get(&alice, &data_id), Some(ValidationRecord {
author: alice,
resolvers: resolvers,
data_type: data_type,
tags: tags,
entries: BoundedVec::truncate_from(vec![ValidationEntry {
checksum,
block,
}]),
}));
}

add_validation_record_entry {
let alice = account::<T>("Alice");

let data_id = setup_validation_record::<T>(alice.clone());

let checksum = H256::from_low_u64_be(125);
}: _(origin::<T>(&alice), data_id, checksum)
let checksum = H256::from_low_u64_be(123);
}: _(origin::<T>(&alice), data_id.clone(), checksum.clone())
verify {
assert_eq!(ValidationRecords::<T>::get(&alice, &data_id), Some(ValidationRecord {
author: alice,
resolvers: BoundedVec::new(),
data_type: bounded_string::<T>("data-type"),
tags: BoundedVec::new(),
entries: BoundedVec::truncate_from(vec![ValidationEntry {
checksum,
block: 0_u32.into(),
}, ValidationEntry {
checksum,
block: 1_u32.into(),
}]),
}));
}

update_validation_record {
let p in 1 .. MAX_RESOLVERS;
let q in 1 .. MAX_TAGS;
let p in 1 .. STRING_LIMIT;
let q in 1 .. MAX_RESOLVERS;
let r in 1 .. MAX_TAGS;

let alice = account::<T>("Alice");

let data_id = setup_validation_record::<T>(alice.clone());

let mut resolvers = BoundedVec::new();
for i in 1 .. p {
for i in 1 .. q {
let mut resolver_id = String::from("sylo-resolver");
resolver_id.push_str(i.to_string().as_str());
let mut resolver_id = bounded_string::<T>(resolver_id.as_str());
let max_affix = max_bounded_string::<T>(
p - <usize as TryInto<u32>>::try_into(resolver_id.len()
).unwrap());
resolver_id.try_append(&mut max_affix.to_vec()).unwrap();

let resolver_id = setup_resolver::<T>(alice.clone(), bounded_string::<T>(resolver_id.as_str()));
let resolver_id = setup_resolver::<T>(alice.clone(), resolver_id);
resolvers.force_push(ResolverId {
method: bounded_string::<T>("sylo_resolver"),
method: max_bounded_string::<T>(p),
identifier: resolver_id,
});
}

let data_type = bounded_string::<T>("data-type");
let data_type = max_bounded_string::<T>(p);

let mut tags = BoundedVec::new();
for _ in 1 .. q {
tags.force_push(bounded_string::<T>("tag"));
for _ in 1 .. r {
tags.force_push(max_bounded_string::<T>(p));
}
}: _(origin::<T>(&alice), data_id, Some(resolvers), Some(data_type), Some(tags))

let block: BlockNumberFor<T> = 1_u32.into();
}: _(origin::<T>(&alice), data_id.clone(), Some(resolvers.clone()), Some(data_type.clone()), Some(tags.clone()))
verify {
assert_eq!(ValidationRecords::<T>::get(&alice, &data_id), Some(ValidationRecord {
author: alice,
resolvers: resolvers,
data_type: data_type,
tags: tags,
entries: BoundedVec::truncate_from(vec![ValidationEntry {
checksum: H256::from_low_u64_be(123),
block,
}]),
}));
}

delete_validation_record {
let alice = account::<T>("Alice");

let data_id = setup_validation_record::<T>(alice.clone());
}: _(origin::<T>(&alice), data_id)
}: _(origin::<T>(&alice), data_id.clone())
verify {
assert_eq!(ValidationRecords::<T>::get(&alice, &data_id), None);
}
}

impl_benchmark_test_suite!(
Expand Down
8 changes: 2 additions & 6 deletions pallet/sylo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub mod pallet {

#[pallet::call_index(3)]
#[pallet::weight({
T::WeightInfo::update_resolver(<T::MaxServiceEndpoints>::get())
T::WeightInfo::update_resolver(<T::StringLimit>::get(), <T::MaxServiceEndpoints>::get())
})]
pub fn update_resolver(
origin: OriginFor<T>,
Expand Down Expand Up @@ -304,7 +304,7 @@ pub mod pallet {

#[pallet::call_index(5)]
#[pallet::weight({
T::WeightInfo::create_validation_record(<T::MaxResolvers>::get(), <T::MaxTags>::get())
T::WeightInfo::create_validation_record(<T::StringLimit>::get(), <T::MaxResolvers>::get(), <T::MaxTags>::get())
})]
pub fn create_validation_record(
origin: OriginFor<T>,
Expand Down Expand Up @@ -467,9 +467,5 @@ pub mod pallet {

Ok(())
}

pub fn payment_asset() -> Option<AssetId> {
<SyloAssetId<T>>::get()
}
}
}
Loading

0 comments on commit 7ed93e4

Please sign in to comment.