aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSjors Provoost <sjors@sprovoost.nl>2024-05-30 17:06:59 +0200
committerSjors Provoost <sjors@sprovoost.nl>2024-06-18 18:47:52 +0200
commit7b4d3249ced93ec5986500e43b324005ed89502f (patch)
tree4403ed205051e0873ff424b1bb2ac523645c9393
parent9e228351e761d8d24413bbc4ac1610b4f3dec2bf (diff)
rpc: call processNewBlock via miner interface
-rw-r--r--src/interfaces/mining.h8
-rw-r--r--src/node/interfaces.cpp5
-rw-r--r--src/rpc/mining.cpp13
3 files changed, 21 insertions, 5 deletions
diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h
index 3ebc48dffa..cd092397f3 100644
--- a/src/interfaces/mining.h
+++ b/src/interfaces/mining.h
@@ -40,6 +40,14 @@ public:
* @returns a block template
*/
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
+ /**
+ * Processes new block. A valid new block is automatically relayed to peers.
+ *
+ * @param[in] block The block we want to process.
+ * @param[out] new_block A boolean which is set to indicate if the block was first received via this call
+ * @returns If the block was processed, independently of block validity
+ */
+ virtual bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) = 0;
//! Return the number of transaction updates in the mempool,
//! used to decide whether to make a new block template.
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
index 91ee858597..2633ff1b98 100644
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -855,6 +855,11 @@ public:
return tip->GetBlockHash();
}
+ bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
+ {
+ return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
+ }
+
unsigned int getTransactionsUpdated() override
{
return context()->mempool->GetTransactionsUpdated();
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 3cca6a53fa..e404cea90f 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -129,7 +129,7 @@ static RPCHelpMan getnetworkhashps()
};
}
-static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
+static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
{
block_out.reset();
block.hashMerkleRoot = BlockMerkleRoot(block);
@@ -149,7 +149,7 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
if (!process_new_block) return true;
- if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
+ if (!miner.processNewBlock(block_out, nullptr)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
}
@@ -165,7 +165,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
std::shared_ptr<const CBlock> block_out;
- if (!GenerateBlock(chainman, pblocktemplate->block, nMaxTries, block_out, /*process_new_block=*/true)) {
+ if (!GenerateBlock(chainman, miner, pblocktemplate->block, nMaxTries, block_out, /*process_new_block=*/true)) {
break;
}
@@ -398,7 +398,7 @@ static RPCHelpMan generateblock()
std::shared_ptr<const CBlock> block_out;
uint64_t max_tries{DEFAULT_MAX_TRIES};
- if (!GenerateBlock(chainman, block, max_tries, block_out, process_new_block) || !block_out) {
+ if (!GenerateBlock(chainman, miner, block, max_tries, block_out, process_new_block) || !block_out) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
}
@@ -1049,10 +1049,13 @@ static RPCHelpMan submitblock()
}
}
+ NodeContext& node = EnsureAnyNodeContext(request.context);
+ Mining& miner = EnsureMining(node);
+
bool new_block;
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
CHECK_NONFATAL(chainman.m_options.signals)->RegisterSharedValidationInterface(sc);
- bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
+ bool accepted = miner.processNewBlock(blockptr, /*new_block=*/&new_block);
CHECK_NONFATAL(chainman.m_options.signals)->UnregisterSharedValidationInterface(sc);
if (!new_block && accepted) {
return "duplicate";