Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForeignInvestments: Unitary tests & fuzzer #1509

Merged
merged 34 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
be75091
base mock file
lemunozm Aug 20, 2023
1b6c7ff
configure mock pallet with placeholders
lemunozm Aug 20, 2023
b4a450e
add InvestmentId mock
lemunozm Aug 20, 2023
9342e75
add Investment mock
lemunozm Aug 20, 2023
5968fef
add TokenSwaps mock
lemunozm Aug 20, 2023
edbfc8f
add StatusNotificationHook mock. mock-builder support for instanciabl…
lemunozm Aug 20, 2023
2f6a7b6
add CurrencyConversion mock
lemunozm Aug 21, 2023
0511578
example test compiling with foreign mock
lemunozm Aug 21, 2023
a15347c
simplify mock reducing bounds
lemunozm Aug 21, 2023
1260cc3
add base new invest test case
lemunozm Aug 21, 2023
a5cb594
finish new invest use case
lemunozm Aug 21, 2023
93cc744
add increase_pending_invest
lemunozm Aug 22, 2023
8898cae
add more tests
lemunozm Aug 25, 2023
6bef0e3
updates
lemunozm Aug 28, 2023
79ddb65
update test after rebase
lemunozm Aug 30, 2023
5bb12b7
prepare database for State transition UTs
lemunozm Aug 30, 2023
0a2d5e0
add increment transition state UTs
lemunozm Aug 30, 2023
0a68890
minor test changes
lemunozm Aug 31, 2023
72baee7
add fuzzier skeleon
lemunozm Aug 31, 2023
cb09d5b
reduce unused transitions for fuzzer
lemunozm Aug 31, 2023
e585c0c
fix taplo
lemunozm Aug 31, 2023
5246f09
rebase & fix tests
lemunozm Sep 12, 2023
d3db28f
fuzzer drafts
lemunozm Sep 12, 2023
ee6fc83
fix mocks
lemunozm Sep 13, 2023
85fc8a6
base fuzzer working
lemunozm Sep 15, 2023
f154253
improve randomness and fix test
lemunozm Sep 15, 2023
106b9ad
fuzzer fixes
lemunozm Sep 18, 2023
fb10df2
changes over last rebase
lemunozm Sep 18, 2023
63e7feb
completed invest fuzzer
lemunozm Sep 18, 2023
49e2a6c
redeem fuzzer working
lemunozm Sep 18, 2023
289991c
remove mock builder artifact
lemunozm Sep 18, 2023
937f9f2
minor fixes
lemunozm Sep 18, 2023
542e5ce
fix comments
lemunozm Sep 21, 2023
5208d07
Merge with last foreign investment changes of #1556
lemunozm Sep 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions libs/mocks/src/currency_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::IdentityCurrencyConversion;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Balance;
type CurrencyId;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config> Pallet<T> {
pub fn mock_stable_to_stable(
f: impl Fn(T::CurrencyId, T::CurrencyId, T::Balance) -> Result<T::Balance, DispatchError>
+ 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
}

impl<T: Config> IdentityCurrencyConversion for Pallet<T> {
type Balance = T::Balance;
type Currency = T::CurrencyId;
type Error = DispatchError;

fn stable_to_stable(
a: Self::Currency,
b: Self::Currency,
c: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
execute_call!((a, b, c))
}
}
}
160 changes: 160 additions & 0 deletions libs/mocks/src/investment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::investments::{Investment, InvestmentCollector};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Amount;
type CurrencyId;
type InvestmentId;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config> Pallet<T> {
pub fn mock_update_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_accepted_payment_currency(
f: impl Fn(T::InvestmentId, T::CurrencyId) -> bool + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_investment(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::Amount, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_update_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_accepted_payout_currency(
f: impl Fn(T::InvestmentId, T::CurrencyId) -> bool + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::Amount, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_collect_investment(
f: impl Fn(T::AccountId, T::InvestmentId) -> Result<(), DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_collect_redemption(
f: impl Fn(T::AccountId, T::InvestmentId) -> Result<(), DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_investment_requires_collect(
f: impl Fn(&T::AccountId, T::InvestmentId) -> bool + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_redemption_requires_collect(
f: impl Fn(&T::AccountId, T::InvestmentId) -> bool + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}
}

impl<T: Config> Investment<T::AccountId> for Pallet<T> {
type Amount = T::Amount;
type CurrencyId = T::CurrencyId;
type Error = DispatchError;
type InvestmentId = T::InvestmentId;

fn update_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn accepted_payment_currency(a: Self::InvestmentId, b: Self::CurrencyId) -> bool {
execute_call!((a, b))
}

fn investment(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Amount, Self::Error> {
execute_call!((a, b))
}

fn update_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn accepted_payout_currency(a: Self::InvestmentId, b: Self::CurrencyId) -> bool {
execute_call!((a, b))
}

fn redemption(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Amount, Self::Error> {
execute_call!((a, b))
}

fn investment_requires_collect(a: &T::AccountId, b: T::InvestmentId) -> bool {
execute_call!((a, b))
}

fn redemption_requires_collect(a: &T::AccountId, b: T::InvestmentId) -> bool {
execute_call!((a, b))
}
}

impl<T: Config> InvestmentCollector<T::AccountId> for Pallet<T> {
type Error = DispatchError;
type InvestmentId = T::InvestmentId;
type Result = ();

fn collect_investment(
a: T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Result, Self::Error> {
execute_call!((a, b))
}

fn collect_redemption(
a: T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Result, Self::Error> {
execute_call!((a, b))
}
}
}
8 changes: 8 additions & 0 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
mod change_guard;
mod currency_conversion;
mod data;
mod fees;
mod investment;
mod liquidity_pools;
mod liquidity_pools_gateway_routers;
mod permissions;
mod pools;
mod rewards;
mod status_notification;
mod time;
mod token_swaps;
mod try_convert;
mod write_off_policy;

pub use change_guard::pallet_mock_change_guard;
pub use currency_conversion::pallet as pallet_mock_currency_conversion;
pub use data::pallet as pallet_mock_data;
pub use fees::pallet as pallet_mock_fees;
pub use investment::pallet as pallet_mock_investment;
pub use liquidity_pools::{pallet as pallet_mock_liquidity_pools, MessageMock};
pub use liquidity_pools_gateway_routers::{pallet as pallet_mock_routers, RouterMock};
pub use permissions::pallet as pallet_mock_permissions;
pub use pools::pallet as pallet_mock_pools;
pub use rewards::pallet as pallet_mock_rewards;
pub use status_notification::pallet as pallet_mock_status_notification;
pub use time::pallet as pallet_mock_time;
pub use token_swaps::pallet as pallet_mock_token_swaps;
pub use try_convert::pallet as pallet_mock_try_convert;
pub use write_off_policy::pallet as pallet_mock_write_off_policy;

Expand Down
40 changes: 40 additions & 0 deletions libs/mocks/src/status_notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::StatusNotificationHook;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call_instance, register_call_instance};

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
type Id;
type Status;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T, I = ()>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn mock_notify_status_change(f: impl Fn(T::Id, T::Status) -> DispatchResult + 'static) {
register_call_instance!(move |(a, b)| f(a, b));
}
}

impl<T: Config<I>, I: 'static> StatusNotificationHook for Pallet<T, I> {
type Error = DispatchError;
type Id = T::Id;
type Status = T::Status;

fn notify_status_change(a: Self::Id, b: Self::Status) -> DispatchResult {
execute_call_instance!((a, b))
}
}
}
Loading