Skip to content

Commit b71e558

Browse files
committed
refactoring: add pointer to mempool in BlockAssembler and refactor related code
Using only new constructors of BlockAssembler with smaller amount of arguments and removing old constructors
1 parent 6531acc commit b71e558

File tree

10 files changed

+33
-58
lines changed

10 files changed

+33
-58
lines changed

src/miner.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ BlockAssembler::Options::Options() {
5858
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
5959
}
6060

61-
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
62-
const llmq::CQuorumBlockProcessor& quorumBlockProcessor, llmq::CChainLocksHandler& clhandler,
63-
llmq::CInstantSendManager& isman, CEvoDB& evoDb, const CTxMemPool& mempool, const CChainParams& params, const Options& options)
61+
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CTxMemPool& mempool,
62+
const NodeContext& node_context, const CChainParams& params, const Options& options)
6463
: spork_manager(sporkManager),
6564
governance_manager(governanceManager),
66-
quorum_block_processor(quorumBlockProcessor),
65+
quorum_block_processor(*node_context.llmq_ctx->quorum_block_processor),
6766
chainparams(params),
6867
m_mempool(mempool),
69-
m_clhandler(clhandler),
70-
m_isman(isman),
71-
m_evoDb(evoDb)
68+
m_clhandler(*node_context.llmq_ctx->clhandler),
69+
m_isman(*node_context.llmq_ctx->isman),
70+
m_evoDb(*node_context.evodb)
7271
{
7372
blockMinFeeRate = options.blockMinFeeRate;
7473
// Limit size to between 1K and MaxBlockSize()-1K for sanity:
@@ -92,18 +91,9 @@ static BlockAssembler::Options DefaultOptions()
9291
return options;
9392
}
9493

95-
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
96-
const llmq::CQuorumBlockProcessor& quorumBlockProcessor, llmq::CChainLocksHandler& clhandler,
97-
llmq::CInstantSendManager& isman, CEvoDB& evoDb, const CTxMemPool& mempool, const CChainParams& params)
98-
: BlockAssembler(sporkManager, governanceManager, quorumBlockProcessor, clhandler, isman, evoDb, mempool, params, DefaultOptions()) {}
99-
100-
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
94+
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CTxMemPool& mempool,
10195
const NodeContext& node_context, const CChainParams& params)
102-
: BlockAssembler(sporkManager, governanceManager, node_context, params, DefaultOptions()) {}
103-
104-
BlockAssembler::BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
105-
const NodeContext& node_context, const CChainParams& params, const Options& options)
106-
: BlockAssembler(sporkManager, governanceManager, *node_context.llmq_ctx->quorum_block_processor, *node_context.llmq_ctx->clhandler, *node_context.llmq_ctx->isman, *node_context.evodb, *node_context.mempool, params, options) {}
96+
: BlockAssembler(sporkManager, governanceManager, mempool, node_context, params, DefaultOptions()) {}
10797

10898
void BlockAssembler::resetBlock()
10999
{

src/miner.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,12 @@ class BlockAssembler
170170
CFeeRate blockMinFeeRate;
171171
};
172172

173-
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
173+
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CTxMemPool& mempool,
174174
const NodeContext& node_context, const CChainParams& params);
175175

176-
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
176+
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CTxMemPool& mempool,
177177
const NodeContext& node_context, const CChainParams& params, const Options& options);
178178

179-
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
180-
const llmq::CQuorumBlockProcessor& quorumBlockProcessor, llmq::CChainLocksHandler& clhandler,
181-
llmq::CInstantSendManager& isman, CEvoDB& evoDb, const CTxMemPool& mempool, const CChainParams& params);
182-
183-
explicit BlockAssembler(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
184-
const llmq::CQuorumBlockProcessor& quorumBlockProcessor, llmq::CChainLocksHandler& clhandler,
185-
llmq::CInstantSendManager& isman, CEvoDB& evoDb, const CTxMemPool& mempool, const CChainParams& params, const Options& options);
186-
187179
/** Construct a new block template with coinbase to scriptPubKeyIn */
188180
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
189181

src/rpc/mining.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
146146
}
147147

148148
// quorum_block_processor, clhandler, isman, evodb, mempool,
149-
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, CEvoDB& evodb,
150-
llmq::CQuorumBlockProcessor& quorum_block_processor, llmq::CChainLocksHandler& clhandler,
151-
llmq::CInstantSendManager& isman, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
149+
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const NodeContext& node_context,
150+
const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
152151
{
153152
int nHeightEnd = 0;
154153
int nHeight = 0;
@@ -162,7 +161,8 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
162161
UniValue blockHashes(UniValue::VARR);
163162
while (nHeight < nHeightEnd && !ShutdownRequested())
164163
{
165-
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(*sporkManager, *governance, quorum_block_processor, clhandler, isman, evodb, mempool, Params()).CreateNewBlock(coinbase_script));
164+
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(*sporkManager, *governance, mempool, node_context,
165+
Params()).CreateNewBlock(coinbase_script));
166166
if (!pblocktemplate.get())
167167
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
168168
CBlock *pblock = &pblocktemplate->block;
@@ -180,13 +180,6 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
180180
return blockHashes;
181181
}
182182

183-
static UniValue generateBlocks(ChainstateManager& chainman, const NodeContext& node_context,
184-
const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
185-
{
186-
return generateBlocks(chainman, *node_context.mempool, *node_context.evodb, *node_context.llmq_ctx->quorum_block_processor, *node_context.llmq_ctx->clhandler,
187-
*node_context.llmq_ctx->isman, coinbase_script, nGenerate, nMaxTries);
188-
}
189-
190183
static bool getScriptFromDescriptor(const std::string& descriptor, CScript& script, std::string& error)
191184
{
192185
FlatSigningProvider key_provider;
@@ -253,10 +246,10 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
253246

254247
const NodeContext& node_context = EnsureNodeContext(request.context);
255248
ChainstateManager& chainman = EnsureChainman(request.context);
256-
EnsureMemPool(request.context);
249+
const CTxMemPool& mempool = EnsureMemPool(request.context);
257250
EnsureLLMQContext(request.context);
258251

259-
return generateBlocks(chainman, node_context, coinbase_script, num_blocks, max_tries);
252+
return generateBlocks(chainman, mempool, node_context, coinbase_script, num_blocks, max_tries);
260253
}
261254

262255
static UniValue generatetoaddress(const JSONRPCRequest& request)
@@ -293,12 +286,12 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
293286

294287
const NodeContext& node_context = EnsureNodeContext(request.context);
295288
ChainstateManager& chainman = EnsureChainman(request.context);
296-
EnsureMemPool(request.context);
289+
const CTxMemPool& mempool = EnsureMemPool(request.context);
297290
EnsureLLMQContext(request.context);
298291

299292
CScript coinbase_script = GetScriptForDestination(destination);
300293

301-
return generateBlocks(chainman, node_context, coinbase_script, nGenerate, nMaxTries);
294+
return generateBlocks(chainman, mempool, node_context, coinbase_script, nGenerate, nMaxTries);
302295
}
303296

304297
static UniValue generateblock(const JSONRPCRequest& request)
@@ -375,7 +368,7 @@ static UniValue generateblock(const JSONRPCRequest& request)
375368
LOCK(cs_main);
376369

377370
CTxMemPool empty_mempool;
378-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(*sporkManager, *governance, *llmq_ctx.quorum_block_processor, *llmq_ctx.clhandler, *llmq_ctx.isman, *node_context.evodb, empty_mempool, chainparams).CreateNewBlock(coinbase_script));
371+
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(*sporkManager, *governance, empty_mempool, node_context, chainparams).CreateNewBlock(coinbase_script));
379372
if (!blocktemplate) {
380373
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
381374
}
@@ -787,7 +780,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
787780
// Create new block
788781
CScript scriptDummy = CScript() << OP_TRUE;
789782
LLMQContext& llmq_ctx = EnsureLLMQContext(request.context);
790-
pblocktemplate = BlockAssembler(*sporkManager, *governance, *llmq_ctx.quorum_block_processor, *llmq_ctx.clhandler, *llmq_ctx.isman, *node_context.evodb, mempool, Params()).CreateNewBlock(scriptDummy);
783+
pblocktemplate = BlockAssembler(*sporkManager, *governance, mempool, node, Params()).CreateNewBlock(scriptDummy);
791784
if (!pblocktemplate)
792785
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
793786

src/test/block_reward_reallocation_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
219219
BOOST_CHECK_EQUAL(VersionBitsTipState(consensus_params, deployment_id), ThresholdState::STARTED);
220220
BOOST_CHECK_EQUAL(VersionBitsTipStatistics(consensus_params, deployment_id).threshold, threshold(0));
221221
// Next block should be signaling by default
222-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
222+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
223223
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
224224
BOOST_CHECK_EQUAL(::ChainActive().Tip()->nVersion & bitmask, 0);
225225
BOOST_CHECK_EQUAL(pblocktemplate->block.nVersion & bitmask, bitmask);
@@ -286,7 +286,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
286286
deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip());
287287
BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash()));
288288
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
289-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
289+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
290290
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
291291
}
292292

@@ -297,7 +297,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
297297
{
298298
LOCK(cs_main);
299299
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
300-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
300+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
301301
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 13748571607);
302302
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
303303
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 6874285801); // 0.4999999998
@@ -312,7 +312,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
312312
}
313313
LOCK(cs_main);
314314
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
315-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
315+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
316316
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
317317
}
318318
}
@@ -321,7 +321,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
321321
// Reward split should reach ~60/40 after reallocation is done
322322
LOCK(cs_main);
323323
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
324-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
324+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
325325
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 10221599170);
326326
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
327327
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 6132959502); // 0.6
@@ -335,15 +335,15 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
335335
}
336336
LOCK(cs_main);
337337
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
338-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
338+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
339339
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
340340
}
341341

342342
{
343343
// Reward split should reach ~60/40 after reallocation is done
344344
LOCK(cs_main);
345345
auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
346-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
346+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
347347
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 9491484944);
348348
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
349349
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 5694890966); // 0.6

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
6969
const CScript& scriptPubKey)
7070
{
7171
const CChainParams& chainparams = Params();
72-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, chainparams).CreateNewBlock(scriptPubKey);
72+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, chainparams).CreateNewBlock(scriptPubKey);
7373
CBlock& block = pblocktemplate->block;
7474
block.hashPrevBlock = prev->GetBlockHash();
7575
block.nTime = prev->nTime + 1;

src/test/dynamic_activation_thresholds_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct TestChainDATSetup : public TestChainSetup
7979
BOOST_CHECK_EQUAL(VersionBitsTipState(consensus_params, deployment_id), ThresholdState::STARTED);
8080
BOOST_CHECK_EQUAL(VersionBitsTipStatistics(consensus_params, deployment_id).threshold, threshold(0));
8181
// Next block should be signaling by default
82-
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(coinbasePubKey);
82+
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(coinbasePubKey);
8383
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
8484
BOOST_CHECK_EQUAL(::ChainActive().Tip()->nVersion & bitmask, 0);
8585
BOOST_CHECK_EQUAL(pblocktemplate->block.nVersion & bitmask, bitmask);

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
5151

5252
options.nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
5353
options.blockMinFeeRate = blockMinFeeRate;
54-
return BlockAssembler(*sporkManager, *governance, m_node, params, options);
54+
return BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, params, options);
5555
}
5656

5757
constexpr static struct {

src/test/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coi
8181
{
8282
assert(node.mempool);
8383
auto block = std::make_shared<CBlock>(
84-
BlockAssembler{*sporkManager, *governance, node, Params()}
84+
BlockAssembler{*sporkManager, *governance, *node.mempool, node, Params()}
8585
.CreateNewBlock(coinbase_scriptPubKey)
8686
->block);
8787

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ CBlock TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransacti
308308
CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
309309
{
310310
const CChainParams& chainparams = Params();
311-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*sporkManager, *governance, m_node, chainparams).CreateNewBlock(scriptPubKey);
311+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, chainparams).CreateNewBlock(scriptPubKey);
312312
CBlock& block = pblocktemplate->block;
313313

314314
std::vector<CTransactionRef> llmqCommitments;

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
7575
CScript pubKey;
7676
pubKey << i++ << OP_TRUE;
7777

78-
auto ptemplate = BlockAssembler(*sporkManager, *governance, m_node, Params()).CreateNewBlock(pubKey);
78+
auto ptemplate = BlockAssembler(*sporkManager, *governance, *m_node.mempool, m_node, Params()).CreateNewBlock(pubKey);
7979
auto pblock = std::make_shared<CBlock>(ptemplate->block);
8080
pblock->hashPrevBlock = prev_hash;
8181
pblock->nTime = ++time;

0 commit comments

Comments
 (0)