Skip to content

Commit dabc74e

Browse files
committed
Merge bitcoin/bitcoin#30409: Introduce waitTipChanged() mining interface, replace RPCNotifyBlockChange, drop CRPCSignals & g_best_block
7942951 Remove unused g_best_block (Ryan Ofsky) e3a560c rpc: use waitTipChanged for longpoll (Ryan Ofsky) 460687a Remove unused CRPCSignals (Sjors Provoost) dca9231 Replace RPCNotifyBlockChange with waitTipChanged() (Sjors Provoost) 2a40ee1 rpc: check for negative timeout arg in waitfor* (Sjors Provoost) de7c855 rpc: recommend -rpcclienttimeout=0 for waitfor* (Sjors Provoost) 77ec072 rpc: fix waitfornewblock description (Sjors Provoost) 285fe9f rpc: add test for waitforblock and waitfornewblock (Sjors Provoost) b94b27c Add waitTipChanged to Mining interface (Sjors Provoost) 7eccdaf node: Track last block that received a blockTip notification (Sjors Provoost) ebb8215 Rename getTipHash() to getTip() and return BlockRef (Sjors Provoost) 89a8f74 refactor: rename BlockKey to BlockRef (Sjors Provoost) Pull request description: This continues the work in #30200 so that a future Stratum v2 Template Provider (see #29432) can avoid accessing node internals. It needs to know when a new block arrives in order to push new templates to connected clients. `waitTipChanged()` uses a new kernel notification `notifications().m_tip_block_mutex`, which this PR also introduces (a previous version used `g_best_block`). In order to ensure the new method works as intended, the `waitfornewblock`, `waitforblock` and `waitforblockheight` RPC methods are refactored to use it. This allows removing `RPCNotifyBlockChange`. There's a commit to add (direct) tests for the methods that are about to be refactored: - `waitfornewblock` was already implicitly tested by `feature_shutdown.py`. - `waitforblockheight` by `feature_coinstatsindex.py` and `example_test.py` This PR renames `getTipHash()` to `getTip()` and returns a `BlockRef` (renamed from `BlockKey`) so that callers can use either the height or hash. The later commits make trivial improvements to the `waitfor*` RPC calls (not needed for this PR). The `waitTipChanged()` method could probably also be used for the longpoll functionality in `getblocktemplate`, but I'm a bit reluctant to touch that. `RPCServer::OnStarted` no longer does anything and `RPCServer::OnStopped` merely prints a log statement. They were added in #5711 as a refactor. This PR drops them entirely. Finally `g_best_block` is also dropped. ACKs for top commit: achow101: ACK 7942951 ryanofsky: Code review ACK 7942951. Just rebased since last review TheCharlatan: Re-ACK 7942951 Tree-SHA512: a5559446b4000c95e07aad33284b7ee2e57aafd87e1ae778b3825d59689566d047a8047e47a10f76e6e341e7dc72fd265a65afbc0a9c011d17c4cafd55031837
2 parents 33adc75 + 7942951 commit dabc74e

22 files changed

+203
-164
lines changed

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ bool BaseIndex::Init()
113113

114114
// Child init
115115
const CBlockIndex* start_block = m_best_block_index.load();
116-
if (!CustomInit(start_block ? std::make_optional(interfaces::BlockKey{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
116+
if (!CustomInit(start_block ? std::make_optional(interfaces::BlockRef{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
117117
return false;
118118
}
119119

src/index/base.h

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

88
#include <dbwrapper.h>
99
#include <interfaces/chain.h>
10+
#include <interfaces/types.h>
1011
#include <util/string.h>
1112
#include <util/threadinterrupt.h>
1213
#include <validationinterface.h>
@@ -107,7 +108,7 @@ class BaseIndex : public CValidationInterface
107108
void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;
108109

109110
/// Initialize internal state from the database and block index.
110-
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
111+
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockRef>& block) { return true; }
111112

112113
/// Write update index entries for a newly connected block.
113114
[[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
@@ -118,7 +119,7 @@ class BaseIndex : public CValidationInterface
118119

119120
/// Rewind index to an earlier chain tip during a chain reorg. The tip must
120121
/// be an ancestor of the current best block.
121-
[[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
122+
[[nodiscard]] virtual bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) { return true; }
122123

123124
virtual DB& GetDB() const = 0;
124125

src/index/blockfilterindex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, Blo
112112
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
113113
}
114114

115-
bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
115+
bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
116116
{
117117
if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
118118
// Check that the cause of the read failure is that the key does not exist. Any other errors
@@ -316,7 +316,7 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
316316
return true;
317317
}
318318

319-
bool BlockFilterIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
319+
bool BlockFilterIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
320320
{
321321
CDBBatch batch(*m_db);
322322
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());

src/index/blockfilterindex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class BlockFilterIndex final : public BaseIndex
5252
std::optional<uint256> ReadFilterHeader(int height, const uint256& expected_block_hash);
5353

5454
protected:
55-
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
55+
bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;
5656

5757
bool CustomCommit(CDBBatch& batch) override;
5858

5959
bool CustomAppend(const interfaces::BlockInfo& block) override;
6060

61-
bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
61+
bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) override;
6262

6363
BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
6464

src/index/coinstatsindex.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
265265
return true;
266266
}
267267

268-
bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
268+
bool CoinStatsIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
269269
{
270270
CDBBatch batch(*m_db);
271271
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
@@ -304,7 +304,7 @@ bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const
304304
return true;
305305
}
306306

307-
static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, DBVal& result)
307+
static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, DBVal& result)
308308
{
309309
// First check if the result is stored under the height index and the value
310310
// there matches the block hash. This should be the case if the block is on
@@ -350,7 +350,7 @@ std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_
350350
return stats;
351351
}
352352

353-
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
353+
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
354354
{
355355
if (!m_db->Read(DB_MUHASH, m_muhash)) {
356356
// Check that the cause of the read failure is that the key does not

src/index/coinstatsindex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ class CoinStatsIndex final : public BaseIndex
4343
bool AllowPrune() const override { return true; }
4444

4545
protected:
46-
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
46+
bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;
4747

4848
bool CustomCommit(CDBBatch& batch) override;
4949

5050
bool CustomAppend(const interfaces::BlockInfo& block) override;
5151

52-
bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
52+
bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) override;
5353

5454
BaseIndex::DB& GetDB() const override { return *m_db; }
5555

src/init.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void Shutdown(NodeContext& node)
284284

285285
StopHTTPRPC();
286286
StopREST();
287-
StopRPC();
287+
StopRPC(&node);
288288
StopHTTPServer();
289289
for (const auto& client : node.chain_clients) {
290290
client->flush();
@@ -429,20 +429,6 @@ static void registerSignalHandler(int signal, void(*handler)(int))
429429
}
430430
#endif
431431

432-
static boost::signals2::connection rpc_notify_block_change_connection;
433-
static void OnRPCStarted()
434-
{
435-
rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(std::bind(RPCNotifyBlockChange, std::placeholders::_2));
436-
}
437-
438-
static void OnRPCStopped()
439-
{
440-
rpc_notify_block_change_connection.disconnect();
441-
RPCNotifyBlockChange(nullptr);
442-
g_best_block_cv.notify_all();
443-
LogDebug(BCLog::RPC, "RPC stopped.\n");
444-
}
445-
446432
void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
447433
{
448434
SetupHelpOptions(argsman);
@@ -723,8 +709,6 @@ static void StartupNotify(const ArgsManager& args)
723709
static bool AppInitServers(NodeContext& node)
724710
{
725711
const ArgsManager& args = *Assert(node.args);
726-
RPCServer::OnStarted(&OnRPCStarted);
727-
RPCServer::OnStopped(&OnRPCStopped);
728712
if (!InitHTTPServer(*Assert(node.shutdown))) {
729713
return false;
730714
}
@@ -2017,11 +2001,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20172001
// cannot yet be called. Before we make it callable, we need to make sure
20182002
// that the RPC's view of the best block is valid and consistent with
20192003
// ChainstateManager's active tip.
2020-
//
2021-
// If we do not do this, RPC's view of the best block will be height=0 and
2022-
// hash=0x0. This will lead to erroroneous responses for things like
2023-
// waitforblockheight.
2024-
RPCNotifyBlockChange(WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()));
20252004
SetRPCWarmupFinished();
20262005

20272006
uiInterface.InitMessage(_("Done loading").translated);

src/interfaces/chain.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ namespace interfaces {
4141
class Handler;
4242
class Wallet;
4343

44-
//! Hash/height pair to help track and identify blocks.
45-
struct BlockKey {
46-
uint256 hash;
47-
int height = -1;
48-
};
49-
5044
//! Helper for findBlock to selectively return pieces of block data. If block is
5145
//! found, data will be returned by setting specified output variables. If block
5246
//! is not found, output variables will keep their previous values.

src/interfaces/mining.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#define BITCOIN_INTERFACES_MINING_H
77

88
#include <consensus/amount.h> // for CAmount
9+
#include <interfaces/types.h> // for BlockRef
910
#include <node/types.h> // for BlockCreateOptions
1011
#include <primitives/block.h> // for CBlock, CBlockHeader
1112
#include <primitives/transaction.h> // for CTransactionRef
1213
#include <stdint.h> // for int64_t
1314
#include <uint256.h> // for uint256
15+
#include <util/time.h> // for MillisecondsDouble
1416

1517
#include <memory> // for unique_ptr, shared_ptr
1618
#include <optional> // for optional
@@ -55,10 +57,21 @@ class Mining
5557
//! Returns whether IBD is still in progress.
5658
virtual bool isInitialBlockDownload() = 0;
5759

58-
//! Returns the hash for the tip of this chain
59-
virtual std::optional<uint256> getTipHash() = 0;
60+
//! Returns the hash and height for the tip of this chain
61+
virtual std::optional<BlockRef> getTip() = 0;
6062

6163
/**
64+
* Waits for the tip to change
65+
*
66+
* @param[in] current_tip block hash of the current chain tip. Function waits
67+
* for the chain tip to change if this matches, otherwise
68+
* it returns right away.
69+
* @param[in] timeout how long to wait for a new tip
70+
* @returns Hash and height of the current chain tip after this call.
71+
*/
72+
virtual BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
73+
74+
/**
6275
* Construct a new block template
6376
*
6477
* @param[in] script_pub_key the coinbase output

src/interfaces/types.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_TYPES_H
6+
#define BITCOIN_INTERFACES_TYPES_H
7+
8+
#include <uint256.h>
9+
10+
namespace interfaces {
11+
12+
//! Hash/height pair to help track and identify blocks.
13+
struct BlockRef {
14+
uint256 hash;
15+
int height = -1;
16+
};
17+
18+
} // namespace interfaces
19+
20+
#endif // BITCOIN_INTERFACES_TYPES_H

0 commit comments

Comments
 (0)