Skip to content

Commit 51aa249

Browse files
committed
Chainparams: Refactor: Decouple IsSuperMajority from Params()
1 parent 00820f9 commit 51aa249

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/chainparams.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class CChainParams
5050
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
5151
int GetDefaultPort() const { return nDefaultPort; }
5252
int SubsidyHalvingInterval() const { return consensus.nSubsidyHalvingInterval; }
53-
int EnforceBlockUpgradeMajority() const { return consensus.nMajorityEnforceBlockUpgrade; }
54-
int RejectBlockOutdatedMajority() const { return consensus.nMajorityRejectBlockOutdated; }
55-
int ToCheckBlockUpgradeMajority() const { return consensus.nMajorityWindow; }
5653

5754
/** Used if GenerateBitcoins is called with a negative number of threads */
5855
int DefaultMinerThreads() const { return nMinerThreads; }

src/main.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ void EraseOrphansFor(NodeId peer);
7575

7676
/**
7777
* Returns true if there are nRequired or more blocks of minVersion or above
78-
* in the last Params().ToCheckBlockUpgradeMajority() blocks, starting at pstart
79-
* and going backwards.
78+
* in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
8079
*/
81-
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired);
80+
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
8281
static void CheckBlockIndex();
8382

8483
/** Constant stuff for coinbase transactions we create: */
@@ -1747,7 +1746,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17471746
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
17481747

17491748
// Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
1750-
if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, Params().EnforceBlockUpgradeMajority())) {
1749+
if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
17511750
flags |= SCRIPT_VERIFY_DERSIG;
17521751
}
17531752

@@ -2644,25 +2643,22 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
26442643
}
26452644

26462645
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2647-
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
2648-
{
2646+
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
26492647
return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
26502648
REJECT_OBSOLETE, "bad-version");
2651-
}
26522649

26532650
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
2654-
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated))
2655-
{
2651+
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
26562652
return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
26572653
REJECT_OBSOLETE, "bad-version");
2658-
}
26592654

26602655
return true;
26612656
}
26622657

26632658
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
26642659
{
26652660
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
2661+
const Consensus::Params& consensusParams = Params().GetConsensus();
26662662

26672663
// Check that all transactions are finalized
26682664
BOOST_FOREACH(const CTransaction& tx, block.vtx)
@@ -2672,7 +2668,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
26722668

26732669
// Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
26742670
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
2675-
if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, Params().EnforceBlockUpgradeMajority()))
2671+
if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
26762672
{
26772673
CScript expect = CScript() << nHeight;
26782674
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
@@ -2779,11 +2775,10 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
27792775
return true;
27802776
}
27812777

2782-
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired)
2778+
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
27832779
{
2784-
unsigned int nToCheck = Params().ToCheckBlockUpgradeMajority();
27852780
unsigned int nFound = 0;
2786-
for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++)
2781+
for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
27872782
{
27882783
if (pstart->nVersion >= minVersion)
27892784
++nFound;

0 commit comments

Comments
 (0)