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

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

Closed
wants to merge 1 commit into from

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Aug 1, 2021

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

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 1, 2021

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.

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.

Left a review comment. Also, unrelated:

DeploymentActiveAt could be a template as well:

diff --git a/src/deploymentstatus.h b/src/deploymentstatus.h
index f95c5996f5..f133d6ec6a 100644
--- a/src/deploymentstatus.h
+++ b/src/deploymentstatus.h
@@ -27,13 +27,8 @@ inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus
 }
 
 /** Determine if a deployment is active for this block */
-inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep)
-{
-    assert(Consensus::ValidDeployment(dep));
-    return index.nHeight >= params.DeploymentHeight(dep);
-}
-
-inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep)
+template <typename Dep>
+bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Dep dep)
 {
     assert(Consensus::ValidDeployment(dep));
     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 (comment)

* (specific) valid deployment so that lower bounds don't need to be checked.
*/

template<typename T, typename X, typename U=typename std::underlying_type<T>::type>
Copy link
Member

@maflcko maflcko Aug 1, 2021

Choose a reason for hiding this comment

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

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

template <typename X>
static constexpr bool is_minimum(const X x)
{
    using U = typename std::underlying_type<X>::type;
    return x == std::numeric_limits<U>::min();
}

static_assert(is_minimum(Consensus::DEPLOYMENT_HEIGHTINCB), "heightincb is not minimum value for BuriedDeployment");
static_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:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Better?

@practicalswift
Copy link
Contributor

Concept ACK

@tryphe
Copy link
Contributor

tryphe commented Aug 2, 2021

tested ACK 9bf43b0

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


enum DeploymentPos : uint16_t {
DEPLOYMENT_TESTDUMMY,
DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
MAX_VERSION_BITS_DEPLOYMENTS
};
constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_TAPROOT; }
constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

@maflcko
Copy link
Member

maflcko commented Aug 2, 2021

review ACK 0591710

@maflcko maflcko added this to the 22.0 milestone Aug 2, 2021
@maflcko
Copy link
Member

maflcko commented Aug 2, 2021

Added for rc3 (if there is one)

@Rspigler
Copy link
Contributor

Rspigler commented Aug 2, 2021

After @tryphe showed me how to reproduce, I tested 0591710 and this does solve the problem!

@tryphe
Copy link
Contributor

tryphe commented Aug 3, 2021

retested ACK 0591710
approach ACK 0591710

gcc 8.3.0

@maflcko
Copy link
Member

maflcko commented Aug 3, 2021

tested on aarch64:

gcc (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Aug 3, 2021
…nt check to avoid gcc warning

0591710 consensus/params: simplify ValidDeployment check to avoid gcc warning (Anthony Towns)

Pull request description:

  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

ACKs for top commit:
  MarcoFalke:
    review ACK 0591710
  tryphe:
    retested ACK 0591710

Tree-SHA512: e53b5d478b46d35ec476d004e3c92803afb874c138dd6ef3848861f4281cc113fe88921bc4ac74fd4decbf318ed776d3f816c3a1185f99dc36a5cfecfff51f7c
@fanquake fanquake closed this Aug 3, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 4, 2021
… to avoid gcc warning

0591710 consensus/params: simplify ValidDeployment check to avoid gcc warning (Anthony Towns)

Pull request description:

  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 bitcoin#22587

ACKs for top commit:
  MarcoFalke:
    review ACK 0591710
  tryphe:
    retested ACK 0591710

Tree-SHA512: e53b5d478b46d35ec476d004e3c92803afb874c138dd6ef3848861f4281cc113fe88921bc4ac74fd4decbf318ed776d3f816c3a1185f99dc36a5cfecfff51f7c
@maflcko maflcko mentioned this pull request Aug 5, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 5, 2021
@hebasto
Copy link
Member

hebasto commented Aug 5, 2021

Backported in #22629.

hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 6, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 6, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 6, 2021
SmartArray pushed a commit to JaredTate/digibyte that referenced this pull request Aug 7, 2021
Github-Pull: bitcoin/bitcoin#22597
Rebased-From: 059171009b0138555f311cedc2553015ff618323
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 7, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 9, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 9, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 19, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 20, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 20, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 20, 2021
laanwj added a commit that referenced this pull request Aug 26, 2021
32e1424 Fix build with Boost 1.77.0 (Rafael Sadowski)
cb34a0a qt: Handle new added plurals in bitcoin_en.ts (Hennadii Stepanov)
068985c doc: Mention the flat directory structure for uploads (Andrew Chow)
27d43e5 guix: Don't include directory name in SHA256SUMS (Andrew Chow)
88fb7e3 test: fix bug in 22686 (S3RK)
63fec7e clientversion: No suffix #if CLIENT_VERSION_IS_RELEASE (Carl Dong)
dfaffbe test: Test for ApproximateBestSubset edge case with too little fees (Andrew Chow)
e86b023 wallet: Assert that enough was selected to cover the fees (Andrew Chow)
ffc81e2 wallet: Use GetSelectionAmount for target value calculations (Andrew Chow)
ce77b45 release: Release with separate SHA256SUMS and sig files (Carl Dong)
cb491bd guix-verify: Non-zero exit code when anything fails (Carl Dong)
6a611d2 gui: ensure external signer option remains disabled without signers (Andrew Chow)
e9b4487 qt: Fix regression in "Encrypt Wallet" menu item (Hennadii Stepanov)
57fce06 consensus/params: simplify ValidDeployment check to avoid gcc warning (Anthony Towns)
e9d30fb ci: Run fuzzer task for the master branch only (Hennadii Stepanov)

Pull request description:

  Backported:

  1) #22730
  1) bitcoin-core/gui#393
  1) #22597
  1) bitcoin-core/gui#396
  1) #22643
  1) #22642
  1) #22685
  1) #22686
  1) #22654
  1) #22742
  1) bitcoin-core/gui#406
  1) #22713

ACKs for top commit:
  laanwj:
    Code list-of-backported-PRs review ACK 32e1424

Tree-SHA512: f5e2dd1be6cdcd39368eeb5d297b3ff4418d0bf2e70c90e59ab4ba1dbf16f773045d877b4997511de58c3aca75a978dcf043e338bad23951557e2a27ccc845f6
fujicoin pushed a commit to fujicoin/fujicoin-22.0 that referenced this pull request Aug 27, 2021
Github-Pull: bitcoin/bitcoin#22597
Rebased-From: 059171009b0138555f311cedc2553015ff618323
gwillen pushed a commit to gwillen/elements that referenced this pull request Jul 27, 2022
gwillen pushed a commit to gwillen/elements that referenced this pull request Aug 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gcc -Wtype-limits warnings in consensus/params.h
8 participants