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

Loans: Integration tests #1600

Merged
merged 12 commits into from
Oct 27, 2023
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions libs/types/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ impl<const DIV: u128> FixedU128<DIV> {
pub const fn from_inner(inner: u128) -> Self {
Self(inner)
}

/// const version of `FixedPointNumber::saturating_from_integer`.
pub const fn from_integer(n: u128) -> Self {
Self::from_inner(n * DIV)
}
}

impl<const DIV: u128> Saturating for FixedU128<DIV> {
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ package policy {
}

enum WriteOffTrigger {
PrincipalOverdueDays,
PrincipalOverdue,
PriceOutdated,
}

Expand Down
8 changes: 4 additions & 4 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,16 @@ impl<T: Config> ActiveLoan<T> {
#[scale_info(skip_type_params(T))]
pub struct ActiveLoanInfo<T: Config> {
/// Related active loan
active_loan: ActiveLoan<T>,
pub active_loan: ActiveLoan<T>,

/// Present value of the loan
present_value: T::Balance,
pub present_value: T::Balance,

/// Current outstanding principal of this loan
outstanding_principal: T::Balance,
pub outstanding_principal: T::Balance,

/// Current outstanding interest of this loan
outstanding_interest: T::Balance,
pub outstanding_interest: T::Balance,
}

impl<T: Config> TryFrom<(T::PoolId, ActiveLoan<T>)> for ActiveLoanInfo<T> {
Expand Down
1 change: 1 addition & 0 deletions runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ orml-asset-registry = { git = "https://github.com/open-web3-stack/open-runtime-m
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-oracle = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }

# Misc
xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "754f3b90ecc65af735a6c9a2e1792c5253926ff6" }
Expand Down
23 changes: 23 additions & 0 deletions runtime/integration-tests/src/generic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Runtime Generic tests

The aim of this module is to make integration-tests independently for all runtimes at once.

You can choose the environment for each of your use cases:
- `RuntimeEnv`: Simple environment that acts as a wrapper over the runtime
- `FudgeEnv`: Advanced environment that use a client and connect the runtime to a relay chain.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
- `FudgeEnv`: Advanced environment that use a client and connect the runtime to a relay chain.
- `FudgeEnv`: Advanced environment that uses a client and connects the runtime to a relay chain.


Both environment uses the same interface so jumping from one to the another should be something "smooth".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
Both environment uses the same interface so jumping from one to the another should be something "smooth".
Both environment use the same interface, so jumping from one to the another should be somewhat "smooth".


## Where I start?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
## Where I start?
## Where do I start?

- Create a new file in `cases/<file.rs>` for the use case you want to test.
- Maybe you need to update the `Runtime` trait in `config.rs` file with extra information from your new pallet.
This could imply:
- Adding bounds to the `Runtime` trait with your new pallet.
- Adding bounds to `T::RuntimeCallExt` to support calls from your pallet.
- Adding bounds to `T::EventExt` to support events from your pallet.
- Adding bounds to `T::Api` to support new api calls.
- You can add `GenesisBuild` builders for setting the initial state of your pallet for others in `utils/genesis.rs`.
Please be as generic and simple as possible to leave others to compose its own requirement using your method,
without hidden initializations.
- You can add any utility that helps to initialize states for others under `utils` folder.
Again, focus in simplity but without side effects or hidden / non-obvious state changes.
21 changes: 18 additions & 3 deletions runtime/integration-tests/src/generic/cases/example.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use cfg_primitives::{Balance, CFG};
use cfg_primitives::{Balance, CFG, SECONDS_PER_YEAR};
use cfg_traits::IntoSeconds;
use frame_support::traits::Get;
use sp_api::runtime_decl_for_Core::CoreV4;

use crate::{
generic::{
environment::{Blocks, Env},
config::Runtime,
env::{Blocks, Env},
envs::{
fudge_env::{FudgeEnv, FudgeSupport},
runtime_env::RuntimeEnv,
},
runtime::Runtime,
utils::genesis::Genesis,
},
utils::accounts::Keyring,
Expand Down Expand Up @@ -156,7 +157,21 @@ fn fudge_call_api<T: Runtime + FudgeSupport>() {
})
}

fn pass_time_one_block<T: Runtime>() {
let mut env = RuntimeEnv::<T>::from_storage(Default::default());

let before = env.state(|| pallet_timestamp::Pallet::<T>::get());

// Not supported in fudge
env.pass(Blocks::JumpBySeconds(SECONDS_PER_YEAR));

let after = env.state(|| pallet_timestamp::Pallet::<T>::get());

assert_eq!((after - before).into_seconds(), SECONDS_PER_YEAR)
}

crate::test_for_runtimes!([development, altair, centrifuge], transfer_balance);
Comment on lines +160 to +172
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdamian I need your fudge wisdom on this.

What I'm doing with the JumpBySeconds in the RuntimeEnv is jumping to a block in the future where it has passed SECONDS_PER_YEAR. This works quite well to "emulate" a state in the future, only creating one block. It's not as accurate as passing all blocks equivalent to a year, but it's much much faster and unlocks these tests for CI.

Do you think we can modify fudge to add this behavior, jumping to a block in the future without computing the intermediate blocks?

cc @mustermeiszer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for missing this. I don't think that will be feasible given our evolve logic, but I'm still lacking knowledge on the topic. @mustermeiszer Might know more.

crate::test_for_runtimes!(all, call_api);
crate::test_for_runtimes!(all, fudge_transfer_balance);
crate::test_for_runtimes!(all, fudge_call_api);
crate::test_for_runtimes!(all, pass_time_one_block);
Loading
Loading