Skip to content

Add prevhash to template cache#58

Merged
Sjors merged 1 commit intomasterfrom
2025/11/tp-cache
Nov 18, 2025
Merged

Add prevhash to template cache#58
Sjors merged 1 commit intomasterfrom
2025/11/tp-cache

Conversation

@Sjors
Copy link
Collaborator

@Sjors Sjors commented Nov 7, 2025

This prevents repeating calls to getBlockHeader()

@Sjors
Copy link
Collaborator Author

Sjors commented Nov 7, 2025

@ryanofsky is there a way to make methods like getBlockHeader() cache their own result?

@Sjors
Copy link
Collaborator Author

Sjors commented Nov 11, 2025

Rebased after #57.

};

while (!m_flag_interrupt_sv2) {
uint256 prev_hash;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @Sjors, I think there's an issue with the prev_hash variable in ThreadSv2ClientHandler.

Right now, prev_hash is declared at the top of the while loop but only gets assigned when block_template is null (inside that first if block). This works fine on the first iteration, but on subsequent iterations block_template won't be null anymore (since it gets updated at line 341), so the assignment never happens. This means the comparison if (new_prev_hash != prev_hash) ends up reading uninitialized memory.

I think the fix is pretty straightforward - just move prev_hash outside the loop and update it after each waitNext() call:

uint256 prev_hash;  // Move this before the while loop

while (!m_flag_interrupt_sv2) {
    if (!block_template) {
        // ... create template ...
        prev_hash = block_template->getBlockHeader().hashPrevBlock;
        m_block_template_cache.insert({template_id, std::make_pair(prev_hash, block_template)});
    }

    std::shared_ptr<BlockTemplate> tmpl = block_template->waitNext(options);
    if (tmpl) {
        block_template = tmpl;
        uint256 new_prev_hash{block_template->getBlockHeader().hashPrevBlock};
        
        if (new_prev_hash != prev_hash) {
            future_template = true;
            // ...
        }
        
        prev_hash = new_prev_hash;  // Add this line to update for next iteration
        m_block_template_cache.insert({m_template_id, std::make_pair(new_prev_hash, block_template)});
    }
}

This also has the nice side effect of removing that getBlockHeader() call that was happening every iteration in the original code (the old old_prev_hash line), so it's actually more optimized.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

uint256 is a class, so it's default initialized to 0.

But you're right that it should be set outside the while loop.

Also cache the most recent template prev_hash
between while iterations.

This prevents repeating calls to getBlockHeader()
@Sjors Sjors requested a review from xyephy November 18, 2025 14:37
Copy link
Contributor

@xyephy xyephy left a comment

Choose a reason for hiding this comment

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

looks good, it can be merged now.

@Sjors Sjors merged commit debc0c1 into master Nov 18, 2025
24 checks passed
Sjors added a commit to Sjors/sv2-tp that referenced this pull request Nov 24, 2025
This was broken by stratum-mining#58 resulting in spurious "Tip changed" messages.

Worse however is that that this caused m_last_block_time to update too often. When -sv2interval is set to 10 seconds or less, this results in PruneBlockTemplateCache never running.
@Sjors
Copy link
Collaborator Author

Sjors commented Nov 24, 2025

Unfortunately this was incorrect, see #66.

{
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.

@xyephy
Copy link
Contributor

xyephy commented Nov 24, 2025

Unfortunately this was incorrect, see #66.

let me read through it and have a better understanding of it, sorry about my previous mistake.

@Sjors
Copy link
Collaborator Author

Sjors commented Nov 25, 2025

@xyephy I didn't see it either, it's not the best code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants