Skip to content

Commit fa0c473

Browse files
committed
Merge bitcoin/bitcoin#31196: Prune mining interface
c991cea Remove processNewBlock() from mining interface (Sjors Provoost) 9a47852 Remove getTransactionsUpdated() from mining interface (Sjors Provoost) bfc4e02 Remove testBlockValidity() from mining interface (Sjors Provoost) Pull request description: There are three methods in the mining interface that can be dropped. The Template Provider doesn't need them and other application should probably not use them either. 1. `processNewBlock()` was added in 7b4d324, but became unnecessary with the introduction of interfaces::BlockTemplate::submitSolution in 7b4d324. Dropping it was suggested in bitcoin/bitcoin#30200 (comment) 2. `getTransactionsUpdated()`: this is used in the implementation of #31003 `waitFeesChanged`. It's not very useful generically because the mempool updates very frequently. 3. `testBlockValidity()`: it might be useful for mining application to have a way to check the validity of a block template they modified, but the Stratum v2 Template Provider doesn't do that, and this method is a bit brittle (e.g. the block needs to build on the tip). ACKs for top commit: TheCharlatan: Re-ACK c991cea ryanofsky: Code review ACK c991cea. Since last review, just rebased to avoid conflicts in surrounding code, and edited a commit message tdb3: code review ACK c991cea Tree-SHA512: 2138e54f920b26e01c068b24498c6a210c5c4358138dce0702ab58185d9ae148a18f04c97ac9f043646d40f8031618d80a718a176b1ce4779c237de6fb9c4a67
2 parents ea53568 + c991cea commit fa0c473

File tree

5 files changed

+15
-67
lines changed

5 files changed

+15
-67
lines changed

src/interfaces/mining.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,6 @@ class Mining
9393
*/
9494
virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}) = 0;
9595

96-
/**
97-
* Processes new block. A valid new block is automatically relayed to peers.
98-
*
99-
* @param[in] block The block we want to process.
100-
* @param[out] new_block A boolean which is set to indicate if the block was first received via this call
101-
* @returns If the block was processed, independently of block validity
102-
*/
103-
virtual bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) = 0;
104-
105-
//! Return the number of transaction updates in the mempool,
106-
//! used to decide whether to make a new block template.
107-
virtual unsigned int getTransactionsUpdated() = 0;
108-
109-
/**
110-
* Check a block is completely valid from start to finish.
111-
* Only works on top of our current best block.
112-
* Does not check proof-of-work.
113-
*
114-
* @param[in] block the block to validate
115-
* @param[in] check_merkle_root call CheckMerkleRoot()
116-
* @param[out] state details of why a block failed to validate
117-
* @returns false if it does not build on the current tip, or any of the checks fail
118-
*/
119-
virtual bool testBlockValidity(const CBlock& block, bool check_merkle_root, BlockValidationState& state) = 0;
120-
12196
//! Get internal node context. Useful for RPC and testing,
12297
//! but not accessible across processes.
12398
virtual node::NodeContext* context() { return nullptr; }

src/ipc/capnp/mining.capnp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ interface Mining $Proxy.wrap("interfaces::Mining") {
1818
getTip @2 (context :Proxy.Context) -> (result: Common.BlockRef, hasResult: Bool);
1919
waitTipChanged @3 (context :Proxy.Context, currentTip: Data, timeout: Float64) -> (result: Common.BlockRef);
2020
createNewBlock @4 (options: BlockCreateOptions) -> (result: BlockTemplate);
21-
processNewBlock @5 (context :Proxy.Context, block: Data) -> (newBlock: Bool, result: Bool);
22-
getTransactionsUpdated @6 (context :Proxy.Context) -> (result: UInt32);
23-
testBlockValidity @7 (context :Proxy.Context, block: Data, checkMerkleRoot: Bool) -> (state: BlockValidationState, result: Bool);
2421
}
2522

2623
interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {

src/node/interfaces.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -979,29 +979,6 @@ class MinerImpl : public Mining
979979
return BlockRef{chainman().ActiveChain().Tip()->GetBlockHash(), chainman().ActiveChain().Tip()->nHeight};
980980
}
981981

982-
bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
983-
{
984-
return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
985-
}
986-
987-
unsigned int getTransactionsUpdated() override
988-
{
989-
return context()->mempool->GetTransactionsUpdated();
990-
}
991-
992-
bool testBlockValidity(const CBlock& block, bool check_merkle_root, BlockValidationState& state) override
993-
{
994-
LOCK(cs_main);
995-
CBlockIndex* tip{chainman().ActiveChain().Tip()};
996-
// Fail if the tip updated before the lock was taken
997-
if (block.hashPrevBlock != tip->GetBlockHash()) {
998-
state.Error("Block does not connect to current chain tip.");
999-
return false;
1000-
}
1001-
1002-
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root);
1003-
}
1004-
1005982
std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options) override
1006983
{
1007984
BlockAssembler::Options assemble_options{options};

src/rpc/mining.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static RPCHelpMan getnetworkhashps()
131131
};
132132
}
133133

134-
static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock&& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
134+
static bool GenerateBlock(ChainstateManager& chainman, CBlock&& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
135135
{
136136
block_out.reset();
137137
block.hashMerkleRoot = BlockMerkleRoot(block);
@@ -151,7 +151,7 @@ static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock&& b
151151

152152
if (!process_new_block) return true;
153153

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

@@ -166,7 +166,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
166166
CHECK_NONFATAL(block_template);
167167

168168
std::shared_ptr<const CBlock> block_out;
169-
if (!GenerateBlock(chainman, miner, block_template->getBlock(), nMaxTries, block_out, /*process_new_block=*/true)) {
169+
if (!GenerateBlock(chainman, block_template->getBlock(), nMaxTries, block_out, /*process_new_block=*/true)) {
170170
break;
171171
}
172172

@@ -384,16 +384,17 @@ static RPCHelpMan generateblock()
384384
RegenerateCommitments(block, chainman);
385385

386386
{
387+
LOCK(::cs_main);
387388
BlockValidationState state;
388-
if (!miner.testBlockValidity(block, /*check_merkle_root=*/false, state)) {
389-
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("testBlockValidity failed: %s", state.ToString()));
389+
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
390+
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
390391
}
391392
}
392393

393394
std::shared_ptr<const CBlock> block_out;
394395
uint64_t max_tries{DEFAULT_MAX_TRIES};
395396

396-
if (!GenerateBlock(chainman, miner, std::move(block), max_tries, block_out, process_new_block) || !block_out) {
397+
if (!GenerateBlock(chainman, std::move(block), max_tries, block_out, process_new_block) || !block_out) {
397398
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
398399
}
399400

@@ -709,12 +710,12 @@ static RPCHelpMan getblocktemplate()
709710
return "duplicate-inconclusive";
710711
}
711712

712-
// testBlockValidity only supports blocks built on the current Tip
713+
// TestBlockValidity only supports blocks built on the current Tip
713714
if (block.hashPrevBlock != tip) {
714715
return "inconclusive-not-best-prevblk";
715716
}
716717
BlockValidationState state;
717-
miner.testBlockValidity(block, /*check_merkle_root=*/true, state);
718+
TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/true);
718719
return BIP22ValidationResult(state);
719720
}
720721

@@ -742,6 +743,7 @@ static RPCHelpMan getblocktemplate()
742743
}
743744

744745
static unsigned int nTransactionsUpdatedLast;
746+
const CTxMemPool& mempool = EnsureMemPool(node);
745747

746748
if (!lpval.isNull())
747749
{
@@ -772,7 +774,7 @@ static RPCHelpMan getblocktemplate()
772774
tip = miner.waitTipChanged(hashWatchedChain, checktxtime).hash;
773775
// Timeout: Check transactions for update
774776
// without holding the mempool lock to avoid deadlocks
775-
if (miner.getTransactionsUpdated() != nTransactionsUpdatedLastLP)
777+
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
776778
break;
777779
checktxtime = std::chrono::seconds(10);
778780
}
@@ -803,13 +805,13 @@ static RPCHelpMan getblocktemplate()
803805
static int64_t time_start;
804806
static std::unique_ptr<BlockTemplate> block_template;
805807
if (!pindexPrev || pindexPrev->GetBlockHash() != tip ||
806-
(miner.getTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
808+
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
807809
{
808810
// Clear pindexPrev so future calls make a new block, despite any failures from here on
809811
pindexPrev = nullptr;
810812

811813
// Store the pindexBest used before createNewBlock, to avoid races
812-
nTransactionsUpdatedLast = miner.getTransactionsUpdated();
814+
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
813815
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(tip);
814816
time_start = GetTime();
815817

@@ -1032,13 +1034,10 @@ static RPCHelpMan submitblock()
10321034
}
10331035
}
10341036

1035-
NodeContext& node = EnsureAnyNodeContext(request.context);
1036-
Mining& miner = EnsureMining(node);
1037-
10381037
bool new_block;
10391038
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
10401039
CHECK_NONFATAL(chainman.m_options.signals)->RegisterSharedValidationInterface(sc);
1041-
bool accepted = miner.processNewBlock(blockptr, /*new_block=*/&new_block);
1040+
bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
10421041
CHECK_NONFATAL(chainman.m_options.signals)->UnregisterSharedValidationInterface(sc);
10431042
if (!new_block && accepted) {
10441043
return "duplicate";

test/functional/rpc_generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_generateblock(self):
8787
txid1 = miniwallet.send_self_transfer(from_node=node)['txid']
8888
utxo1 = miniwallet.get_utxo(txid=txid1)
8989
rawtx2 = miniwallet.create_self_transfer(utxo_to_spend=utxo1)['hex']
90-
assert_raises_rpc_error(-25, 'testBlockValidity failed: bad-txns-inputs-missingorspent', self.generateblock, node, address, [rawtx2, txid1])
90+
assert_raises_rpc_error(-25, 'TestBlockValidity failed: bad-txns-inputs-missingorspent', self.generateblock, node, address, [rawtx2, txid1])
9191

9292
self.log.info('Fail to generate block with txid not in mempool')
9393
missing_txid = '0000000000000000000000000000000000000000000000000000000000000000'

0 commit comments

Comments
 (0)