Skip to content

Commit b169499

Browse files
Merge #6933: backport: merge bitcoin#24595 (move g_versionbitscache global to ChainstateManager)
2dc0a25 lint: update circular dependencies allowlist (Kittywhiskers Van Gogh) 95d5f97 merge bitcoin#24595: move g_versionbitscache global to ChainstateManager (Kittywhiskers Van Gogh) 14a77f6 refactor: accept `ChainstateManager` in `CMNHFManager` ctor (Kittywhiskers Van Gogh) 838cfcb refactor: move `IsQuorumTypeEnabled` to `ChainstateManager` (Kittywhiskers Van Gogh) d4dc191 refactor: adapt LLMQ logic to `DeploymentActive{At,After}` (Kittywhiskers Van Gogh) 2f9210f refactor: abstract masternode structures from Qt wallet (Kittywhiskers Van Gogh) dc3f6bd trivial: force stricter lambda signature for `ForEachMN{,Shared}()` (Kittywhiskers Van Gogh) a223b8c refactor: adapt CbTx, CreditPool logic to `DeploymentActive{At,After}` (Kittywhiskers Van Gogh) b25e4fd refactor: adapt ProTx logic to `DeploymentActive{At,After}` (Kittywhiskers Van Gogh) 07ea998 partial bitcoin#24595: move g_versionbitscache global to ChainstateManager (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Depends on #6923 * To reduce the diff size owing to Dash's extensive use of hardforks, `DeploymentActive{After,At}()` calls that simply require a height check have been moved to an explicit height check. This reduces the amount of "thread the needle" refactoring needed to provide `ChainstateManager`. * As the versionbits cache is now a member of `ChainstateManager`, all `DeploymentActive{After,At}()` calls needed to be adapted to pass a reference to `ChainstateManager`, which has necessitated substantial refactoring. For this reason, the backport has been isolated into a separate PR. > ℹ️ **Note** > > This text was written back when `CDeterministicMNList` and `CDeterministicMN` needed to accept `ChainstateManager`. Through hardening, this is no longer true. The underlying logic has still been retained to allow for future forks which may impact deterministic masternode logic. * Currently, Qt code is knowledgeable of objects like `CDeterministicMNList` and `CDeterministicMN`. This was fine because none of the functions called demanded knowledge of classes explicitly isolated from Qt code like `CChainState`, access to which is instead mediated through interfaces. * As the deglobalization now requires knowledge of `ChainstateManager`, both `CDeterministicMNList` and `CDeterministicMN` have been implemented as interfaces `interfaces::MnList` and `interfaces::MnEntry` respectively. * The problem of ensuring access to the context as `CDeterministicMNList` is updated from sources that cannot set the context themselves has been managed with a workaround, namely `interfaces::MnList::copyContextTo()`. The lifecycle of `MnList` will be as follows: A blank `MnList` is initialised without a context https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/qt/clientmodel.cpp#L53 The latest `MnList` is fetched with `interfaces::EVO::getListAtChainTip()` https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/qt/clientmodel.cpp#L120-L126 `interfaces::EVO` lends its node context to the `MnList` https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/node/interfaces.cpp#L197-L206 Calls like `interfaces::MnList::getProjectedMNPayees()` now work https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/node/interfaces.cpp#L166-L174 Event handler gives us the newest masternode list but doesn't have access to node context https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/qt/clientmodel.cpp#L322-L325 Context from older masternode list is copied into newer list https://github.com/dashpay/dash/blob/1dcd28147a1eed2bb1096b5eb20073aa368b9dae/src/qt/clientmodel.cpp#L108 ## Breaking Changes None expected. ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation **(note: N/A)** - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: UdjinM6: utACK 2dc0a25 Tree-SHA512: 43523121366dd940ebf3eca3e30a7988412d438a4b11e7d9b1e27adfbdfb9778888a8f7bd67b2837bf907704e6a2c43d6884ad284dbbdf9fc8ecee17b71992cd
2 parents 6fbe514 + 2dc0a25 commit b169499

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+977
-660
lines changed

src/coinjoin/client.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,8 @@ CDeterministicMNCPtr CCoinJoinClientManager::GetRandomNotUsedMasternode()
10441044
// fill a vector
10451045
std::vector<CDeterministicMNCPtr> vpMasternodesShuffled;
10461046
vpMasternodesShuffled.reserve(nCountEnabled);
1047-
mnList.ForEachMNShared(true, [&vpMasternodesShuffled](const CDeterministicMNCPtr& dmn) {
1048-
vpMasternodesShuffled.emplace_back(dmn);
1049-
});
1047+
mnList.ForEachMNShared(/*onlyValid=*/true,
1048+
[&vpMasternodesShuffled](const auto& dmn) { vpMasternodesShuffled.emplace_back(dmn); });
10501049

10511050
// shuffle pointers
10521051
Shuffle(vpMasternodesShuffled.begin(), vpMasternodesShuffled.end(), FastRandomContext());

src/deploymentstatus.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include <type_traits>
1111

12-
VersionBitsCache g_versionbitscache;
13-
1412
/* Basic sanity checking for BuriedDeployment/DeploymentPos enums and
1513
* ValidDeployment check */
1614

src/deploymentstatus.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@
1010

1111
#include <limits>
1212

13-
/** Global cache for versionbits deployment status */
14-
extern VersionBitsCache g_versionbitscache;
15-
1613
/** Determine if a deployment is active for the next block */
1714
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep)
1815
{
1916
assert(Consensus::ValidDeployment(dep));
2017
return (pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1) >= params.DeploymentHeight(dep);
2118
}
2219

23-
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep)
20+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
2421
{
2522
assert(Consensus::ValidDeployment(dep));
26-
return ThresholdState::ACTIVE == g_versionbitscache.State(pindexPrev, params, dep);
23+
return ThresholdState::ACTIVE == versionbitscache.State(pindexPrev, params, dep);
2724
}
2825

2926
/** Determine if a deployment is active for this block */
@@ -33,10 +30,10 @@ inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params
3330
return index.nHeight >= params.DeploymentHeight(dep);
3431
}
3532

36-
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep)
33+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
3734
{
3835
assert(Consensus::ValidDeployment(dep));
39-
return DeploymentActiveAfter(index.pprev, params, dep);
36+
return DeploymentActiveAfter(index.pprev, params, dep, versionbitscache);
4037
}
4138

4239
/** Determine if a deployment is enabled (can ever be active) */

src/evo/creditpool.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
6161
const Consensus::Params& consensusParams)
6262
{
6363
// There's no CbTx before DIP0003 activation
64-
if (!DeploymentActiveAt(*block_index, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0003)) {
64+
if (!DeploymentActiveAt(*block_index, consensusParams, Consensus::DEPLOYMENT_DIP0003)) {
6565
return std::nullopt;
6666
}
6767

@@ -117,7 +117,7 @@ std::string CCreditPool::ToString() const
117117

118118
std::optional<CCreditPool> CCreditPoolManager::GetFromCache(const CBlockIndex& block_index)
119119
{
120-
if (!DeploymentActiveAt(block_index, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) return CCreditPool{};
120+
if (!DeploymentActiveAt(block_index, m_chainman.GetConsensus(), Consensus::DEPLOYMENT_V20)) return CCreditPool{};
121121

122122
const uint256 block_hash = block_index.GetBlockHash();
123123
CCreditPool pool;
@@ -148,10 +148,9 @@ void CCreditPoolManager::AddToCache(const uint256& block_hash, int height, const
148148
}
149149
}
150150

151-
CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CBlockIndex*> block_index,
152-
CCreditPool prev, const Consensus::Params& consensusParams)
151+
CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CBlockIndex*> block_index, CCreditPool prev)
153152
{
154-
std::optional<CreditPoolDataPerBlock> opt_block_data = GetCreditDataFromBlock(block_index, consensusParams);
153+
std::optional<CreditPoolDataPerBlock> opt_block_data = GetCreditDataFromBlock(block_index, m_chainman.GetConsensus());
155154
if (!opt_block_data) {
156155
// If reading of previous block is not successfully, but
157156
// prev contains credit pool related data, something strange happened
@@ -178,20 +177,21 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CB
178177
}
179178

180179
const CBlockIndex* distant_block_index{
181-
block_index->GetAncestor(block_index->nHeight - Params().CreditPoolPeriodBlocks())};
180+
block_index->GetAncestor(block_index->nHeight - m_chainman.GetParams().CreditPoolPeriodBlocks())};
182181
CAmount distantUnlocked{0};
183182
if (distant_block_index) {
184-
if (std::optional<CreditPoolDataPerBlock> distant_block{GetCreditDataFromBlock(distant_block_index, consensusParams)};
183+
if (std::optional<CreditPoolDataPerBlock> distant_block{
184+
GetCreditDataFromBlock(distant_block_index, m_chainman.GetConsensus())};
185185
distant_block) {
186186
distantUnlocked = distant_block->unlocked;
187187
}
188188
}
189189

190190
CAmount currentLimit = blockData.credit_pool;
191191
const CAmount latelyUnlocked = prev.latelyUnlocked + blockData.unlocked - distantUnlocked;
192-
if (DeploymentActiveAt(*block_index, Params().GetConsensus(), Consensus::DEPLOYMENT_V24)) {
192+
if (DeploymentActiveAt(*block_index, m_chainman, Consensus::DEPLOYMENT_V24)) {
193193
currentLimit = std::max(CAmount(0), std::min(currentLimit, LimitAmountV24 - latelyUnlocked));
194-
} else if (DeploymentActiveAt(*block_index, Params().GetConsensus(), Consensus::DEPLOYMENT_WITHDRAWALS)) {
194+
} else if (DeploymentActiveAt(*block_index, m_chainman.GetConsensus(), Consensus::DEPLOYMENT_WITHDRAWALS)) {
195195
currentLimit = std::min(currentLimit, LimitAmountV22);
196196
} else {
197197
// Unlock limits in pre-v22 are max(100, min(.10 * assetlockpool, 1000)) inside window
@@ -221,7 +221,7 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CB
221221

222222
}
223223

224-
CCreditPool CCreditPoolManager::GetCreditPool(const CBlockIndex* block_index, const Consensus::Params& consensusParams)
224+
CCreditPool CCreditPoolManager::GetCreditPool(const CBlockIndex* block_index)
225225
{
226226
std::stack<gsl::not_null<const CBlockIndex*>> to_calculate;
227227

@@ -232,14 +232,15 @@ CCreditPool CCreditPoolManager::GetCreditPool(const CBlockIndex* block_index, co
232232
}
233233
if (block_index == nullptr) poolTmp = CCreditPool{};
234234
while (!to_calculate.empty()) {
235-
poolTmp = ConstructCreditPool(to_calculate.top(), *poolTmp, consensusParams);
235+
poolTmp = ConstructCreditPool(to_calculate.top(), *poolTmp);
236236
to_calculate.pop();
237237
}
238238
return *poolTmp;
239239
}
240240

241-
CCreditPoolManager::CCreditPoolManager(CEvoDB& _evoDb) :
242-
evoDb{_evoDb}
241+
CCreditPoolManager::CCreditPoolManager(CEvoDB& _evoDb, const ChainstateManager& chainman) :
242+
evoDb{_evoDb},
243+
m_chainman{chainman}
243244
{
244245
}
245246

@@ -326,7 +327,7 @@ std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpo
326327
const CAmount blockSubsidy, BlockValidationState& state)
327328
{
328329
try {
329-
const CCreditPool creditPool = cpoolman.GetCreditPool(pindexPrev, consensusParams);
330+
const CCreditPool creditPool = cpoolman.GetCreditPool(pindexPrev);
330331
LogPrint(BCLog::CREDITPOOL, "%s: CCreditPool is %s\n", __func__, creditPool.ToString());
331332
CCreditPoolDiff creditPoolDiff(creditPool, pindexPrev, consensusParams, blockSubsidy);
332333
for (size_t i = 1; i < block.vtx.size(); ++i) {

src/evo/creditpool.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
#include <evo/assetlocktx.h>
1818

1919
#include <gsl/pointers.h>
20+
2021
#include <optional>
2122
#include <unordered_set>
2223

24+
class BlockValidationState;
2325
class CBlock;
2426
class CBlockIndex;
25-
class BlockValidationState;
27+
class ChainstateManager;
2628
class CEvoDB;
2729
class TxValidationState;
2830
namespace Consensus {
@@ -114,6 +116,7 @@ class CCreditPoolManager
114116
Uint256LruHashMap<CCreditPool> creditPoolCache GUARDED_BY(cache_mutex){CreditPoolCacheSize};
115117

116118
CEvoDB& evoDb;
119+
const ChainstateManager& m_chainman;
117120

118121
static constexpr int DISK_SNAPSHOT_PERIOD = 576; // once per day
119122

@@ -126,23 +129,22 @@ class CCreditPoolManager
126129
CCreditPoolManager() = delete;
127130
CCreditPoolManager(const CCreditPoolManager&) = delete;
128131
CCreditPoolManager& operator=(const CCreditPoolManager&) = delete;
129-
explicit CCreditPoolManager(CEvoDB& _evoDb);
132+
explicit CCreditPoolManager(CEvoDB& _evoDb, const ChainstateManager& chainman);
130133
~CCreditPoolManager();
131134

132135
/**
133136
* @return CCreditPool with data or with empty depends on activation V19 at that block
134137
* In case if block is invalid the function GetCreditPool throws an exception
135138
* it can happen if there limits of withdrawal (unlock) exceed
136139
*/
137-
CCreditPool GetCreditPool(const CBlockIndex* block, const Consensus::Params& consensusParams)
138-
EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
140+
CCreditPool GetCreditPool(const CBlockIndex* block) EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
139141

140142
private:
141143
std::optional<CCreditPool> GetFromCache(const CBlockIndex& block_index) EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
142144
void AddToCache(const uint256& block_hash, int height, const CCreditPool& pool) EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
143145

144-
CCreditPool ConstructCreditPool(const gsl::not_null<const CBlockIndex*> block_index, CCreditPool prev,
145-
const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
146+
CCreditPool ConstructCreditPool(const gsl::not_null<const CBlockIndex*> block_index, CCreditPool prev)
147+
EXCLUSIVE_LOCKS_REQUIRED(!cache_mutex);
146148
};
147149

148150
std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpoolman, const node::BlockManager& blockman, const llmq::CQuorumManager& qman,

0 commit comments

Comments
 (0)