Repository navigation
Expand file tree
/
Copy pathblock_template_manager.cpp
More file actions
288 lines (251 loc) · 11.6 KB
/
Copy pathblock_template_manager.cpp
File metadata and controls
288 lines (251 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <node/block_template_manager.h>
#include <chain.h>
#include <consensus/amount.h>
#include <consensus/params.h>
#include <consensus/validation.h>
#include <interfaces/types.h>
#include <kernel/chainparams.h>
#include <node/kernel_notifications.h>
#include <node/miner.h>
#include <node/mining_args.h>
#include <primitives/block.h>
#include <sync.h>
#include <uint256.h>
#include <util/check.h>
#include <util/signalinterrupt.h>
#include <validation.h>
#include <validationinterface.h>
#include <algorithm>
#include <compare>
#include <condition_variable>
#include <numeric>
#include <utility>
#include <vector>
namespace node {
using interfaces::BlockRef;
BlockTemplateManager::BlockTemplateManager(CTxMemPool& mempool, ChainstateManager& chainman,
KernelNotifications& notifications,
BlockCreateOptions block_create_args)
: m_mempool(mempool), m_chainman(chainman), m_notifications(notifications), m_block_create_args(std::move(block_create_args))
{
}
std::unique_ptr<CBlockTemplate> BlockTemplateManager::CreateNewTemplate(const BlockCreateOptions& options)
{
return BlockAssembler{
m_chainman.ActiveChainstate(),
&m_mempool,
MergeMiningOptions(options, m_block_create_args),
}.CreateNewBlock();
}
namespace {
class SubmitBlockStateCatcher final : public CValidationInterface
{
public:
uint256 m_hash;
bool m_found{false};
BlockValidationState m_state;
explicit SubmitBlockStateCatcher(const uint256& hash) : m_hash{hash} {}
protected:
void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override
{
if (block->GetHash() != m_hash) return;
// ProcessNewBlock emits BlockChecked synchronously while holding cs_main,
// so SubmitBlock can read these fields after ProcessNewBlock returns
// without extra synchronization.
m_found = true;
m_state = state;
}
};
} // namespace
bool BlockTemplateManager::SubmitBlock(const std::shared_ptr<const CBlock>& block, std::string& reason, std::string& debug)
{
reason.clear();
debug.clear();
// This follows the submitblock RPC's validation-state capture pattern, but
// is intentionally kept separate from the RPC implementation. The RPC entry
// point decodes hex, formats BIP22/JSONRPC results, and calls
// UpdateUncommittedBlockStructures() for legacy witness handling. IPC
// callers submit already-formed blocks and need bool + reason/debug
// results.
auto sc = std::make_shared<SubmitBlockStateCatcher>(block->GetHash());
CHECK_NONFATAL(m_chainman.m_options.signals)->RegisterSharedValidationInterface(sc);
bool new_block;
bool accepted = m_chainman.ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
// No queue drain is needed. The BlockChecked notification used above is
// emitted synchronously by ProcessNewBlock, unlike most validation signals.
CHECK_NONFATAL(m_chainman.m_options.signals)->UnregisterSharedValidationInterface(sc);
if (!new_block && accepted) {
reason = "duplicate";
} else if (!accepted && (!sc->m_found || sc->m_state.IsValid())) {
// ProcessNewBlock can fail without a validation result, for example
// from an activation or system error. It can also fail after a valid
// BlockChecked result. In these cases the validation result is
// inconclusive.
reason = "inconclusive";
} else if (!sc->m_found) {
// The block was accepted but not connected, for example if it does not
// have more work than the current tip.
reason = "inconclusive";
} else if (!sc->m_state.IsValid()) {
reason = sc->m_state.GetRejectReason();
debug = sc->m_state.GetDebugMessage();
}
const bool result{accepted && new_block && reason.empty()};
CHECK_NONFATAL(result == reason.empty());
return result;
}
std::optional<BlockRef> BlockTemplateManager::GetTip()
{
LOCK(::cs_main);
CBlockIndex* tip{m_chainman.ActiveChain().Tip()};
if (!tip) return {};
return BlockRef{tip->GetBlockHash(), tip->nHeight};
}
void BlockTemplateManager::InterruptWait(bool& interrupt_wait)
{
LOCK(m_notifications.m_tip_block_mutex);
interrupt_wait = true;
m_notifications.m_tip_block_cv.notify_all();
}
std::unique_ptr<CBlockTemplate> BlockTemplateManager::WaitAndCreateNewBlock(
const std::unique_ptr<CBlockTemplate>& block_template,
const BlockWaitOptions& wait_options,
const BlockCreateOptions& create_options,
bool& interrupt_wait)
{
// Delay calculating the current template fees, just in case a new block
// comes in before the next tick.
CAmount current_fees = -1;
// Alternate waiting for a new tip and checking if fees have risen.
// The latter check is expensive so we only run it once per second.
auto now{NodeClock::now()};
const auto deadline = now + wait_options.timeout;
const MillisecondsDouble tick{1000};
const bool allow_min_difficulty{m_chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
do {
bool tip_changed{false};
{
WAIT_LOCK(m_notifications.m_tip_block_mutex, lock);
// Note that wait_until() checks the predicate before waiting
m_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(m_notifications.m_tip_block_mutex) {
AssertLockHeld(m_notifications.m_tip_block_mutex);
const auto tip_block{m_notifications.TipBlock()};
// We assume tip_block is set, because this is an instance
// method on BlockTemplate and no template could have been
// generated before a tip exists.
tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
return tip_changed || m_chainman.m_interrupt || interrupt_wait;
});
if (interrupt_wait) {
interrupt_wait = false;
return nullptr;
}
}
if (m_chainman.m_interrupt) return nullptr;
// At this point the tip changed, a full tick went by or we reached
// the deadline.
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
LOCK(::cs_main);
// On test networks return a minimum difficulty block after 20 minutes
if (!tip_changed && allow_min_difficulty) {
const NodeClock::time_point tip_time{std::chrono::seconds{m_chainman.ActiveChain().Tip()->GetBlockTime()}};
if (now > tip_time + 20min) {
tip_changed = true;
}
}
/**
* We determine if fees increased compared to the previous template by generating
* a fresh template. There may be more efficient ways to determine how much
* (approximate) fees for the next block increased, perhaps more so after
* Cluster Mempool.
*
* We'll also create a new template if the tip changed during this iteration.
*/
if (wait_options.fee_threshold < MAX_MONEY || tip_changed) {
auto new_tmpl{CreateNewTemplate(create_options)};
// If the tip changed, return the new template regardless of its fees.
if (tip_changed) return new_tmpl;
// Calculate the original template total fees if we haven't already
if (current_fees == -1) {
current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0});
}
// Check if fees increased enough to return the new template
const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0});
Assume(wait_options.fee_threshold != MAX_MONEY);
if (new_fees >= current_fees + wait_options.fee_threshold) return new_tmpl;
}
now = NodeClock::now();
} while (now < deadline);
return nullptr;
}
bool BlockTemplateManager::CooldownIfHeadersAhead(const BlockRef& last_tip, bool& interrupt_mining)
{
uint256 last_tip_hash{last_tip.hash};
while (const std::optional<int> remaining = m_chainman.BlocksAheadOfTip()) {
const int cooldown_seconds = std::clamp(*remaining, 3, 20);
const auto cooldown_deadline{MockableSteadyClock::now() + std::chrono::seconds{cooldown_seconds}};
{
WAIT_LOCK(m_notifications.m_tip_block_mutex, lock);
m_notifications.m_tip_block_cv.wait_until(lock, cooldown_deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(m_notifications.m_tip_block_mutex) {
const auto tip_block = m_notifications.TipBlock();
return m_chainman.m_interrupt || interrupt_mining || (tip_block && *tip_block != last_tip_hash);
});
if (m_chainman.m_interrupt || interrupt_mining) {
interrupt_mining = false;
return false;
}
// If the tip changed during the wait, extend the deadline
const auto tip_block = m_notifications.TipBlock();
if (tip_block && *tip_block != last_tip_hash) {
last_tip_hash = *tip_block;
continue;
}
}
// No tip change and the cooldown window has expired.
if (MockableSteadyClock::now() >= cooldown_deadline) break;
}
return true;
}
std::optional<BlockRef> BlockTemplateManager::WaitTipChanged(const uint256& current_tip, MillisecondsDouble timeout)
{
bool interrupt_wait{false};
return WaitTipChanged(current_tip, timeout, interrupt_wait);
}
std::optional<BlockRef> BlockTemplateManager::WaitTipChanged(const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt)
{
Assume(timeout >= 0ms); // No internal callers should use a negative timeout
if (timeout < 0ms) timeout = 0ms;
if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
auto deadline{std::chrono::steady_clock::now() + timeout};
{
WAIT_LOCK(m_notifications.m_tip_block_mutex, lock);
// For callers convenience, wait longer than the provided timeout
// during startup for the tip to be non-null. That way this function
// always returns valid tip information when possible and only
// returns null when shutting down, not when timing out.
m_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(m_notifications.m_tip_block_mutex) {
AssertLockHeld(m_notifications.m_tip_block_mutex);
return m_notifications.TipBlock() || m_chainman.m_interrupt || interrupt;
});
if (m_chainman.m_interrupt || interrupt) {
interrupt = false;
return {};
}
// At this point TipBlock is set, so continue to wait until it is
// different from `current_tip` provided by caller.
m_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(m_notifications.m_tip_block_mutex) {
return Assume(m_notifications.TipBlock()) != current_tip || m_chainman.m_interrupt || interrupt;
});
if (m_chainman.m_interrupt || interrupt) {
interrupt = false;
return {};
}
}
// Must release m_tip_block_mutex before GetTip() locks cs_main, to
// avoid deadlocks.
return GetTip();
}
} // namespace node