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

Test accounts logic in mina_base. #12805

Merged
merged 16 commits into from
Apr 5, 2023
Merged

Conversation

Sventimir
Copy link
Contributor

Explain your changes:

  • Add unit test for account logic

Explain how you tested your changes:

  • Run in CI.

Checklist:

  • Document code purpose, how to use it
    • Mention expected invariants, implicit constraints
  • All tests pass (CI will check this if you didn't)
  • Does this close issues? List them

@Sventimir Sventimir requested a review from a team as a code owner March 10, 2023 13:23
@Sventimir Sventimir self-assigned this Mar 10, 2023
@Sventimir Sventimir changed the title Sventimir/test accounts Test accounts logic in mina_base. Mar 10, 2023
@Sventimir
Copy link
Contributor Author

I notice that GitHub allows me to merge this PR even though there's no review and the CI is failing. I guess this is likely a problem in the CODEOWNERS file…

@Sventimir Sventimir force-pushed the sventimir/test-zkapp-command-logic2 branch from 3701154 to 93ce5a2 Compare March 20, 2023 07:56
@Sventimir Sventimir force-pushed the sventimir/test-zkapp-command-logic2 branch from 93ce5a2 to ff56f0b Compare March 22, 2023 09:43
@Sventimir Sventimir force-pushed the sventimir/test-accounts branch 2 times, most recently from e22e047 to fa6745a Compare March 22, 2023 09:46
@Sventimir Sventimir requested a review from a team as a code owner March 22, 2023 09:46
@Sventimir Sventimir force-pushed the sventimir/test-zkapp-command-logic2 branch from ff56f0b to 5eb3655 Compare March 22, 2023 09:46
@Sventimir
Copy link
Contributor Author

!ci-build-me

Copy link
Member

@nholland94 nholland94 left a comment

Choose a reason for hiding this comment

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

I see that none of the quickcheck tests have specified the number of trials. The number of trials defaults to 10k. This is probably fine for these tests, as I would expect them to be fast, but sometimes the generators can be slow. Have you measured the amount of time it takes to run these new tests?

|> Balance.to_amount |> Amount.to_uint64
in
UInt64.Infix.(min_balance_at_start_slot - min_balance_at_end_slot)
if Global_slot.(end_slot <= start_slot) then UInt64.zero
Copy link
Member

Choose a reason for hiding this comment

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

When would this function ever be called with such arguments? Depending on context, I would almost prefer to "fail fast" in this case and raise an error, as miscomputing this value as 0 by passing in the parameters backwards could introduce logical errors to the account model.

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 think I have accidentally called it with reversed arguments and observed an overflow, which is obviously bad. Hence I added a specific test, which verifies that the function behaves correctly in this case. What the correct behaviour is, is debatable. I could change it to return an option, although I think it makes sense to keep the types simple. In a sense, 0 is a correct result, because over 0 blocks, the incremental balance is always 0.

If someone accidentally calls it with end_slot smaller than start_slot in the wild, it certainly means things have gone wrong somewhere else. I don't really like the idea that I have to check this function for correct result (Ok or Some or whatever) just because it could potentially be called backawrds. The caller should validate the arguments before calling the function, not afterwards. Having them pattern match on the result is punishing them for doing their job and complicating the code for the reader as well. I'd rather avoid that. I think it goes without saying that raising an exception is even worse, because it could crash a node in the middle of block production.

src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
@@ -0,0 +1,31 @@
(library
(name account_test)
Copy link
Member

Choose a reason for hiding this comment

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

It might be a good idea to namespace this (library names must be globally unique). A common pattern for doing this would be to call this library mina_base.account_test. We don't do this everywhere now, but I'm trying to do this more because sometimes we hit conflicting library names, and dune generates unhelpful error messages when this happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

File "src/lib/mina_base/test/account/dune", line 2, characters 7-29:
2 |  (name mina_base.account_test)
           ^^^^^^^^^^^^^^^^^^^^^^
Error: "mina_base.account_test" is an invalid library name.
Library names must be non-empty and composed only of the following
characters: 'A'..'Z', 'a'..'z', '_' or '0'..'9'.
Hint: mina_base_account_test would be a correct library name

Would mina_base_account_test be okay? Sounds a lot less appealing to me, but I guess it's better than nothing…

Copy link
Member

Choose a reason for hiding this comment

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

Apologies for the confusion. It's a bit odd in dune because there is a difference between the local name of a library, and the public name. The local name can't have ., but the public one can. Here is an example of a library that uses this namespacing for the public name while setting a local name that just has an underscore in its place. It reads odd, but this does have the desired effect of fixing the global namespacing problem. https://github.com/MinaProtocol/mina/blob/develop/src/lib/mina_metrics/no_metrics/dune

src/lib/mina_base/test/account/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/test/account/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/test/account/account.ml Show resolved Hide resolved
src/lib/mina_base/test/account/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/test/account/account.ml Outdated Show resolved Hide resolved
@Sventimir
Copy link
Contributor Author

I see that none of the quickcheck tests have specified the number of trials. The number of trials defaults to 10k. This is probably fine for these tests, as I would expect them to be fast, but sometimes the generators can be slow. Have you measured the amount of time it takes to run these new tests?

These tests compile and run in a little over a minute on my laptop. I think it's quick enough, isn't it?

@Sventimir
Copy link
Contributor Author

!ci-build-me

@mergify mergify bot mentioned this pull request Apr 4, 2023
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/test/account/account.ml Outdated Show resolved Hide resolved
@ejMina226
Copy link
Member

!ci-build-me

src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
src/lib/mina_base/account.ml Outdated Show resolved Hide resolved
@gltrost gltrost requested a review from nholland94 April 4, 2023 18:24
@gltrost gltrost requested a review from a team as a code owner April 4, 2023 19:22
@gltrost gltrost changed the base branch from sventimir/test-zkapp-command-logic2 to berkeley April 4, 2023 19:22
@gltrost gltrost requested a review from nholland94 April 4, 2023 19:24
@gltrost gltrost force-pushed the sventimir/test-accounts branch 2 times, most recently from 13c9c04 to 41842f4 Compare April 4, 2023 19:41
@nholland94 nholland94 merged commit cdaec78 into berkeley Apr 5, 2023
@nholland94 nholland94 deleted the sventimir/test-accounts branch April 5, 2023 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants