Skip to content

Commit fa20718

Browse files
committed
refactor: drop usage of chainstate globals in asset locks logic
1 parent 21cc12c commit fa20718

11 files changed

+73
-61
lines changed

src/evo/assetlocktx.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
/**
2323
* Common code for Asset Lock and Asset Unlock
2424
*/
25-
bool CheckAssetLockUnlockTx(const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state)
25+
bool CheckAssetLockUnlockTx(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state)
2626
{
2727
switch (tx.nType) {
2828
case TRANSACTION_ASSET_LOCK:
2929
return CheckAssetLockTx(tx, state);
3030
case TRANSACTION_ASSET_UNLOCK:
31-
return CheckAssetUnlockTx(qman, tx, pindexPrev, indexes, state);
31+
return CheckAssetUnlockTx(blockman, qman, tx, pindexPrev, indexes, state);
3232
default:
3333
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-not-asset-locks-at-all");
3434
}
@@ -141,7 +141,7 @@ bool CAssetUnlockPayload::VerifySig(const llmq::CQuorumManager& qman, const uint
141141
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-not-verified");
142142
}
143143

144-
bool CheckAssetUnlockTx(const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state)
144+
bool CheckAssetUnlockTx(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state)
145145
{
146146
// Some checks depends from blockchain status also, such as `known indexes` and `withdrawal limits`
147147
// They are omitted here and done by CCreditPool
@@ -171,7 +171,7 @@ bool CheckAssetUnlockTx(const llmq::CQuorumManager& qman, const CTransaction& tx
171171
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-duplicated-index");
172172
}
173173

174-
if (LOCK(cs_main); g_chainman.m_blockman.LookupBlockIndex(assetUnlockTx.getQuorumHash()) == nullptr) {
174+
if (LOCK(cs_main); blockman.LookupBlockIndex(assetUnlockTx.getQuorumHash()) == nullptr) {
175175
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-quorum-hash");
176176
}
177177

src/evo/assetlocktx.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <optional>
1616

17+
class BlockManager;
1718
class CBlockIndex;
1819
class CRangesSet;
1920
class TxValidationState;
@@ -172,8 +173,8 @@ class CAssetUnlockPayload
172173
};
173174

174175
bool CheckAssetLockTx(const CTransaction& tx, TxValidationState& state);
175-
bool CheckAssetUnlockTx(const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state);
176-
bool CheckAssetLockUnlockTx(const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state);
176+
bool CheckAssetUnlockTx(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state);
177+
bool CheckAssetLockUnlockTx(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev, const std::optional<CRangesSet>& indexes, TxValidationState& state);
177178
bool GetAssetUnlockFee(const CTransaction& tx, CAmount& txfee, TxValidationState& state);
178179

179180
#endif // BITCOIN_EVO_ASSETLOCKTX_H

src/evo/chainhelper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CChainstateHelper::CChainstateHelper(CCreditPoolManager& cpoolman, CDeterministi
1313
const CMasternodeSync& mn_sync, const CSporkManager& sporkman, const llmq::CChainLocksHandler& clhandler,
1414
const llmq::CQuorumManager& qman)
1515
: mn_payments{std::make_unique<CMNPaymentsProcessor>(dmnman, govman, chainman, consensus_params, mn_sync, sporkman)},
16-
special_tx{std::make_unique<CSpecialTxProcessor>(cpoolman, dmnman, mnhfman, qblockman, consensus_params, clhandler, qman)}
16+
special_tx{std::make_unique<CSpecialTxProcessor>(cpoolman, dmnman, mnhfman, qblockman, chainman, consensus_params, clhandler, qman)}
1717
{}
1818

1919
CChainstateHelper::~CChainstateHelper() = default;

src/evo/creditpool.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ bool CCreditPoolDiff::Unlock(const CTransaction& tx, TxValidationState& state)
267267
return true;
268268
}
269269

270-
bool CCreditPoolDiff::ProcessLockUnlockTransaction(const llmq::CQuorumManager& qman, const CTransaction& tx, TxValidationState& state)
270+
bool CCreditPoolDiff::ProcessLockUnlockTransaction(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, TxValidationState& state)
271271
{
272272
if (!tx.IsSpecialTxVersion()) return true;
273273
if (tx.nType != TRANSACTION_ASSET_LOCK && tx.nType != TRANSACTION_ASSET_UNLOCK) return true;
274274

275-
if (!CheckAssetLockUnlockTx(qman, tx, pindexPrev, pool.indexes, state)) {
275+
if (!CheckAssetLockUnlockTx(blockman, qman, tx, pindexPrev, pool.indexes, state)) {
276276
// pass the state returned by the function above
277277
return false;
278278
}
@@ -292,8 +292,9 @@ bool CCreditPoolDiff::ProcessLockUnlockTransaction(const llmq::CQuorumManager& q
292292
}
293293
}
294294

295-
std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpoolman, const llmq::CQuorumManager& qman, const CBlock& block, const CBlockIndex* pindexPrev,
296-
const Consensus::Params& consensusParams, const CAmount blockSubsidy, BlockValidationState& state)
295+
std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpoolman, const BlockManager& blockman, const llmq::CQuorumManager& qman,
296+
const CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams,
297+
const CAmount blockSubsidy, BlockValidationState& state)
297298
{
298299
try {
299300
const CCreditPool creditPool = cpoolman.GetCreditPool(pindexPrev, consensusParams);
@@ -302,7 +303,7 @@ std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpo
302303
for (size_t i = 1; i < block.vtx.size(); ++i) {
303304
const auto& tx = *block.vtx[i];
304305
TxValidationState tx_state;
305-
if (!creditPoolDiff.ProcessLockUnlockTransaction(qman, tx, tx_state)) {
306+
if (!creditPoolDiff.ProcessLockUnlockTransaction(blockman, qman, tx, tx_state)) {
306307
assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS);
307308
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
308309
strprintf("Process Lock/Unlock Transaction failed at Credit Pool (tx hash %s) %s", tx.GetHash().ToString(), tx_state.GetDebugMessage()));

src/evo/creditpool.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <optional>
2121
#include <unordered_set>
2222

23+
class BlockManager;
2324
class CBlockIndex;
2425
class BlockValidationState;
2526
class TxValidationState;
@@ -83,7 +84,7 @@ class CCreditPoolDiff {
8384
* to change amount of credit pool
8485
* @return true if transaction can be included in this block
8586
*/
86-
bool ProcessLockUnlockTransaction(const llmq::CQuorumManager& qman, const CTransaction& tx, TxValidationState& state);
87+
bool ProcessLockUnlockTransaction(const BlockManager& blockman, const llmq::CQuorumManager& qman, const CTransaction& tx, TxValidationState& state);
8788

8889
/**
8990
* this function returns total amount of credits for the next block
@@ -135,7 +136,8 @@ class CCreditPoolManager
135136
CCreditPool ConstructCreditPool(const CBlockIndex* block_index, CCreditPool prev, const Consensus::Params& consensusParams);
136137
};
137138

138-
std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpoolman, const llmq::CQuorumManager& qman, const CBlock& block, const CBlockIndex* pindexPrev,
139-
const Consensus::Params& consensusParams, const CAmount blockSubsidy, BlockValidationState& state);
139+
std::optional<CCreditPoolDiff> GetCreditPoolDiffForBlock(CCreditPoolManager& cpoolman, const BlockManager& blockman, const llmq::CQuorumManager& qman,
140+
const CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams,
141+
const CAmount blockSubsidy, BlockValidationState& state);
140142

141143
#endif

src/evo/specialtxman.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#include <llmq/blockprocessor.h>
1818
#include <llmq/commitment.h>
1919
#include <primitives/block.h>
20+
#include <validation.h>
2021

21-
static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const llmq::CQuorumManager& qman, const CTransaction& tx, const CBlockIndex* pindexPrev,
22-
const CCoinsViewCache& view, const std::optional<CRangesSet>& indexes, bool check_sigs, TxValidationState& state)
22+
static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const llmq::CQuorumManager& qman, const CTransaction& tx,
23+
const CBlockIndex* pindexPrev, const CCoinsViewCache& view, const std::optional<CRangesSet>& indexes, bool check_sigs,
24+
TxValidationState& state)
2325
{
2426
AssertLockHeld(cs_main);
2527

@@ -54,7 +56,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const llmq::CQu
5456
if (!DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
5557
return state.Invalid(TxValidationResult::TX_CONSENSUS, "assetlocks-before-v20");
5658
}
57-
return CheckAssetLockUnlockTx(qman, tx, pindexPrev, indexes, state);
59+
return CheckAssetLockUnlockTx(chainman.m_blockman, qman, tx, pindexPrev, indexes, state);
5860
case TRANSACTION_ASSET_UNLOCK:
5961
if (Params().NetworkIDString() == CBaseChainParams::REGTEST && !DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_V20)) {
6062
// TODO: adjust functional tests to make it activated by MN_RR on regtest too
@@ -63,7 +65,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const llmq::CQu
6365
if (Params().NetworkIDString() != CBaseChainParams::REGTEST && !DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_MN_RR)) {
6466
return state.Invalid(TxValidationResult::TX_CONSENSUS, "assetunlocks-before-mn_rr");
6567
}
66-
return CheckAssetLockUnlockTx(qman, tx, pindexPrev, indexes, state);
68+
return CheckAssetLockUnlockTx(chainman.m_blockman, qman, tx, pindexPrev, indexes, state);
6769
}
6870
} catch (const std::exception& e) {
6971
LogPrintf("%s -- failed: %s\n", __func__, e.what());
@@ -76,7 +78,7 @@ static bool CheckSpecialTxInner(CDeterministicMNManager& dmnman, const llmq::CQu
7678
bool CSpecialTxProcessor::CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state)
7779
{
7880
AssertLockHeld(cs_main);
79-
return CheckSpecialTxInner(m_dmnman, m_qman, tx, pindexPrev, view, std::nullopt, check_sigs, state);
81+
return CheckSpecialTxInner(m_dmnman, m_chainman, m_qman, tx, pindexPrev, view, std::nullopt, check_sigs, state);
8082
}
8183

8284
[[nodiscard]] bool CSpecialTxProcessor::ProcessSpecialTx(const CTransaction& tx, const CBlockIndex* pindex, TxValidationState& state)
@@ -155,7 +157,7 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(const CBlock& block, const CB
155157
TxValidationState tx_state;
156158
// At this moment CheckSpecialTx() and ProcessSpecialTx() may fail by 2 possible ways:
157159
// consensus failures and "TX_BAD_SPECIAL"
158-
if (!CheckSpecialTxInner(m_dmnman, m_qman, *ptr_tx, pindex->pprev, view, creditPool.indexes, fCheckCbTxMerkleRoots, tx_state)) {
160+
if (!CheckSpecialTxInner(m_dmnman, m_chainman, m_qman, *ptr_tx, pindex->pprev, view, creditPool.indexes, fCheckCbTxMerkleRoots, tx_state)) {
159161
assert(tx_state.GetResult() == TxValidationResult::TX_CONSENSUS || tx_state.GetResult() == TxValidationResult::TX_BAD_SPECIAL);
160162
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, tx_state.GetRejectReason(),
161163
strprintf("Special Transaction check failed (tx hash %s) %s", ptr_tx->GetHash().ToString(), tx_state.GetDebugMessage()));
@@ -273,10 +275,12 @@ bool CSpecialTxProcessor::UndoSpecialTxsInBlock(const CBlock& block, const CBloc
273275

274276
bool CSpecialTxProcessor::CheckCreditPoolDiffForBlock(const CBlock& block, const CBlockIndex* pindex, const CAmount blockSubsidy, BlockValidationState& state)
275277
{
278+
AssertLockHeld(cs_main);
279+
276280
try {
277281
if (!DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_V20)) return true;
278282

279-
auto creditPoolDiff = GetCreditPoolDiffForBlock(m_cpoolman, m_qman, block, pindex->pprev, m_consensus_params, blockSubsidy, state);
283+
auto creditPoolDiff = GetCreditPoolDiffForBlock(m_cpoolman, m_chainman.m_blockman, m_qman, block, pindex->pprev, m_consensus_params, blockSubsidy, state);
280284
if (!creditPoolDiff.has_value()) return false;
281285

282286
// If we get there we have v20 activated and credit pool amount must be included in block CbTx

src/evo/specialtxman.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CBlockIndex;
1717
class CCoinsViewCache;
1818
class CCreditPoolManager;
1919
class CDeterministicMNManager;
20+
class ChainstateManager;
2021
class CMNHFManager;
2122
class TxValidationState;
2223
struct MNListUpdates;
@@ -37,6 +38,7 @@ class CSpecialTxProcessor
3738
CDeterministicMNManager& m_dmnman;
3839
CMNHFManager& m_mnhfman;
3940
llmq::CQuorumBlockProcessor& m_qblockman;
41+
const ChainstateManager& m_chainman;
4042
const Consensus::Params& m_consensus_params;
4143
const llmq::CChainLocksHandler& m_clhandler;
4244
const llmq::CQuorumManager& m_qman;
@@ -46,10 +48,11 @@ class CSpecialTxProcessor
4648
[[nodiscard]] bool UndoSpecialTx(const CTransaction& tx, const CBlockIndex* pindex);
4749

4850
public:
49-
explicit CSpecialTxProcessor(CCreditPoolManager& cpoolman, CDeterministicMNManager& dmnman, CMNHFManager& mnhfman, llmq::CQuorumBlockProcessor& qblockman,
50-
const Consensus::Params& consensus_params, const llmq::CChainLocksHandler& clhandler, const llmq::CQuorumManager& qman) :
51-
m_cpoolman(cpoolman), m_dmnman{dmnman}, m_mnhfman{mnhfman}, m_qblockman{qblockman}, m_consensus_params{consensus_params}, m_clhandler{clhandler},
52-
m_qman{qman} {}
51+
explicit CSpecialTxProcessor(CCreditPoolManager& cpoolman, CDeterministicMNManager& dmnman, CMNHFManager& mnhfman,
52+
llmq::CQuorumBlockProcessor& qblockman, const ChainstateManager& chainman, const Consensus::Params& consensus_params,
53+
const llmq::CChainLocksHandler& clhandler, const llmq::CQuorumManager& qman) :
54+
m_cpoolman(cpoolman), m_dmnman{dmnman}, m_mnhfman{mnhfman}, m_qblockman{qblockman}, m_chainman(chainman), m_consensus_params{consensus_params},
55+
m_clhandler{clhandler}, m_qman{qman} {}
5356

5457
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCoinsViewCache& view, bool check_sigs, TxValidationState& state)
5558
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@@ -58,7 +61,8 @@ class CSpecialTxProcessor
5861
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
5962
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, std::optional<MNListUpdates>& updatesRet)
6063
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
61-
bool CheckCreditPoolDiffForBlock(const CBlock& block, const CBlockIndex* pindex, const CAmount blockSubsidy, BlockValidationState& state);
64+
bool CheckCreditPoolDiffForBlock(const CBlock& block, const CBlockIndex* pindex, const CAmount blockSubsidy, BlockValidationState& state)
65+
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6266
};
6367

6468
#endif // BITCOIN_EVO_SPECIALTXMAN_H

src/miner.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,19 @@ BlockAssembler::Options::Options() {
6363
}
6464

6565
BlockAssembler::BlockAssembler(CChainState& chainstate, const NodeContext& node, const CTxMemPool& mempool, const CChainParams& params, const Options& options) :
66-
chainparams(params),
67-
m_mempool(mempool),
68-
m_chainstate(chainstate),
69-
m_dmnman(*Assert(node.dmnman)),
66+
m_blockman(Assert(node.chainman)->m_blockman),
7067
m_cpoolman(*Assert(node.cpoolman)),
7168
m_chain_helper(*Assert(node.chain_helper)),
69+
m_chainstate(chainstate),
70+
m_dmnman(*Assert(node.dmnman)),
71+
m_evoDb(*Assert(node.evodb)),
7272
m_mnhfman(*Assert(node.mnhf_manager)),
73-
m_quorum_block_processor(*Assert(Assert(node.llmq_ctx)->quorum_block_processor)),
74-
m_qman(*Assert(Assert(node.llmq_ctx)->qman)),
7573
m_clhandler(*Assert(Assert(node.llmq_ctx)->clhandler)),
7674
m_isman(*Assert(Assert(node.llmq_ctx)->isman)),
77-
m_evoDb(*Assert(node.evodb))
75+
chainparams(params),
76+
m_mempool(mempool),
77+
m_quorum_block_processor(*Assert(Assert(node.llmq_ctx)->quorum_block_processor)),
78+
m_qman(*Assert(Assert(node.llmq_ctx)->qman))
7879
{
7980
blockMinFeeRate = options.blockMinFeeRate;
8081
nBlockMaxSize = options.nBlockMaxSize;
@@ -221,7 +222,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
221222
cbTx.nHeight = nHeight;
222223

223224
BlockValidationState state;
224-
if (!CalcCbTxMerkleRootMNList(*pblock, pindexPrev, cbTx.merkleRootMNList, m_dmnman, state, ::ChainstateActive().CoinsTip())) {
225+
if (!CalcCbTxMerkleRootMNList(*pblock, pindexPrev, cbTx.merkleRootMNList, m_dmnman, state, m_chainstate.CoinsTip())) {
225226
throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootMNList failed: %s", __func__, state.ToString()));
226227
}
227228
if (fDIP0008Active_context) {
@@ -236,7 +237,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
236237
LogPrintf("CreateNewBlock() h[%d] CbTx failed to find best CL. Inserting null CL\n", nHeight);
237238
}
238239
BlockValidationState state;
239-
const auto creditPoolDiff = GetCreditPoolDiffForBlock(m_cpoolman, m_qman, *pblock, pindexPrev, chainparams.GetConsensus(), blockSubsidy, state);
240+
const auto creditPoolDiff = GetCreditPoolDiffForBlock(m_cpoolman, m_blockman, m_qman, *pblock, pindexPrev, chainparams.GetConsensus(), blockSubsidy, state);
240241
if (creditPoolDiff == std::nullopt) {
241242
throw std::runtime_error(strprintf("%s: GetCreditPoolDiffForBlock failed: %s", __func__, state.ToString()));
242243
}
@@ -472,7 +473,7 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
472473
// `state` is local here because used only to log info about this specific tx
473474
TxValidationState state;
474475

475-
if (!creditPoolDiff->ProcessLockUnlockTransaction(m_qman, iter->GetTx(), state)) {
476+
if (!creditPoolDiff->ProcessLockUnlockTransaction(m_blockman, m_qman, iter->GetTx(), state)) {
476477
if (fUsingModified) {
477478
mapModifiedTx.get<ancestor_score>().erase(modit);
478479
failedTx.insert(iter);

0 commit comments

Comments
 (0)