diff --git a/src/consensus/activation.h b/src/consensus/activation.h --- a/src/consensus/activation.h +++ b/src/consensus/activation.h @@ -10,19 +10,26 @@ class CBlockIndex; class Config; +namespace Consensus { +struct Params; +} + /** Check if UAHF has activated. */ -bool IsUAHFenabled(const Config &config, const CBlockIndex *pindexPrev); +bool IsUAHFenabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev); /** Check if DAA HF has activated. */ -bool IsDAAEnabled(const Config &config, const CBlockIndex *pindexPrev); +bool IsDAAEnabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev); /** Check if Nov 15, 2018 HF has activated using block height. */ -bool IsMagneticAnomalyEnabled(const Config &config, int32_t nHeight); +bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, int32_t nHeight); /** Check if Nov 15, 2018 HF has activated using previous block index. */ -bool IsMagneticAnomalyEnabled(const Config &config, +bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, const CBlockIndex *pindexPrev); /** Check if Nov 15th, 2019 protocol upgrade has activated. */ -bool IsGravitonEnabled(const Config &config, const CBlockIndex *pindexPrev); +bool IsGravitonEnabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev); #endif // BITCOIN_CONSENSUS_ACTIVATION_H diff --git a/src/consensus/activation.cpp b/src/consensus/activation.cpp --- a/src/consensus/activation.cpp +++ b/src/consensus/activation.cpp @@ -5,55 +5,56 @@ #include #include -#include -#include +#include #include -static bool IsUAHFenabled(const Config &config, int nHeight) { - return nHeight >= config.GetChainParams().GetConsensus().uahfHeight; +static bool IsUAHFenabled(const Consensus::Params ¶ms, int nHeight) { + return nHeight >= params.uahfHeight; } -bool IsUAHFenabled(const Config &config, const CBlockIndex *pindexPrev) { +bool IsUAHFenabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev) { if (pindexPrev == nullptr) { return false; } - return IsUAHFenabled(config, pindexPrev->nHeight); + return IsUAHFenabled(params, pindexPrev->nHeight); } -static bool IsDAAEnabled(const Config &config, int nHeight) { - return nHeight >= config.GetChainParams().GetConsensus().daaHeight; +static bool IsDAAEnabled(const Consensus::Params ¶ms, int nHeight) { + return nHeight >= params.daaHeight; } -bool IsDAAEnabled(const Config &config, const CBlockIndex *pindexPrev) { +bool IsDAAEnabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev) { if (pindexPrev == nullptr) { return false; } - return IsDAAEnabled(config, pindexPrev->nHeight); + return IsDAAEnabled(params, pindexPrev->nHeight); } -bool IsMagneticAnomalyEnabled(const Config &config, int32_t nHeight) { - return nHeight >= - config.GetChainParams().GetConsensus().magneticAnomalyHeight; +bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, + int32_t nHeight) { + return nHeight >= params.magneticAnomalyHeight; } -bool IsMagneticAnomalyEnabled(const Config &config, +bool IsMagneticAnomalyEnabled(const Consensus::Params ¶ms, const CBlockIndex *pindexPrev) { if (pindexPrev == nullptr) { return false; } - return IsMagneticAnomalyEnabled(config, pindexPrev->nHeight); + return IsMagneticAnomalyEnabled(params, pindexPrev->nHeight); } -bool IsGravitonEnabled(const Config &config, const CBlockIndex *pindexPrev) { +bool IsGravitonEnabled(const Consensus::Params ¶ms, + const CBlockIndex *pindexPrev) { if (pindexPrev == nullptr) { return false; } return pindexPrev->GetMedianTimePast() >= - gArgs.GetArg( - "-gravitonactivationtime", - config.GetChainParams().GetConsensus().gravitonActivationTime); + gArgs.GetArg("-gravitonactivationtime", + params.gravitonActivationTime); } diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -6,7 +6,9 @@ #include #include +#include #include +#include #include #include #include @@ -47,7 +49,8 @@ "non-final transaction"); } - if (IsMagneticAnomalyEnabled(config, nHeight)) { + if (IsMagneticAnomalyEnabled(config.GetChainParams().GetConsensus(), + nHeight)) { // Size limit if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) < MIN_TX_SIZE) { diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -164,7 +164,7 @@ int nDescendantsUpdated = 0; addPackageTxs(nPackagesSelected, nDescendantsUpdated); - if (IsMagneticAnomalyEnabled(*config, pindexPrev)) { + if (IsMagneticAnomalyEnabled(chainparams.GetConsensus(), pindexPrev)) { // If magnetic anomaly is enabled, we make sure transaction are // canonically ordered. // FIXME: Use a zipped list. See T479 diff --git a/src/pow.cpp b/src/pow.cpp --- a/src/pow.cpp +++ b/src/pow.cpp @@ -104,7 +104,7 @@ return pindexPrev->nBits; } - if (IsDAAEnabled(config, pindexPrev)) { + if (IsDAAEnabled(params, pindexPrev)) { return GetNextCashWorkRequired(pindexPrev, pblock, config); } diff --git a/src/test/activation_tests.cpp b/src/test/activation_tests.cpp --- a/src/test/activation_tests.cpp +++ b/src/test/activation_tests.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -24,13 +23,12 @@ } BOOST_AUTO_TEST_CASE(isgravitonenabled) { - DummyConfig config; CBlockIndex prev; - const auto activation = - config.GetChainParams().GetConsensus().gravitonActivationTime; + const Consensus::Params ¶ms = Params().GetConsensus(); + const auto activation = params.gravitonActivationTime; - BOOST_CHECK(!IsGravitonEnabled(config, nullptr)); + BOOST_CHECK(!IsGravitonEnabled(params, nullptr)); std::array blocks; for (size_t i = 1; i < blocks.size(); ++i) { @@ -38,13 +36,13 @@ } SetMTP(blocks, activation - 1); - BOOST_CHECK(!IsGravitonEnabled(config, &blocks.back())); + BOOST_CHECK(!IsGravitonEnabled(params, &blocks.back())); SetMTP(blocks, activation); - BOOST_CHECK(IsGravitonEnabled(config, &blocks.back())); + BOOST_CHECK(IsGravitonEnabled(params, &blocks.back())); SetMTP(blocks, activation + 1); - BOOST_CHECK(IsGravitonEnabled(config, &blocks.back())); + BOOST_CHECK(IsGravitonEnabled(params, &blocks.back())); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -314,7 +314,7 @@ static FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false); static FlatFileSeq BlockFileSeq(); static FlatFileSeq UndoFileSeq(); -static uint32_t GetNextBlockScriptFlags(const Config &config, +static uint32_t GetNextBlockScriptFlags(const Consensus::Params ¶ms, const CBlockIndex *pindex); bool TestLockPointValidity(const LockPoints *lp) { @@ -415,40 +415,40 @@ state.GetRejectCode()); } -static bool IsMagneticAnomalyEnabledForCurrentBlock(const Config &config) { +static bool +IsMagneticAnomalyEnabledForCurrentBlock(const Consensus::Params ¶ms) { AssertLockHeld(cs_main); - return IsMagneticAnomalyEnabled(config, chainActive.Tip()); + return IsMagneticAnomalyEnabled(params, chainActive.Tip()); } -static bool IsGravitonEnabledForCurrentBlock(const Config &config) { +static bool IsGravitonEnabledForCurrentBlock(const Consensus::Params ¶ms) { AssertLockHeld(cs_main); - return IsGravitonEnabled(config, chainActive.Tip()); + return IsGravitonEnabled(params, chainActive.Tip()); } // Command-line argument "-replayprotectionactivationtime=" will // cause the node to switch to replay protected SigHash ForkID value when the // median timestamp of the previous 11 blocks is greater than or equal to // . Defaults to the pre-defined timestamp when not set. -static bool IsReplayProtectionEnabled(const Config &config, +static bool IsReplayProtectionEnabled(const Consensus::Params ¶ms, int64_t nMedianTimePast) { - return nMedianTimePast >= - gArgs.GetArg( - "-replayprotectionactivationtime", - config.GetChainParams().GetConsensus().phononActivationTime); + return nMedianTimePast >= gArgs.GetArg("-replayprotectionactivationtime", + params.phononActivationTime); } -static bool IsReplayProtectionEnabled(const Config &config, +static bool IsReplayProtectionEnabled(const Consensus::Params ¶ms, const CBlockIndex *pindexPrev) { if (pindexPrev == nullptr) { return false; } - return IsReplayProtectionEnabled(config, pindexPrev->GetMedianTimePast()); + return IsReplayProtectionEnabled(params, pindexPrev->GetMedianTimePast()); } -static bool IsReplayProtectionEnabledForCurrentBlock(const Config &config) { +static bool +IsReplayProtectionEnabledForCurrentBlock(const Consensus::Params ¶ms) { AssertLockHeld(cs_main); - return IsReplayProtectionEnabled(config, chainActive.Tip()); + return IsReplayProtectionEnabled(params, chainActive.Tip()); } // Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool @@ -499,6 +499,9 @@ std::vector &coins_to_uncache, bool test_accept) { AssertLockHeld(cs_main); + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + const CTransaction &tx = *ptx; const TxId txid = tx.GetId(); @@ -741,15 +744,15 @@ // Set extraFlags as a set of flags that needs to be activated. uint32_t extraFlags = SCRIPT_VERIFY_NONE; - if (IsReplayProtectionEnabledForCurrentBlock(config)) { + if (IsReplayProtectionEnabledForCurrentBlock(consensusParams)) { extraFlags |= SCRIPT_ENABLE_REPLAY_PROTECTION; } - if (IsMagneticAnomalyEnabledForCurrentBlock(config)) { + if (IsMagneticAnomalyEnabledForCurrentBlock(consensusParams)) { extraFlags |= SCRIPT_VERIFY_CHECKDATASIG_SIGOPS; } - if (IsGravitonEnabledForCurrentBlock(config)) { + if (IsGravitonEnabledForCurrentBlock(consensusParams)) { extraFlags |= SCRIPT_ENABLE_SCHNORR_MULTISIG; extraFlags |= SCRIPT_VERIFY_MINIMALDATA; } @@ -779,7 +782,7 @@ // invalid blocks (using TestBlockValidity), however allowing such // transactions into the mempool can be exploited as a DoS attack. uint32_t nextBlockScriptVerifyFlags = - GetNextBlockScriptFlags(config, chainActive.Tip()); + GetNextBlockScriptFlags(consensusParams, chainActive.Tip()); if (!CheckInputsFromMempoolAndCache(tx, state, view, pool, nextBlockScriptVerifyFlags, true, @@ -1572,36 +1575,33 @@ // Returns the script flags which should be checked for the block after // the given block. -static uint32_t GetNextBlockScriptFlags(const Config &config, +static uint32_t GetNextBlockScriptFlags(const Consensus::Params ¶ms, const CBlockIndex *pindex) { AssertLockHeld(cs_main); - const Consensus::Params &consensusParams = - config.GetChainParams().GetConsensus(); - uint32_t flags = SCRIPT_VERIFY_NONE; // Start enforcing P2SH (BIP16) - if ((pindex->nHeight + 1) >= consensusParams.BIP16Height) { + if ((pindex->nHeight + 1) >= params.BIP16Height) { flags |= SCRIPT_VERIFY_P2SH; } // Start enforcing the DERSIG (BIP66) rule. - if ((pindex->nHeight + 1) >= consensusParams.BIP66Height) { + if ((pindex->nHeight + 1) >= params.BIP66Height) { flags |= SCRIPT_VERIFY_DERSIG; } // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule. - if ((pindex->nHeight + 1) >= consensusParams.BIP65Height) { + if ((pindex->nHeight + 1) >= params.BIP65Height) { flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY; } // Start enforcing CSV (BIP68, BIP112 and BIP113) rule. - if ((pindex->nHeight + 1) >= consensusParams.CSVHeight) { + if ((pindex->nHeight + 1) >= params.CSVHeight) { flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY; } // If the UAHF is enabled, we start accepting replay protected txns - if (IsUAHFenabled(config, pindex)) { + if (IsUAHFenabled(params, pindex)) { flags |= SCRIPT_VERIFY_STRICTENC; flags |= SCRIPT_ENABLE_SIGHASH_FORKID; } @@ -1610,7 +1610,7 @@ // s in their signature. We also make sure that signature that are supposed // to fail (for instance in multisig or other forms of smart contracts) are // null. - if (IsDAAEnabled(config, pindex)) { + if (IsDAAEnabled(params, pindex)) { flags |= SCRIPT_VERIFY_LOW_S; flags |= SCRIPT_VERIFY_NULLFAIL; } @@ -1619,20 +1619,20 @@ // transactions using the OP_CHECKDATASIG opcode and it's verify // alternative. We also start enforcing push only signatures and // clean stack. - if (IsMagneticAnomalyEnabled(config, pindex)) { + if (IsMagneticAnomalyEnabled(params, pindex)) { flags |= SCRIPT_VERIFY_CHECKDATASIG_SIGOPS; flags |= SCRIPT_VERIFY_SIGPUSHONLY; flags |= SCRIPT_VERIFY_CLEANSTACK; } - if (IsGravitonEnabled(config, pindex)) { + if (IsGravitonEnabled(params, pindex)) { flags |= SCRIPT_ENABLE_SCHNORR_MULTISIG; flags |= SCRIPT_VERIFY_MINIMALDATA; } // We make sure this node will have replay protection during the next hard // fork. - if (IsReplayProtectionEnabled(config, pindex)) { + if (IsReplayProtectionEnabled(params, pindex)) { flags |= SCRIPT_ENABLE_REPLAY_PROTECTION; } @@ -1809,7 +1809,8 @@ nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE; } - const uint32_t flags = GetNextBlockScriptFlags(config, pindex->pprev); + const uint32_t flags = + GetNextBlockScriptFlags(consensusParams, pindex->pprev); int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1; @@ -2229,12 +2230,15 @@ return false; } + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + // If this block is deactivating a fork, we move all mempool transactions // in front of disconnectpool for reprocessing in a future // updateMempoolForReorg call if (pindexDelete->pprev != nullptr && - GetNextBlockScriptFlags(config, pindexDelete) != - GetNextBlockScriptFlags(config, pindexDelete->pprev)) { + GetNextBlockScriptFlags(consensusParams, pindexDelete) != + GetNextBlockScriptFlags(consensusParams, pindexDelete->pprev)) { LogPrint(BCLog::MEMPOOL, "Disconnecting mempool due to rewind of upgrade block\n"); if (disconnectpool) { @@ -2513,12 +2517,15 @@ g_mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); disconnectpool.removeForBlock(blockConnecting.vtx); + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + // If this block is activating a fork, we move all mempool transactions // in front of disconnectpool for reprocessing in a future // updateMempoolForReorg call if (pindexNew->pprev != nullptr && - GetNextBlockScriptFlags(config, pindexNew) != - GetNextBlockScriptFlags(config, pindexNew->pprev)) { + GetNextBlockScriptFlags(consensusParams, pindexNew) != + GetNextBlockScriptFlags(consensusParams, pindexNew->pprev)) { LogPrint(BCLog::MEMPOOL, "Disconnecting mempool due to acceptance of upgrade block\n"); disconnectpool.importMempool(g_mempool); @@ -3703,7 +3710,7 @@ : block.GetBlockTime(); const bool fIsMagneticAnomalyEnabled = - IsMagneticAnomalyEnabled(config, pindexPrev); + IsMagneticAnomalyEnabled(consensusParams, pindexPrev); // Check that all transactions are finalized const CTransaction *prevTx = nullptr;