Skip to content

Commit 6d22b2b

Browse files
committed
Pull script verify flags calculation out of ConnectBlock
1 parent 75e898c commit 6d22b2b

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/validation.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool
312312
return EvaluateSequenceLocks(index, lockPair);
313313
}
314314

315+
// Returns the script flags which should be checked for a given block
316+
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
317+
315318
static void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
316319
int expired = pool.Expire(GetTime() - age);
317320
if (expired != 0) {
@@ -1475,6 +1478,41 @@ class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
14751478
// Protected by cs_main
14761479
static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
14771480

1481+
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams) {
1482+
AssertLockHeld(cs_main);
1483+
1484+
// BIP16 didn't become active until Apr 1 2012
1485+
int64_t nBIP16SwitchTime = 1333238400;
1486+
bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1487+
1488+
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1489+
1490+
// Start enforcing the DERSIG (BIP66) rule
1491+
if (pindex->nHeight >= consensusparams.BIP66Height) {
1492+
flags |= SCRIPT_VERIFY_DERSIG;
1493+
}
1494+
1495+
// Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
1496+
if (pindex->nHeight >= consensusparams.BIP65Height) {
1497+
flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1498+
}
1499+
1500+
// Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
1501+
if (VersionBitsState(pindex->pprev, consensusparams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
1502+
flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
1503+
}
1504+
1505+
// Start enforcing WITNESS rules using versionbits logic.
1506+
if (IsWitnessEnabled(pindex->pprev, consensusparams)) {
1507+
flags |= SCRIPT_VERIFY_WITNESS;
1508+
flags |= SCRIPT_VERIFY_NULLDUMMY;
1509+
}
1510+
1511+
return flags;
1512+
}
1513+
1514+
1515+
14781516
static int64_t nTimeCheck = 0;
14791517
static int64_t nTimeForks = 0;
14801518
static int64_t nTimeVerify = 0;
@@ -1578,34 +1616,14 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
15781616
}
15791617
}
15801618

1581-
// BIP16 didn't become active until Apr 1 2012
1582-
int64_t nBIP16SwitchTime = 1333238400;
1583-
bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1584-
1585-
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1586-
1587-
// Start enforcing the DERSIG (BIP66) rule
1588-
if (pindex->nHeight >= chainparams.GetConsensus().BIP66Height) {
1589-
flags |= SCRIPT_VERIFY_DERSIG;
1590-
}
1591-
1592-
// Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
1593-
if (pindex->nHeight >= chainparams.GetConsensus().BIP65Height) {
1594-
flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1595-
}
1596-
15971619
// Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
15981620
int nLockTimeFlags = 0;
15991621
if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
1600-
flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
16011622
nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
16021623
}
16031624

1604-
// Start enforcing WITNESS rules using versionbits logic.
1605-
if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus())) {
1606-
flags |= SCRIPT_VERIFY_WITNESS;
1607-
flags |= SCRIPT_VERIFY_NULLDUMMY;
1608-
}
1625+
// Get the script flags for this block
1626+
unsigned int flags = GetBlockScriptFlags(pindex, chainparams.GetConsensus());
16091627

16101628
int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
16111629
LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeForks * 0.000001);

0 commit comments

Comments
 (0)