consensus/params: simplify ValidDeployment check to avoid gcc warning #22597

pull ajtowns wants to merge 1 commits into bitcoin:master from ajtowns:202108-deploygccwarn changing 2 files +19 −2
  1. ajtowns commented at 8:55 am on August 1, 2021: member

    Simplifies the ValidDeployment check to only check the upperbound, relying on the lower bound to be trivially true due to enum values starting at the minimum value of the underlying type (which is checked at compile time in deploymentstatus.cpp). Avoids a “comparison always true” warning in some versions of gcc.

    Fixes #22587

  2. DrahtBot commented at 11:37 am on August 1, 2021: 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. in src/deploymentstatus.cpp:26 in 9bf43b09b6 outdated
    21+/* ValidDeployment only checks upper bounds for ensuring validity.
    22+ * This checks that the lowest possible value or the type is also a
    23+ * (specific) valid deployment so that lower bounds don't need to be checked.
    24+ */
    25+
    26+template<typename T, typename X, typename U=typename std::underlying_type<T>::type>
    


    MarcoFalke commented at 2:13 pm on August 1, 2021:

    Is there a reason to use three template parameters when a single one would be sufficient?

    0template <typename X>
    1static constexpr bool is_minimum(const X x)
    2{
    3    using U = typename std::underlying_type<X>::type;
    4    return x == std::numeric_limits<U>::min();
    5}
    6
    7static_assert(is_minimum(Consensus::DEPLOYMENT_HEIGHTINCB), "heightincb is not minimum value for BuriedDeployment");
    8static_assert(is_minimum(Consensus::DEPLOYMENT_TESTDUMMY), "testdummy is not minimum value for DeploymentPos");
    

    This seems verbose, confusing and dangerous. For example the following asserts are wrong, but don’t fail:

    0static_assert(is_minimum<Consensus::BuriedDeployment, Consensus::BuriedDeployment, signed short>(Consensus::DEPLOYMENT_HEIGHTINCB), "heightincb is not minimum value for BuriedDeployment");
    1static_assert(is_minimum<Consensus::DeploymentPos, Consensus::DeploymentPos, unsigned short>(Consensus::DEPLOYMENT_TESTDUMMY), "testdummy is not minimum value for DeploymentPos");
    

    ajtowns commented at 1:49 pm on August 2, 2021:
    Changed. Better?
  4. MarcoFalke commented at 2:15 pm on August 1, 2021: member

    Left a review comment. Also, unrelated:

    DeploymentActiveAt could be a template as well:

     0diff --git a/src/deploymentstatus.h b/src/deploymentstatus.h
     1index f95c5996f5..f133d6ec6a 100644
     2--- a/src/deploymentstatus.h
     3+++ b/src/deploymentstatus.h
     4@@ -27,13 +27,8 @@ inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus
     5 }
     6 
     7 /** Determine if a deployment is active for this block */
     8-inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep)
     9-{
    10-    assert(Consensus::ValidDeployment(dep));
    11-    return index.nHeight >= params.DeploymentHeight(dep);
    12-}
    13-
    14-inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep)
    15+template <typename Dep>
    16+bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Dep dep)
    17 {
    18     assert(Consensus::ValidDeployment(dep));
    19     return DeploymentActiveAfter(index.pprev, params, dep);
    

    The benefit (apart from less code) would also be a way to enforce that the two function have the same signature. See #22564 (review)

  5. practicalswift commented at 7:57 pm on August 1, 2021: contributor
    Concept ACK
  6. tryphe commented at 4:53 am on August 2, 2021: contributor

    tested ACK 9bf43b09b6efb069e2b2e8551b4c5a63b9348815

    It solves the warnings on gcc 8.3.0 (Debian 10).

  7. in src/consensus/params.h:34 in 9bf43b09b6 outdated
    31     DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
    32     // NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
    33     MAX_VERSION_BITS_DEPLOYMENTS
    34 };
    35-constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_TAPROOT; }
    36+constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
    


    tryphe commented at 5:07 am on August 2, 2021:

    I think it’s important to let other reviewers know that MAX_VERSION_BITS_DEPLOYMENTS is not intended to be assigned, hence we only check if it’s less, unlike on line 26 where dep might equal DEPLOYMENT_SEGWIT.

    At least that was my interpretation.

  8. consensus/params: simplify ValidDeployment check to avoid gcc warning 059171009b
  9. ajtowns force-pushed on Aug 2, 2021
  10. MarcoFalke commented at 1:58 pm on August 2, 2021: member
    review ACK 059171009b0138555f311cedc2553015ff618323
  11. MarcoFalke added the label Refactoring on Aug 2, 2021
  12. MarcoFalke added this to the milestone 22.0 on Aug 2, 2021
  13. MarcoFalke added the label Needs backport (22.x) on Aug 2, 2021
  14. MarcoFalke commented at 1:59 pm on August 2, 2021: member
    Added for rc3 (if there is one)
  15. Rspigler commented at 11:19 pm on August 2, 2021: contributor
    After @tryphe showed me how to reproduce, I tested 059171009b0138555f311cedc2553015ff618323 and this does solve the problem!
  16. tryphe commented at 4:06 am on August 3, 2021: contributor

    retested ACK 059171009b0138555f311cedc2553015ff618323 approach ACK 059171009b0138555f311cedc2553015ff618323

    gcc 8.3.0

  17. MarcoFalke commented at 11:53 am on August 3, 2021: member

    tested on aarch64:

    0gcc (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0
    
  18. MarcoFalke referenced this in commit 31a481be29 on Aug 3, 2021
  19. fanquake closed this on Aug 3, 2021

  20. sidhujag referenced this in commit 4c76de657b on Aug 4, 2021
  21. hebasto referenced this in commit 1d8790ea05 on Aug 5, 2021
  22. hebasto commented at 3:25 pm on August 5, 2021: member
    Backported in #22629.
  23. hebasto removed the label Needs backport (22.x) on Aug 5, 2021
  24. hebasto referenced this in commit be35464291 on Aug 6, 2021
  25. hebasto referenced this in commit eb4ad9f734 on Aug 6, 2021
  26. hebasto referenced this in commit 4f027ac43a on Aug 6, 2021
  27. SmartArray referenced this in commit bd5af37cd7 on Aug 7, 2021
  28. hebasto referenced this in commit 489128a67d on Aug 7, 2021
  29. hebasto referenced this in commit 318275f0e0 on Aug 9, 2021
  30. hebasto referenced this in commit 28cbdc3d08 on Aug 9, 2021
  31. hebasto referenced this in commit 4f9bb380bc on Aug 19, 2021
  32. hebasto referenced this in commit 08078ab3c9 on Aug 20, 2021
  33. hebasto referenced this in commit f897140003 on Aug 20, 2021
  34. hebasto referenced this in commit 57fce067a3 on Aug 20, 2021
  35. laanwj referenced this in commit 4a25e39624 on Aug 26, 2021
  36. fujicoin referenced this in commit 307060811d on Aug 27, 2021
  37. gwillen referenced this in commit 76727e73ca on Jul 27, 2022
  38. gwillen referenced this in commit 68f17e6b67 on Aug 1, 2022
  39. DrahtBot locked this on Aug 16, 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-12-18 21:12 UTC

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