Skip to content

Commit

Permalink
rewrote the benchmarking for cancel_orders_batch
Browse files Browse the repository at this point in the history
  • Loading branch information
syan095 committed Feb 23, 2025
1 parent 37cf04d commit 9452489
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 115 deletions.
44 changes: 20 additions & 24 deletions state-chain/pallets/cf-pools/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use frame_support::{
traits::{EnsureOrigin, UnfilteredDispatchable},
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sp_std::vec;

fn new_lp_account<T: Chainflip + Config>() -> T::AccountId {
let caller = <T as Chainflip>::AccountRoleRegistry::whitelisted_caller_with_role(
Expand Down Expand Up @@ -269,7 +268,7 @@ mod benchmarks {
}

#[benchmark]
fn cancel_orders_batch() {
fn cancel_orders_batch(n: Linear<1, 100>) {
let caller = new_lp_account::<T>();
assert_ok!(Pallet::<T>::new_pool(
T::EnsureGovernance::try_successful_origin().unwrap(),
Expand All @@ -280,28 +279,25 @@ mod benchmarks {
));
T::LpBalance::credit_account(&caller, Asset::Eth, 1_000_000_000);
T::LpBalance::credit_account(&caller, Asset::Usdc, 1_000_000_000);
let mut orders_to_delete: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>> =
vec![].try_into().unwrap();
for i in 1..101i32 {
assert_ok!(Pallet::<T>::set_range_order(
RawOrigin::Signed(caller.clone()).into(),
Asset::Eth,
Asset::Usdc,
i as u64,
Some(-i..i),
RangeOrderSize::AssetAmounts {
maximum: AssetAmounts { base: 1_000_000, quote: 1_000_000 },
minimum: AssetAmounts { base: 500_000, quote: 500_000 },
},
));
orders_to_delete
.try_push(CloseOrder::Range {
base_asset: Asset::Eth,
quote_asset: Asset::Usdc,
id: i as u64,
})
.expect("cannot fail");
}

let orders_to_delete = (1i32..n as i32)
.map(|i| {
assert_ok!(Pallet::<T>::set_range_order(
RawOrigin::Signed(caller.clone()).into(),
Asset::Eth,
Asset::Usdc,
i as u64,
Some(-i..i),
RangeOrderSize::AssetAmounts {
maximum: AssetAmounts { base: 1_000_000, quote: 1_000_000 },
minimum: AssetAmounts { base: 500_000, quote: 500_000 },
},
));
CloseOrder::Range { base_asset: Asset::Eth, quote_asset: Asset::Usdc, id: i as u64 }
})
.collect::<Vec<_>>()
.try_into()
.unwrap();

#[extrinsic_call]
crate::benchmarking::benchmarks::cancel_orders_batch(
Expand Down
2 changes: 1 addition & 1 deletion state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ pub mod pallet {
}

#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::cancel_orders_batch())]
#[pallet::weight(T::WeightInfo::cancel_orders_batch(orders.len() as u32))]
pub fn cancel_orders_batch(
origin: OriginFor<T>,
orders: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>>,
Expand Down
Loading

0 comments on commit 9452489

Please sign in to comment.