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

Stratum v2 Template Provider (take 2) #28983

Closed
wants to merge 20 commits into from

Conversation

Sjors
Copy link
Member

@Sjors Sjors commented Dec 1, 2023

Based on on @Fi3's master...Fi3:bitcoin:PatchTemplates which is based on @ccdle12's #27854. I rebased it and re-wrote the commit history. See the original branch for the evolution of the spec.

See docs/stratum-v2.md for a brief description of Stratum v2 and the role of Bitcoin Core in that system..

What to test and review?

See the testing guide for various ways to test this PR. This branch is actively used by (testnet) pools, so it should be ready for high level review.

I'll make separate pull requests for parts that are ready for detailed review. Probably starting with ECDH and the Noise Protocol.

Implementation notes

Silent Payments also needs the ECDH module, so I cherry-picked the commit from #28122. It uses ECDH is a slightly different way, but perhaps there's more overlap to be had.

Contributing

If you want to help out with any of the issues below, please open a PR to my fork. I will then squash your commits into my own where needed.

Upstream issues

Spec

  • modify spec to use ProvideMissingTransactions? (followup?)
  • pick a good default for default_coinbase_tx_additional_output_size (see getblocktemplate RPC)

Noise

Networking

Testing

  • expand sv2_template_provider_tests
  • add transport fuzzer
  • add template provider fuzzer

Template generation and updating

  • hold on to templates a bit after a block is found JDC panics after receiving RequestTransactionDataError message stratum-mining/stratum#709 (in case of race to prevent downstream crashes, though we still wouldn't relay it without additional changes)
  • hold on to template transactions even if the mempool drops them (for some time)
  • group templates with the same coinbase_tx_additional_output_size
  • don't generate templates when no client is connected

Misc

Potential followups

  • implement Noise protocol and mock client in Python, add functional tests (based on test/sv2_template_provider_tests.cpp)
  • use process separation, e.g. a bitcoin-tp binary, see multiprocess.md
  • make template updates push based, on top of Cluster Mempool, see docs/stratum-v2.md (for new blocks it's already push based)
  • push empty template for the next block (downstream can ignore or use, Implement a clever way to create and manage future jobs  stratum-mining/stratum#715)
    • send prevhash for this template as soon as any new block arrives
  • push optimistic template for the next block
    • send prevhash if and only if our template won (i.e. we got a SubmitSolution message)

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 1, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29295 (CKey: add Serialize and Unserialize by Sjors)
  • #29256 (Improve new LogDebug/Trace/Info/Warning/Error Macros by ryanofsky)
  • #29229 (Make (Read/Write)BinaryFile work with char vector, use AutoFile by Sjors)
  • #29119 (refactor: Use std::span over Span by maflcko)
  • #29071 (refactor: Remove Span operator==, Use std::ranges::equal by maflcko)
  • #29015 (kernel: Streamline util library by ryanofsky)
  • #28955 (index: block filters sync, reduce disk read operations by caching last header by furszy)
  • #28875 (build: Pass sanitize flags to instrument libsecp256k1 code by hebasto)
  • #28417 (contrib/signet/miner updates by ajtowns)
  • #28241 (Silent payment index (for light wallets and consistency check) by Sjors)
  • #26697 (logging: use bitset for categories by LarryRuane)
  • #26593 (tracing: Only prepare tracepoint arguments when actually tracing by 0xB10C)
  • #24539 (Add a "tx output spender" index by sstone)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Sjors
Copy link
Member Author

Sjors commented Dec 8, 2023

Rebased and fixed encoding mistake I made during rebase:

diff --git a/src/node/sv2_messages.h b/src/node/sv2_messages.h
index fd6974b998..d3b257c31c 100644
--- a/src/node/sv2_messages.h
+++ b/src/node/sv2_messages.h
@@ -533,11 +533,11 @@ struct Sv2SubmitSolutionMsg
         s >> m_template_id >> m_version >> m_header_timestamp >> m_header_nonce;
 
         // Ignore the 2 byte length as the rest of the stream is assumed to be
         // the m_coinbase_tx.
         s.ignore(2);
-        s >> TX_NO_WITNESS(m_coinbase_tx);
+        s >> TX_WITH_WITNESS(m_coinbase_tx);
     }
 };

Yes, the coinbase transaction has a witness.

@Sjors
Copy link
Member Author

Sjors commented Dec 11, 2023

I moved sv2_noise.h and sv2_messages.h to common and template_provider.h to node.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from 2991270 to 19d37d3 Compare December 11, 2023 21:34
@Sjors
Copy link
Member Author

Sjors commented Dec 11, 2023

I de-duplicated code in Send() by having it call SendBuf(). Later I'd like to see if we can reuse some code from Transport, since there are some similarities with v2 transport.

I dropped SendTemplate and overhauled SendWork, ThreadSv2Handler, the COINBASE_OUTPUT_DATA_SIZE handler and BuildNewWorkSet to de-duplicate stuff, make it easier to understand and to avoid passing m_ variables.

Hopefully I didn't break anything, but I did test on my custom CPU mined signet.

@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

Pleased clang-tidy. Fixed memory issue by disabling a broken test, added Assume to noise.cpp to check the message size. Added a (possibly overkill) destructor to Sv2Template.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from 30417b2 to e1b7e8a Compare December 12, 2023 18:05
@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

Ran clang-tidy locally to (hopefully) fix its remaining complaints. Included @Fi3's Sjors#25 (plus additional header and test change) which drops the CIPHER_CONFIRMATION state, as per stratum-mining/sv2-spec#60.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from a546ff0 to 31f9ae2 Compare December 12, 2023 19:56
@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

I guess I didn't catch them all locally. Pushed a change to the last (linter commit). Also fixed the broken noise handshake test. Replaced usleep with UninterruptibleSleep to keep Windows happy.

@hebasto any idea how to handle D:\a\bitcoin\bitcoin\src\common\sv2_noise.h(92,13): error C3646: 'WriteMsg': unknown override specifier ?

class Sv2CipherState
{
public:
    Sv2CipherState() = default;
    ...
    ssize_t WriteMsg(Span<std::byte> msg);

Sjors and others added 7 commits January 16, 2024 11:25
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 <chris.coverdale24@gmail.com>
Co-Authored-By: Fi3
Co-Authored-By: Christopher Coverdale <chris.coverdale24@gmail.com>
Co-Authored-By: Christopher Coverdale <chris.coverdale24@gmail.com>
@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/20524379427

@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@Sjors
Copy link
Member Author

Sjors commented Jan 29, 2024

(I plan to rebase this after some other stuff is merged)

@Sjors
Copy link
Member Author

Sjors commented Jan 29, 2024

I opened a (draft) pull request for the Noise part of this: #29346

(will rebase this PR on that later, but right now that would break compatibility)

@Sjors
Copy link
Member Author

Sjors commented Feb 2, 2024

Added two commits to stay in sync with SRI: stratum-mining/stratum#742

Next week I'll try to rebase this on the latest #29346, which has diverged a bit.

@Sjors
Copy link
Member Author

Sjors commented Feb 5, 2024

I rebased on top of #29346 and pushed the result to: https://github.com/Sjors/bitcoin/tree/2024/02/sv2-poll-ellswift

Once EllSwift lands on the SRI side (or if I make a revert commit for it) I'll update this PR.

@Sjors
Copy link
Member Author

Sjors commented Feb 5, 2024

Added to 2024/02/sv2-poll-ellswift: we now hold on to templates for 10 seconds after a new block arrives, in a case a miner was still working on it. The resulting block still wouldn't get broadcast.

@Sjors
Copy link
Member Author

Sjors commented Feb 8, 2024

I added tests for RequestTransactionData to 2024/02/sv2-poll-ellswift, including an RBF test. It seems that the node does hold on to transaction data after an RBF. I don't know if that's because the block template has a p pointer, or if there's some other mechanism at play.

We'll need some way to limit the amount of memory that this can take up. Perhaps by pruning old (lower fee) templates more aggressively.

@Sjors
Copy link
Member Author

Sjors commented Feb 14, 2024

This PR is superseeded by #29432.

@Sjors Sjors closed this Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants