Skip to content

Commit

Permalink
fix: ensure channel open fee can be paid in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Feb 19, 2024
1 parent 4a0a97b commit e8bf2ca
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
10 changes: 7 additions & 3 deletions state-chain/pallets/cf-lp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

use super::*;
use cf_chains::{address::EncodedAddress, benchmarking_value::BenchmarkValue};
use cf_primitives::Asset;
use cf_traits::AccountRoleRegistry;
use cf_primitives::{Asset, FLIPPERINOS_PER_FLIP};
use cf_traits::{AccountRoleRegistry, FeePayment};
use frame_benchmarking::v2::*;
use frame_support::{assert_ok, traits::OnNewAccount};
use frame_system::RawOrigin;

#[benchmarks]
#[benchmarks(
where <T::FeePayment as cf_traits::FeePayment>::Amount: From<u128>
)]
mod benchmarks {
use super::*;

Expand All @@ -21,6 +23,8 @@ mod benchmarks {
RawOrigin::Signed(caller.clone()).into(),
EncodedAddress::Eth(Default::default()),
));
// A non-zero balance is required to pay for the channel opening fee.
T::FeePayment::mint_to_account(&caller, (5 * FLIPPERINOS_PER_FLIP).into());

#[extrinsic_call]
request_liquidity_deposit_address(RawOrigin::Signed(caller), Asset::Eth, 0);
Expand Down
6 changes: 6 additions & 0 deletions state-chain/pallets/cf-lp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ pub mod pallet {

/// Benchmark weights
type WeightInfo: WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
type FeePayment: cf_traits::FeePayment<
Amount = <Self as Chainflip>::Amount,
AccountId = <Self as frame_system::Config>::AccountId,
>;
}

#[pallet::error]
Expand Down
4 changes: 4 additions & 0 deletions state-chain/pallets/cf-lp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use cf_chains::{
AnyChain, Chain, Ethereum,
};
use cf_primitives::{chains::assets, AccountId, ChannelId};
#[cfg(feature = "runtime-benchmarks")]
use cf_traits::mocks::fee_payment::MockFeePayment;
use cf_traits::{
impl_mock_chainflip, impl_mock_runtime_safe_mode,
mocks::{
Expand Down Expand Up @@ -102,6 +104,8 @@ impl crate::Config for Test {
type SafeMode = MockRuntimeSafeMode;
type WeightInfo = ();
type PoolApi = Self;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = MockFeePayment<Self>;
}

pub const LP_ACCOUNT: [u8; 32] = [1u8; 32];
Expand Down
9 changes: 7 additions & 2 deletions state-chain/pallets/cf-swapping/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
use super::*;

use cf_chains::{address::EncodedAddress, benchmarking_value::BenchmarkValue};
use cf_traits::AccountRoleRegistry;
use cf_primitives::FLIPPERINOS_PER_FLIP;
use cf_traits::{AccountRoleRegistry, FeePayment};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
traits::{OnNewAccount, UnfilteredDispatchable},
};
use frame_system::RawOrigin;

#[benchmarks]
#[benchmarks(
where <T::FeePayment as cf_traits::FeePayment>::Amount: From<u128>
)]
mod benchmarks {
use super::*;

Expand All @@ -20,6 +23,8 @@ mod benchmarks {
let caller: T::AccountId = whitelisted_caller();
<T as frame_system::Config>::OnNewAccount::on_new_account(&caller);
assert_ok!(T::AccountRoleRegistry::register_as_broker(&caller));
// A non-zero balance is required to pay for the channel opening fee.
T::FeePayment::mint_to_account(&caller, (5 * FLIPPERINOS_PER_FLIP).into());

let origin = RawOrigin::Signed(caller);
let call = Call::<T>::request_swap_deposit_address {
Expand Down
6 changes: 6 additions & 0 deletions state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ pub mod pallet {

/// The Weight information.
type WeightInfo: WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
type FeePayment: cf_traits::FeePayment<
Amount = <Self as Chainflip>::Amount,
AccountId = <Self as frame_system::Config>::AccountId,
>;
}

#[pallet::pallet]
Expand Down
4 changes: 4 additions & 0 deletions state-chain/pallets/cf-swapping/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use core::cell::Cell;
use crate::{self as pallet_cf_swapping, PalletSafeMode, WeightInfo};
use cf_chains::AnyChain;
use cf_primitives::{Asset, AssetAmount};
#[cfg(feature = "runtime-benchmarks")]
use cf_traits::mocks::fee_payment::MockFeePayment;
use cf_traits::{
impl_mock_chainflip, impl_mock_runtime_safe_mode,
mocks::{
Expand Down Expand Up @@ -140,6 +142,8 @@ impl pallet_cf_swapping::Config for Test {
type SwappingApi = MockSwappingApi;
type SafeMode = MockRuntimeSafeMode;
type WeightInfo = MockWeightInfo;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = MockFeePayment<Self>;
}

pub const ALICE: <Test as frame_system::Config>::AccountId = 123u64;
Expand Down
4 changes: 4 additions & 0 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ impl pallet_cf_swapping::Config for Runtime {
type AddressConverter = ChainAddressConverter;
type SafeMode = RuntimeSafeMode;
type WeightInfo = pallet_cf_swapping::weights::PalletWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = Flip;
}

impl pallet_cf_vaults::Config<EthereumInstance> for Runtime {
Expand Down Expand Up @@ -349,6 +351,8 @@ impl pallet_cf_lp::Config for Runtime {
type SafeMode = RuntimeSafeMode;
type PoolApi = LiquidityPools;
type WeightInfo = pallet_cf_lp::weights::PalletWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = Flip;
}

impl pallet_cf_account_roles::Config for Runtime {
Expand Down

0 comments on commit e8bf2ca

Please sign in to comment.