kernel_notifications.h raw
1 // Copyright (c) 2023 The Limenka 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 LIMENKA_NODE_KERNEL_NOTIFICATIONS_H
6 #define LIMENKA_NODE_KERNEL_NOTIFICATIONS_H
7
8 #include <kernel/notifications_interface.h>
9
10 #include <sync.h>
11 #include <threadsafety.h>
12 #include <uint256.h>
13
14 #include <atomic>
15 #include <cstdint>
16 #include <functional>
17
18 class ArgsManager;
19 class CBlockIndex;
20 enum class SynchronizationState;
21 struct bilingual_str;
22
23 namespace kernel {
24 enum class Warning;
25 } // namespace kernel
26
27 namespace node {
28
29 class Warnings;
30 static constexpr int DEFAULT_STOPATHEIGHT{0};
31
32 class KernelNotifications : public kernel::Notifications
33 {
34 public:
35 KernelNotifications(const std::function<bool()>& shutdown_request, std::atomic<int>& exit_status, node::Warnings& warnings)
36 : m_shutdown_request(shutdown_request), m_exit_status{exit_status}, m_warnings{warnings} {}
37
38 [[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
39
40 void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
41
42 void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override;
43
44 void warningSet(kernel::Warning id, const bilingual_str& message, bool update) override;
45
46 void warningUnset(kernel::Warning id) override;
47
48 void flushError(const bilingual_str& message) override;
49
50 void fatalError(const bilingual_str& message) override;
51
52 //! Block height after which blockTip notification will return Interrupted{}, if >0.
53 int m_stop_at_height{DEFAULT_STOPATHEIGHT};
54 //! Useful for tests, can be set to false to avoid shutdown on fatal error.
55 bool m_shutdown_on_fatal_error{true};
56
57 Mutex m_tip_block_mutex;
58 std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
59 //! The block for which the last blockTip notification was received.
60 //! It's first set when the tip is connected during node initialization.
61 //! Might be unset during an early shutdown.
62 std::optional<uint256> TipBlock() EXCLUSIVE_LOCKS_REQUIRED(m_tip_block_mutex);
63
64 private:
65 const std::function<bool()>& m_shutdown_request;
66 std::atomic<int>& m_exit_status;
67 node::Warnings& m_warnings;
68
69 std::optional<uint256> m_tip_block GUARDED_BY(m_tip_block_mutex);
70 };
71
72 void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications);
73
74 } // namespace node
75
76 #endif // LIMENKA_NODE_KERNEL_NOTIFICATIONS_H
77