0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

rpc: call CreateNewBlock via miner interface

This commit is contained in:
Sjors Provoost 2024-06-10 17:03:33 +02:00
parent 404b01c436
commit 4bf2e361da
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
3 changed files with 30 additions and 12 deletions

View file

@ -8,11 +8,13 @@
#include <uint256.h> #include <uint256.h>
namespace node { namespace node {
struct CBlockTemplate;
struct NodeContext; struct NodeContext;
} // namespace node } // namespace node
class BlockValidationState; class BlockValidationState;
class CBlock; class CBlock;
class CScript;
namespace interfaces { namespace interfaces {
@ -30,6 +32,15 @@ public:
//! Returns the hash for the tip of this chain, 0 if none //! Returns the hash for the tip of this chain, 0 if none
virtual uint256 getTipHash() = 0; virtual uint256 getTipHash() = 0;
/**
* Construct a new block template
*
* @param[in] script_pub_key the coinbase output
* @param[in] use_mempool set false to omit mempool transactions
* @returns a block template
*/
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
/** /**
* Check a block is completely valid from start to finish. * Check a block is completely valid from start to finish.
* Only works on top of our current best block. * Only works on top of our current best block.

View file

@ -32,6 +32,7 @@
#include <node/context.h> #include <node/context.h>
#include <node/interface_ui.h> #include <node/interface_ui.h>
#include <node/mini_miner.h> #include <node/mini_miner.h>
#include <node/miner.h>
#include <node/transaction.h> #include <node/transaction.h>
#include <node/types.h> #include <node/types.h>
#include <node/warnings.h> #include <node/warnings.h>
@ -859,6 +860,12 @@ public:
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, chainman().ActiveChain().Tip(), /*fCheckPOW=*/false, check_merkle_root); return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, chainman().ActiveChain().Tip(), /*fCheckPOW=*/false, check_merkle_root);
} }
std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool) override
{
LOCK(::cs_main);
return BlockAssembler{chainman().ActiveChainstate(), use_mempool ? context()->mempool.get() : nullptr}.CreateNewBlock(script_pub_key);
}
NodeContext* context() override { return &m_node; } NodeContext* context() override { return &m_node; }
ChainstateManager& chainman() { return *Assert(m_node.chainman); } ChainstateManager& chainman() { return *Assert(m_node.chainman); }
NodeContext& m_node; NodeContext& m_node;

View file

@ -156,11 +156,11 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
return true; return true;
} }
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries) static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
{ {
UniValue blockHashes(UniValue::VARR); UniValue blockHashes(UniValue::VARR);
while (nGenerate > 0 && !chainman.m_interrupt) { while (nGenerate > 0 && !chainman.m_interrupt) {
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script)); std::unique_ptr<CBlockTemplate> pblocktemplate(miner.createNewBlock(coinbase_script));
if (!pblocktemplate.get()) if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
@ -241,10 +241,10 @@ static RPCHelpMan generatetodescriptor()
} }
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
const CTxMemPool& mempool = EnsureMemPool(node); Mining& miner = EnsureMining(node);
ChainstateManager& chainman = EnsureChainman(node); ChainstateManager& chainman = EnsureChainman(node);
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); return generateBlocks(chainman, miner, coinbase_script, num_blocks, max_tries);
}, },
}; };
} }
@ -287,12 +287,12 @@ static RPCHelpMan generatetoaddress()
} }
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
const CTxMemPool& mempool = EnsureMemPool(node); Mining& miner = EnsureMining(node);
ChainstateManager& chainman = EnsureChainman(node); ChainstateManager& chainman = EnsureChainman(node);
CScript coinbase_script = GetScriptForDestination(destination); CScript coinbase_script = GetScriptForDestination(destination);
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries); return generateBlocks(chainman, miner, coinbase_script, num_blocks, max_tries);
}, },
}; };
} }
@ -373,7 +373,7 @@ static RPCHelpMan generateblock()
{ {
LOCK(cs_main); LOCK(cs_main);
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler{chainman.ActiveChainstate(), nullptr}.CreateNewBlock(coinbase_script)); std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock(coinbase_script, /*use_mempool=*/false)};
if (!blocktemplate) { if (!blocktemplate) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
} }
@ -671,7 +671,6 @@ static RPCHelpMan getblocktemplate()
std::string strMode = "template"; std::string strMode = "template";
UniValue lpval = NullUniValue; UniValue lpval = NullUniValue;
std::set<std::string> setClientRules; std::set<std::string> setClientRules;
Chainstate& active_chainstate = chainman.ActiveChainstate();
if (!request.params[0].isNull()) if (!request.params[0].isNull())
{ {
const UniValue& oparam = request.params[0].get_obj(); const UniValue& oparam = request.params[0].get_obj();
@ -810,18 +809,19 @@ static RPCHelpMan getblocktemplate()
// Clear pindexPrev so future calls make a new block, despite any failures from here on // Clear pindexPrev so future calls make a new block, despite any failures from here on
pindexPrev = nullptr; pindexPrev = nullptr;
// Store the pindexBest used before CreateNewBlock, to avoid races // Store the pindexBest used before createNewBlock, to avoid races
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash()); CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash());
time_start = GetTime(); time_start = GetTime();
// Create new block // Create new block
CScript scriptDummy = CScript() << OP_TRUE; CScript scriptDummy = CScript() << OP_TRUE;
pblocktemplate = BlockAssembler{active_chainstate, &mempool}.CreateNewBlock(scriptDummy); pblocktemplate = miner.createNewBlock(scriptDummy);
if (!pblocktemplate) if (!pblocktemplate) {
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory"); throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
}
// Need to update only after we know CreateNewBlock succeeded // Need to update only after we know createNewBlock succeeded
pindexPrev = pindexPrevNew; pindexPrev = pindexPrevNew;
} }
CHECK_NONFATAL(pindexPrev); CHECK_NONFATAL(pindexPrev);