Skip to content

Commit 4bf2e36

Browse files
committed
rpc: call CreateNewBlock via miner interface
1 parent 404b01c commit 4bf2e36

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/interfaces/mining.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include <uint256.h>
99

1010
namespace node {
11+
struct CBlockTemplate;
1112
struct NodeContext;
1213
} // namespace node
1314

1415
class BlockValidationState;
1516
class CBlock;
17+
class CScript;
1618

1719
namespace interfaces {
1820

@@ -30,6 +32,15 @@ class Mining
3032
//! Returns the hash for the tip of this chain, 0 if none
3133
virtual uint256 getTipHash() = 0;
3234

35+
/**
36+
* Construct a new block template
37+
*
38+
* @param[in] script_pub_key the coinbase output
39+
* @param[in] use_mempool set false to omit mempool transactions
40+
* @returns a block template
41+
*/
42+
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
43+
3344
/**
3445
* Check a block is completely valid from start to finish.
3546
* Only works on top of our current best block.

src/node/interfaces.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <node/context.h>
3333
#include <node/interface_ui.h>
3434
#include <node/mini_miner.h>
35+
#include <node/miner.h>
3536
#include <node/transaction.h>
3637
#include <node/types.h>
3738
#include <node/warnings.h>
@@ -859,6 +860,12 @@ class MinerImpl : public Mining
859860
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, chainman().ActiveChain().Tip(), /*fCheckPOW=*/false, check_merkle_root);
860861
}
861862

863+
std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool) override
864+
{
865+
LOCK(::cs_main);
866+
return BlockAssembler{chainman().ActiveChainstate(), use_mempool ? context()->mempool.get() : nullptr}.CreateNewBlock(script_pub_key);
867+
}
868+
862869
NodeContext* context() override { return &m_node; }
863870
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
864871
NodeContext& m_node;

src/rpc/mining.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
156156
return true;
157157
}
158158

159-
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
159+
static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
160160
{
161161
UniValue blockHashes(UniValue::VARR);
162162
while (nGenerate > 0 && !chainman.m_interrupt) {
163-
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script));
163+
std::unique_ptr<CBlockTemplate> pblocktemplate(miner.createNewBlock(coinbase_script));
164164
if (!pblocktemplate.get())
165165
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
166166

@@ -241,10 +241,10 @@ static RPCHelpMan generatetodescriptor()
241241
}
242242

243243
NodeContext& node = EnsureAnyNodeContext(request.context);
244-
const CTxMemPool& mempool = EnsureMemPool(node);
244+
Mining& miner = EnsureMining(node);
245245
ChainstateManager& chainman = EnsureChainman(node);
246246

247-
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
247+
return generateBlocks(chainman, miner, coinbase_script, num_blocks, max_tries);
248248
},
249249
};
250250
}
@@ -287,12 +287,12 @@ static RPCHelpMan generatetoaddress()
287287
}
288288

289289
NodeContext& node = EnsureAnyNodeContext(request.context);
290-
const CTxMemPool& mempool = EnsureMemPool(node);
290+
Mining& miner = EnsureMining(node);
291291
ChainstateManager& chainman = EnsureChainman(node);
292292

293293
CScript coinbase_script = GetScriptForDestination(destination);
294294

295-
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
295+
return generateBlocks(chainman, miner, coinbase_script, num_blocks, max_tries);
296296
},
297297
};
298298
}
@@ -373,7 +373,7 @@ static RPCHelpMan generateblock()
373373
{
374374
LOCK(cs_main);
375375

376-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler{chainman.ActiveChainstate(), nullptr}.CreateNewBlock(coinbase_script));
376+
std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock(coinbase_script, /*use_mempool=*/false)};
377377
if (!blocktemplate) {
378378
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
379379
}
@@ -671,7 +671,6 @@ static RPCHelpMan getblocktemplate()
671671
std::string strMode = "template";
672672
UniValue lpval = NullUniValue;
673673
std::set<std::string> setClientRules;
674-
Chainstate& active_chainstate = chainman.ActiveChainstate();
675674
if (!request.params[0].isNull())
676675
{
677676
const UniValue& oparam = request.params[0].get_obj();
@@ -810,18 +809,19 @@ static RPCHelpMan getblocktemplate()
810809
// Clear pindexPrev so future calls make a new block, despite any failures from here on
811810
pindexPrev = nullptr;
812811

813-
// Store the pindexBest used before CreateNewBlock, to avoid races
812+
// Store the pindexBest used before createNewBlock, to avoid races
814813
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
815814
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash());
816815
time_start = GetTime();
817816

818817
// Create new block
819818
CScript scriptDummy = CScript() << OP_TRUE;
820-
pblocktemplate = BlockAssembler{active_chainstate, &mempool}.CreateNewBlock(scriptDummy);
821-
if (!pblocktemplate)
819+
pblocktemplate = miner.createNewBlock(scriptDummy);
820+
if (!pblocktemplate) {
822821
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
822+
}
823823

824-
// Need to update only after we know CreateNewBlock succeeded
824+
// Need to update only after we know createNewBlock succeeded
825825
pindexPrev = pindexPrevNew;
826826
}
827827
CHECK_NONFATAL(pindexPrev);

0 commit comments

Comments
 (0)