Skip to content

Commit 31a481b

Browse files
author
MarcoFalke
committed
Merge bitcoin#22597: consensus/params: simplify ValidDeployment 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 bitcoin#22587 ACKs for top commit: MarcoFalke: review ACK 0591710 tryphe: retested ACK 0591710 Tree-SHA512: e53b5d478b46d35ec476d004e3c92803afb874c138dd6ef3848861f4281cc113fe88921bc4ac74fd4decbf318ed776d3f816c3a1185f99dc36a5cfecfff51f7c
2 parents 87257d8 + 0591710 commit 31a481b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/consensus/params.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ enum BuriedDeployment : int16_t {
2323
DEPLOYMENT_CSV,
2424
DEPLOYMENT_SEGWIT,
2525
};
26-
constexpr bool ValidDeployment(BuriedDeployment dep) { return DEPLOYMENT_HEIGHTINCB <= dep && dep <= DEPLOYMENT_SEGWIT; }
26+
constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_SEGWIT; }
2727

2828
enum DeploymentPos : uint16_t {
2929
DEPLOYMENT_TESTDUMMY,
3030
DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
3131
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
3232
MAX_VERSION_BITS_DEPLOYMENTS
3333
};
34-
constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_TAPROOT; }
34+
constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
3535

3636
/**
3737
* Struct for each individual consensus rule change using BIP9.

src/deploymentstatus.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <consensus/params.h>
88
#include <versionbits.h>
99

10+
#include <type_traits>
11+
1012
VersionBitsCache g_versionbitscache;
1113

1214
/* Basic sanity checking for BuriedDeployment/DeploymentPos enums and
@@ -15,3 +17,18 @@ VersionBitsCache g_versionbitscache;
1517
static_assert(ValidDeployment(Consensus::DEPLOYMENT_TESTDUMMY), "sanity check of DeploymentPos failed (TESTDUMMY not valid)");
1618
static_assert(!ValidDeployment(Consensus::MAX_VERSION_BITS_DEPLOYMENTS), "sanity check of DeploymentPos failed (MAX value considered valid)");
1719
static_assert(!ValidDeployment(static_cast<Consensus::BuriedDeployment>(Consensus::DEPLOYMENT_TESTDUMMY)), "sanity check of BuriedDeployment failed (overlaps with DeploymentPos)");
20+
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, T x>
27+
static constexpr bool is_minimum()
28+
{
29+
using U = typename std::underlying_type<T>::type;
30+
return x == std::numeric_limits<U>::min();
31+
}
32+
33+
static_assert(is_minimum<Consensus::BuriedDeployment, Consensus::DEPLOYMENT_HEIGHTINCB>(), "heightincb is not minimum value for BuriedDeployment");
34+
static_assert(is_minimum<Consensus::DeploymentPos, Consensus::DEPLOYMENT_TESTDUMMY>(), "testdummy is not minimum value for DeploymentPos");

0 commit comments

Comments
 (0)