Skip to content

Commit 7b4d324

Browse files
committed
rpc: call processNewBlock via miner interface
1 parent 9e22835 commit 7b4d324

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/interfaces/mining.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class Mining
4040
* @returns a block template
4141
*/
4242
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
43+
/**
44+
* Processes new block. A valid new block is automatically relayed to peers.
45+
*
46+
* @param[in] block The block we want to process.
47+
* @param[out] new_block A boolean which is set to indicate if the block was first received via this call
48+
* @returns If the block was processed, independently of block validity
49+
*/
50+
virtual bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) = 0;
4351

4452
//! Return the number of transaction updates in the mempool,
4553
//! used to decide whether to make a new block template.

src/node/interfaces.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,11 @@ class MinerImpl : public Mining
855855
return tip->GetBlockHash();
856856
}
857857

858+
bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
859+
{
860+
return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
861+
}
862+
858863
unsigned int getTransactionsUpdated() override
859864
{
860865
return context()->mempool->GetTransactionsUpdated();

src/rpc/mining.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static RPCHelpMan getnetworkhashps()
129129
};
130130
}
131131

132-
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
132+
static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
133133
{
134134
block_out.reset();
135135
block.hashMerkleRoot = BlockMerkleRoot(block);
@@ -149,7 +149,7 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
149149

150150
if (!process_new_block) return true;
151151

152-
if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
152+
if (!miner.processNewBlock(block_out, nullptr)) {
153153
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
154154
}
155155

@@ -165,7 +165,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
165165
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
166166

167167
std::shared_ptr<const CBlock> block_out;
168-
if (!GenerateBlock(chainman, pblocktemplate->block, nMaxTries, block_out, /*process_new_block=*/true)) {
168+
if (!GenerateBlock(chainman, miner, pblocktemplate->block, nMaxTries, block_out, /*process_new_block=*/true)) {
169169
break;
170170
}
171171

@@ -398,7 +398,7 @@ static RPCHelpMan generateblock()
398398
std::shared_ptr<const CBlock> block_out;
399399
uint64_t max_tries{DEFAULT_MAX_TRIES};
400400

401-
if (!GenerateBlock(chainman, block, max_tries, block_out, process_new_block) || !block_out) {
401+
if (!GenerateBlock(chainman, miner, block, max_tries, block_out, process_new_block) || !block_out) {
402402
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
403403
}
404404

@@ -1049,10 +1049,13 @@ static RPCHelpMan submitblock()
10491049
}
10501050
}
10511051

1052+
NodeContext& node = EnsureAnyNodeContext(request.context);
1053+
Mining& miner = EnsureMining(node);
1054+
10521055
bool new_block;
10531056
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
10541057
CHECK_NONFATAL(chainman.m_options.signals)->RegisterSharedValidationInterface(sc);
1055-
bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
1058+
bool accepted = miner.processNewBlock(blockptr, /*new_block=*/&new_block);
10561059
CHECK_NONFATAL(chainman.m_options.signals)->UnregisterSharedValidationInterface(sc);
10571060
if (!new_block && accepted) {
10581061
return "duplicate";

0 commit comments

Comments
 (0)