Chainparams: Refactor: Decouple IsSuperMajority from Params()

This commit is contained in:
Jorge Timón 2015-04-01 16:10:37 +02:00
parent 00820f921d
commit 51aa24927e
2 changed files with 9 additions and 17 deletions

View File

@ -50,9 +50,6 @@ public:
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; } const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; } int GetDefaultPort() const { return nDefaultPort; }
int SubsidyHalvingInterval() const { return consensus.nSubsidyHalvingInterval; } int SubsidyHalvingInterval() const { return consensus.nSubsidyHalvingInterval; }
int EnforceBlockUpgradeMajority() const { return consensus.nMajorityEnforceBlockUpgrade; }
int RejectBlockOutdatedMajority() const { return consensus.nMajorityRejectBlockOutdated; }
int ToCheckBlockUpgradeMajority() const { return consensus.nMajorityWindow; }
/** Used if GenerateBitcoins is called with a negative number of threads */ /** Used if GenerateBitcoins is called with a negative number of threads */
int DefaultMinerThreads() const { return nMinerThreads; } int DefaultMinerThreads() const { return nMinerThreads; }

View File

@ -75,10 +75,9 @@ void EraseOrphansFor(NodeId peer);
/** /**
* Returns true if there are nRequired or more blocks of minVersion or above * Returns true if there are nRequired or more blocks of minVersion or above
* in the last Params().ToCheckBlockUpgradeMajority() blocks, starting at pstart * in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
* and going backwards.
*/ */
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired); static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
static void CheckBlockIndex(); static void CheckBlockIndex();
/** Constant stuff for coinbase transactions we create: */ /** Constant stuff for coinbase transactions we create: */
@ -1747,7 +1746,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE; unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
// Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded: // Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, Params().EnforceBlockUpgradeMajority())) { if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
flags |= SCRIPT_VERIFY_DERSIG; flags |= SCRIPT_VERIFY_DERSIG;
} }
@ -2644,18 +2643,14 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
} }
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded: // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated)) if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
{
return state.Invalid(error("%s: rejected nVersion=1 block", __func__), return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
REJECT_OBSOLETE, "bad-version"); REJECT_OBSOLETE, "bad-version");
}
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded: // Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated)) if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
{
return state.Invalid(error("%s : rejected nVersion=2 block", __func__), return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
REJECT_OBSOLETE, "bad-version"); REJECT_OBSOLETE, "bad-version");
}
return true; return true;
} }
@ -2663,6 +2658,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev) bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
{ {
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1; const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
const Consensus::Params& consensusParams = Params().GetConsensus();
// Check that all transactions are finalized // Check that all transactions are finalized
BOOST_FOREACH(const CTransaction& tx, block.vtx) BOOST_FOREACH(const CTransaction& tx, block.vtx)
@ -2672,7 +2668,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
// Enforce block.nVersion=2 rule that the coinbase starts with serialized block height // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
// if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet): // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, Params().EnforceBlockUpgradeMajority())) if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
{ {
CScript expect = CScript() << nHeight; CScript expect = CScript() << nHeight;
if (block.vtx[0].vin[0].scriptSig.size() < expect.size() || if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
@ -2779,11 +2775,10 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
return true; return true;
} }
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired) static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
{ {
unsigned int nToCheck = Params().ToCheckBlockUpgradeMajority();
unsigned int nFound = 0; unsigned int nFound = 0;
for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++) for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
{ {
if (pstart->nVersion >= minVersion) if (pstart->nVersion >= minVersion)
++nFound; ++nFound;