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

ForeignInvestments: Unitary tests & fuzzer #1509

merged 34 commits into from
Sep 26, 2023

Conversation

lemunozm
Copy link
Contributor

@lemunozm lemunozm commented Aug 20, 2023

Description

Unitary tests for foreign investments

Changes and Descriptions

  • Mock files
  • UTs with mocks. It seems like this path is never-ending and collides with integration tests.
  • UTs for states. It seems like this path is never-ending and very difficult to maintain if future changes are made.
  • Fuzzer. Fix the above problem with programmatic testing in order to check all cases at once with fewer lines.

@lemunozm lemunozm added I4-tests Test needs fixing or improving. P5-soon Issue should be addressed soon. crcl-protocol Circle protocol related. labels Aug 20, 2023
@lemunozm lemunozm requested a review from wischli August 20, 2023 06:04
@lemunozm lemunozm self-assigned this Aug 20, 2023
@lemunozm lemunozm force-pushed the fi/unitary-tests branch 2 times, most recently from 5075dc1 to 8012557 Compare August 24, 2023 10:22
@lemunozm lemunozm mentioned this pull request Aug 25, 2023
42 tasks
@lemunozm lemunozm force-pushed the fi/unitary-tests branch 2 times, most recently from 85f8aaf to 46e922a Compare August 30, 2023 06:06
@lemunozm
Copy link
Contributor Author

lemunozm commented Aug 30, 2023

@wischli I've needed to tweak a bit the InvestStateConfig trait here: c12b203.

I missed the fact that auto implementations for any T do not allow creating a different implementation later for any other type, so we would still need Config in tests, and then we are not taking advantage of such a new trait. So, I've added a new type Of<T> (that emulates the jargon we use when a type uses T), that will be used only for production and implements InvestStateConfig instead of doing it for any T. That way, we can create other types with other implementations for testing.

Tell me WDYT and if you prefer this change in your PR

@wischli
Copy link
Contributor

wischli commented Aug 30, 2023

@wischli I've needed to tweak a bit the InvestStateConfig trait here: c12b203.

I missed the fact that auto implementations for any T do not allow creating a different implementation later for any other type, so we would still need Config in tests, and then we are not taking advantage of such a new trait. So, I've added a new type Of<T> (that emulates the jargon we use when a type uses T), that will be used only for production and implements InvestStateConfig instead of doing it for any T. That way, we can create other types with other implementations for testing.

Tell me WDYT and if you prefer this change in your PR

I suppose it will make maintaining sync between this PR and mine much easier for you? In that case, I would add to to mine as well. Otherwise, we can also leave it untouched in #1473.

@lemunozm
Copy link
Contributor Author

If the intention is to merge your PR today, and no changes there are expected, we can leave it as it is. If you find that you need to make some "big" change, then I think we can put this change just before. By now, we can leave it here 👍🏻

@lemunozm
Copy link
Contributor Author

Just as a thought (mostly to myself).

Given the number of state combinations we have, maybe the best way to test this is to extract the invariant laws of this (i.e. the amount of money in the system must remain the same, independently of how many swaps are done) and create a fuzzier ensuring the invariant is always fulfilled for all combinations.

@lemunozm lemunozm force-pushed the fi/unitary-tests branch 2 times, most recently from ddeff07 to ee83285 Compare September 13, 2023 11:39
@lemunozm lemunozm changed the base branch from feat/wf-foreign-investments to main September 18, 2023 07:02
@lemunozm lemunozm marked this pull request as ready for review September 18, 2023 07:03
@lemunozm lemunozm changed the title ForeignInvestments: Unitary tests ForeignInvestments: Unitary tests & fuzzer Sep 18, 2023
wischli
wischli previously approved these changes Sep 18, 2023
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.

Super clean and elegant code as always! Thanks for complementing the existing integration tests with valuable fuzz tests plus providing mock skeletons.

I have three super minor nits which can be ignored. We could merge this PR right now or wait until RedeemState fuzzing is implemented. Totally fine with doing this in a subsequent PR.

pallets/foreign-investments/src/impls/invest.rs Outdated Show resolved Hide resolved
pallets/foreign-investments/src/impls/invest.rs Outdated Show resolved Hide resolved
pallets/foreign-investments/src/impls/redeem.rs Outdated Show resolved Hide resolved
pallets/foreign-investments/src/impls/redeem.rs Outdated Show resolved Hide resolved
@lemunozm
Copy link
Contributor Author

plus providing mock skeletons.

My first intention was to create individual tests using mocks, but I realized that testing for all transitions could take me months, because I need to evaluate each parameter of each mocked call, and most of what I would check is already covered by integration tests. Anyway, it still can be seen as a future work (or used by other pallets), that's why I leave the mock skeletons there.

@cdamian
Copy link
Contributor

cdamian commented Sep 19, 2023

Awesome stuff @lemunozm!

@lemunozm
Copy link
Contributor Author

Updated with last changes of #1556. Should be ready to be merged now

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.

Re-approving!

@lemunozm lemunozm enabled auto-merge (squash) September 26, 2023 08:07
@lemunozm lemunozm merged commit ccb6e2e into main Sep 26, 2023
10 checks passed
@lemunozm lemunozm deleted the fi/unitary-tests branch September 26, 2023 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crcl-protocol Circle protocol related. I4-tests Test needs fixing or improving. P5-soon Issue should be addressed soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants