From 5bb5402eab501fe26fa6a21af587a08afd99160c Mon Sep 17 00:00:00 2001 From: Cosmin Damian <17934949+cdamian@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:31:05 +0300 Subject: [PATCH] benchmarks: Add default for Message type (wip) --- libs/traits/src/liquidity_pools.rs | 4 ++++ .../queue/src/benchmarking.rs | 11 +++++------ pallets/liquidity-pools-gateway/queue/src/lib.rs | 5 +++++ .../liquidity-pools-gateway/queue/src/mock.rs | 6 ++---- pallets/liquidity-pools/src/message.rs | 16 +++++----------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/libs/traits/src/liquidity_pools.rs b/libs/traits/src/liquidity_pools.rs index 68af8e2705..2c8b5b3b57 100644 --- a/libs/traits/src/liquidity_pools.rs +++ b/libs/traits/src/liquidity_pools.rs @@ -30,6 +30,10 @@ pub mod test_util { use super::*; #[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)] + #[cfg_attr( + any(test, feature = "std", feature = "runtime-benchmarks"), + derive(Default) + )] pub struct Message; impl LPEncoding for Message { fn serialize(&self) -> Vec { diff --git a/pallets/liquidity-pools-gateway/queue/src/benchmarking.rs b/pallets/liquidity-pools-gateway/queue/src/benchmarking.rs index cee05c124c..c6b3806f8c 100644 --- a/pallets/liquidity-pools-gateway/queue/src/benchmarking.rs +++ b/pallets/liquidity-pools-gateway/queue/src/benchmarking.rs @@ -11,7 +11,6 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -use cfg_traits::liquidity_pools::test_util::Message as LPTestMessage; use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*}; use frame_system::RawOrigin; use parity_scale_codec::EncodeLike; @@ -20,7 +19,7 @@ use super::*; #[benchmarks( where - T: Config, + T: Config, T::AccountId: EncodeLike<::AccountId>, )] mod benchmarks { @@ -29,13 +28,13 @@ mod benchmarks { #[benchmark] fn process_message() -> Result<(), BenchmarkError> { let caller: T::AccountId = account("acc_0", 0, 0); - let message = LPTestMessage {}; + let message = T::Message::default(); let nonce = T::MessageNonce::one(); MessageQueue::::insert(nonce, message.clone()); #[cfg(test)] - mock::mock_lp_gateway_process_success(message); + mock::mock_lp_gateway_process_success::(); #[extrinsic_call] process_message(RawOrigin::Signed(caller), nonce); @@ -46,14 +45,14 @@ mod benchmarks { #[benchmark] fn process_failed_message() -> Result<(), BenchmarkError> { let caller: T::AccountId = account("acc_0", 0, 0); - let message = LPTestMessage {}; + let message = T::Message::default(); let error = DispatchError::Unavailable; let nonce = T::MessageNonce::one(); FailedMessageQueue::::insert(nonce, (message.clone(), error)); #[cfg(test)] - mock::mock_lp_gateway_process_success(message); + mock::mock_lp_gateway_process_success::(); #[extrinsic_call] process_failed_message(RawOrigin::Signed(caller), nonce); diff --git a/pallets/liquidity-pools-gateway/queue/src/lib.rs b/pallets/liquidity-pools-gateway/queue/src/lib.rs index b850f3b037..b5ef2464f3 100644 --- a/pallets/liquidity-pools-gateway/queue/src/lib.rs +++ b/pallets/liquidity-pools-gateway/queue/src/lib.rs @@ -49,9 +49,14 @@ pub mod pallet { pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + #[cfg(not(feature = "runtime-benchmarks"))] /// The message type. type Message: Clone + Debug + PartialEq + MaxEncodedLen + TypeInfo + FullCodec; + #[cfg(feature = "runtime-benchmarks")] + /// The message type. + type Message: Clone + Debug + PartialEq + MaxEncodedLen + TypeInfo + FullCodec + Default; + /// Type used for message identification. type MessageNonce: Parameter + Member diff --git a/pallets/liquidity-pools-gateway/queue/src/mock.rs b/pallets/liquidity-pools-gateway/queue/src/mock.rs index 298ba3e86c..54712ca5d7 100644 --- a/pallets/liquidity-pools-gateway/queue/src/mock.rs +++ b/pallets/liquidity-pools-gateway/queue/src/mock.rs @@ -61,10 +61,8 @@ impl Config for Runtime { type WeightInfo = (); } -pub fn mock_lp_gateway_process_success(expected_message: LPTestMessage) { - LPGatewayMock::mock_process(move |msg| { - assert_eq!(msg, expected_message); - +pub fn mock_lp_gateway_process_success() { + LPGatewayMock::mock_process(move |_| { Ok(PostDispatchInfo { // Defensive weight that we should also use during bookmarks. actual_weight: Some(Weight::from_parts(DEFAULT_WEIGHT_REF_TIME, 256)), diff --git a/pallets/liquidity-pools/src/message.rs b/pallets/liquidity-pools/src/message.rs index 5bb8815b73..a6d9943b49 100644 --- a/pallets/liquidity-pools/src/message.rs +++ b/pallets/liquidity-pools/src/message.rs @@ -201,7 +201,9 @@ impl BatchMessages { TypeInfo, MaxEncodedLen, )] +#[cfg_attr(feature = "runtime-benchmarks", derive(Default))] pub enum Message { + #[default] Invalid, // --- Gateway --- /// Proof a message has been executed. @@ -286,9 +288,7 @@ pub enum Message { /// Add a pool to a domain. /// /// Directionality: Centrifuge -> EVM Domain. - AddPool { - pool_id: u64, - }, + AddPool { pool_id: u64 }, /// Add a tranche to an already existing pool on the target domain. /// The decimals of a tranche MUST be equal to the decimals of a pool. /// Thus, consuming domains MUST take care of storing the decimals upon @@ -309,18 +309,12 @@ pub enum Message { /// Allow a currency to be used as a pool currency and to invest in a pool. /// /// Directionality: Centrifuge -> EVM Domain. - AllowAsset { - pool_id: u64, - currency: u128, - }, + AllowAsset { pool_id: u64, currency: u128 }, /// Disallow a currency to be used as a pool currency and to invest in a /// pool. /// /// Directionality: Centrifuge -> EVM Domain. - DisallowAsset { - pool_id: u64, - currency: u128, - }, + DisallowAsset { pool_id: u64, currency: u128 }, /// Update the price of a tranche token on the target domain. /// /// Directionality: Centrifuge -> EVM Domain.