Skip to content

Conversation

jonatack
Copy link
Member

@jonatack jonatack commented Jan 21, 2023

  • Move random test utilities from setup_common to a new random file, as many tests don't use this code.

  • Create a helper to generate semi-random CAmounts up to MONEY_RANGE rather than only uint32, and use the helper in the unit tests.

  • De-duplicate a shared add_coin method by extracting it to a coins test utility.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 21, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK john-moffett, pinheadmz, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26817 (doc: Bump copyright years to present (headers only) by MarcoFalke)
  • #25325 (Add pool based memory resource by martinus)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot DrahtBot added the Tests label Jan 21, 2023
@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch from 43c7ff8 to 3cd4609 Compare January 21, 2023 01:01
@jonatack jonatack marked this pull request as ready for review January 21, 2023 02:06
Copy link
Contributor

@john-moffett john-moffett left a comment

Choose a reason for hiding this comment

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

utACK 3cd4609

I like the idea of having more intelligent "random" data that includes edge cases a significant portion of the time.

@jonatack jonatack changed the title test: add and use a helper for generating semi-random CAmounts test: Move rand utils from setup_common to random and add a helper Jan 22, 2023
@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch 3 times, most recently from 884a5f8 to 4e5bf23 Compare January 22, 2023 19:46
@jonatack
Copy link
Member Author

Updated with feedback from @john-moffett and @fanquake (thanks!)

@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch 2 times, most recently from bc24055 to 4c3d011 Compare January 22, 2023 22:32
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

No objection, but not seeing the benefit over a fuzz test either?

@jonatack jonatack changed the title test: Move rand utils from setup_common to random and add a helper test: create random and coins utils, add amount helper, dedupe add_coin Feb 1, 2023
@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch 2 times, most recently from 632a581 to 0196907 Compare February 1, 2023 18:05
@jonatack
Copy link
Member Author

jonatack commented Feb 1, 2023

Rebased, squashed the money helper changes to one commit, and de-duplicated a shared add_coin method by extracting it to a test utility.

@pinheadmz
Copy link
Member

concept ACK
tested and reviewed, I like the deliberate introduction of edge cases into the tests and certainly makes sense to refactor out AddTestCoin() since it is duplicated. I'm not entirely sure about the move of all the random functions is a net benefit, you had to include that in 33 files when those functions were already sitting nicely in setup_common

@pinheadmz
Copy link
Member

ACK d34bc34

The location of the random functions isn't a blocker for me.

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK d34bc34c8c69781c913faa3e572af8030d099551
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmPhNmYACgkQ5+KYS2KJ
yTqGYA//cUrRxVOTho65ZbiTMKbev32/NMPI4aTt+j4Jl3UtSt96kqlBgSYdGftC
1V4B6pj2efxjFEuGQd8n1pzUt5pm8bkdg1r6JfVjEFNaz/ZqzagJS/93RU4/+3Ui
ffYt1jLI55ws2iy/owStLNmWqyRmh2YazTRPb+h1YAQ8FsuyIlpz75HJnKORsoA4
gcZwJ84YxSPaYYyCLpQ/++0PcofcR7q2CFidnZ8Dlcc3LwNSbdEk7zOwSkdAbT51
KIjIWH8Zh0LAgMbWCyXnmf1er3IdnCW/DSczQRqqbr+Mpn+0iQvyKsfqS+wJMOFj
Yjjg7aZ8NBx4nCw9mK+Ey3/XO1oAqcNVjRJinTKW7I6WhxShBY2vr9sF9ttX4nSS
7mEQf6iYRkCPCkRDrgTCtdNMzXtVQQRwZm+vN9w5Rs7AHz7SBJFDCpxC2hnMa1ij
FkowcK/FW1/LYYTceJmvwwbP5UuM5JN6z8j4DE6m/wztkiAOM0JSBe6IushlPplT
xeByjBkj3HEUoRStd4bZhZindIDplqHnzBEbqMuJFY8rLXrN2Vr6pP4esuUHlmh4
Nnm7In2py8RABm5sjeMugb5ElZR+zyDwnUElfqhwQf7iVKTqBfV+Jw05dH0Xmh9l
dM/qOScbDnDlg1IaIkHvFTkJSTirdb5pwalG5dg1i+rjsYeDoxw=
=2W5E
-----END PGP SIGNATURE-----

pinheadmz's public key is on keybase

as many of the unit tests don't use this code
@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch from d34bc34 to e6de5ad Compare February 6, 2023 20:44
@jonatack
Copy link
Member Author

jonatack commented Feb 6, 2023

Trivial rebase per git range-diff 52ddbd5 d34bc34 e6de5ad for the merge of #26345. Thank you for the ACKs, @john-moffett and @pinheadmz, mind re-ACKing?

I don't have a strong opinion on extracting the random utils to a helper. As most of the test files don't need them to be a part of their compilation unit, it might be beneficial to speed up builds a bit, and it provides clearer code organization. OTOH it's more to review so am happy also to leave them in setup_common or extract only the new helper.

@pinheadmz
Copy link
Member

ACK e6de5ad

verified rebase change is minimal, also double checked that all add_coin() calls were updated in the unit tests

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK e6de5ad8dce4fea053f75de5ee0189ed5f91df41
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmPic+UACgkQ5+KYS2KJ
yToqeA//Uf61CfE9JSLcqfWT2IOu2J7AsFKY09zsXG+TpQQz1NiVOUvKPkQC0RYA
MdPnvrXTodlsYxf1c1K9xbAkmeQ356ptlcl/sDi5w5y2eQ82Ul5kFIrfEpPMB7M8
qfkcIdRsz3Ctv9ps0wbfeXSeW0rw2LbokQ8qhChtyzJAeUbyTDkjvjHq9vEMNs8G
QiGRFDEZzwxj+VkmsKOhYfJvOKUXXZJDGTzbUaATuLdXkbB17pE06n4nOAGUqbJa
rFbSC8EZVdwBp4Ryh3jtF7+FXlDizjh1Wc+Mushx1/LvBA2vRkz4U39e97VIRTEW
wHPvLkSyCtUX6HxuOCC2hGQY0mMfrxrtMTgUUQvTwIdu8S7JEHeEmQ2GhAGJ1T67
Bsnih3FMpHN7VI6ugek1Egdu08CXO5ybU6hyJNgaRTsIC29XlEmYVBGqj5XuyHsO
TYLStNA9A2cgHr/HfTrR/FB7RwaFqA47uPBvOapwmdV0dK7a1tCfJHrQxTO+SNkF
Gdg/gBE+yYqjEgA9uqa9t6rHPrQnFtdjG3Bp0AVFJ8WSh3l8P/ZJjmtGvYEFd7xt
LP6EEGPnt/vfmXnmViqDSmASRqnIa7sDdWcI92CG9Ds3+77SeHpFzRfPBH0zN3bi
X/zMr8XjCxbw8hswNUxSve4yc0KW5Nu2jTiv9zfJTSfoLYdDhC8=
=3Nhu
-----END PGP SIGNATURE-----

pinheadmz's public key is on keybase

@maflcko
Copy link
Member

maflcko commented Feb 7, 2023

I still don't understand why this change is being made. #26940 (comment)

It claims to add coverage, without providing any evidence or even intuition.

I am thinking that, absent that, it should be assumed that it will remove coverage. For example, sanitizers, such as the UB signed integer overflow sanitizer require large enough values to detect an overflow. If a majority of the values are now 0 and 1, where before they weren't, it'll have a harder time to trigger an overflow and sanitize the code.

@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch from e6de5ad to 25ea24d Compare February 7, 2023 16:50
@jonatack
Copy link
Member Author

jonatack commented Feb 7, 2023

If a majority of the values are now 0 and 1

Only 1% was 0 and 1% was 1. I liked that it would check edge cases often enough to provide feedback when running make check that our current tests may miss, especially for developers running the unit tests locally to verify they are green, say, before pushing an update.

Anyway, that's gone now; removed the edge cases in the last push. It uses the money range rather than only uint32, while remaining in the same vein as the existing unit test rand helpers, and updated the PR description.

to generate semi-random CAmounts up to MAX_MONEY rather
than only uint32, and use it in the unit tests.
@jonatack jonatack force-pushed the unit-tests-CAmount-generation branch from 25ea24d to 4275195 Compare February 9, 2023 23:08
Copy link
Contributor

@john-moffett john-moffett left a comment

Choose a reason for hiding this comment

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

ACK 4275195

@DrahtBot DrahtBot requested a review from pinheadmz February 10, 2023 11:59
@pinheadmz
Copy link
Member

ACK 4275195

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 4275195606e6f42466d9a8ef766b3035833df4d5
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmPmX4QACgkQ5+KYS2KJ
yTp9Qg/8DS+6sPz95i2whVHQhnphYYAbbF68udZnle8PqeKof7lWGHOnCF3our+/
m2oOGfwXdviYMtIJzB+qBpwRHWonJStIl90Tz6yWqwnM2H5djWhpRBKUsjXMSPsI
vpFJCL2eQNSohNcBJ3aG2rZ3SIrMRZaFBiC1cKKan5XYbZBistXFg5H7odmTTwa/
j70qkh3dK+NINELUC5Q+exVSPK9LSwH8tmfIKjd0mb0RL7ABXISEVEQSjkdIgOG9
tNFc9o3Nlcseyha0V7bzkDEL0cSPoxInbxlUHv/o8+6+XnPL3cn2hGrFy424OMg6
IvZeiQacQbaHzDjDNI9bcK4nyunLqPz1G3r53ibs2CU507Mfv/TNIoToFyJzg/HF
kgb9qRhg/KwqaZ/2bT4OatAOja+t8lnFMvVL0NB0Uhmudv1kbcGERqIQWc8/0dru
17vdbiu3YqAstDMhtndvy/hupEuBEqEc97xNmXu9xmB8TP9FKDpRS2qWWpvbQNna
7gh49R+eBu/0kxeMhIrc2oXL1S3oaVH1kNqYwGT6cM7Qvt1nVJgGohm2JCE9PuPf
Md+VcMm/EhQwUjl7Ja58Sv3UdkWMmQKFj/DhVowJHht4tfDPtl6hR6iBitnB3/69
aoh/KBaiey/cSOXVv7zX5TQv21452+fdDuZPjFeGPct+vHxf/II=
=r6pu
-----END PGP SIGNATURE-----

pinheadmz's public key is on keybase

@DrahtBot DrahtBot removed the request for review from pinheadmz February 10, 2023 15:19
@achow101
Copy link
Member

ACK 4275195

@achow101 achow101 merged commit a245429 into bitcoin:master Feb 17, 2023
@jonatack jonatack deleted the unit-tests-CAmount-generation branch February 17, 2023 22:28
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 25, 2023
@glozow
Copy link
Member

glozow commented Apr 5, 2023

What was the rationale for 81f5ade? test/util/random.h depends on test/util/setup_common because it's using g_insecure_rand_ctx, which means setup_common can't use any of the random functions without creating a circular dependency. Was this intentional / am I missing something? Should we be using a different set of random functions within setup_common?

@jonatack
Copy link
Member Author

jonatack commented Apr 5, 2023

@glozow Thanks for bringing it up! The rationale was in this thread: #26940 (comment). I noticed this as well, and that src/test/util/chainstate.h has the same dependency, and made a patch to remove those. Let me see if I can dig it up.

@jonatack
Copy link
Member Author

jonatack commented Apr 5, 2023

setup_common can't use any of the random functions without creating a circular dependency

Addressed with #27425.

fanquake added a commit to bitcoin-core/gui that referenced this pull request Jul 19, 2023
…/setup_common to util/random

1cd45d4 test: move random.h include header from setup_common.h to cpp (Jon Atack)
1b246fd test: move remaining random test util code from setup_common to random (jonatack)

Pull request description:

  and drop the `util/random` dependency on `util/setup_common`.  This improves code separation and allows `util/setup_common` to call `util/random` functions without creating a circular dependency, thereby addressing bitcoin/bitcoin#26940 (comment) by glozow (thanks!)

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 1cd45d4 🌂

Tree-SHA512: 6ce63d9103ba9b04eebbd8ad02fe9aa79e356296533404034a1ae88e9b7ca0bc9a5c51fd754b71cf4e7b55b18bcd4d5474b2d588edee3851e3b3ce0e4d309a93
@bitcoin bitcoin locked and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants