Skip to content

Commit 9ba7d06

Browse files
committed
Consensus: Refactor: Introduce Consensus::GetFlags
and use it instead of MANDATORY_SCRIPT_VERIFY_FLAGS in miner::CreateNewBlock()
1 parent 8ba5596 commit 9ba7d06

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/consensus/blockverify.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "arith_uint256.h"
99
#include "consensus/validation.h"
1010
#include "primitives/block.h"
11+
#include "script/interpreter.h"
1112
#include "tinyformat.h"
1213

1314
#include <algorithm>
@@ -134,6 +135,17 @@ bool Consensus::CheckBlockHeader(const CBlockHeader& block, CValidationState& st
134135
return true;
135136
}
136137

138+
unsigned Consensus::GetFlags(const CBlock& block, const Consensus::Params& consensusParams, CBlockIndexBase* pindex, PrevIndexGetter indexGetter)
139+
{
140+
int64_t nBIP16SwitchTime = 1333238400;
141+
bool fStrictPayToScriptHash = ((int64_t)pindex->nTime >= nBIP16SwitchTime);
142+
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
143+
144+
if (block.nVersion >= 3 && IsSuperMajority(3, indexGetter(pindex), consensusParams.nMajorityEnforceBlockUpgrade, consensusParams, indexGetter))
145+
flags |= SCRIPT_VERIFY_DERSIG;
146+
return flags;
147+
}
148+
137149
bool Consensus::ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndexBase* pindexPrev, PrevIndexGetter indexGetter)
138150
{
139151
// Check proof of work

src/consensus/consensus.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ bool ContextualCheckBlockHeader(const CBlockHeader&, CValidationState&, const Co
6565
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& params, int64_t nTime, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
6666
bool ContextualCheckBlock(const CBlock&, CValidationState&, const Consensus::Params&, const CBlockIndexBase*, PrevIndexGetter);
6767

68+
/** Block validation utilities */
69+
70+
/**
71+
* BIP16 didn't become active until Apr 1 2012
72+
* Starts enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded
73+
*/
74+
unsigned int GetFlags(const CBlock&, const Consensus::Params&, CBlockIndexBase*, PrevIndexGetter indexGetter);
75+
6876
} // namespace Consensus
6977

7078
/** Transaction validation utility functions */

src/main.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,22 +1612,11 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
16121612
REJECT_INVALID, "bad-txns-BIP30");
16131613
}
16141614
}
1615-
1616-
// BIP16 didn't become active until Apr 1 2012
1617-
int64_t nBIP16SwitchTime = 1333238400;
1618-
bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1619-
1620-
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1621-
1622-
// Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
1623-
if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus(), GetPrevIndex)) {
1624-
flags |= SCRIPT_VERIFY_DERSIG;
1625-
}
1626-
16271615
CBlockUndo blockundo;
16281616

16291617
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
16301618

1619+
unsigned int flags = Consensus::GetFlags(block, chainparams.GetConsensus(), pindex, GetPrevIndex);
16311620
int64_t nTimeStart = GetTimeMicros();
16321621
CAmount nFees = 0;
16331622
int nInputs = 0;

src/miner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
242242

243243
TxPriorityCompare comparer(fSortedByFee);
244244
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
245+
unsigned int flags = Consensus::GetFlags(*pblock, chainparams.GetConsensus(), pindexPrev, GetPrevIndex);
245246

246247
while (!vecPriority.empty())
247248
{
@@ -288,7 +289,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
288289
// Note that flags: we don't want to set mempool/IsStandard()
289290
// policy here, but we still have to ensure that the block we
290291
// create only contains transactions that are valid in new blocks.
291-
if (!Consensus::CheckTxInputsScripts(tx, state, view, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
292+
if (!Consensus::CheckTxInputsScripts(tx, state, view, flags, true))
292293
continue;
293294

294295
CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();

0 commit comments

Comments
 (0)