Skip to content

Commit 5f77626

Browse files
committed
Add prevhash to template cache
This prevents repeating calls to getBlockHeader()
1 parent 3f699b9 commit 5f77626

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/sv2/template_provider.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void Sv2TemplateProvider::Interrupt()
130130
LOCK(m_tp_mutex);
131131
try {
132132
for (auto& t : GetBlockTemplates()) {
133-
t.second->interruptWait();
133+
t.second.second->interruptWait();
134134
}
135135
} catch (const ipc::Exception& e) {
136136
// Bitcoin Core v30 does not yet implement interruptWait(), fall back
@@ -266,6 +266,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
266266
};
267267

268268
while (!m_flag_interrupt_sv2) {
269+
uint256 prev_hash;
269270
if (!block_template) {
270271
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Generate initial block template for client id=%zu\n",
271272
client_id);
@@ -288,7 +289,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
288289
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Assemble template: %.2fms\n",
289290
Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
290291

291-
uint256 prev_hash{block_template->getBlockHeader().hashPrevBlock};
292+
prev_hash = block_template->getBlockHeader().hashPrevBlock;
292293
{
293294
LOCK(m_tp_mutex);
294295
if (prev_hash != m_best_prev_hash) {
@@ -299,7 +300,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
299300

300301
// Add template to cache before sending it, to prevent race
301302
// condition: https://github.com/stratum-mining/stratum/issues/1773
302-
m_block_template_cache.insert({template_id,block_template});
303+
m_block_template_cache.insert({template_id,std::make_pair(prev_hash, block_template)});
303304
}
304305

305306
{
@@ -346,7 +347,6 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
346347
client_id);
347348
}
348349

349-
uint256 old_prev_hash{block_template->getBlockHeader().hashPrevBlock};
350350
std::shared_ptr<BlockTemplate> tmpl = block_template->waitNext(options);
351351
// The client may have disconnected during the wait, check now to avoid
352352
// a spurious IPC call and confusing log statements.
@@ -362,7 +362,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
362362

363363
{
364364
LOCK(m_tp_mutex);
365-
if (new_prev_hash != old_prev_hash) {
365+
if (new_prev_hash != prev_hash) {
366366
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Tip changed, client id=%zu\n",
367367
client_id);
368368
future_template = true;
@@ -375,7 +375,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
375375

376376
// Add template to cache before sending it, to prevent race
377377
// condition: https://github.com/stratum-mining/stratum/issues/1773
378-
m_block_template_cache.insert({m_template_id,block_template});
378+
m_block_template_cache.insert({m_template_id, std::make_pair(new_prev_hash,block_template)});
379379
}
380380

381381
{
@@ -422,7 +422,7 @@ void Sv2TemplateProvider::RequestTransactionData(Sv2Client& client, node::Sv2Req
422422

423423
return;
424424
}
425-
block = (*cached_block->second).getBlock();
425+
block = (*cached_block->second.second).getBlock();
426426
}
427427

428428
{
@@ -497,7 +497,7 @@ void Sv2TemplateProvider::SubmitSolution(node::Sv2SubmitSolutionMsg solution)
497497
* on the network. In case of a reorg the node will be able to switch
498498
* faster because it already has (but not fully validated) the block.
499499
*/
500-
block_template = cached_block_template->second;
500+
block_template = cached_block_template->second.second;
501501
}
502502

503503
// Submit the solution to construct and process the block
@@ -555,7 +555,7 @@ void Sv2TemplateProvider::PruneBlockTemplateCache()
555555
// If the blocks prevout is not the tip's prevout, delete it.
556556
uint256 prev_hash = m_best_prev_hash;
557557
std::erase_if(m_block_template_cache, [prev_hash] (const auto& kv) {
558-
if (kv.second->getBlockHeader().hashPrevBlock != prev_hash) {
558+
if (kv.second.first != prev_hash) {
559559
return true;
560560
}
561561
return false;

src/sv2/template_provider.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ class Sv2TemplateProvider : public Sv2EventsInterface
104104
std::chrono::nanoseconds m_last_block_time GUARDED_BY(m_tp_mutex);
105105

106106
/**
107-
* A cache that maps ids used in NewTemplate messages and its associated block template.
107+
* A cache that maps ids used in NewTemplate messages and its associated
108+
* <prevhash,block template>.
108109
*/
109-
using BlockTemplateCache = std::map<uint64_t, std::shared_ptr<BlockTemplate>>;
110+
using BlockTemplateCache = std::map<uint64_t, std::pair<uint256, std::shared_ptr<BlockTemplate>>>;
110111
BlockTemplateCache m_block_template_cache GUARDED_BY(m_tp_mutex);
111112

112113
public:

0 commit comments

Comments
 (0)