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

Liquidity-pools: Add unitary tests #1879

Merged
merged 17 commits into from
Jun 25, 2024
Merged

Liquidity-pools: Add unitary tests #1879

merged 17 commits into from
Jun 25, 2024

Conversation

lemunozm
Copy link
Contributor

@lemunozm lemunozm commented Jun 19, 2024

Description

We didn't have UTs for the liquidity pool. Although the Integration Tests cover more or less the basics of liquidity-pools, having those checks there makes Integration Tests more boilerplate. We can simplify some tests if the basics are moved to the unitary tests.

Changes:

  • Some cleanings / simplifications
  • Added UT tests to check all errors when transferring
  • Removed liquidity::gateway tests, which are correctly checked in UTs.
  • Removed IT utils::democracy which are no longer needed (see comment below)
  • Removed liquidity_pools::transfer covered in UTs.

BLOCKED BY #1869

@lemunozm lemunozm added the I4-tests Test needs fixing or improving. label Jun 19, 2024
@lemunozm lemunozm self-assigned this Jun 19, 2024
@lemunozm lemunozm changed the base branch from main to it/simplify-xtransfer-tests June 19, 2024 06:49
Comment on lines -92 to -105
impl Convert<(Domain, [u8; 32]), AccountId32> for AccountConverter {
fn convert((domain, account): (Domain, [u8; 32])) -> AccountId32 {
match domain {
Domain::Centrifuge => AccountId32::new(account),
// EVM AccountId20 addresses are right-padded to 32 bytes
Domain::EVM(chain_id) => {
let mut bytes20 = [0; 20];
bytes20.copy_from_slice(&account[..20]);
Self::convert_evm_address(chain_id, bytes20)
}
}
}
}

Copy link
Contributor Author

@lemunozm lemunozm Jun 19, 2024

Choose a reason for hiding this comment

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

We can implement this converter as a composition of the above two converters. This is also valid for liquidity pools, and we do not need to pass the extra Convert as a trait to the Config.

Comment on lines +69 to +72
pub fn domain_account_to_account(domain: Domain, account_id: AccountId) -> AccountId {
let domain_address = Self::convert((domain, account_id.into()));
Self::convert(domain_address)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A utility is still here for testing or other stuff

@@ -61,10 +61,15 @@ impl AccountConverter {
}
}

pub fn into_account_id<R: pallet_evm_chain_id::Config>(address: H160) -> AccountId {
pub fn evm_to_account<R: pallet_evm_chain_id::Config>(address: H160) -> AccountId {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified the name (open to other names that match better) for two reasons:

  • The into_* names are usually used when the own type is converted into something, not when the thing to convert is used as a parameter. The name into_account_id() was inherited from the name used by the trait AddressMapping, not chosen by us. Anyways, the into_account_id() is still accessible through the trait.
  • The new name follows the format: FROM_to_TO, as other methods:
    • location_to_account()
    • domain_account_to_account()

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - evm_address_to_account_id ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like it more!

@@ -1,276 +0,0 @@
use std::ops::Add;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed this file because:

Anyways, I am open to discussing this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with you!

@lemunozm lemunozm marked this pull request as ready for review June 19, 2024 11:53
ForeignInvestment: cfg_mocks::foreign_investment::pallet,
Gateway: cfg_mocks::outbound_queue::pallet,
DomainAddressToAccountId: cfg_mocks::converter::pallet::<Instance1>,
DomainAccountToDomainAddress: cfg_mocks::converter::pallet::<Instance3>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have to use Instance3 here? IIRC we used maximum 2 instances in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! I had an Instance2 before. I will change it! Good eye 😆

cdamian
cdamian previously approved these changes Jun 19, 2024
Copy link
Contributor

@cdamian cdamian left a comment

Choose a reason for hiding this comment

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

Ultra-clean, as always :D Thank you @lemunozm!

Comment on lines -396 to -400
ensure!(
T::PoolInspect::tranche_exists(pool_id, tranche_id),
Error::<T>::TrancheNotFound
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already checked under derive_invest_id() to create the investment_id

@@ -1097,25 +1068,3 @@ pub mod pallet {
}
}
}

#[cfg(test)]
mod tests {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to domain_address.rs tests

Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 30.00000% with 49 lines in your changes missing coverage. Please review.

Project coverage is 47.65%. Comparing base (ee8fc69) to head (c3a56ef).

Files Patch % Lines
libs/mocks/src/foreign_investment.rs 0.00% 25 Missing ⚠️
pallets/liquidity-pools/src/lib.rs 26.31% 14 Missing ⚠️
runtime/common/src/account_conversion.rs 0.00% 5 Missing ⚠️
pallets/liquidity-pools/src/inbound.rs 0.00% 3 Missing ⚠️
pallets/liquidity-pools/src/hooks.rs 0.00% 1 Missing ⚠️
runtime/common/src/gateway.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1879      +/-   ##
==========================================
+ Coverage   46.81%   47.65%   +0.83%     
==========================================
  Files         170      171       +1     
  Lines       13221    13233      +12     
==========================================
+ Hits         6190     6306     +116     
+ Misses       7031     6927     -104     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

wischli
wischli previously approved these changes Jun 24, 2024
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

Great improvement! Loving the clean distinction between all test scenarios instead of the previous huge blocks. I have two minor nits which can be ignored.

Comment on lines +106 to +115
assert_ok!(LiquidityPools::transfer(
RuntimeOrigin::signed(ALICE),
CurrencyId::ForeignAsset(1),
EVM_ADDRESS,
AMOUNT
));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Please add the previously existing check that the balance of the sender and total issuance of the assed was reduced by AMOUNT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I removed that check because it's checking that orml_tokens is correctly working (which is out of scope from this UT test), but thinking again, it makes sense to check that actually this method calls correctly to orml_tokens, with the correct amount. So, will do 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point BTW

Comment on lines 309 to 315
assert_ok!(LiquidityPools::transfer_tranche_tokens(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS,
AMOUNT
),);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Same as above: Can we add a check which ensures the balance was altered correctly on the sender and the domain account?

Copy link
Contributor Author

@lemunozm lemunozm Jun 24, 2024

Choose a reason for hiding this comment

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

Done! I've also simplified in the process some minor thing regarding this: ebdebbd

Generally, the Convert trait is quite obscure when you have different conversions, and in that case, we didn't need it.

@lemunozm lemunozm changed the base branch from it/simplify-xtransfer-tests to main June 24, 2024 19:14
@lemunozm lemunozm dismissed stale reviews from wischli and cdamian June 24, 2024 19:14

The base branch was changed.

@lemunozm lemunozm enabled auto-merge (squash) June 25, 2024 05:35
@lemunozm
Copy link
Contributor Author

@wischli @wischli, ready for your re-approvals and get this in 🙏🏻

Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

Thanks for adhering to my nits!

@lemunozm lemunozm merged commit 47384e9 into main Jun 25, 2024
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I4-tests Test needs fixing or improving.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants