Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/sv2/template_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Sv2TemplateProvider::Interrupt()
LOCK(m_tp_mutex);
try {
for (auto& t : GetBlockTemplates()) {
t.second->interruptWait();
t.second.second->interruptWait();
}
} catch (const ipc::Exception& e) {
// Bitcoin Core v30 does not yet implement interruptWait(), fall back
Expand Down Expand Up @@ -250,7 +250,6 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
{
try {
Timer timer(m_options.fee_check_interval);
std::shared_ptr<BlockTemplate> block_template;

const auto prepare_block_create_options = [this, client_id](node::BlockCreateOptions& options) -> bool {
{
Expand All @@ -265,6 +264,9 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
return true;
};

std::shared_ptr<BlockTemplate> block_template;
// Cache most recent block_template->getBlockHeader().hashPrevBlock result.
uint256 prev_hash;
while (!m_flag_interrupt_sv2) {
if (!block_template) {
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Generate initial block template for client id=%zu\n",
Expand All @@ -288,7 +290,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Assemble template: %.2fms\n",
Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));

uint256 prev_hash{block_template->getBlockHeader().hashPrevBlock};
prev_hash = block_template->getBlockHeader().hashPrevBlock;
{
LOCK(m_tp_mutex);
if (prev_hash != m_best_prev_hash) {
Expand All @@ -299,7 +301,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)

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

{
Expand Down Expand Up @@ -346,7 +348,6 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)
client_id);
}

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

{
LOCK(m_tp_mutex);
if (new_prev_hash != old_prev_hash) {
if (new_prev_hash != prev_hash) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously old_prev_hash probably should have been m_best_prev_hash.

LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Tip changed, client id=%zu\n",
client_id);
future_template = true;
Expand All @@ -375,7 +376,7 @@ void Sv2TemplateProvider::ThreadSv2ClientHandler(size_t client_id)

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

{
Expand Down Expand Up @@ -422,7 +423,7 @@ void Sv2TemplateProvider::RequestTransactionData(Sv2Client& client, node::Sv2Req

return;
}
block = (*cached_block->second).getBlock();
block = (*cached_block->second.second).getBlock();
}

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

// Submit the solution to construct and process the block
Expand Down Expand Up @@ -555,7 +556,7 @@ void Sv2TemplateProvider::PruneBlockTemplateCache()
// If the blocks prevout is not the tip's prevout, delete it.
uint256 prev_hash = m_best_prev_hash;
std::erase_if(m_block_template_cache, [prev_hash] (const auto& kv) {
if (kv.second->getBlockHeader().hashPrevBlock != prev_hash) {
if (kv.second.first != prev_hash) {
return true;
}
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/sv2/template_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ class Sv2TemplateProvider : public Sv2EventsInterface
std::chrono::nanoseconds m_last_block_time GUARDED_BY(m_tp_mutex);

/**
* A cache that maps ids used in NewTemplate messages and its associated block template.
* A cache that maps ids used in NewTemplate messages and its associated
* <prevhash,block template>.
*/
using BlockTemplateCache = std::map<uint64_t, std::shared_ptr<BlockTemplate>>;
using BlockTemplateCache = std::map<uint64_t, std::pair<uint256, std::shared_ptr<BlockTemplate>>>;
BlockTemplateCache m_block_template_cache GUARDED_BY(m_tp_mutex);

public:
Expand Down
Loading