From 8245ce291ff310b4b4d9fc22d894343349552150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Mon, 5 Aug 2024 16:23:54 +0200 Subject: [PATCH] Mock builder update. Improved FI coverage (#1945) * fix issues with default_features * check if hooks are called for FI * correct foss3 dependency * cargo update * fix dep chagne * fix features * revert procedural crate version --- Cargo.lock | 6 +- libs/mocks/src/foreign_investment_hooks.rs | 14 +- libs/mocks/src/token_swaps.rs | 2 +- libs/traits/Cargo.toml | 9 +- pallets/foreign-investments/src/tests.rs | 145 +++++++++++++-------- pallets/restricted-tokens/Cargo.toml | 10 +- runtime/common/Cargo.toml | 3 +- 7 files changed, 111 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95d7f9d5b4..8d26155359 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6678,8 +6678,8 @@ dependencies = [ [[package]] name = "mock-builder" -version = "0.2.0" -source = "git+https://github.com/foss3/runtime-pallet-library?branch=polkadot-v1.7.2#8ca5efb3e1eaeb87cc2cdc1a4c3057f15b899907" +version = "0.3.0" +source = "git+https://github.com/foss3/runtime-pallet-library?branch=polkadot-v1.7.2#818b74dea62974d23da75206105b97dc82df66f8" [[package]] name = "mockall" @@ -8804,7 +8804,7 @@ dependencies = [ [[package]] name = "pallet-remarks" version = "0.0.1" -source = "git+https://github.com/foss3/runtime-pallet-library?branch=polkadot-v1.7.2#8ca5efb3e1eaeb87cc2cdc1a4c3057f15b899907" +source = "git+https://github.com/foss3/runtime-pallet-library?branch=polkadot-v1.7.2#818b74dea62974d23da75206105b97dc82df66f8" dependencies = [ "frame-benchmarking", "frame-support", diff --git a/libs/mocks/src/foreign_investment_hooks.rs b/libs/mocks/src/foreign_investment_hooks.rs index a0c30845fe..a0c9edc7fd 100644 --- a/libs/mocks/src/foreign_investment_hooks.rs +++ b/libs/mocks/src/foreign_investment_hooks.rs @@ -2,7 +2,7 @@ pub mod pallet { use cfg_traits::investments::ForeignInvestmentHooks; use frame_support::pallet_prelude::*; - use mock_builder::{execute_call, register_call}; + use mock_builder::{execute_call, register_call, CallHandler}; #[pallet::config] pub trait Config: frame_system::Config { @@ -28,8 +28,8 @@ pub mod pallet { T::Amount, ) -> DispatchResult + 'static, - ) { - register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)); + ) -> CallHandler { + register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)) } pub fn mock_fulfill_collect_investment( @@ -41,8 +41,8 @@ pub mod pallet { T::TrancheAmount, ) -> DispatchResult + 'static, - ) { - register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)); + ) -> CallHandler { + register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)) } pub fn mock_fulfill_collect_redemption( @@ -54,8 +54,8 @@ pub mod pallet { T::Amount, ) -> DispatchResult + 'static, - ) { - register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)); + ) -> CallHandler { + register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e)) } } diff --git a/libs/mocks/src/token_swaps.rs b/libs/mocks/src/token_swaps.rs index 53177226df..10fce2685e 100644 --- a/libs/mocks/src/token_swaps.rs +++ b/libs/mocks/src/token_swaps.rs @@ -74,7 +74,7 @@ pub mod pallet { pub fn mock_fill_order( f: impl Fn(T::AccountId, T::OrderId, T::BalanceOut) -> DispatchResult + 'static, ) { - register_call!(move |(a, b, c)| f(a, b, c)) + register_call!(move |(a, b, c)| f(a, b, c)); } } diff --git a/libs/traits/Cargo.toml b/libs/traits/Cargo.toml index e8592f6295..59b7f6a871 100644 --- a/libs/traits/Cargo.toml +++ b/libs/traits/Cargo.toml @@ -25,9 +25,9 @@ sp-std = { workspace = true } strum = { workspace = true } [dev-dependencies] -cfg-mocks = { workspace = true, default_features = true } -frame-system = { workspace = true, default_features = true } -mock-builder = { workspace = true, default_features = true } +cfg-mocks = { workspace = true, default-features = true } +frame-system = { workspace = true, default-features = true } +mock-builder = { workspace = true, default-features = true } [features] default = ["std"] @@ -45,11 +45,14 @@ std = [ runtime-benchmarks = [ "cfg-primitives/runtime-benchmarks", "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "cfg-mocks/runtime-benchmarks", ] try-runtime = [ "frame-support/try-runtime", + "frame-system/try-runtime", "cfg-primitives/try-runtime", "sp-runtime/try-runtime", + "cfg-mocks/try-runtime", ] diff --git a/pallets/foreign-investments/src/tests.rs b/pallets/foreign-investments/src/tests.rs index 6b9b2d2b5f..dd65701618 100644 --- a/pallets/foreign-investments/src/tests.rs +++ b/pallets/foreign-investments/src/tests.rs @@ -378,7 +378,7 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |who, investment_id, curr, amount_cancelled, fulfilled| { assert_eq!(*who, USER); assert_eq!(investment_id, INVESTMENT_ID); @@ -394,6 +394,7 @@ mod investment { INVESTMENT_ID, FOREIGN_CURR )); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -416,13 +417,14 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment(|_, _, _, _, _| Ok(())); + let handler = MockHooks::mock_fulfill_cancel_investment(|_, _, _, _, _| Ok(())); assert_ok!(ForeignInvestment::cancel_foreign_investment( &USER, INVESTMENT_ID, FOREIGN_CURR )); + assert_eq!(handler.times(), 1); assert_ok!(ForeignInvestment::increase_foreign_investment( &USER, @@ -687,13 +689,16 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment(|_, _, _, amount_cancelled, fulfilled| { - assert_eq!(amount_cancelled, AMOUNT); - assert_eq!(fulfilled, AMOUNT); - Ok(()) - }); + let handler = MockHooks::mock_fulfill_cancel_investment( + |_, _, _, amount_cancelled, fulfilled| { + assert_eq!(amount_cancelled, AMOUNT); + assert_eq!(fulfilled, AMOUNT); + Ok(()) + }, + ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(3 * AMOUNT / 4)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -724,13 +729,16 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment(|_, _, _, amount_cancelled, fulfilled| { - assert_eq!(amount_cancelled, AMOUNT); - assert_eq!(fulfilled, AMOUNT); - Ok(()) - }); + let handler = MockHooks::mock_fulfill_cancel_investment( + |_, _, _, amount_cancelled, fulfilled| { + assert_eq!(amount_cancelled, AMOUNT); + assert_eq!(fulfilled, AMOUNT); + Ok(()) + }, + ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -811,13 +819,16 @@ mod investment { util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT / 2)); - MockHooks::mock_fulfill_cancel_investment(|_, _, _, amount_cancelled, fulfilled| { - assert_eq!(amount_cancelled, AMOUNT); - assert_eq!(fulfilled, AMOUNT); - Ok(()) - }); + let handler = MockHooks::mock_fulfill_cancel_investment( + |_, _, _, amount_cancelled, fulfilled| { + assert_eq!(amount_cancelled, AMOUNT); + assert_eq!(fulfilled, AMOUNT); + Ok(()) + }, + ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT / 2)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -843,7 +854,7 @@ mod investment { util::fulfill_last_swap(Action::Investment, AMOUNT / 2); util::process_investment(foreign_to_pool(AMOUNT / 4)); - MockHooks::mock_fulfill_collect_investment( + let handler = MockHooks::mock_fulfill_collect_investment( |who, investment_id, currency, amount_collected, tranche_tokens_payout| { assert_eq!(*who, USER); assert_eq!(investment_id, INVESTMENT_ID); @@ -858,6 +869,7 @@ mod investment { ); assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -896,15 +908,10 @@ mod investment { util::fulfill_last_swap(Action::Investment, AMOUNT / 2); util::process_investment(foreign_to_pool(AMOUNT / 4)); - MockHooks::mock_fulfill_collect_investment(|_, _, _, _, _| Ok(())); + let handler = MockHooks::mock_fulfill_collect_investment(|_, _, _, _, _| Ok(())); assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID)); - - MockHooks::mock_fulfill_cancel_investment(|_, _, _, amount_cancelled, fulfilled| { - assert_eq!(amount_cancelled, 3 * AMOUNT / 4); - assert_eq!(fulfilled, 3 * AMOUNT / 4); - Ok(()) - }); + assert_eq!(handler.times(), 1); assert_ok!(ForeignInvestment::cancel_foreign_investment( &USER, @@ -912,7 +919,16 @@ mod investment { FOREIGN_CURR )); + let handler = MockHooks::mock_fulfill_cancel_investment( + |_, _, _, amount_cancelled, fulfilled| { + assert_eq!(amount_cancelled, 3 * AMOUNT / 4); + assert_eq!(fulfilled, 3 * AMOUNT / 4); + Ok(()) + }, + ); + util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT / 4)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -938,7 +954,7 @@ mod investment { util::fulfill_last_swap(Action::Investment, AMOUNT); util::process_investment(foreign_to_pool(AMOUNT)); - MockHooks::mock_fulfill_collect_investment( + let handler = MockHooks::mock_fulfill_collect_investment( |_, _, _, amount_collected, tranche_tokens_payout| { assert_eq!(amount_collected, AMOUNT); assert_eq!( @@ -949,7 +965,8 @@ mod investment { }, ); - assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID,)); + assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -982,23 +999,25 @@ mod investment { let total_foreign_collected = Arc::new(Mutex::new(0)); - for _ in 0..foreign_to_pool(AMOUNT) { - util::process_investment(1 /* pool_amount */); + let handler = MockHooks::mock_fulfill_collect_investment({ + let total_foreign_collected = total_foreign_collected.clone(); + move |_, _, _, amount_collected, _| { + // First messages returns nothing, until last messages fix the expected + // returned value. - MockHooks::mock_fulfill_collect_investment({ - let total_foreign_collected = total_foreign_collected.clone(); - move |_, _, _, amount_collected, _| { - // First messages returns nothing, until last messages fix the expected - // returned value. + *total_foreign_collected.lock().unwrap() += amount_collected; + Ok(()) + } + }); - *total_foreign_collected.lock().unwrap() += amount_collected; - Ok(()) - } - }); + const TIMES: u32 = foreign_to_pool(AMOUNT) as u32; + for _ in 0..TIMES { + util::process_investment(1 /* pool_amount */); assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID)); } + assert_eq!(handler.times(), TIMES); assert_eq!(*total_foreign_collected.lock().unwrap(), AMOUNT); }); } @@ -1025,22 +1044,24 @@ mod investment { let foreign_fulfilled = Arc::new(Mutex::new(0)); + let handler = MockHooks::mock_fulfill_collect_investment({ + let foreign_fulfilled = foreign_fulfilled.clone(); + move |_, _, _, amount_collected, _| { + *foreign_fulfilled.lock().unwrap() += amount_collected; + Ok(()) + } + }); + // Iterate all expect 1 iteration to later be able to cancel const REMAINDER: Balance = 1; - for _ in 0..foreign_to_pool(AMOUNT) - REMAINDER { + const TIMES: u32 = (foreign_to_pool(AMOUNT) - REMAINDER) as u32; + for _ in 0..TIMES { util::process_investment(1 /* pool_amount */); - MockHooks::mock_fulfill_collect_investment({ - let foreign_fulfilled = foreign_fulfilled.clone(); - move |_, _, _, amount_collected, _| { - *foreign_fulfilled.lock().unwrap() += amount_collected; - Ok(()) - } - }); - assert_ok!(MockInvestment::collect_investment(USER, INVESTMENT_ID)); } + assert_eq!(handler.times(), TIMES); assert_eq!(*foreign_fulfilled.lock().unwrap(), AMOUNT - REMAINDER); assert_eq!( @@ -1059,7 +1080,7 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment({ + let handler = MockHooks::mock_fulfill_cancel_investment({ let foreign_fulfilled = foreign_fulfilled.clone(); move |_, _, _, _, fulfilled| { *foreign_fulfilled.lock().unwrap() += fulfilled; @@ -1068,6 +1089,7 @@ mod investment { }); util::fulfill_last_swap(Action::Investment, REMAINDER); + assert_eq!(handler.times(), 1); assert_eq!(*foreign_fulfilled.lock().unwrap(), AMOUNT); }); @@ -1123,7 +1145,7 @@ mod investment { POOL_CURR )); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, foreign_to_pool(AMOUNT)); assert_eq!(fulfilled, foreign_to_pool(AMOUNT)); @@ -1137,6 +1159,7 @@ mod investment { INVESTMENT_ID, POOL_CURR )); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1181,7 +1204,7 @@ mod investment { }) }); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, AMOUNT / RATIO_CHANGE); // Receive less assert_eq!(fulfilled, AMOUNT); // The original increased amount @@ -1190,6 +1213,7 @@ mod investment { ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1228,7 +1252,7 @@ mod investment { }) }); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, AMOUNT * RATIO_CHANGE); // Receive more assert_eq!(fulfilled, AMOUNT); // The original increased amount @@ -1237,6 +1261,7 @@ mod investment { ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(AMOUNT)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1278,7 +1303,7 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, (3 * AMOUNT / 4) * MULTIPLIER + AMOUNT / 4); assert_eq!(fulfilled, AMOUNT); @@ -1287,6 +1312,7 @@ mod investment { ); util::fulfill_last_swap(Action::Investment, foreign_to_pool(3 * AMOUNT / 4)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1328,7 +1354,7 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, (3 * AMOUNT / 4) * MULTIPLIER + AMOUNT / 4); assert_eq!(fulfilled, AMOUNT); @@ -1340,6 +1366,7 @@ mod investment { Action::Investment, foreign_to_pool(3 * AMOUNT / 4) * MULTIPLIER, ); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1386,7 +1413,7 @@ mod investment { FOREIGN_CURR )); - MockHooks::mock_fulfill_cancel_investment( + let handler = MockHooks::mock_fulfill_cancel_investment( |_, _, _, amount_cancelled, fulfilled| { assert_eq!(amount_cancelled, FOREIGN_AMOUNT + 1); assert_eq!(fulfilled, FOREIGN_AMOUNT); @@ -1395,6 +1422,7 @@ mod investment { ); util::fulfill_last_swap(Action::Investment, FOREIGN_AMOUNT * 3); + assert_eq!(handler.times(), 1); assert_eq!( ForeignInvestmentInfo::::get(&USER, INVESTMENT_ID), @@ -1604,7 +1632,7 @@ mod redemption { util::fulfill_last_swap(Action::Redemption, tranche_to_pool(TRANCHE_AMOUNT / 2)); - MockHooks::mock_fulfill_collect_redemption( + let handler = MockHooks::mock_fulfill_collect_redemption( |who, investment_id, currency, tranche_tokens_collected, amount_payout| { assert_eq!(*who, USER); assert_eq!(investment_id, INVESTMENT_ID); @@ -1619,6 +1647,7 @@ mod redemption { ); util::fulfill_last_swap(Action::Redemption, tranche_to_pool(TRANCHE_AMOUNT / 4)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignRedemptionInfo::::get(&USER, INVESTMENT_ID), @@ -1653,7 +1682,7 @@ mod redemption { assert_ok!(MockInvestment::collect_redemption(USER, INVESTMENT_ID)); - MockHooks::mock_fulfill_collect_redemption( + let handler = MockHooks::mock_fulfill_collect_redemption( |_, _, _, tranche_tokens_collected, amount_payout| { assert_eq!( amount_payout, @@ -1665,6 +1694,7 @@ mod redemption { ); util::fulfill_last_swap(Action::Redemption, tranche_to_pool(TRANCHE_AMOUNT)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignRedemptionInfo::::get(&USER, INVESTMENT_ID), @@ -1692,7 +1722,7 @@ mod redemption { util::process_redemption(TRANCHE_AMOUNT); - MockHooks::mock_fulfill_collect_redemption( + let handler = MockHooks::mock_fulfill_collect_redemption( |_, _, _, tranche_tokens_collected, amount_payout| { assert_eq!(amount_payout, tranche_to_pool(TRANCHE_AMOUNT)); assert_eq!(tranche_tokens_collected, TRANCHE_AMOUNT); @@ -1702,6 +1732,7 @@ mod redemption { // Automatically "fulfills" because there no need of swapping assert_ok!(MockInvestment::collect_redemption(USER, INVESTMENT_ID)); + assert_eq!(handler.times(), 1); assert_eq!( ForeignRedemptionInfo::::get(&USER, INVESTMENT_ID), diff --git a/pallets/restricted-tokens/Cargo.toml b/pallets/restricted-tokens/Cargo.toml index 720e047365..020f897055 100644 --- a/pallets/restricted-tokens/Cargo.toml +++ b/pallets/restricted-tokens/Cargo.toml @@ -33,11 +33,11 @@ pallet-balances = { workspace = true, optional = true } pallet-permissions = { workspace = true, optional = true } [dev-dependencies] -cfg-types = { workspace = true, default_features = true } -orml-tokens = { workspace = true, default_features = true } -orml-traits = { workspace = true, default_features = true } -pallet-balances = { workspace = true, default_features = true } -sp-io = { workspace = true, default_features = true } +cfg-types = { workspace = true, default-features = true } +orml-tokens = { workspace = true, default-features = true } +orml-traits = { workspace = true, default-features = true } +pallet-balances = { workspace = true, default-features = true } +sp-io = { workspace = true, default-features = true } [features] default = ["std"] diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 90c138f9e4..823acb7caa 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -126,7 +126,7 @@ staging-parachain-info = { workspace = true } frame-benchmarking = { workspace = true, optional = true } [dev-dependencies] -cfg-mocks = { workspace = true } +cfg-mocks = { workspace = true, default-features = true } [features] default = ["std"] @@ -172,7 +172,6 @@ std = [ "cfg-traits/std", "cfg-types/std", "cfg-utils/std", - "cfg-mocks/std", # Pallet exporting list "axelar-gateway-precompile/std", "chainbridge/std",