Skip to content

Commit 323b0ac

Browse files
committed
Merge bitcoin/bitcoin#30200: Introduce Mining interface
a9716c5 rpc: call IsInitialBlockDownload via miner interface (Sjors Provoost) dda0b08 rpc: minize getTipHash() calls in gbt (Sjors Provoost) 7b4d324 rpc: call processNewBlock via miner interface (Sjors Provoost) 9e22835 rpc: getTransactionsUpdated via miner interface (Sjors Provoost) 64ebb0f Always pass options to BlockAssembler constructor (Sjors Provoost) 4bf2e36 rpc: call CreateNewBlock via miner interface (Sjors Provoost) 404b01c rpc: getblocktemplate getTipHash() via Miner interface (Sjors Provoost) d8a3496 rpc: call TestBlockValidity via miner interface (Sjors Provoost) 8ecb681 Introduce Mining interface (Sjors Provoost) Pull request description: Introduce a `Mining` interface for the `getblocktemplate`, `generateblock` and other mining RPCs to use now, and for Stratum v2 to use later. Suggested here: bitcoin/bitcoin#29346 (comment) The selection of methods added to the interface is mostly based on what the Template Provider in #29432 uses. It could be expanded further so that `rpc/mining.cpp` no longer needs `EnsureMemPool` and `EnsureChainman`. This PR should be a pure refactor. ACKs for top commit: tdb3: re ACK a9716c5 itornaza: Code review and std-tests ACK a9716c5 ryanofsky: Code review ACK a9716c5 with one minor suggestion in case you update. Only changes since last review were other small changes to the interface. Tree-SHA512: cf97f87d6e9ed89da3835a0730da3b24a7b14c8605ea221149103a5915e79598cf082a95f2bc88e33f1c450e3d4aad88aed1163a29195acca88bcace055af724
2 parents a57da5e + a9716c5 commit 323b0ac

21 files changed

+226
-52
lines changed

doc/developer-notes.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,9 @@ independent (node, wallet, GUI), are defined in
14571457
there are [`interfaces::Chain`](../src/interfaces/chain.h), used by wallet to
14581458
access the node's latest chain state,
14591459
[`interfaces::Node`](../src/interfaces/node.h), used by the GUI to control the
1460-
node, and [`interfaces::Wallet`](../src/interfaces/wallet.h), used by the GUI
1461-
to control an individual wallet. There are also more specialized interface
1460+
node, [`interfaces::Wallet`](../src/interfaces/wallet.h), used by the GUI
1461+
to control an individual wallet and [`interfaces::Mining`](../src/interfaces/mining.h),
1462+
used by RPC to generate block templates. There are also more specialized interface
14621463
types like [`interfaces::Handler`](../src/interfaces/handler.h)
14631464
[`interfaces::ChainClient`](../src/interfaces/chain.h) passed to and from
14641465
various interface methods.

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ BITCOIN_CORE_H = \
177177
interfaces/handler.h \
178178
interfaces/init.h \
179179
interfaces/ipc.h \
180+
interfaces/mining.h \
180181
interfaces/node.h \
181182
interfaces/wallet.h \
182183
kernel/blockmanager_opts.h \

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <init/common.h>
3232
#include <interfaces/chain.h>
3333
#include <interfaces/init.h>
34+
#include <interfaces/mining.h>
3435
#include <interfaces/node.h>
3536
#include <kernel/context.h>
3637
#include <key.h>
@@ -1117,6 +1118,7 @@ bool AppInitLockDataDirectory()
11171118
bool AppInitInterfaces(NodeContext& node)
11181119
{
11191120
node.chain = node.init->makeChain();
1121+
node.mining = node.init->makeMining();
11201122
return true;
11211123
}
11221124

src/init/bitcoin-node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BitcoinNodeInit : public interfaces::Init
3030
}
3131
std::unique_ptr<interfaces::Node> makeNode() override { return interfaces::MakeNode(m_node); }
3232
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
33+
std::unique_ptr<interfaces::Mining> makeMining() override { return interfaces::MakeMining(m_node); }
3334
std::unique_ptr<interfaces::WalletLoader> makeWalletLoader(interfaces::Chain& chain) override
3435
{
3536
return MakeWalletLoader(chain, *Assert(m_node.args));

src/init/bitcoin-qt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <interfaces/chain.h>
77
#include <interfaces/echo.h>
88
#include <interfaces/init.h>
9+
#include <interfaces/mining.h>
910
#include <interfaces/node.h>
1011
#include <interfaces/wallet.h>
1112
#include <node/context.h>
@@ -25,6 +26,7 @@ class BitcoinQtInit : public interfaces::Init
2526
}
2627
std::unique_ptr<interfaces::Node> makeNode() override { return interfaces::MakeNode(m_node); }
2728
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
29+
std::unique_ptr<interfaces::Mining> makeMining() override { return interfaces::MakeMining(m_node); }
2830
std::unique_ptr<interfaces::WalletLoader> makeWalletLoader(interfaces::Chain& chain) override
2931
{
3032
return MakeWalletLoader(chain, *Assert(m_node.args));

src/init/bitcoind.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <interfaces/chain.h>
77
#include <interfaces/echo.h>
88
#include <interfaces/init.h>
9+
#include <interfaces/mining.h>
910
#include <interfaces/node.h>
1011
#include <interfaces/wallet.h>
1112
#include <node/context.h>
@@ -27,6 +28,7 @@ class BitcoindInit : public interfaces::Init
2728
}
2829
std::unique_ptr<interfaces::Node> makeNode() override { return interfaces::MakeNode(m_node); }
2930
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
31+
std::unique_ptr<interfaces::Mining> makeMining() override { return interfaces::MakeMining(m_node); }
3032
std::unique_ptr<interfaces::WalletLoader> makeWalletLoader(interfaces::Chain& chain) override
3133
{
3234
return MakeWalletLoader(chain, *Assert(m_node.args));

src/interfaces/init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <interfaces/chain.h>
99
#include <interfaces/echo.h>
10+
#include <interfaces/mining.h>
1011
#include <interfaces/node.h>
1112
#include <interfaces/wallet.h>
1213

@@ -32,6 +33,7 @@ class Init
3233
virtual ~Init() = default;
3334
virtual std::unique_ptr<Node> makeNode() { return nullptr; }
3435
virtual std::unique_ptr<Chain> makeChain() { return nullptr; }
36+
virtual std::unique_ptr<Mining> makeMining() { return nullptr; }
3537
virtual std::unique_ptr<WalletLoader> makeWalletLoader(Chain& chain) { return nullptr; }
3638
virtual std::unique_ptr<Echo> makeEcho() { return nullptr; }
3739
virtual Ipc* ipc() { return nullptr; }

src/interfaces/mining.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2024 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_MINING_H
6+
#define BITCOIN_INTERFACES_MINING_H
7+
8+
#include <optional>
9+
#include <uint256.h>
10+
11+
namespace node {
12+
struct CBlockTemplate;
13+
struct NodeContext;
14+
} // namespace node
15+
16+
class BlockValidationState;
17+
class CBlock;
18+
class CScript;
19+
20+
namespace interfaces {
21+
22+
//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
23+
//! ability to create block templates.
24+
25+
class Mining
26+
{
27+
public:
28+
virtual ~Mining() {}
29+
30+
//! If this chain is exclusively used for testing
31+
virtual bool isTestChain() = 0;
32+
33+
//! Returns whether IBD is still in progress.
34+
virtual bool isInitialBlockDownload() = 0;
35+
36+
//! Returns the hash for the tip of this chain
37+
virtual std::optional<uint256> getTipHash() = 0;
38+
39+
/**
40+
* Construct a new block template
41+
*
42+
* @param[in] script_pub_key the coinbase output
43+
* @param[in] use_mempool set false to omit mempool transactions
44+
* @returns a block template
45+
*/
46+
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
47+
/**
48+
* Processes new block. A valid new block is automatically relayed to peers.
49+
*
50+
* @param[in] block The block we want to process.
51+
* @param[out] new_block A boolean which is set to indicate if the block was first received via this call
52+
* @returns If the block was processed, independently of block validity
53+
*/
54+
virtual bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) = 0;
55+
56+
//! Return the number of transaction updates in the mempool,
57+
//! used to decide whether to make a new block template.
58+
virtual unsigned int getTransactionsUpdated() = 0;
59+
60+
/**
61+
* Check a block is completely valid from start to finish.
62+
* Only works on top of our current best block.
63+
* Does not check proof-of-work.
64+
*
65+
* @param[out] state details of why a block failed to validate
66+
* @param[in] block the block to validate
67+
* @param[in] check_merkle_root call CheckMerkleRoot()
68+
* @returns false if any of the checks fail
69+
*/
70+
virtual bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root = true) = 0;
71+
72+
//! Get internal node context. Useful for RPC and testing,
73+
//! but not accessible across processes.
74+
virtual node::NodeContext* context() { return nullptr; }
75+
};
76+
77+
//! Return implementation of Mining interface.
78+
std::unique_ptr<Mining> MakeMining(node::NodeContext& node);
79+
80+
} // namespace interfaces
81+
82+
#endif // BITCOIN_INTERFACES_MINING_H

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <addrman.h>
88
#include <banman.h>
99
#include <interfaces/chain.h>
10+
#include <interfaces/mining.h>
1011
#include <kernel/context.h>
1112
#include <key.h>
1213
#include <net.h>

src/node/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PeerManager;
2727
namespace interfaces {
2828
class Chain;
2929
class ChainClient;
30+
class Mining;
3031
class Init;
3132
class WalletLoader;
3233
} // namespace interfaces
@@ -74,6 +75,7 @@ struct NodeContext {
7475
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
7576
//! Reference to chain client that should used to load or create wallets
7677
//! opened by the gui.
78+
std::unique_ptr<interfaces::Mining> mining;
7779
interfaces::WalletLoader* wallet_loader{nullptr};
7880
std::unique_ptr<CScheduler> scheduler;
7981
std::function<void()> rpc_interruption_point = [] {};

0 commit comments

Comments
 (0)