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 b0bb520 commit 19aa77c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
13 changes: 10 additions & 3 deletions state-chain/pallets/cf-lp/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#![cfg(feature = "runtime-benchmarks")]

#[cfg(test)]
mod mock;

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::*;
use sp_std::vec;
Expand All @@ -22,6 +27,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 @@ -13,6 +13,8 @@ use cf_traits::{
},
AccountRoleRegistry,
};
#[cfg(feature = "runtime-benchmarks")]
use fee_payment::MockFeePayment;
use frame_support::{assert_ok, parameter_types, sp_runtime::app_crypto::sp_core::H160};
use frame_system as system;
use sp_core::H256;
Expand Down Expand Up @@ -99,6 +101,8 @@ impl crate::Config for Test {
type SafeMode = MockRuntimeSafeMode;
type WeightInfo = ();
type PoolApi = Self;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = MockFeePayment;
}

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::*;
use sp_std::vec;
Expand All @@ -21,6 +24,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 @@ -11,6 +11,8 @@ use cf_traits::{
},
AccountRoleRegistry, SwappingApi,
};
#[cfg(feature = "runtime-benchmarks")]
use fee_payment::MockFeePayment;
use frame_support::{pallet_prelude::DispatchError, parameter_types, weights::Weight};
use frame_system as system;
use sp_core::H256;
Expand Down Expand Up @@ -139,6 +141,8 @@ impl pallet_cf_swapping::Config for Test {
type SwappingApi = MockSwappingApi;
type SafeMode = MockRuntimeSafeMode;
type WeightInfo = MockWeightInfo;
#[cfg(feature = "runtime-benchmarks")]
type FeePayment = MockFeePayment;
}

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 @@ -234,6 +234,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 @@ -348,6 +350,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 19aa77c

Please sign in to comment.