Line data Source code
1 : // Copyright (c) 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_NODE_BLOCK_TEMPLATE_MANAGER_H
6 : #define BITCOIN_NODE_BLOCK_TEMPLATE_MANAGER_H
7 :
8 : #include <node/mining_types.h>
9 : #include <util/not_null.h>
10 : #include <util/time.h>
11 :
12 : #include <memory>
13 : #include <optional>
14 : #include <string>
15 :
16 : class CBlock;
17 : class ChainstateManager;
18 : class CTxMemPool;
19 : class uint256;
20 :
21 : namespace interfaces {
22 : struct BlockRef;
23 : } // namespace interfaces
24 :
25 : namespace node {
26 : class KernelNotifications;
27 : struct CBlockTemplate;
28 :
29 : /**
30 : * Creates block templates, submits solved blocks, and provides tip-waiting
31 : * helpers for mining code. Owns the init-time block creation args.
32 : */
33 1388 : class BlockTemplateManager
34 : {
35 : private:
36 : CTxMemPool& m_mempool;
37 : ChainstateManager& m_chainman;
38 : KernelNotifications& m_notifications;
39 : const BlockCreateOptions m_block_create_args;
40 :
41 : public:
42 : explicit BlockTemplateManager(CTxMemPool& mempool,
43 : ChainstateManager& chainman,
44 : KernelNotifications& notifications,
45 : BlockCreateOptions block_create_args = {});
46 :
47 : /** @return the block creation args set during node init. */
48 25 : const BlockCreateOptions& BlockCreateArgs() const { return m_block_create_args; }
49 :
50 : /** Create a fresh block template, applying init-time defaults to any unset options. */
51 : util::NotNullUniquePtr<CBlockTemplate> CreateNewTemplate(const BlockCreateOptions& options);
52 :
53 : /** Submit a block via ProcessNewBlock and capture validation state.
54 : * @return whether the block was accepted as a new valid block. */
55 : bool SubmitBlock(const std::shared_ptr<const CBlock>& block, std::string& reason, std::string& debug);
56 :
57 : /** Locks cs_main.
58 : * @return the active chain tip, or nullopt if none exists. */
59 : std::optional<interfaces::BlockRef> GetTip();
60 :
61 : /** Wait for the tip to differ from @p current_tip or timeout.
62 : * Waits indefinitely during startup for a non-null tip.
63 : * @return the current tip, or nullopt if the node is shutting down or
64 : * interrupt is set (not when the timeout is reached). */
65 : std::optional<interfaces::BlockRef> WaitTipChanged(const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt);
66 :
67 : /** Convenience overload for in-process callers (e.g. RPC) that have no
68 : * interrupt handle. Takes the timeout by value so the caller's variable is
69 : * left unchanged; the wait still ends on chain shutdown. */
70 : std::optional<interfaces::BlockRef> WaitTipChanged(const uint256& current_tip, MillisecondsDouble timeout = MillisecondsDouble::max());
71 :
72 : /**
73 : * Wait while the best known header extends the current chain tip AND at
74 : * least one block is being added to the tip every 3 seconds. If the tip is
75 : * sufficiently far behind, allow up to 20 seconds for the next tip update.
76 : *
77 : * It's not safe to keep waiting, because a malicious miner could announce
78 : * a header and delay revealing the block, causing all other miners using
79 : * this software to stall. At the same time, we need to balance between the
80 : * default waiting time being brief, but not ending the cooldown prematurely
81 : * when a random block is slow to download (or process).
82 : *
83 : * The cooldown only applies to createNewBlock(), which is typically called
84 : * once per connected client. Subsequent templates are provided by
85 : * waitNext().
86 : *
87 : * @param last_tip tip at the start of the cooldown window.
88 : * @param interrupt_mining set to true to interrupt the cooldown.
89 : *
90 : * @returns false if interrupted.
91 : */
92 : bool CooldownIfHeadersAhead(const interfaces::BlockRef& last_tip, bool& interrupt_mining);
93 :
94 : /** Interrupt a blocking wait. */
95 : void InterruptWait(bool& interrupt_wait);
96 :
97 : /** Return a new block template when fees rise to a certain threshold or
98 : * after a new tip; return nullptr if timeout is reached. */
99 : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(
100 : const CBlockTemplate& block_template,
101 : const BlockWaitOptions& wait_options,
102 : const BlockCreateOptions& create_options,
103 : bool& interrupt_wait);
104 : };
105 : } // namespace node
106 :
107 : #endif // BITCOIN_NODE_BLOCK_TEMPLATE_MANAGER_H
|