Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: speculative templates #54

Open
wants to merge 34 commits into
base: sv2
Choose a base branch
from
Open

WIP: speculative templates #54

wants to merge 34 commits into from

Conversation

Sjors
Copy link
Owner

@Sjors Sjors commented Jul 22, 2024

After reading @0xB10C's blog post on mining pool behaviour during forks, I felt motivated to implement Stratum v2's future block template functionality.

Well, this is just a very rough sketch which doesn't compile.

There's two relevant protocol messages (for the Template Provider in bitcoin#29432):

  1. NewTemplate
  2. SetNewPrevHash

Whenever the tip updates, the current working implementation first sends a NewTemplate message with future_template set to true, immediately followed by SetNewPrevHash for the new tip.

This PR tries to improve upon that for two different scenarios:

  1. We didn't win the last block
  2. We did win the last block

So prepare for either out, after we've send out the current work as described above, we send two additional templates. Both have future_template = true and both build for the block on top of the last template. The first is empty, the second has all the transactions that didn't fit in the original block.

Especially this last bit will require an overhaul of BlockAssembler::CreateNewBlock, but it would be a nice selling point for stratum v2.

So now the following things can happen:

  1. We receive a block via p2p, this means we didn't* win. The Template Provider needs to look though the template cache and send the connected client(s) a SetNewPrevHash message referring to the match.

  2. We receive a block via SubmitSolution. That means we found the block. The message handling code already finds the matching template. All we need to do is reply to the client with a NewPrevHash message.

For both (1) and (2) we then proceed as normal, creating and sending a template with non-empty block.

* = actually it's possible that the Job Declarator server reconstructed the block and broadcast it for us, so I need to add detection for this case (maybe by comparing the coinbase merkle path).

Sjors and others added 30 commits July 19, 2024 09:00
Co-Authored-By: Christopher Coverdale <[email protected]>
This commit adds the simplest stratum v2 message. The remaining messages are introduced in later commits.

Co-Authored-By: Christopher Coverdale <[email protected]>
This allows us to subclass Transport.
Implemented starting from a copy of V2Transport and the V2TransportTester, modifying it to fit Stratum v2 and Noise Protocol requirements.

Co-Authored-By: Christopher Coverdale <[email protected]>
Co-Authored-By: Fi3
Co-Authored-By: Christopher Coverdale <[email protected]>
…lass

This allows reusing them in other mocked implementations.

Also move the implementation (method definitions) to
`test/util/net.cpp` to make the header `test/util/net.h` easier to follow.
…o it

And also allows gradually providing the data to be returned by `Recv()`
and sending and receiving net messages (`CNetMessage`).
Co-authored-by: Vasil Dimov <[email protected]>
Co-authored-by: Christopher Coverdale <[email protected]>
The template provider will listen for a Job Declarator client.
It can establish a connection and detect various protocol errors.

Co-Authored-By: Christopher Coverdale <[email protected]>
Co-Authored-By: Fi3
An external program that uses the Mining interface may need quick access to some information in the block template, while it can wait a bit longer for the full raw transaction data.

This would be the case for a Stratum v2 Template Provider which needs to send a NewTemplate message (which doesn't include transactions) as quickly as possible.
fjahr and others added 4 commits July 19, 2024 10:48
Similar to the previous commit, however the behavior isn't opt-in
with NO_BRANCH, because Github CI lacks custom variables.
Have the Template Provider create speculative templates for the next block. Either empty or full of transactions (optimistic, if our current template wins)
@Sjors
Copy link
Owner Author

Sjors commented Jul 22, 2024

cc @TheBlueMatt & @Fi3 thoughts?

@Fi3
Copy link

Fi3 commented Jul 23, 2024

I would add a third template with very low fee txs if another miner/pool found the block while we are building the new template we very fast compare the inputs of all the txs in the new block with the one in the template if no input are the same we send SNPH for that template otherwise we send it for the empty one. Didn't think too much about it but I guess that is enough to have valid templates? More generally I think that if there is a way to avoid empty template that is still several time faster then create a new template we should use it.

@Sjors
Copy link
Owner Author

Sjors commented Jul 23, 2024

Indeed it would be nice to have a non-empty template that we can very quickly verify, and only fall back to the empty template if needed.

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.

4 participants