Skip to content

Commit 7eccdaf

Browse files
SjorsTheCharlatanryanofsky
committed
node: Track last block that received a blockTip notification
Also signal m_tip_block_cv when StopRPC is called, for consistency with g_best_block_cv. This is handled in StopRPC instead of OnRPCStopped() because the latter is deleted in a later commit. Co-authored-by: TheCharlatan <[email protected]> Co-authored-by: Ryan Ofsky <[email protected]>
1 parent ebb8215 commit 7eccdaf

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
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();

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class NodeImpl : public Node
139139
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
140140
if (args().GetBoolArg("-server", false)) {
141141
InterruptRPC();
142-
StopRPC();
142+
StopRPC(m_context);
143143
}
144144
}
145145
bool shutdownRequested() override { return ShutdownRequested(*Assert(m_context)); };

src/node/kernel_notifications.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ namespace node {
5050

5151
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
5252
{
53+
{
54+
LOCK(m_tip_block_mutex);
55+
m_tip_block = index.GetBlockHash();
56+
m_tip_block_cv.notify_all();
57+
}
58+
5359
uiInterface.NotifyBlockTip(state, &index);
5460
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
5561
if (!m_shutdown()) {

src/node/kernel_notifications.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include <kernel/notifications_interface.h>
99

10+
#include <sync.h>
11+
#include <threadsafety.h>
12+
#include <uint256.h>
13+
1014
#include <atomic>
1115
#include <cstdint>
1216

@@ -34,7 +38,7 @@ class KernelNotifications : public kernel::Notifications
3438
KernelNotifications(util::SignalInterrupt& shutdown, std::atomic<int>& exit_status, node::Warnings& warnings)
3539
: m_shutdown(shutdown), m_exit_status{exit_status}, m_warnings{warnings} {}
3640

37-
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override;
41+
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
3842

3943
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
4044

@@ -52,6 +56,12 @@ class KernelNotifications : public kernel::Notifications
5256
int m_stop_at_height{DEFAULT_STOPATHEIGHT};
5357
//! Useful for tests, can be set to false to avoid shutdown on fatal error.
5458
bool m_shutdown_on_fatal_error{true};
59+
60+
Mutex m_tip_block_mutex;
61+
std::condition_variable m_tip_block_cv;
62+
//! The block for which the last blockTip notification was received for.
63+
uint256 m_tip_block;
64+
5565
private:
5666
util::SignalInterrupt& m_shutdown;
5767
std::atomic<int>& m_exit_status;

src/rpc/server.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <common/system.h>
1212
#include <logging.h>
1313
#include <node/context.h>
14+
#include <node/kernel_notifications.h>
1415
#include <rpc/server_util.h>
1516
#include <rpc/util.h>
1617
#include <sync.h>
@@ -311,16 +312,19 @@ void InterruptRPC()
311312
});
312313
}
313314

314-
void StopRPC()
315+
void StopRPC(const std::any& context)
315316
{
316317
static std::once_flag g_rpc_stop_flag;
317318
// This function could be called twice if the GUI has been started with -server=1.
318319
assert(!g_rpc_running);
319-
std::call_once(g_rpc_stop_flag, []() {
320+
std::call_once(g_rpc_stop_flag, [&]() {
320321
LogDebug(BCLog::RPC, "Stopping RPC\n");
321322
WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
322323
DeleteAuthCookie();
323324
g_rpcSignals.Stopped();
325+
node::NodeContext& node = EnsureAnyNodeContext(context);
326+
// The notifications interface doesn't exist between initialization step 4a and 7.
327+
if (node.notifications) node.notifications->m_tip_block_cv.notify_all();
324328
});
325329
}
326330

src/rpc/server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rpc/request.h>
1010
#include <rpc/util.h>
1111

12+
#include <any>
1213
#include <functional>
1314
#include <map>
1415
#include <stdint.h>
@@ -178,7 +179,7 @@ extern CRPCTable tableRPC;
178179

179180
void StartRPC();
180181
void InterruptRPC();
181-
void StopRPC();
182+
void StopRPC(const std::any& context);
182183
UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
183184

184185
#endif // BITCOIN_RPC_SERVER_H

0 commit comments

Comments
 (0)