Skip to content

Commit

Permalink
Reject RequestTransactionData for stale templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Feb 14, 2024
1 parent e477666 commit 50b9afa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
34 changes: 25 additions & 9 deletions src/node/sv2_template_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ void Sv2TemplateProvider::ThreadSv2Handler()
WAIT_LOCK(g_best_block_mutex, lock);
auto checktime = std::chrono::steady_clock::now() + std::chrono::milliseconds(50);
g_best_block_cv.wait_until(lock, checktime);
if (m_best_prev_hash.m_prev_hash != g_best_block) {
m_best_prev_hash.m_prev_hash = g_best_block;
if (m_best_prev_hash != g_best_block && g_best_block != uint256(0)) {
m_best_prev_hash = g_best_block;
return true;
}
return false;
Expand Down Expand Up @@ -472,7 +472,7 @@ void Sv2TemplateProvider::PruneBlockTemplateCache()
auto recent = GetTime<std::chrono::seconds>() - std::chrono::seconds(10);
if (m_last_block_time > recent) return;
// If the blocks prevout is not the tip's prevout, delete it.
uint256 prev_hash = m_best_prev_hash.m_prev_hash;
uint256 prev_hash = m_best_prev_hash;
std::erase_if(m_block_template_cache, [prev_hash] (const auto& kv) {
if (kv.second->block.hashPrevBlock != prev_hash) {
return true;
Expand All @@ -495,6 +495,12 @@ bool Sv2TemplateProvider::SendWork(Sv2Client& client, bool send_new_prevhash)
++m_template_id;
auto new_work_set = BuildNewWorkSet(/*future_template=*/send_new_prevhash, client.m_coinbase_tx_outputs_size);

if (m_best_prev_hash == uint256(0)) {
// g_best_block is set UpdateTip(), so will be 0 when the node starts
// and no new blocks have arrived.
m_best_prev_hash = new_work_set.block_template->block.hashPrevBlock;
}

// Do not submit new template if the fee increase is insufficient:
CAmount fees = 0;
for (CAmount fee : new_work_set.block_template->vTxFees) {
Expand All @@ -506,6 +512,7 @@ bool Sv2TemplateProvider::SendWork(Sv2Client& client, bool send_new_prevhash)

m_block_template_cache.insert({m_template_id, std::move(new_work_set.block_template)});


LogPrintLevel(BCLog::SV2, BCLog::Level::Debug, "Send 0x71 NewTemplate to client id=%zu\n", client.m_id);
client.m_send_messages.emplace_back(new_work_set.new_template);

Expand Down Expand Up @@ -708,14 +715,23 @@ void Sv2TemplateProvider::ProcessSv2Message(const Sv2NetMsg& sv2_net_msg, Sv2Cli
if (cached_block != m_block_template_cache.end()) {
CBlock& block = (*cached_block->second).block;

if (block.hashPrevBlock != m_best_prev_hash) {
LogTrace(BCLog::SV2, "Template id=%lu prevhash=%s, tip=%s\n", request_tx_data.m_template_id, HexStr(block.hashPrevBlock), HexStr(m_best_prev_hash));
Sv2RequestTransactionDataErrorMsg request_tx_data_error{request_tx_data.m_template_id, "stale-template-id"};


LogDebug(BCLog::SV2, "Send 0x75 RequestTransactionData.Error (stale-template-id) to client id=%zu\n",
client.m_id);
client.m_send_messages.emplace_back(request_tx_data_error);
return;
}

std::vector<uint8_t> witness_reserve_value;
if (!block.IsNull()) {
auto scriptWitness = block.vtx[0]->vin[0].scriptWitness;
if (!scriptWitness.IsNull()) {
std::copy(scriptWitness.stack[0].begin(), scriptWitness.stack[0].end(), std::back_inserter(witness_reserve_value));
}
auto scriptWitness = block.vtx[0]->vin[0].scriptWitness;
if (!scriptWitness.IsNull()) {
std::copy(scriptWitness.stack[0].begin(), scriptWitness.stack[0].end(), std::back_inserter(witness_reserve_value));
}
std::vector<CTransactionRef> txs;
std::vector<CTransactionRef> txs;
if (block.vtx.size() > 0) {
std::copy(block.vtx.begin() + 1, block.vtx.end(), std::back_inserter(txs));
}
Expand Down
5 changes: 2 additions & 3 deletions src/node/sv2_template_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ class Sv2TemplateProvider
uint64_t m_template_id;

/**
* The current best known SetNewPrevHash that references the current best known
* block hash in the network.
* The current best known block hash in the network.
*/
node::Sv2SetNewPrevHashMsg m_best_prev_hash;
uint256 m_best_prev_hash;


/** When we last saw a new block connection. Used to cache stale templates
Expand Down

0 comments on commit 50b9afa

Please sign in to comment.