[tests] Reduced number of validations in tx_validationcache_tests #13533

pull lucash-dev wants to merge 1 commits into bitcoin:master from lucash-dev:reduce-txvalidationcache_tests-runtime changing 2 files +12 −3
  1. lucash-dev commented at 0:55 am on June 25, 2018: contributor

    Following a suggestion in the comments, changed ValidateCheckInputsForAllFlags from testing all possible flag combinations to testing a random subset. Also created a new enum constant for the highest flag, so that this test doesn’t keep testing an incomplete subset in case a new flag is added.

    Timing for checkinputs_test:

    0Before:   6.8s
    1After:    3.7s
    2----------------
    3Saved:    3.1s (45%)
    

    This PR was split from #13050. Also see #10026.

  2. DrahtBot commented at 11:17 am on June 26, 2018: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    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.

  3. fanquake added the label Tests on Jul 1, 2018
  4. in src/test/txvalidationcache_tests.cpp:111 in ccd99f7360 outdated
    109-    // rewrite in the future to randomly pick a set of flags to evaluate.
    110-    for (uint32_t test_flags=0; test_flags < (1U << 16); test_flags += 1) {
    111+
    112+    for (uint32_t count=0; count < 10000; count++) {
    113+        // Randomly selects flag combinations
    114+        uint32_t test_flags = rand() % (HIGHEST_FLAG << 1);
    


    laanwj commented at 4:40 pm on July 5, 2018:
    we don’t use rand() anywhere in the code, would be preferable to use a FastRandomContext insecure_rand(true); here, also for determinism - for reproducible it is undesirable for different test runs to test a different subset of flags

    lucash-dev commented at 6:43 am on July 6, 2018:
    Fixed. Thanks for the explanation!
  5. DrahtBot closed this on Aug 25, 2018

  6. DrahtBot reopened this on Aug 25, 2018

  7. in src/test/txvalidationcache_tests.cpp:113 in ec135f4cc1 outdated
    111+
    112+    FastRandomContext insecure_rand(true);
    113+
    114+    for (uint32_t count=0; count < 10000; count++) {
    115+        // Randomly selects flag combinations
    116+        uint32_t test_flags = insecure_rand.randrange(HIGHEST_FLAG << 1);
    


    practicalswift commented at 8:37 pm on October 4, 2018:
    Make this implicit conversion from uint64_t to uint32_t explicit :-)

    lucash-dev commented at 5:08 pm on October 7, 2018:
    Actually there’s quite a lot of confusion of types around these types. randrange return a uint64_t, but CheckInputsaccepts aunsigned int, while the flags are actually an enum which has an implementation-specific type. I guess the best is to change it here to unsigned int` (and make the conversion explicit), and maybe open a separate PR for fixing these type inconsistencies.
  8. DrahtBot closed this on Aug 16, 2019

  9. DrahtBot commented at 1:49 pm on August 16, 2019: member
  10. DrahtBot reopened this on Aug 16, 2019

  11. DrahtBot added the label Needs rebase on Oct 30, 2019
  12. adamjonas commented at 7:01 pm on October 30, 2019: member

    Concept ACK

    I was looking at this before the conflict popped up and the savings on my machine was:

    0Before: 3.7s
    1After: 2.8s
    2------------
    3Saved 0.9s (24%).
    

    My rebase is here for reference.

  13. lucash-dev commented at 9:44 pm on October 30, 2019: contributor
    Thx @adamjonas. Will be away for a few days, probably will rebase over the weekend.
  14. lucash-dev commented at 10:52 pm on November 5, 2019: contributor
    @adamjonas rebased. thx
  15. DrahtBot removed the label Needs rebase on Nov 5, 2019
  16. in src/script/interpreter.h:122 in c1a8b47d52 outdated
    113@@ -114,6 +114,12 @@ enum
    114     // Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
    115     //
    116     SCRIPT_VERIFY_CONST_SCRIPTCODE = (1U << 16),
    117+
    118+
    119+    // Constants to point to the highest flag in use. Add new flags above this line.
    120+    //
    121+    HIGHEST_FLAG_PLUS_ONE,
    122+    HIGHEST_FLAG = HIGHEST_FLAG_PLUS_ONE - 1
    


    theStack commented at 8:41 pm on January 23, 2020:
    I’d suggest to prefix those two constants with SCRIPT_VERIFY_ as well. Otherwise ever single translation unit including script/interpreter.h will have a bare HIGHEST_FLAG {_PLUS_ONE} constant in its global namespace and there isn’t any reference in the name to which flags it refers to.
  17. in src/test/txvalidationcache_tests.cpp:117 in c1a8b47d52 outdated
    115-    // rewrite in the future to randomly pick a set of flags to evaluate.
    116-    for (uint32_t test_flags=0; test_flags < (1U << 16); test_flags += 1) {
    117+
    118+    FastRandomContext insecure_rand(true);
    119+
    120+    for (uint32_t count=0; count < 10000; count++) {
    


    theStack commented at 8:42 pm on January 23, 2020:
    Now that the loop variable is a simple counter, not representing a bitfield anymore, I’d change the data type to just a plain int.
  18. in src/test/txvalidationcache_tests.cpp:121 in c1a8b47d52 outdated
    119+
    120+    for (uint32_t count=0; count < 10000; count++) {
    121         TxValidationState state;
    122+
    123+        // Randomly selects flag combinations
    124+        unsigned int test_flags = (unsigned int) insecure_rand.randrange(HIGHEST_FLAG << 1);
    


    theStack commented at 8:48 pm on January 23, 2020:
    nit: The data type was an explicit uint32_t before, so I’d also that type here now rather than the more general unsigned int (in practice it won’t matter though, so feel free to ignore).
  19. theStack commented at 9:10 pm on January 23, 2020: member
    Concept ACK Though on the current master branch enumerating through all possible script-verify-flags combinations is still feasible (running tx_validationcache_tests takes less than 2 seconds on my machine), the execution time for the validation loop would obviously increase exponentially with each script-verify-flag added.
  20. lucash-dev commented at 6:05 am on February 13, 2020: contributor

    Concept ACK Though on the current master branch enumerating through all possible script-verify-flags combinations is still feasible (running tx_validationcache_tests takes less than 2 seconds on my machine), the execution time for the validation loop would obviously increase exponentially with each script-verify-flag added.

    Hi, @theStack. Thank you for reviewing this PR. I agree with your comments and will implement them. Just a little bit busy, but hopefully will have some time for that in the next few weeks.

    thanks!

  21. lucash-dev commented at 7:07 pm on March 4, 2020: contributor
    hi @theStack. finally implemented the changes you suggested, also rebased – and test are passing.
  22. theStack approved
  23. DrahtBot added the label Needs rebase on Apr 19, 2020
  24. adamjonas commented at 7:09 pm on June 16, 2020: member
    Hi @lucash-dev, pinging for a rebase. Mine is here for reference.
  25. DrahtBot removed the label Needs rebase on Jun 19, 2020
  26. lucash-dev commented at 5:28 am on June 23, 2020: contributor
    thx @adamjonas. rebased.
  27. in src/script/interpreter.h:142 in f723288c4f outdated
    113@@ -114,6 +114,12 @@ enum
    114     // Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
    115     //
    116     SCRIPT_VERIFY_CONST_SCRIPTCODE = (1U << 16),
    117+
    118+
    


    kallewoof commented at 1:42 am on August 26, 2020:
    Nit: extraneous whitespace (1 empty line is usually enough).
  28. in src/script/interpreter.h:122 in f723288c4f outdated
    113@@ -114,6 +114,12 @@ enum
    114     // Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
    115     //
    116     SCRIPT_VERIFY_CONST_SCRIPTCODE = (1U << 16),
    117+
    118+
    119+    // Constants to point to the highest flag in use. Add new flags above this line.
    120+    //
    121+    SCRIPT_VERIFY_HIGHEST_FLAG_PLUS_ONE,
    122+    SCRIPT_VERIFY_HIGHEST_FLAG = SCRIPT_VERIFY_HIGHEST_FLAG_PLUS_ONE - 1
    


    kallewoof commented at 1:43 am on August 26, 2020:
    Suggest you call this SCRIPT_VERIFY_END_MARKER, and then use < SCRIPT_VERIFY_END_MARKER logic instead of this, i.e. only add one.
  29. in src/test/txvalidationcache_tests.cpp:115 in f723288c4f outdated
    113-    // rewrite in the future to randomly pick a set of flags to evaluate.
    114-    for (uint32_t test_flags=0; test_flags < (1U << 16); test_flags += 1) {
    115+
    116+    FastRandomContext insecure_rand(true);
    117+
    118+    for (int count=0; count < 10000; count++) {
    


    kallewoof commented at 1:44 am on August 26, 2020:

    Style-nit: spacing, ++ before varname:

    0for (int count = 0; count < 10000; ++count) {
    
  30. in src/test/txvalidationcache_tests.cpp:119 in f723288c4f outdated
    117+
    118+    for (int count=0; count < 10000; count++) {
    119         TxValidationState state;
    120+
    121+        // Randomly selects flag combinations
    122+        uint32_t test_flags = (uint32_t) insecure_rand.randrange(SCRIPT_VERIFY_HIGHEST_FLAG << 1);
    


    kallewoof commented at 1:48 am on August 26, 2020:

    Using the marker style, to get the same you’d randrange((SCRIPT_VERIFY_END_MARKER - 1) << 1) here.

    However, I’m not sure why you are doing << 1 here (1U << 17), as the original code only goes up to SCRIPT_VERIFY_END_MARKER - 1 (i.e. (1U << 16)). To retain the same, this would be randrange(SCRIPT_VERIFY_END_MARKER - 1);


    lucash-dev commented at 0:12 am on September 16, 2020:
    Hi, @kalewoof. Thanks for reviewing. The reason for that is that we want to possibly test all flag combinations (or a random subset thereof). The lowest value possible with all flags off is 0, the highest would be the sum of all constants. So if the highest constant is 1«16, having the maximum value used for test be 1«16 would preclude testing combinations of that flag being used. We need to test from 0 to 1111111111111111 (1«17 -1). Because of the range being exclusive of the highest value, I pass 1<<17 = (SCRIPT_VERIFY_END_MARKER - 1) << 1 Hope that clarifies.

    lucash-dev commented at 0:19 am on September 16, 2020:
    The test currently only going to 1«16 is probably a mistake caused by adding a flag but not updating the test.

  31. kallewoof commented at 1:49 am on August 26, 2020: member
    Concept ACK; the change looks reasonable, and cutting down on redundant testing is definitely desirable.
  32. fanquake added the label Up for grabs on Sep 15, 2020
  33. fanquake closed this on Sep 15, 2020

  34. lucash-dev commented at 3:29 pm on September 15, 2020: contributor
    Hi @fanquake What’s the reason for closing it now? I was meaning to implement the latest round of requested changes and it has concept acks. Should I work on this, open a new one or just move on? Just looking for clarification— don’t want to waste anyone’s time if maintainers don’t want this change. I know you have plenty on your plate. Thanks!
  35. fanquake commented at 10:09 pm on September 15, 2020: member
    Hi @lucash-dev. I closed this because of the length of time it’s been open overall, the length of time it’s been since comments were left and had been unaddressed, and after looking at your GitHub profile it seemed that you were no-longer active here (hence throwing it up for grabs). However if you are going to address the concerns, I’ll re-open this.
  36. fanquake reopened this on Sep 15, 2020

  37. fanquake removed the label Up for grabs on Sep 15, 2020
  38. lucash-dev commented at 0:20 am on September 16, 2020: contributor
    Thanks @fanquake. Sorry for leaving this PR unattended for so long.
  39. lucash-dev commented at 0:25 am on September 16, 2020: contributor
    Updated PR to fix @kallewoof’s NIT’s.
  40. in src/test/txvalidationcache_tests.cpp:119 in 203472e8ec outdated
    117+
    118+    for (int count = 0; count < 10000; ++count) {
    119         TxValidationState state;
    120+
    121+        // Randomly selects flag combinations
    122+        uint32_t test_flags = (uint32_t) insecure_rand.randrange((SCRIPT_VERIFY_END_MARKER - 1) << 1);
    


    kallewoof commented at 5:21 am on September 16, 2020:

    You want to swap the - and «; right now you get 111...0, i.e.

    0        uint32_t test_flags = (uint32_t) insecure_rand.randrange((SCRIPT_VERIFY_END_MARKER << 1) - 1);
    

    lucash-dev commented at 6:43 am on September 16, 2020:
    Not really. Right now the last flag is 1«16. Bc of the way the enum works my new constant becomes (1«16)+1. That’s why I used the “…PLUS_ONE” constant before. So the argument to the range is (((1«16)+1)-1)«1 = (1«16)«1 = 1«17 Because the range is exclusive, the highest possible value is (1«17)-1 = 1111111111111111b

    kallewoof commented at 7:06 am on September 16, 2020:
    Ahh, I misread it as a mask for some reason.
  41. DrahtBot added the label Needs rebase on Oct 15, 2020
  42. Reduced number of validations in `tx_validationcache_tests` to keep the run time reasonable.
    Following a suggestion in the comments, changed `ValidateCheckInputsForAllFlags` from testing all possible flag combinations to testing a random subset. Also created a new enum constant for the highest flag, so that this test doesn’t keep testing an incomplete subset in case a new flag is added.
    c3e111a7da
  43. DrahtBot removed the label Needs rebase on Oct 15, 2020
  44. lucash-dev commented at 6:14 pm on October 16, 2020: contributor

    Rebased. @kallewoof would you mind ACK-ing since I addressed your comments (or otherwise make further comments)?

    I think this PR has become (more) important after the new taproot flags were introduced – since none of those flags are tested by this test case in master.

  45. leonardojobim commented at 7:26 am on February 23, 2021: none

    tACK https://github.com/bitcoin/bitcoin/pull/13533/commits/c3e111a7daf5800026dda4455c737de0412528f1. Tested on Ubuntu 20.04 on VMWAre.

    Average tests execution time: . Master branch: 1.02s . This PR branch: 0.60s

    These values have remained consistent across several runs.

  46. kallewoof commented at 4:43 am on July 17, 2021: member
    ACK c3e111a7daf5800026dda4455c737de0412528f1
  47. theStack approved
  48. theStack commented at 9:23 am on July 18, 2021: member
    re-ACK c3e111a7daf5800026dda4455c737de0412528f1
  49. MarcoFalke merged this on Jul 24, 2021
  50. MarcoFalke closed this on Jul 24, 2021

  51. lucash-dev commented at 7:31 pm on July 26, 2021: contributor

    Thanks everyone!

    I know reviewing PRs is a lot of work, and this one wasn’t a priority – so I’m really glad to see it finally merged.

    I learned a lot from writing it and from your feedback!

  52. sidhujag referenced this in commit 52375cf41a on Jul 28, 2021
  53. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-21 15:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me