Skip to content

Commit 88588c3

Browse files
committed
Add prevhash to template cache
This prevents repeating calls to getBlockHeader()
1 parent d812921 commit 88588c3

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/sv2/template_provider.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
247247
};
248248

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

272-
uint256 prev_hash{block_template->getBlockHeader().hashPrevBlock};
273+
prev_hash = block_template->getBlockHeader().hashPrevBlock;
273274
{
274275
LOCK(m_tp_mutex);
275276
if (prev_hash != m_best_prev_hash) {
@@ -280,7 +281,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
280281

281282
// Add template to cache before sending it, to prevent race
282283
// condition: https://github.com/stratum-mining/stratum/issues/1773
283-
m_block_template_cache.insert({template_id,block_template});
284+
m_block_template_cache.insert({template_id,std::make_pair(prev_hash, block_template)});
284285
}
285286

286287
{
@@ -327,7 +328,6 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
327328
client_id);
328329
}
329330

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

344344
{
345345
LOCK(m_tp_mutex);
346-
if (new_prev_hash != old_prev_hash) {
346+
if (new_prev_hash != prev_hash) {
347347
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Tip changed, client id=%zu\n",
348348
client_id);
349349
future_template = true;
@@ -356,7 +356,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
356356

357357
// Add template to cache before sending it, to prevent race
358358
// condition: https://github.com/stratum-mining/stratum/issues/1773
359-
m_block_template_cache.insert({m_template_id,block_template});
359+
m_block_template_cache.insert({m_template_id, std::make_pair(new_prev_hash,block_template)});
360360
}
361361

362362
{
@@ -403,7 +403,7 @@ void Sv2TemplateProvider::RequestTransactionData(Sv2Client& client, node::Sv2Req
403403

404404
return;
405405
}
406-
block = (*cached_block->second).getBlock();
406+
block = (*cached_block->second.second).getBlock();
407407
}
408408

409409
{
@@ -478,7 +478,7 @@ void Sv2TemplateProvider::SubmitSolution(node::Sv2SubmitSolutionMsg solution)
478478
* on the network. In case of a reorg the node will be able to switch
479479
* faster because it already has (but not fully validated) the block.
480480
*/
481-
block_template = cached_block_template->second;
481+
block_template = cached_block_template->second.second;
482482
}
483483

484484
// Submit the solution to construct and process the block
@@ -536,7 +536,7 @@ void Sv2TemplateProvider::PruneBlockTemplateCache()
536536
// If the blocks prevout is not the tip's prevout, delete it.
537537
uint256 prev_hash = m_best_prev_hash;
538538
std::erase_if(m_block_template_cache, [prev_hash] (const auto& kv) {
539-
if (kv.second->getBlockHeader().hashPrevBlock != prev_hash) {
539+
if (kv.second.first != prev_hash) {
540540
return true;
541541
}
542542
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)